\documentclass[]{article}

%opening
\usepackage[a4paper, total={7.5in, 8in}]{geometry}
\usepackage{amsmath, amsfonts, amssymb, amsthm}
\usepackage{geometry}
\usepackage{float}
\usepackage{braket}
\usepackage{upgreek}
\usepackage{bbold}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{cancel}
\graphicspath{{./figures/}}
\makeatletter
\newcommand{\distas}[1]{\mathbin{\overset{#1}{\kern\z@\sim}}}%
\newsavebox{\mybox}\newsavebox{\mysim}
\newcommand{\distras}[1]{%
	\savebox{\mybox}{\hbox{\kern3pt$\scriptstyle#1$\kern3pt}}%
	\savebox{\mysim}{\hbox{$\sim$}}%
	\mathbin{\overset{#1}{\kern\z@\resizebox{\wd\mybox}{\ht\mysim}{$\sim$}}}%
}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Ps}{\mathbb{P}}
\renewcommand{\vec}[1]{\mathbf{#1}}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\newcommand{\indep}{\perp \!\!\! \perp}
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}
\makeatother

\usepackage{hyperref}
\hypersetup{
	colorlinks=true,
	linkcolor=blue,
	filecolor=magenta,
	urlcolor=cyan,
}

\newtheorem*{mydef}{Definition}

\title{Easy Expectation Maximisation}
\author{Victor Btesh}

\begin{document}

\maketitle

\section{Introduction and motivation}

This small statistical computing project has the aim of providing a general purpose architecture to perform clustering on a set of data points and features. I am sure there exist better more complex packages to handle such problems but I need practice and having such a tool suits my current research needs. Finally, it's fun. Be advised, it will probably (for sure) be buggy, lack rigour and have unclear notation.

\section{Models}

This section details the type of model this package will be able to handle. The basic assumptions are that all data points are i.i.d, which is standard, but also that all features are independent given the hidden clustering variable. This allows for much simpler maximisation steps which can be standardised for each distribution. I aim to provide support for all standard distributions from the exponential family, starting with Poisson and Gaussian with known variance and unknown mean. Derivations will be below. I also want it to handle discrete time discrete space Markov chains, as it is a classic use case for this algorithm.

\subsection{Joint distributions}

Given two sets of variables, where $h$ is unique, hidden and the cluster multinomial distribution with $m$ possible outcomes and $v = \{v_1, v_2, ..., v_k\}$ are visible, and a parameter set $\theta$, we have must have the following joint probability distribution:
\[
	p(h, v|\theta) = p(h|\theta) \prod_{j=1}^{k} p(v_j|h, \theta)
\]
Yielding the following if we observed $n$ i.i.d samples, where $i$ are indices, not powers:
\[
	p(\vec{h}, \vec{v}|\theta) = \prod_{i=1}^{n} p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)
\]
This is important as we want the log likelihood to have the following form:
\begin{align*}
	\log p(h, v|\theta) &= \log \left[ p(h|\theta) \prod_{j=1}^{k} p(v_i|h, \theta) \right] \\
	&= \log p(h|\theta) + \sum_{j=1}^{k} \log p(v_j|h, \theta)
\end{align*}
Again, after observing $n$ i.i.d. samples:
\[
	\log p(\vec{h}, \vec{v}|\theta) = \sum_{i=1}^{n}\left[ \log p(h^i|\theta) +  \sum_{j=1}^{k} \log p(v_j^i|h^i, \theta) \right]
\]

\subsection{Maximisation steps}
This form of log likelihood is useful as the energy term, which we want to optimise in the maximisation step, will look as follows:
\begin{align*}
	\mathbb{E}(\theta) &= \sum_{i=1}^{n} \left\langle \log p(h^i|\theta) +  \sum_{j=1}^{k} \log p(v_j^i|h^i, \theta) \right\rangle_{q^i(h)} \\
	&= \sum_{i=1}^{n} \left[ \left\langle \log p(h^i|\theta) \right\rangle_{q^i(h)}  +  \sum_{j=1}^{k} \left\langle \log p(v_j^i|h^i, \theta) \right\rangle_{q^i(h)}  \right] \\
	&= \sum_{i=1}^{n}  \left\langle \log p(h^i|\theta) \right\rangle_{q^i(h)}   + \sum_{i=1}^{n}  \sum_{j=1}^{k} \left\langle \log p(v_j^i|h^i, \theta) \right\rangle_{q^i(h)}  \\
	&= \sum_{i=1}^{n} \left\langle \log p(h^i|\theta) \right\rangle_{q^i(h)}  + \sum_{j=1}^{k}  \sum_{i=1}^{n}  \left\langle \log p(v_j^i|h^i, \theta) \right\rangle_{q^i(h)} \\
\end{align*}

As a result, when we optimise w.r.t to each parameter in $\theta$, for instance $\theta^h$, the parameter of the hidden variable, all other terms can be ignored.

\subparagraph{On $q^i(h)$, the variational distribution} This is the variational distribution over hidden states $h$ w.r.t to which we are performing the Expectation step. Two things are worthy to note here. First, we $q(h)$ actually represent $p(h|v, \theta^{old})$, which is the probability of being in a given cluster given observed variables and previous values of $\theta$. This is because the expectation step is simply setting $q(h) = p(h|v, \theta^{old})$. The second thing is that it does not depend on the $\theta$ we are currently optimising but on $\theta^{old}$, i.e. the $\theta$ optimised in the previous iteration of the algorithm. Rigorously, $q^i(h)$ should be written as $p^i(h|v, \theta^{old})$. Finally, the $i$ index represents the fact that we compute the $p^i(h|v, \theta^{old})$ for each observations, therefore leading to different $q(h)$ for each sample.

\subparagraph{On $\theta$, the parameter matrix} Note $\theta$ is matrix where each row corresponds to parameters for each variable associated with a specific cluster. If we have only one parameter per variable, as in a set of means for a Gaussians, then $\theta$ will be a $c$ by $k+1$ matrix where $c$ is the number of clusters and $k$ is the number of observed variables. If visible variables have multiple parameters, such as multinomial distributions, $\theta$ will be a $c$ by $k+1$ by $o$ 3d matrix, where $o$ is the number of parameters, i.e. outcomes, to learn for each cluster for a given observed variable. As observed variables can have different numbers of parameters, e.g. if we observe a multinomial and a Poisson random variable, as such we will treat each column in $\theta$ separately, guaranteeing that each $\theta^k$ is at a most a 2d matrix. \\

Optimisation should happen w.r.t to each entry $\theta$. We are actually computing the gradient w.r.t $\theta$ for all variables. As discussed, we split $\theta$ so obtain one $\theta^k$ for each variable. We would then have for the prior probability of being in a given cluster:

\begin{align*}
	\nabla_{\theta^h} \mathbb{E}(\theta) &= \nabla_{\theta^h} \left[ \sum_{i=1}^{n} \left\langle \log p(h^i|\theta) \right\rangle_{q^i(h)} \right] \\
	&= \sum_{i=1}^{n} \nabla_{\theta^h} \left\langle  \log p(h^i|\theta) \right\rangle_{q^i(h)} \\
	&= \sum_{i=1}^{n} \left\langle \nabla_{\theta^h} \log p(h^i|\theta) \right\rangle_{q^i(h)} \\
\end{align*}
What we get here is a generic expression that can be applied to any variable in the model. The gradient can be push inside the expectation as $q(h|v)$ does not depend on $\theta$. As such, we can find generic expressions for gradient of each standard distribution w.r.t to a set of parameters, where each parameter in the set corresponds to one of the values of $h$, i.e. our clusters.

\subsubsection{Prior distribution over hidden states}
\paragraph{Log likelihood}
\[
	\log p(h^i|v^i, \theta^h) = \log \theta^h
\]
Where $\theta^h$ is the vector of probabilities for each cluster.
\paragraph{Optimal parameter value}
\[
	\argmax_{\theta^h} \mathbb{E}(\theta) = \frac{1}{n} \sum_{i=1}^{n} q^i(h)
\]
Where $\theta^h$ is the vector of probabilities for each cluster and $q^i(h) = p(h^i|v^i, \theta^{old})$.
\paragraph{Derivation}

Let us continue here to derive the simplest case of the distribution over hidden states, which is the prior $p(h|\theta)$:
\begin{align*}
	&= \sum_{i=1}^{n} \nabla_{\theta^h}  \sum_{j=1}^{k} q^i(h) \log \theta^h_j  \\
	&= \sum_{i=1}^{n}  \frac{q^i(h)}{\theta^h} \\
\end{align*}

After adding a Lagrange multiplier as $p(h|\theta)$, as a probability distribution must satisfy $\sum_{j=1}^{k} p(h_j|\theta) = 1$, which can be equivalently written as $\sum_{j=1}^{k} \theta^h_j = 1$, such that
\begin{align*}
	f(\theta^h) &= \mathbb{E}(\theta) \\
	g(\theta^h) &= \sum_{j=1}^{k} \theta^h_j - 1
\end{align*}
and
\[
	L(\theta^h, \lambda) = f(\theta^h) + \lambda g(\theta^h)
\]
we can compute the gradient and maximise given the constraints:
\begin{align*}
	\nabla L(\theta^h, \lambda) &= \nabla f(\theta^h) + \nabla \lambda g(\theta^h) \\
								&= \sum_{i=1}^{n} \frac{q^i(h)}{\theta^h} + \lambda
\end{align*}
Optimising by setting it equal to 0, we get the following:
\[
	\theta^h = \frac{\sum_{i=1}^{n} q^i(h)}{\lambda} = \frac{1}{n} \sum_{i=1}^{n} q^i(h)
\]
Where it can be shown that $\lambda$ is a normalisation constraint, which here will be $n$, the number of observations. The expression is therefore the average of all probabilities in the variational distribution over the whole sample.

\subsubsection{Multinomial distribution (categorical random variables)}

This distribution should be used to describe categorical variables.

We call $\theta^{m}$ a generic $c \times o$ parameter matrix where $c$ is the number of hidden clusters, $o$ the number of categories/outcomes in the observed multinomial distribution and each entry $\theta^{m}_{co}$ is a probability observing outcome $o$ given cluster $c$. In addition, let $p(v^i|h^i|\theta^m)$ refer to this generic multinomial distribution. Note that normalisation of $\theta^{m}$ is done row-wise.

\paragraph{Log likelihood}
\[
	\log p(v^i|h^i,\theta_m) = \log \theta^{m}
\]
Thus the log likelihood is simply the element wise log of $\theta^{m}$.
\paragraph{Optimal parameter value}
\[
	\argmax_{\theta^{m}} \mathbb{E}(\theta) \propto \sum_{i=1}^{n} q^i(h) \mathbb{1}{[v^i = v]}
\]
Intuitively, what this expression means is that to find the values of the parameters $\theta_m$ that maximise $\mathbb{E}(\theta)$, we must count all cases where a given visible outcome $v$ has been observed weighted by the variational distribution.

\paragraph{Derivation}

The derivation here will be similar to the case of the hidden state as the distributions or of the same family. We call $\theta_m$ a generic $m$ by $c$ parameter matrix where $m$ is the number of hidden clusters and $c$ the number of categories/outcomes in the observed multinomial distributions.

\subsubsection{Poisson random variables}

This distribution should be used to model random variables that represent counts or the occurrence of events.

\paragraph{Log likelihood}
\[
\log p(v^i=v|h^i, \Lambda) \propto v \log \Lambda - \Lambda
\]
\paragraph{Optimal parameter value}
\[
\argmax_{\Lambda \in \theta} \mathbb{E}(\theta) =  \frac{\sum_{i=1}^{n} q^i(h) v^i}{\sum_{i=1}^{n} q^i(h)}
\]

\paragraph{Derivations}

\subparagraph{Log likelihood}

A Poisson random variable has only rate parameter generally labelled $\lambda$. Let us define $\Lambda \in \theta$ to be a vector of length $c$ of rate parameters where each entry is the rate parameter corresponding to cluster $c$, represented by variable $h$. Let us remind ourselves then of the expression for the likelihood of observing a certain count given the clusters:
\[
p(v^i=v|h^i, \Lambda) = \frac{\Lambda^v e^{-\Lambda}}{v!} \propto \Lambda^v e^{-\Lambda}
\]
The log likelihood is then:
\begin{align*}
	\log p(v^i=v|h^i, \Lambda) &= \log \left[ \frac{\Lambda^v e^{-\Lambda}}{v!} \right] \\
	&= \log \Lambda^v + \log e^{-\Lambda} - \log v!\\
	&= v \log \Lambda - \Lambda - \log v!
\end{align*}

\subparagraph{Optimisation}

Let us now find the expression for the values of $\Lambda \in \theta$ that maximise the energy $\mathbb{E}(\theta)$. We can start from the generic expression of the gradient of the energy w.r.t to the parameters $\Lambda$ of a visible variable $v$.

\begin{align*}
	\nabla_{\Lambda} \mathbb{E}(\theta) &= \nabla_{\Lambda}\left[ \sum_{i=1}^{n}  \left\langle \log p(v^i|h^i, \theta) \right\rangle_{q^i(h)} \right] \\
	&= \nabla_{\Lambda} \left[ \sum_{i=1}^{n}  \left\langle v^i \log \Lambda - \Lambda - \log v^i! \right\rangle_{q^i(h)} \right] \\
	&= \sum_{i=1}^{n} \nabla_{\Lambda} \sum_{h} q^i(h) (v^i \log \Lambda - \Lambda - \log v^i!) \\
	&= \sum_{i=1}^{n} q^i(h) \nabla_{\Lambda} (v^i \log \Lambda - \Lambda - \log v^i!) \\
	&= \sum_{i=1}^{n} q^i(h) \left(\frac{v^i}{\Lambda} - 1 \right)
\end{align*}

We set this to 0 to optimise and get the following expression.

\begin{align*}
	\sum_{i=1}^{n} q^i(h) \left( \frac{v^i}{\Lambda} - 1 \right) &= 0 \\
	\sum_{i=1}^{n} q^i(h) \frac{v^i}{\Lambda} &= \sum_{i=1}^{n} q^i(h) \\
	\Lambda &= \frac{\sum_{i=1}^{n} q^i(h)v^i}{\sum_{i=1}^{n} q^i(h) }
\end{align*}

Poisson random variables are subject to the constraint $\Lambda > 0$. Here we can see that the output from the optimisation equation cannot be negative. Indeed, as $v^i$ is a count, it will always be a positive integer and $q^i(h)$ is a multinomial probability distribution, which also will always be positive. We therefore do not need to use an optimisation constraint here, the maximum will always satisfy $\Lambda > 0$.

\subsubsection{Normal distribution (Gaussian), known variance unknown mean}

This distribution should be used to model the behaviour of continuous variables that can take any value in $\R$. Can also be used for positive or negative continuous quantities that do not describe event occurrence, e.g. height, scores on a scale, etc.

\paragraph{Log likelihood}
\[
	\log p(v^i=v|h^i, \theta^{\mu}) \propto -\frac{1}{2\sigma^2}(v - \theta^{\mu})^2
\]
\paragraph{Optimal parameter value}
\[
	\argmax_{\theta^{\mu}} \mathbb{E}(\theta) = \frac{\sum_{i=1}^{n} q^i(h) v^i}{\sum_{i=1}^{n} q^i(h)}
\]

\paragraph{Derivations}

\subparagraph{Log likelihood}
We will deal with the case when we assume knowledge of the variance, e.g. approximated by the sample variance, but expect means to differ between clusters. Therefore, we only have one set of parameters to learn here, which we will call $\theta^{\mu}$ and is a 1d vector of length $c$, where each entry will be the mean for each cluster. Let us remind ourselves of the form of the normal likelihood of observing $v$, with a sample variance $\sigma^2$ and possible means $\theta^{\mu}$ depending on the cluster:
\[
p(v^i=v|h^i, \theta^{\mu}) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{1}{2\sigma^2}(v - \theta^{\mu})^2} \propto e^{-\frac{1}{2\sigma^2}(v - \theta^{\mu})^2}
\]
The log likelihood is then
\begin{align*}
	\log p(v^i=v|h^i, \theta^{\mu}) &= \log \left[ \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{1}{2\sigma^2}(v - \theta^{\mu})^2} \right] \\
	&= \log \frac{1}{\sqrt{2\pi\sigma^2}} + \log e^{-\frac{1}{2\sigma^2}(v - \theta^{\mu})^2}  \\
	&=\log \frac{1}{\sqrt{2\pi\sigma^2}} -\frac{1}{2\sigma^2}(v - \theta^{\mu})^2 \\
	&= - \frac{1}{2} \log(2\pi\sigma^2)  -\frac{1}{2\sigma^2}(v - \theta^{\mu})^2 \\
	&\propto -\frac{1}{2\sigma^2}(v - \theta^{\mu})^2
\end{align*}

\subparagraph{Optimisation}

Starting from the generic form of the gradient of the energy, $\mathbb{E}(\theta)$, w.r.t to the parameters, $\theta^{\mu}$, of a visible variable, $v$, all other terms disappear and we have the following:

\begin{align*}
	\nabla_{\theta^{\mu}} \mathbb{E}(\theta) &= \nabla_{\theta^{\mu}}\left[ \sum_{i=1}^{n}  \left\langle \log p(v^i|h^i, \theta) \right\rangle_{q^i(h)} \right]
\end{align*}
replacing the log likelihood by the expression found above
\begin{align*}
	&= \nabla_{\theta^{\mu}}\left[ \sum_{i=1}^{n}  \left\langle \log p(v^i|h^i, \theta) \right\rangle_{q^i(h)} \right] \\
	&= \nabla_{\theta^{\mu}}\left[ \sum_{i=1}^{n}  \left\langle - \frac{1}{2} \log(2\pi\sigma^2)  -\frac{1}{2\sigma^2}(v - \theta^{\mu})^2 \right\rangle_{q^i(h)} \right] \\
	&= - \sum_{i=1}^{n} \nabla_{\theta^{\mu}} \left\langle \frac{1}{2} \log(2\pi\sigma^2)  + \frac{1}{2\sigma^2}(v - \theta^{\mu})^2 \right\rangle_{q^i(h)} \\
	&= - \sum_{i=1}^{n} \nabla_{\theta^{\mu}} \sum_{h} q^i(h) \left[ \frac{1}{2} \log(2\pi\sigma^2)  + \frac{1}{2\sigma^2}(v - \theta^{\mu})^2 \right] \\
	&= - \sum_{i=1}^{n} q^i(h) \nabla_{\theta^{\mu}} \left[ \frac{1}{2} \log(2\pi\sigma^2)  + \frac{1}{2\sigma^2}(v - \theta^{\mu})^2 \right] \\
	&= \sum_{i=1}^{n} q^i(h) \frac{1}{\sigma^2} (v^i - \theta^{\mu})  \\
\end{align*}

Notation is unclear here, but the sum over $h$ disappears because when taking the gradient, each value for $h$ that is not the one we are differentiating w.r.t. amounts to 0.

We can now optimise by setting this to 0. Note that we do not use a Lagrange multiplier as the mean of the normal distribution can take any value on the real line.

\begin{align*}
	\sum_{i=1}^{n} q^i(h) \frac{1}{\sigma^2} (v^i - \theta^{\mu}) &= 0 \\
	\sum_{i=1}^{n} q^i(h) \frac{1}{\sigma^2}\theta^{\mu} &= \sum_{i=1}^{n} q^i(h) \frac{1}{\sigma^2}v^i \\
	\frac{1}{\sigma^2}\theta^{\mu} \sum_{i=1}^{n} q^i(h) &= \sum_{i=1}^{n} q^i(h) \frac{1}{\sigma^2}v^i \\
	\theta^{\mu} \sum_{i=1}^{n} q^i(h) &= \sum_{i=1}^{n} q^i(h) v^i \\
	\theta^{\mu} &= \frac{\sum_{i=1}^{n} q^i(h) v^i}{\sum_{i=1}^{n} q^i(h)} \\
\end{align*}

\subsection{Expectation steps}

The below equation depicts the evidence lower bound for the marginal log likelihood as the sum between the negative KL divergence between the variational distribution and the probability distribution of the hidden states given the observed variables.
\[
	\log p(v|\theta) \geq - \text{KL}[p(h|v,\theta)||q(h)] + \log p(v|\theta)
\]
As the KL divergence is maximal, i.e. equal to 0, when both distribution are the same, we get the following when the variational distribution, $q(h)$, is set to $p(h|v,\theta)$:
\begin{align*}
	\log p(v|\theta) &\geq - \text{KL}[p(h|v,\theta)||p(h|v,\theta)] + \log p(v|\theta) \\
	\log p(v|\theta) &\geq - 0 + \log p(v|\theta) \\
	\log p(v|\theta) &= \log p(v|\theta)
\end{align*}
Thus we can see that the bound is equal when $q(h) = p(h|v,\theta)$. Therefore the expectation step is simply to set the variational distribution to be equal to the posterior. We do not use the normalisation constraint, as explained in the appendix.

Let us derive a generic expression for this. From Bayes' rule and our models' constraints, we have:
\begin{align*}
	p(h^i|v^i,\theta) &= \frac{p(v^i,h^i|\theta)}{p(v^i|\theta)} \\
	&= \frac{p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)}{\sum_{h}p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)} \\
	&\propto p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)
\end{align*}

Taking the log on both side yields:

\begin{align*}
	\log p(h^i|v^i,\theta) &\propto \log \left[ p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta) \right] \\
	&= \log p(h^i|\theta) + \sum_{j=1}^{k} \log p(v_j^i|h^i, \theta)
\end{align*}

This expression is easy to deal with as all log probabilities are well separated in their own addition term. We can use the log of the distribution in each case, which are well known for distributions in the exponential family. Also, we use the log likelihoods up to proportionality as we are then normalising $p(h^i|v^i, \theta)$.

\section{Appendix}

\subsection{Numerical stability}

\paragraph{Normalisation of small log likelihoods}

As the expectation steps requires us to compute log likelihoods for the joint distribution of each observation without allowing us easy compute the normalisation constant, we need to come up with a solution to deal with very negative log values so that they do not go to 0 when exponentiated. Let us remind ourselves of the issue. We have, for the expectation step, the following quantity to compute:

\begin{align*}
	p(h^i|v^i,\theta) &= \frac{p(v^i,h^i|\theta)}{p(v^i|\theta)} \\
	&= \frac{p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)}{\sum_{h}p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)}
\end{align*}

Taking the log on both side yields:

\begin{align*}
	\log p(h^i|v^i,\theta) &= \log \left[ \frac{p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)}{\sum_{h}p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta)} \right] \\
	&= \log \left[ p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta) \right] - \log \left[ \sum_{h}p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta) \right]  \\
	&= \log p(h^i|\theta) + \sum_{j=1}^{k} \log p(v_j^i|h^i, \theta) - \log \left[ \sum_{h}p(h^i|\theta) \prod_{j=1}^{k} p(v_j^i|h^i, \theta) \right]
\end{align*}
The first term is the log likelihood and can be easily computed by pushing the log inside and adding all inner terms. However, as we are marginalising $h$ out, the normalisation constant is a sum and is not easily computable. It would not be a problem for cases where we do not have a lot of features but we still need a method that can deal with cases in which the probabilities get too small to be numerically approximated.

The solution is to find a way to not have to compute the normalising constant and "normalise" the log likelihood. A thread on StackExchange provides an elegant solution. The thread can be found here: \url{https://stats.stackexchange.com/questions/66616/converting-normalizing-very-small-likelihood-values-to-probability}.

The idea is to subtract the maximum logarithm from all log in the likelihood array. This will not change the relative weights of each case and will render exponentiation much easier. The index of the maximum will be 0 which will yield 1 when exponentiated, with a lower log values indices being closer to 0.

The next step is to define an underflow threshold when using a programming language that does not automatically output 0 for values that underflow, which is not the case for python.

Therefore, if we have a log likelihood array, $\vec{L}$,  with $m$ entries $\lambda_1, \lambda_2, ..., \lambda_m$, and maximum entry we perform the following operation:
\[
	\vec{L}_{new} = \vec{L} - \max \vec{L}
\]
We can then exponentiate $L_{new}$ element-wise and then normalise.
\end{document}


\documentclass[12pt]{cdblatex}
\usepackage{exercises}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% --------------------------------------------------------------------------------------------
\section*{Exercise 5.3 Deleting a term using patterns}

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices(position=independent).

   expr := A_{a b} B^{a b} + A_{a b} A_{c d} B^{a b} B^{c d} - C_{a b} B^{a b}.  # cdb (ex-0503.100,expr)

   zoom       (expr, $A_{a b} A_{c d} Q??$)                                      # cdb (ex-0503.101,expr)
   substitute (expr, $A_{a b} -> 0$)                                             # cdb (ex-0503.102,expr)
   unzoom     (expr)                                                             # cdb (ex-0503.103,expr)
\end{cadabra}

\begin{dgroup*}
   \Dmath*{ \cdb*{ex-0503.100} }
   \Dmath*{ \cdb*{ex-0503.101} }
   \Dmath*{ \cdb*{ex-0503.102} }
   \Dmath*{ \cdb*{ex-0503.103} }
\end{dgroup*}

\end{document}


%!TEX root = ../CombinatoricsNotes.tex

\section{Intersecting hypergraphs}

A family $\F\subset \P([n])$ is \defn{intersecting}[intersecting set system] if every two sets $A,B\in \F$ have $A\cap B\neq \emptyset$. If $\F$ is intersecting, then $|\F| \leq 2^{n-1}$ because $\F$ can contain at most one set in each pair $\{A,A^c\}$ for every $A\subset [n]$. On the other hand, $2^{n-1}$ may be acheived by taking $\F = \{A\subset [n]: x\in A\}$ for some $x\in [n]$.
What if $\F\subset [n]^{(r)}$ for some $r$? If $r> n/2$, then $\F = [n]^{(r)}$ is intersecting.

\begin{theorem}[\cite{erdos-ko-rado-1961}] \label{thm:erdos-ko-rado}
Let $r \leq n/2$ and $\F\subset[n]^{(r)}$ be intersecting. Then
\[
|\F| \leq {n-1\choose r-1}
\]
which can be acheived by $\F = \{A\in [n]^{(r)}: x\in A\}$ for some $x\in [n]$.
\end{theorem}
\begin{proof}
Let us consider a particular circular ordering of $[n]$ and upper bound the number of sets in $\F$ which are intervals of size $r$ in this order.
\begin{figure}[ht]
\usetikzlibrary{fit}
\begin{center}
\begin{tikzpicture}
 \begin{scope}
\node[left] at (0,0) {$\circlearrowleft$};

\foreach \x in {1,...,8}
{
 \pgfmathsetmacro\myangle{\x*45}
\filldraw[black] (\myangle:1cm) circle (0.4pt) node[left]{\x};
}
 \end{scope}
 \node[xshift=45pt] at (0,0) {$=$};
% \end{tikzpicture}
% $=$
% \begin{tikzpicture}
\begin{scope}[xshift=100pt]
\node[left] at (0,0) {$\circlearrowleft$};
\foreach \x in {1,...,8}
{
 \pgfmathsetmacro\myangle{\x*45+45}
\filldraw[black] (\myangle:1cm) circle (0.4pt) node[left]{\x};
}
\end{scope}
 \node[xshift=145pt] at (0,0) {$\neq$};

% \end{tikzpicture}
% $\neq$
% \begin{tikzpicture}
\begin{scope}[xshift=200pt]
\node[left] at (0,0) {$\circlearrowleft$};
\foreach \x in {1,...,8}
{
 \pgfmathsetmacro\myangle{-1*\x*45+90}
\filldraw[black] (\myangle:1cm) circle (0.4pt) node[left]{\x};
}
\end{scope}
\end{tikzpicture}
\end{center}
\caption{An illustration of circular orders on $[8]$. We define our order counter-clockwise, and so the order is invariant under rotations, as the equality between the left and center orders demonstrates. However, the right order was obtained by reversing the order of the left, and thus is a new order.}\label{fig:circ_order}
\end{figure}
Let us prove there are at most $r$ intervals.
Let us fix an interval $I= (a_1,a_2,\dotsc,a_r)$, and count the number of intervals which intersect it. For each pair of consecutive points $(a_{i-1},a_i)$\sidenote{Corresponding to gaps between points} in this interval $I$, there is an interval $I_1$ with first element $a_{i+1}$ and an interval $I_2$ with last element $a_i$, yielding $2(r-1)$ intervals intersecting it. But $\F$ can contain at most one interval in this pair $(I_1,I_2)$, because $I_1\cap I_2=\emptyset$. So we are left with $r-1$ intervals intersecting $I$, along with $I$ itself. Thus, we've found $r$ intersecting intervals in this circular order.

How many circular orders are there on $[n]$? Every circular order corresponds to $n$ permutations (all rotations of each other), and there are $n!$ total permutations, yielding $(n-1)!$ circular orders.

\begin{marginfigure}
\begin{center}
\begin{tikzpicture}
\begin{scope}
\node[left] at (0,0) {$\circlearrowleft$};

\foreach \x in {1,...,3}
{
 \pgfmathsetmacro\myangle{\x*45}
\filldraw[black] (\myangle:1cm) circle (0.4pt) node[left]{$x_{\x}$};
}
\foreach \x in {1,...,5}
{
 \pgfmathsetmacro\myangle{(\x+3)*45}
  % \pgfmathsetmacro\y{\x-3}
\filldraw[black] (\myangle:1cm) circle (0.4pt) node[left]{$y_{\x}$};
}
 \end{scope}
\end{tikzpicture}
\end{center}
\caption{An order in which $X = \{x_1,x_2,x_3\} \in [8]^{(3)}$ is an interval, where we've enumerated $[8]\setminus X = \{y_1,\dotsc,y_5\}$.} \label{fig:X_interval_circ_order}
\end{marginfigure}
In how many circular orders is a given $X\in [n]^{(r)}$ an interval? To obtain an interval, we order the elements of $X$ in $r!$ ways, and then order the rest of the set in $(n-r)!$ ways, yielding $r!(n-r)!$ orders in which $X$ is an interval. See \cref{fig:X_interval_circ_order} for an illustration.

We have at most $r$ sets of $\F$ as intervals per circular order, and $(n-1)!$ circular orders, so at most $r(n-1)!$ sets in $\F$ over all circular orders.

On the other hand, each set of $\F$ has only $r!(n-r)!$ orders in which it is an interval. Thus, we have $|\F| r!(n-r)!$ intervals corresponding to sets in $\F$, counted over all orders. Hence,
\[
|\F| r! (n-r)! \leq r(n-1)!
\]
which completes the proof.
\end{proof}

\begin{remark}
For $r < n/2$, if $\F\subset [n]^{(r)}$ is intersecting and has maximal size, i.e. $|\F| = {n-1\choose r-1}$, then for some $x\in n$, we have
\[
\F = \{A\in [n]^{(r)}: x\in A \}.
\]
The proof is left as an exercise. In particular, there are only $n$ possible extremal families.

Consider $r=n/2$. For each set $A \in [n]^{(r)}$, there is only one forbidden set $A^c$. So $\F$ may be formed by choosing one set $A$ from each pair $\{A,A^c\}$ arbitrarily. This yields a doubly-exponential number of extremal families.
\end{remark}

\newthought{We will now consider} a generalization of intersecting.
% \begin{definition}
We say $\F\subset [n]^{(r)}$ is \defn{$t$-intersecting}[t-intersecting] if $|A\cap B|\geq t$ for all $A,B\in \F$.
% \end{definition}
We wish to find $m(n,r,t)$, the maximum size of a $t$-intersecting family $\F$ which is a subset of $[n]^{(r)}$.
A natural guess of an optimal family would be
\[
 \F_0 := \F_0(n,r,t) = \{ A \in [n]^{(r)}: L\subset A\}
\] for some $L\subset [n]$ with $|L| = t$.
Note $|\F_0| = {n-t \choose r-t}$.
On the other hand, for which $n,r,t$ is $m(n,r,t) = {n\choose r}$? I.e., when is the family $[n]^{(r)}$ itself $t$-intersecting?
We may use the inequality
\[
t\leq |A\cap B| \leq |A| + |B| -  |A\cup B| = 2r - n
\]
to obtain the bound $2r-t \geq n$. So, as with the $1$-intersecting case, for large enough $r$ (compared to $t$ and $n$), $[n]^{(r)}$ itself is intersecting.

What about $n=2r-t+1$? Then $|\F_0| = {2r-2t+1 \choose r-t}$. But we could still take all $r$ element subsets of $[n-1]$ to obtain
\[
m(n,r,t) \geq {2r -t \choose r}.
\]
But
\[
{2r -t \choose r} > {2r-t+1 \choose r} \geq {2r -2t+1 \choose r-t}.
\]
So $\F_0$ does not achieve the maximum in this case either.

\begin{exercise}
Prove that $\F_0$ is optimal when $n$ is large compared to $r$ and~$t$.\marginnote{This is the content of \cref{thm:F0max}.}
\end{exercise}

\lect{1}{26}
% \marginnote{Lecture 6: Monday, January 26, 2016.}

Now, consider the family
\[
\F_{r-t} := \{ A\in [n]^{(r)}: |L\cap A|\geq r\}
\]
for some $L$ with $|L| = 2r-t$.
 % maybe:
 % When $n \leq 2r-t$, we may choose all of $[n]^{(r)}$, and this family is optimal.
Define an interpolation between $\F_0$ and $\F_{r-t}$ by
\[
\F_k := \{A\in [n]^{(r)}: |A\cap L|\geq k+t\}
\]
for $|L| = 2k+t$.
Then for $A,B\in \F_k$ we have
\[
|A\cap B\cap L| \geq |A\cap L| + |B\cap L| - |L| \geq 2(k+t) - (2k+t) = t
\]
so for every $0\leq k \leq r-t$, the family $\F_k$ is $t$-intersecting.
~\begin{margintable}[4cm]
\begin{center}
\begin{tabular}{lccc}\toprule
 & \multicolumn{3}{c}{$k$} \\ \cmidrule(r){2-4}
 $n$& $0$ &$1$&$2$\\\midrule
 % & & 0 & 1 & 2\\ \midrule
 $8$ &10 & 16 & \boxed{21}\\
 $11$ & 28 & \boxed{31} & 21\\
 $13$ & \boxed{45}& 41& 21 \\\bottomrule
\end{tabular}
\end{center}
\caption{The size of $\F_k(n)$, given $r=5$ and $t=3$, tabulated over several choices of $k$ and $n$. We see that the maximal $\F_k(n)$ changes based on both $k$ and $n$.}\label{tab:Fks}
\end{margintable}
\begin{example}
Consider $r=5$, $t=3$. Then
\begin{gather*}
|\F_0(n) | = {n-3 \choose 2}, \\
|\F_1(n)| = {5\choose 4}{n-5 \choose 1} + 1 = 5(n-5)+1,\\
|\F_2(n)| = {7\choose 5} = 21.
\end{gather*}
By choosing different values of $n$ (see \cref{tab:Fks}), we see that the maximum size family occurs at different $k$'s: it is a more complicated situation than the $1$-intersecting case.
\end{example}
In general, for $n\geq 2k+t$,
\[
| \F_k(n)| = \sum_{s=k+t}^{2k+t} {2k+t \choose s} {n-2k-t \choose r-s}
\]
where we are summing over possible sizes $s$ of $|A\cap L|$. The first combination comes from choosing within $L$, and the second from choosing outside of $L$.

We may think of $|\F_k(n)|$ as a function of $n$. Then it is a polynomial of degree $r-k-t$ in which the coefficient of the monomial of largest degree is positive. Among the $\F_k(n)$'s, the family $\F_0(n)$ is eventually of the largest size, because it is the polynomial of highest degree. In fact, $\F_0(n)$ is eventually the largest family overall, as the following result shows.
\begin{theorem} \label{thm:F0max}
For all $r,t$ there exists $n_0$ such that for $n\geq n_0$,
\[
m(n,r,t) = |\F_0| = {n-t \choose r-t}.
\]
\end{theorem}
\begin{proof}
Let $\F\subset [n]^{(r)}$ be $t$-intersecting such that $|\F| = m(n,r,t)$. Then there exists $A,B\in \F$ such that
\[
|A\cap B| = t
\]
for $n\geq 2r-t$. Assume not. If $\min_{A,B\in \F} |A\cap B| = t+\ell$ for some $\ell\geq 1$, then choose some  $L\subset A\cap B$ with $|L|=\ell$, and consider the set $A'=A\setminus L$. If $A'\in \F$, then we would have $|A'\cap B|=t < t+\ell$, so $A'\not \in \F$. But for any $C\in \F$, we have
\[
t+\ell \leq |A\cap C| = |A'\cap C| + |L\cap C| \leq |A'\cap C| +\ell,
\]
so $|A'\cap C|\geq t$. Thus, $\F\cup \{A'\}$ is $t$-intersecting and $|\F\cup \{A'\}| > |\F|$, which contradicts the maximality of $\F$.

Now, let $Z=A\cap B$, where $|A\cap B|=t$. If $Z\subset C$ for all $C\in \F$, then $|\F| \leq {n-t \choose r-t}$. So we may assume there exists $C\in \F$ such that $Z\not \subset C$. We will show
\begin{equation}	\label{eq:XcapAcupBcupC_big} \tag{$\star$}
 |X \cap (A\cup B\cup C)|\geq t+1
 \end{equation} for all $X\in \F$. Since each set is of size $r$, the size $L:=|A\cup B\cup C| \leq 3r$. This is enough as it implies
\[
|\F| \leq \sum_{s=t+1}^r {L \choose s} {n-L \choose r-s}
\]
which is a polynomial of degree at most $r-t-1$, and so is eventually less than ${n-t\choose r-t}$, which is a polynomial of degree $n-t$.

Let us show \eqref{eq:XcapAcupBcupC_big}. We know
\[
X\cap (A\cup B\cup C) = (X\cap A)\cup (X\cap B)\cup (X\cap C).
\]
Each set is of size at least $t$; for $|X\cap (A\cup B\cup C)|\leq t$, then $|X\cap (A\cup B\cup C)| = t$, and in particular,  $Y:=X\cap A= X\cap B = X\cap C$. Then $Y\subset Z$, but since $|Y| = |Z| = t$, we have $Y=Z$. Then we have $Z=Y = X\cap C \subset C$, which is a contradiction.
\end{proof}

The following theorem, presented here without proof, resolves our question.
\begin{theorem}[\cite{Ahlswede-Khachatrian-1997}]
For all $n,r,t$,
\[
m(n,r,t) = \max_k |\F_k|.
\]
\end{theorem}

\newthought{Let us consider} a consequence of \erdos-Ko-Rado\sidenote{\Cref{thm:erdos-ko-rado}}. Let $Z_1,\dotsc,Z_n$ be independent Bernoulli random variables  each  with expectation value $p > \tfrac{1}{2}$. Then $\Pr[Z_i=1] = p$, $\Pr[Z_i = 0] = 1-p$.
Suppose the $Z_i$'s are stocks, and the price of each is $\tfrac{1}{2}$. If we invest \$0.50, then our expected return is \$$p$, no matter how we invest.

Suppose we are very conservative and our goal is to have at least $\tfrac{1}{2}$ in the end. A good strategy is to diversify and invest uniformly in each stock; then the law of large numbers says that as the number of stocks goes to infinity, we almost surely recover our $1/2$.
What's the worst possible strategy? It should be to invest in only 1 stock; in that case, our probability of success is $p$.
\begin{theorem}[\cite{LIGGETT197715}]
Let $Z_1,\dotsc,Z_n$ be independent Bernoulli random variables with expectation value $p$. Let $c_1,\dotsc,c_n\geq 0$ with $\sum c_i =1$. Then
\[
\prob \Big[ \sum_{i=1}^nc_i Z_i \geq \tfrac{1}{2} \Big] \geq p.
\]
\end{theorem}
\begin{proof}
Assume $c_i >0$ for all $i$, and $n$ odd for simplicity. Let $\F = \{A\subset [n]: \sum_{i\in A} c_i \geq \tfrac{1}{2} \}$. That is, $\F$ is the collection of all sets of r.v. such that if exactly those random variables obtain return 1, we did not lose.
Let $\F_k = \F \cap [n]^{(k)}$ and $f_k = |\F_k|$. Then
\[
\prob \Big[ \sum_{i=1}^n c_i Z_i \geq \tfrac{1}{2}\Big] = \sum_{A\in \F} \prob [ \text{exactly }Z_i \text{ with indicies }i\in A \text{ have value 1}].
\]
If we fix some $A$ of size $k$, what is the probability that these $k$ trials succeed? $p^k(1-p)^{n-k}$. Thus,
\[
\prob \Big[ \sum_{i=1}^n c_i Z_i \geq \tfrac{1}{2}\Big] = \sum_{k=0}^n f_k p^k (1-p)^{n-k}.
\]
Our goal is to show that the LHS is larger than $p$.
\begin{enumerate}[{Fact }1.]
	\item  $\F_k$ is intersecting for each $k < n/2$.

	\begin{subproof}
	Suppose not: then there exists $A,B\in \F_k$ such that $A\cap B = \emptyset$. Then $\sum_{a\in A} c_i \geq \frac{1}{2}$, and $\sum_{a\in B} c_i \geq \frac{1}{2}$. But since each $c_i>0$, we must have $A\cup B = [n]$, but we know $|A\cup B|< 2\cdot n/2=n$.
	\end{subproof}

	\item[Corollary.] For $k<n/2$, we have $f_k \leq {n-1\choose k-1}$ by \erdos-Ko-Rado. \marginnote{This corollary is the essential idea of the proof; the algebraic computation later is long but trivial.}
	\item $f_k + f_{n-k} \geq {n\choose k}$ for all $k$.

	\begin{subproof}
	For every $A$, either $A\in \F$ or $[n]\setminus A \in \F$: if the sum of some collection of $c_i$ is less than one half, then the sum of the rest must be at least one half. Thus, for $A\in [n]^{(k)}$, either $A\in \F_k$, or $[n]\setminus A\in \F_{n-k}$, and $f_k + f_{n-k} \geq |[n]^{(k)}| = {n\choose k}$.
	\end{subproof}
\end{enumerate}
Now,
\begin{align*}
\sum_{k=0}^n f_k p^k (1-p)^{n-k}&= \sum_{k<n/2} (f_k + f_{n-k}) p^{n-k}(1-p)^k \\
&\qquad+ \sum_{k< n/2}  f_k ( p^k (1-p)^{n-k}) - p^{n-k}(1-p)^k).\\
\intertext{By fact 2,}
&\geq \sum_{k<n/2}{n\choose k} p^{n-k}(1-p)^k \\
&\qquad+ \sum_{k< n/2}  f_k ( p^k (1-p)^{n-k}) - p^{n-k}(1-p)^k).\\
\intertext{Since $ p^k (1-p)^{n-k}) - p^{n-k}(1-p)^k < 0$,  the corollary to fact 1 yields}
&\geq \sum_{k<n/2} {n\choose k}p^{n-k}(1-p)^k \\
&\qquad+ \sum_{k< n/2}  {n-1\choose k-1}( p^k (1-p)^{n-k}) - p^{n-k}(1-p)^k). \\
\intertext{Now we may group powers of $p$ to obtain}
&= \sum_{k<n/2} p^{n-k} \left[ {n\choose k}(1-p)^k - {n-1 \choose k-1}(1-p)^k  \right] \\
&\qquad\qquad +p^k \left[ {n-1\choose k-1}(1-p)^{n-k} \right].\\
\intertext{We may pull out $(1-p)^k$ of the first term, and use that ${n\choose k}- {n-1 \choose k}={n-1\choose k-1}$ to obtain}
&= \sum_{k<n/2} p^{n-k} \left[ {n-1\choose k-1}   \right](1-p)^k + p^k \left[ {n-1\choose k-1} \right](1-p)^{n-k}.\\
\intertext{Now, in first term we may switch to summing over $k>n/2$, swapping $n-k$ with $k$ to get}
&= \sum_{k>n/2} p^{n-k} \left[ {n-1\choose k-1}   \right](1-p)^k \\
&\qquad+\sum_{k<n/2} p^k \left[ {n-1\choose k-1} \right](1-p)^{n-k}.\\
% &= \sum_{k<n/2} {n-1\choose k-1} p^k(1-p)^{n-k} \\
% &\qquad+ \sum_{k>n/2} \Bigg({n\choose k}- {n-1 \choose k} \Bigg) p^k(1-p)^{n-k}\mathnote{Using ${n-1\choose k-1}={n\choose k}- {n-1 \choose k}$.}\\
&=\sum_{k=1}^n {n-1 \choose k-1} p^k (1-p)^{n-k}\\
&= p \sum_{\ell=0}^{n-1} {n-1 \choose \ell} p^\ell (1-p)^{n-1-\ell} \\
&= p( p + (1-p))^{n-1} =p. \qedhere
\end{align*}
\end{proof}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Written By Michael Brodskiy
% Class: Analytic Geometry & Calculus III (Math-292)
% Professor: V. Cherkassky
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[12pt]{article}
\usepackage{alphalph}
\usepackage[utf8]{inputenc}
\usepackage[russian,english]{babel}
\usepackage{titling}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{amssymb}
\usepackage[super]{nth}
\usepackage{everysel}
\usepackage{ragged2e}
\usepackage{geometry}
\usepackage{fancyhdr}
\geometry{top=1.0in,bottom=1.0in,left=1.0in,right=1.0in}
\newcommand{\subtitle}[1]{%
  \posttitle{%
    \par\end{center}
    \begin{center}\large#1\end{center}
    \vskip0.5em}%

}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=blue,
citecolor=blue,
}

\urlstyle{same}

\title{Lecture XX Notes}
\date{\today}
\author{Michael Brodskiy\\ \small Professor: V. Cherkassky}

% Mathematical Operations:

% Sum: $$\sum_{n=a}^{b} f(x) $$
% Integral: $$\int_{lower}^{upper} f(x) dx$$
% Limit: $$\lim_{x\to\infty} f(x)$$

\begin{document}

\maketitle

\section{Line Integrals (Continued) $-$ 16.2}

A line integral defined by the function $f(x,y,z)$ may be integrated by using the formula:

$$\int_C f(x,y,z)\,ds=\int_C f(x(t),y(t),z(t))\sqrt{\left(\frac{dx}{dt}\right)^2+\left(\frac{dy}{dt}\right)^2+\left(\frac{dz}{dt}\right)^2}\,dt$$

Given a vector function, $\overrightarrow{r}(t)$, the above formula may be simplified to yield:

$$\int_C f(x,y,z)\,ds=\int_C f(\overrightarrow{r}(t))|\overrightarrow{r}'(t)|\,dt$$

Line integrals may also appear with respect to $dx$ and $dy$. This may be simplified by finding $dx$ and $dy$ in terms of $dt$:

$$\int_C f(x,y)\,dx = \int_a^b f(x(t),y(t))x'(t)\,dt$$
$$\int_C f(x,y)\,dy = \int_a^b f(x(t),y(t))y'(t)\,dt$$

When line integrals appear in such a form it is abbreviate them:

$$\int_C P(x,y)\,dx+\int_C Q(x,y)\,dy=\int_C P(x,y)\,dx+Q(x,y),dy$$

In physics, the formula for work was defined as $W=Fd\cos(\theta)$

This may be found by integrating:

$$W=\int_a^b \overrightarrow{F}(\overrightarrow{r}(t))\cdot\frac{\overrightarrow{r}'(t)}{|\overrightarrow{r}'(t)|}\,ds$$
$$W=\int_a^b \overrightarrow{F}(\overrightarrow{r}(t))\cdot\frac{\overrightarrow{r}'(t)}{|\overrightarrow{r}'(t)|}|\overrightarrow{r}'(t)|\,dt$$
$$W=\int_a^b \overrightarrow{F}(\overrightarrow{r}(t))\cdot\overrightarrow{r}'(t)\,dt$$
$$W=\int_a^b \overrightarrow{F}(\overrightarrow{r}(t))\cdot\overrightarrow{r}'(t)\,dt$$
$$W=\int_a^b \overrightarrow{F}\cdot\overrightarrow{r}\,dt$$

This yields the work done on a particle by a field.

\end{document}


\section{Decision Tree Surrogate Model}

\begin{frame}[c]
\Huge{\centerline{Decision Tree Surrogate Model}}
\end{frame}

%----------------------------------------------------------------------------------------

\begin{frame}\frametitle{Decision Tree Surrogate Model}
	\begin{itemize}
		\item Given our learned function $g$ and set of predictions, $g(\mathbf{X}) = \hat{\mathbf{Y}}$, we can train a decision tree surrogate model:
			\begin{equation}
			\begin{aligned}
				h_{\text{tree}}: \mathbf{X},\hat{\mathbf{Y}} \xrightarrow{\mathcal{A}_{\text{surrogate}}} h_{\text{tree}}
			\end{aligned}
			\end{equation}

		\item The decision tree surrogate model displays an approximate flow chart of $g$'s decision making process to increase model transparency.
		\item It also shows likely important features and the most important interactions in $g$.

	\end{itemize}
\end{frame}

%----------------------------------------------------------------------------------------


\section{Additions}

Calculer les additions suivantes :
\begin{questions}

	\question[1]  $(-15) + (+ 5)$
	\fillwithdottedlines{1cm}
	\begin{solution}

	\end{solution}

	\question[1]  $(+20) + (+ 40)$
	\fillwithdottedlines{1cm}
	\begin{solution}

	\end{solution}

	\question[1]  $(-305) + (-27)$
	\fillwithdottedlines{1cm}
	\begin{solution}

	\end{solution}

	\question[1]  $(-35) + (+27)  + (-20)$
	\fillwithdottedlines{1.5cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(-35) + (-27)  + (+20)$
	\fillwithdottedlines{1.5cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(-75) + (-25) + (+37)$
	\fillwithdottedlines{1.5cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(+85) + (-17) + (+15) + (-23)$
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(-43) + (-25) + (+57) + (+25)$
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}
\end{questions}

\section{Soustractions}

Transformer les soustractions en additions avant de les calculer
\begin{questions}

	\question[2]  $(+78) - (+24)$
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(-34) - (+57)$
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(+24) - (-32)$
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(-34) + (+57) - (-23) $
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}

	\question[2]  $(-55) - (-35) - (+27) $
	\fillwithdottedlines{2cm}
	\begin{solution}

	\end{solution}

\end{questions}


\section{Normal Coordinate Calculation}\index{Normal coordinates!calculation of|(}
\subsection{Calculation of Vibrational Frequencies}
For a simple harmonic oscillator the period $r$ is given by:
$$
r = 2 \pi \sqrt{\frac{\mu}{k}}
$$
where $k$ is the force constant.  A molecule can absorb a photon that vibrates
at the same frequency as one of its normal vibrational modes.  That is, if a
molecule, initially in its ground vibrational state, could be excited so that
it vibrated at   a given frequency, then that molecule could absorb a photon
that vibrates at the same frequency.  Although vibrational frequencies are
usually expressed as kilohertz or megahertz, in chemistry vibrational
frequencies are normally  expressed in terms of the number of vibrations that
would occur in the time that light travels one centimeter, i.e., $\bar{\nu} =
1/cr$ Using this equation for simple harmonic motion, the vibrational frequency
can be written as:
$$
\bar{\nu} = \frac{1}{2\pi c}\sqrt{\frac{k}{\mu}}.
$$
In order for $\bar{\nu}$ to be in cm$^{-1}$, $c$, the speed of light must be in
cm.sec$^{-1}$, $k$, the force constant in erg/cm$^2$, and $\mu$ the reduced
mass in grams.

For a molecule, the force constants are obtained by diagonalization of the
mass-weighted Hessian matrix.  Most of the work in calculating vibrational
frequencies is spent in constructing the Hessian.

\subsubsection*{Calculation of the Hessian matrix}
The elements of the Hessian are defined as:
$$
H_{i,j} = \frac{\delta^2E}{\delta x_i\delta x_j}
$$
and  are generated by use of finite displacements, that is, for each  atomic
coordinate $x_i$, the coordinate is first incremented by a small amount, the
gradients  calculated, then the coordinate is decremented and the gradients
re-calculated. The second derivative is then obtained from the difference of
the two derivatives and the step size:
$$
H_{i,j} = \frac{(\frac{\delta E}{\delta x_i})_{_{+0.5\Delta x_j}}-
                (\frac{\delta E}{\delta x_i})_{_{-0.5\Delta x_j}}}
          {\Delta x_j}.
$$
This is done for all $3N$ Cartesian coordinates.  Because the Hessian is
symmetric, that is $H_{i,j}=H_{j,i}$, the random errors that occur in the
gradient calculation can be reduced (by a factor of $\sqrt{2}$) by re-defining
the Hessian as:
$$
H_{i,j} = \frac{1}{2}\left(\frac{(\frac{\delta E}{\delta x_i})_{_{+0.5\Delta x_j}}-
                (\frac{\delta E}{\delta x_i})_{_{-0.5\Delta x_j}}}
          {\Delta x_j}+
 \frac{(\frac{\delta E}{\delta x_j})_{_{+0.5\Delta x_i}}-
                (\frac{\delta E}{\delta x_j})_{_{-0.5\Delta x_i}}}
          {\Delta x_i}\right).
$$
A call to the energy - gradient function \comp{COMPFG} will generate the
gradients in kcal/mol/\AA ngstrom at a given geometry.  These can then be
converted into millidynes/\AA ngstrom (or 10$^5$ dynes/cm) as follows:
$$
H_{i,j} (\mathrm{millidynes/\AA}) = 10^5\frac{({\mathrm{Kcal\ to\
ergs}})}{(\mathrm{\AA\ to\ cm})^2(\mathrm{Mole\ to\ molecule})}H_{i,j}(\mathrm{kcal/mol/\AA ^2 })
$$
or
$$
H_{i,j} (\mathrm{millidynes/\AA}) = 10^5\frac{4.184*10^3*10^7}{(10^{-8*2})
(6.022*10^{23})}H_{i,j}(\mathrm{kcal/mol/\AA ^2}).
$$
Diagonalization of this matrix yields the force constants of the system.

In order to calculate the vibrational frequencies, the Hessian matrix is first
mass-weighted:
$$
H^m_{i,j} = \frac{H_{i,j}}{\sqrt{M_i*M_j}}.
$$

Then the Hessian is converted from millidynes per \AA ngstrom to dynes per
centimeter by multiplying by 10$^5$.

Diagonalization of this matrix yields eigenvalues, $\epsilon$, which represent
the quantities $\sqrt{k/\mu}$, from which the vibrational frequencies can be
calculated:
$$
\bar{\nu}_i = \frac{1}{2\pi c}\sqrt{\epsilon_i}.
$$

\subsection{Mechanism of the frame in FORCE calculation}
\index{Frame!description of}
The FORCE calculation uses Cartesian coordinates, and all 3N  modes are
calculated, where N is the number of atoms in the system.  Clearly, there will
be 5 or 6 ``trivial'' vibrations,  which  represent  the  three translations
and two or three rotations.  If the molecule is exactly at a stationary point,
then these ``vibrations'' will have a  force  constant and  frequency  of
precisely  zero.   If the force calculation was done correctly, and the
molecule was not exactly at a stationary point,  then the  three  translations
should be exactly zero, but the rotations would be non-zero.  The extent to
which  the  rotations  are  non-zero  is  a measure of the error in the
geometry.

If  the  distortions  are  non-zero,  the  trivial  vibrations  can interact
with  the  low-lying genuine vibrations or rotations, and with the transition
vibration if present.

To prevent this the analytic form of the rotations  and  vibrations is
calculated,  and arbitrary eigenvalues assigned; these are 500, 600, 700, 800,
900, and 1000 millidynes/\AA ngstrom for Tx, Ty, Tz, Rx,  Ry  and Rz  (if
present),  respectively.  The rotations are about the principal axes of inertia
for the system, taking  into  account  isotopic  masses. The ``force matrix''
for these trivial vibrations is determined, and added on to the calculated
force matrix.  After diagonalization the  arbitrary eigenvalues are subtracted
off the trivial vibrations, and the resulting numbers are the ``true'' values.
Interference with genuine vibrations  is thus avoided.

\subsection{Vibrational Analysis}\index{Vibrational analysis}
Analyzing normal coordinates is very tedious.  Users  are  normally familiar
with the internal coordinates of the system they are studying, but not familiar
with the Cartesian coordinates.  To  help characterize the  normal
coordinates,  a very simple analysis is done automatically, and users are
strongly encouraged to use this analysis first,  and  then to look at the
normal coordinate eigenvectors.

In the analysis, each pair of bonded atoms is examined  to  see  if there  is
a  large  relative  motion  between them.  By bonded is meant \index{Van der
Waals|ff} within the van der Waals' distance.  If there  is  such  a  motion,
the indices  of  the  atoms,  the  relative  distance  in \AA ngstroms, and the
percentage radial motion are printed.   Radial  plus  tangential  motion adds
to  100\%,  but  as there are two orthogonal tangential motions and only one
radial, the radial component is printed.

Vibrations in the range +50 to {\em i}50 cm$^{-1}$ cannot be described
accurately in the vibrational analysis, due to numerical difficulties. However,
the normal coordinates and frequencies are printed in the section above
``DESCRIPTION OF VIBRATIONS''.

\subsection{Reduced masses}\index{Reduced mass|ff}\index{Simple harmonic motion}
\index{Mass!reduced}
A molecular vibration normally involves all the atoms moving simultaneously.
This is clearly very different from the  simple harmonic motion of a mass
attached to a spring that is attached to an immovable object.  Nevertheless, it
is convenient to visualize a molecular vibration as consisting of a single
mass, $M$, on the end of a spring of force constant $k$. For such a  system,
the period of vibration, $T$, is given by:
$$
T=2\pi\sqrt{\frac{M}{k}}.
$$

How, then, do we relate the complicated motion of a molecular vibration to the
mass and spring model?

During a molecular vibration, each atom follows a simple harmonic motion. So
the problem is, to what extent does each atom contribute to the mass, and to
what extent does each atom contribute to the spring?

In order to answer this, first consider some simple systems.  In the system
H--X, where X has a very large mass, compared to that of the H,  the effective
mass is obviously that of H.  In H$_2$, the effective mass is half that of a
single H.   Why is this so?  In H--X, particle X is stationary, and particle H
contributes 100\% of the energy to the vibration.  In H$_2$, each particle
obviously contributes 50\%, but now the center of mass is half way between the
two particles.  If the force constants are the same in H--X and in H--H, then
the period of vibration of H--X will be $\sqrt{2}$ times that of H--H.  This is
the same period as for a system of two particles, each of which having a mass
twice that of a H particle.  For a system of two particles, $A$ and $B$, having
masses $M_A$ and $M_B$,  the vibrational wavefunction, $\psi_v$, is:
$$
\psi_v=\sqrt{\frac{M_B}{M_A+M_B}}\psi_A-\sqrt{\frac{M_A}{M_A+M_B}}\psi_B.
$$
This can be interpreted as particle $A$ moves $(\sqrt{\frac{M_B}{M_A+M_B}})^2$
in the time particle $B$ moves $(\sqrt{\frac{M_A}{M_A+M_B}})^2$.  The center of
mass, $\rho$, stays constant:
$$
\rho=\sum_iM_i\delta x_i = M_A\frac{M_A}{M_A+M_B}- M_B\frac{M_A}{M_A+M_B} = 0.
$$
The square of the coefficients of the wavefunction represent the contribution
to the motion.  The effective mass, $\mu$, for this system is:
$$
\mu = \frac{M_A\times M_B}{M_A+M_B}.
$$
What fraction is due to $A$ and what fraction is due to $B$?  From the
wavefunction, the intensity of $A$ is $\frac{M_B}{M_A+M_B}$, and the relative
rate of motion is also $\frac{M_B}{M_A+M_B}$, so the contribution to the
effective mass due to $A$ is:
$$
(\frac{M_B}{M_A+M_B})M_A;
$$
likewise, for $B$:
$$
(\frac{M_A}{M_A+M_B})M_B.
$$
Consider two particles, $A$ and $B$, of mass 1 and 4, respectively.   The
wavefunction for the vibration is:
$$
\psi_v = \sqrt{\frac{4}{5}}\psi_A-\sqrt{\frac{1}{5}}\psi_B,
$$
where $A$ contributes
$$
\frac{16}{25}\times 1 = 0.64
$$
and $B$ contributes
$$
\frac{1}{25}\times 4 = 0.16
$$
to the effective mass of $\frac{4}{5}$.

In other words, the contribution to the effective mass is equal to the
intensity of the wavefunction on each atom, times the mass of the atom, times
the intensity of the wavefunction.  This is intuitively correct:  the total
vibration is composed of contributions from each particle, and the amount each
particle contributes is proportional to its intensity in the wavefunction.  The
mass of each particle is also proportional to its intensity in the
wavefunction.

Extension to polyatomic molecules is now trivial.  The effective mass is given
by:
$$
\mu = \sum_A <\!\psi_AM_A\psi_A\!>\times <\!\psi_A|\psi_A\!>.
$$
When written in this way, the quantum nature of the expression is obvious.
However, for computational convenience, the effective mass is rewritten as:
$$
\mu = \sum_A(\psi_A^2)^2\times M_A
$$
or
$$
\mu = \sum_A(\sum_{i=1}^3c_{A_i}^2)^2M_A.
$$
This expression is suitable for most systems.  However, it is not a
well-defined quantity.  Under certain circumstances involving degenerate
vibrations, the quantity $\mu$ can become ill-defined.  This phenomenon can be
attributed to the  fact that the reduced mass is not an observable.

\subsection{Effective masses}\index{Mass!effective}\index{Effective mass}
Another way of regarding the effective mass of a mode can be derived from
consideration of the simple harmonic oscillator:
$$
   E =  \sqrt{\frac{k}{\mu}}.
$$
Diagonalization of the mass-weighted Hessian yields the energies, and from the
normal coordinates the force-constants can readily be derived.  From these two
quantities, the effective mass can readily be calculated:
$$
\mu = \frac{k}{E^2}.
$$
For a homonuclear diatomic, the effective mass calculated this way is equal
to the mass of one atom.

\subsection{Travel}\index{Travel}
To continue the idea of representing a normal mode as a simple harmonic
oscillator, the distance the atoms move through can be represented as the
distance the idealized mass moves through.  This can be calculated knowing the
energy of the mode and the force constant:
$$
     E = \frac{1}{2}kx^2.
$$
Here $k$ is the force-constant for the mode, and is given by
$$
k = <\psi|Hessian|\psi>;
$$
$E$ is the energy of the mode.

From this, the distance, $x$, which the system moves through, can be calculated
from
$$
x =\sqrt{\frac{2\times 1.196266\times 10^8 \times 1000 \times 10^8 \nu}{N k}},
$$
where $1.196266\times 10^8$ is the conversion factor from cm$^{-1}$ to ergs,
1000 converts from millidynes to dynes, $10^8$ converts from cm to \AA , and
$N$ converts from moles to molecules.

Note that $x$, which in the output is called TRAVEL, is in mass weighted
space, not simple space.  This quantity can also be calculated using the DRC,
by depositing one quantum of energy into a vibrational mode.  For a system at a
stationary point, the relevant keywords would be \comp{IRC=1 DRC t=1m}.  For
larger systems, the time may need to be increased.  At least one coordinate
must have an optimization flag set to 1.  This is required in order to instruct
the DRC to print the turning points. \index{Normal coordinates!calculation
of|)}


\section{Background}
In this section we will discuss the various tools and break down how we will
formulate the project. In the next section we will discuss our proposition of
how to achieve these goals.

\subsection{Q-Learning}
For this project we will be using a Q-Learning algorithm to play these
videogames. The Q-Learning algorithm is a simple reinforcement learner that
follows Bellman's Equation. A Q matrix can be defined where each row represents
a different state and each column represents an action for that state. These
can be used to solve different Markov Decision Processes (MDPs). An example
MDP is shown in Figure~\ref{fig:MDP}.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.6\textwidth]{MDP.pdf}
\caption{Sample Markov Decision Process}
    \label{fig:MDP}
\end{figure}
Each node, labeled $s_i$, represents a state and the connections between nodes
represent actions. Each action has an associated probability with it. There
are certain ``rewards" that an actor gets for taking certain actions, $a$, which
leaves them in a new state, $s'$. We then want to find a set of actions, also
known as a policy ($\pi$) that gives us our maximal value ($V$). Knowing this
we want to create a learner that will learn the best policy, set of actions,
that will maximize its reward. The Bellman Equation, Equation~\ref{eq:Bell},
does just this. In this equation $\alpha$ represents the learning rate,
$\gamma$ is the discount rate, and $Q(s',a')$ is the value in our $Q$ matrix
given the next state and action.
\begin{equation}
    Q(s,a) = Q(s,a) + \alpha(r + \gamma \max_{a'}Q(s',a') - Q(s,a))
    \label{eq:Bell}
\end{equation}
Essentially what we are doing is looking at the reward we get for taking an
action and all future rewards given our best policy. A discount rate is
applied so that rewards that are achieved sooner are weighted more heavily
than rewards further away. A simple example of why we might want to do this
is because if we give a mouse a reward for completing a maze we want the mouse
to finish the maze as fast as possible and not take an infinite time to finish.
The learning rate applies a weight to to our new reward and future rewards.
Additionally all actions are probabilistic. If we have the actions forward,
left, and right, if we select forward there is still a probability that the
agent goes left or right. This is commonly known as a Monte-Carlo walk (or
Drunken Walk).

For our videogames we discussed that there are potentially an unknown number
of states. There are also limitations in computational hardware. To account
for this we can simply limit the depth of our lookahead values. This will
create an approximate Q-Learner and allow us to solve problems with unknown
or infinite number of states.

\subsection{Deep Q-Learning}
An extension of Q-Learning is deep Q-Learning (DQN) is by using a neural network
to compute the above solution. We can accomplish this because a neural network
is a universal function approximater, meaning it can approximate any function.
Following DeepMind's work~\cite{DeepMind}, if we rewrite the Bellman Equation as
\begin{equation}
    Q^*(s,a) = \mathbb{E}_{s'\sim\mathcal{E}}\left[r + \gamma\max_{a'}Q^*(s',a')\right]
\end{equation}
We can generate a loss function $L_i(\theta_i)$
\begin{equation}
    L_i(\theta_i) = \mathbb{E}_{s,a\sim\rho(\dot)}\left[(y_i - Q(s,a;\theta_i))^2\right]
\end{equation}
where $y_i = \mathbb{E}_{s'\sim\mathcal{E}}\left[r + \gamma\max_{a'}Q(s',a';\theta_{i-1})\right]$. Recognizing that this is the same format as the Mean Square
Error (MSE) we can generate the following gradient (Equation~\ref{eq:DQNgrad}).

\begin{equation}
    \nabla_{\theta_i}L_i(\theta_i) =
\mathbb{E}_{s,a\sim\rho(\dot);s'\sim\mathcal{E}}
\left[\left(r + \gamma\max_{a'}Q(s',a';\theta_{i-1}) - Q(s,a;\theta_i)\right)
\nabla_{\theta_i}Q(s,a;\theta_i)\right]
\label{eq:DQNgrad}
\end{equation}

Here we have everything we need to create a DQN. The property of universal
approximation handles the lookahead depth for us. This is because a
neural network iterates to create an approximate function. Being that our
games are NP- or PSPACE-Hard this is reasonable.

\subsection{Integration into Retro}
Unfortunately SMK was not able to be integrated into Retro's environment, but
much about the environment and integration was learned along the way.
Retro relies on different ``states", which can be defined as different
starting states.
This is placed in quotes because the state will continuously be changing as
the agent progresses through the level, more on this later. For
this project we started with the scenario where there is a single agent
playing the 50cc Mushroom Cup. Essentially we set a place in the game where we
want our agent to start. Retro provides a UI where we can interactively play
the game and save specific states. For this example our initial state looks like
this:
\begin{figure}[h!]
    \centering
    %\includegraphics[clip,trim=0 500 0 10,width=0.55\textwidth]{1P_GrandPrix_50cc_MushroomCup.png}
    \includegraphics[width=0.55\textwidth]{1P_GrandPrix_50cc_MushroomCup.png}
    \caption{Initial state for 1P\_GrandPrix\_50cc\_MushroomCup.state}
\end{figure}
\FloatBarrier
From this we can see that in the upper half we see our player and the world
around us. In the bottom half we see the entire map and can actually identify
certain features like coins and items. We will be focusing on the top half.

Now that we have where we want to start, we need to define everything else.
Retro uses json files to define many different parts to the game. There are
two important files for each game: data.json and scenario.json. Data defines
information about the level, lives, and score. Scenario defines the
ending condition and the reward structure. An example of values are shown
below
\\
\begin{figure}[h!]
\begin{minipage}{0.47\textwidth}
    \begin{lstlisting}[caption=data.json, frame=tlrb]{Name}
{
"info": {
    "lap": {
        "address": 8261825,
        "type": "|u1"
    },
    "lapsize": {
        "address": 8257864,
        "type": "|u1"
    },
    "coins": {
        "address": 8261120,
        "type": "|u1"
    },
    "minute": {
        "address": 8257796,
        "type": "|u1"
    },
    "second": {
        "address": 8257794,
        "type": |u1"
    \end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{0.47\textwidth}
    \begin{lstlisting}[caption=scenario.json, frame=tlrb]{Name}
{
"done": {
    "variables": {
        "finish": {
            "reference": "coins",
            "op": "eq"
        }
    }
},
"reward": {
    "variables": {
        "rank": {
            "reward": 1.0
        }
    }
},
"actions": [
    [[], ["UP"], ["DOWN"]],
    [[], ["LEFT"], ["RIGHT"]],
    [[], ["A"], ["B"], ...
}
    \end{lstlisting}
\end{minipage}
    \caption{Snippets from data.json and scenario.json for SNES Super Mario Kart}
\end{figure}
The data.json file contains hex addresses that represent the RAM address that the
game uses to determine certain aspects of the game. We also need to add these
to a rambase value that is given by retro (warning: this is not noted within
the documentation but is required).
Additionally, scenario.json can contain information about the action set. We can
either restrict our action set to a specific set of buttons, and button
combinations, or we can expose all possible set of actions to the agent. Initially
we will be limiting our agent, since certain actions like ["DOWN","UP"] are
not likely to be useful, excluding glitches. Super Mario Kart has the following
set of actions: Steering with the D-Pad, B for Accelerate, A to use an item,
Y for Brakes, X for the rear-view, L or R for Hop/Power-side (same action),
Start to pause the game, and select for Rear-View. We will limit the agent
so that it may not look behind itself, cannot pause the game, and we will select
R for hop/power-slide (because that is what I use when playing). So our new full
action set for the steering will be \{\{UP\},\{DOWN\},\{LEFT\},\{RIGHT\},
\{UP,LEFT\}, \{UP, RIGHT\}, \{DOWN,RIGHT\}, \{DOWN,LEFT\}\}.
This represents the left side of the controller. For the right side of the
controller we will assume that the agent can play like a human does
(this will also limit glitches that the agent might be able to exploit).
The controls action set will be defined as \{\{B\},\{A\},\{Y\},
\{R\},\{A,B\},\{B,R\},\{A,R\}\}. The full action set is the combination of
actions from the steering set and the control set.

RAM addresses can be found by using emulator tools, such as BizHawk~\cite{BH},
that allow for hex searching and manipulation. Some values are easy to find
since their values in RAM identically match the values displayed on the screen.
Other values may be more difficult to find given that they may be manipulated
by the program. An example of finding a memory address in BizHawk is shown in
Figure~\ref{fig:hex} in the Appendix. We were unable to find all the required
address on our own, but a prominant Speed-Runner\footnote{People who compete
to finish a game as quickly as possible} named SethBling~\cite{SethBling}
kindly provided me with Lua scripts to help me find the missing addresses.
Section~\ref{sec:RAM} provides a more detailed discussion on finding RAM values.

To summarize the integration: we need to fully define how our agent will receive
rewards, when the agent ends, and our action set.

\subsection{Rewards}
Defining rewards is always a challenging problem in any machine learning task.
There are many fantastic examples where a well thought out reward function leads
to outcomes not expected by the machine learning engineer. Rewards can be
simple or extremely complex. For example a simple reward function for Super
Mario Bros might be to use the x position on the screen and reward the agent
for making forward progression through the level.

A more detailed discussion of rewards for Super Mario Kart are given in the
appendix~\ref{sec:SMKRewards}. Additionally sections are provided for
determining the end state~\ref{sec:endstate} and visualizing the environment
\ref{sec:env} specifically in the SMK world.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts, amssymb, hyperref,graphicx}
\usepackage[letterpaper, total={7.5in, 9in}]{geometry}
\title{Intermediate Analysis}
\author{Grant Smith }
\date{Spring 2022}

\begin{document}

\maketitle

\section{Sequence Convergence}

\begin{enumerate}
    \item Prove the convergence of $$\lim_{n\rightarrow \infty} (\sqrt{n^{2}+n}-n)$$
    \begin{enumerate}
        \item First, notice that this equation approaches 0.5 as n increases.  Thus, we will be proving:
        $$\forall \epsilon > 0,  \exists N s.t. \forall n > N, \left| \sqrt{n^{2}+n}-n - \frac{1}{2}\right| < \epsilon$$
        \item which is the same as proving both of the following:
        $$ \sqrt{n^{2}+n}-n - \frac{1}{2} < \epsilon$$
        $$ \sqrt{n^{2}+n}-n - \frac{1}{2} > -\epsilon$$
        \item then add $n + 1/2$ to both sides
        $$ \sqrt{n^{2}+n} < \epsilon + n + \frac{1}{2}$$
        $$ \sqrt{n^{2}+n} > -\epsilon + n + \frac{1}{2}$$
        \item square both sides to get:
        $$ n^{2}+n < \epsilon^2 + 2\epsilon n + 2 \epsilon .5 + n^2 + 2 n .5 + .25$$
        $$ n^{2}+n > \epsilon^2 - 2\epsilon n - 2 \epsilon .5 + n^2 + 2 n .5 + .25 $$
        \item cancel the $n^{2}+n$ from both sides to get
        $$ 0 < \epsilon^2 + 2\epsilon n + 2 \epsilon .5 + 0 .25$$
        $$ 0 > \epsilon^2 - 2\epsilon n - 2 \epsilon .5 + 0 .25 $$
        \item Bring the $2\epsilon n$ to the other side
        $$ -2\epsilon n < \epsilon^2  + 2 \epsilon .5 + 0 .25$$
        $$ 2 \epsilon n > \epsilon^2  - 2 \epsilon .5 + 0 .25 $$
        \item divide by $2\epsilon$ on the second, and divide by $-2\epsilon$ on the first, which flips the direction of the $<$ to $>$.
        $$ n > \frac{\epsilon^2  + 2 \epsilon .5 + 0 .25}{-2\epsilon} $$
        $$ n > \frac {\epsilon^2  - 2 \epsilon .5 + 0 .25}{2 \epsilon} $$
        \item Given that both of these need to be true, and the second is bigger than the first because the second is positive and the first is negative, we can just use the second requirement. Also, notice that this is a decreasing function in $\epsilon$, as desired.
    \end{enumerate}
    \newpage
    \item Prove the convergence of $$\lim_{n\rightarrow \infty} \left(1+\frac{1}{n}\right)^{3}$$
    \begin{enumerate}
        \item First, note that this sequence approaches 1. Thus, we need to prove that:
            $$\forall \epsilon > 0,  \exists N s.t. \forall n > N, \left| \left(1+\frac{1}{n}\right)^{3} - 1\right| < \epsilon$$
        \item Noting that
        $$\left(1+\frac{1}{n}\right)^{3} > 1$$
        We can drop the absolute value and get
        $$\left(1+\frac{1}{n}\right)^{3} - 1 < \epsilon$$
        \item Move the 1 to the other side to get:
        $$\left(1+\frac{1}{n}\right)^{3} < 1+ \epsilon$$
        \item Take the cube root of both sides
        $$1+\frac{1}{n} < \left(1+ \epsilon\right) ^{\frac{1}{3}}$$
        \item subtract from to both sides
        $$\frac{1}{n} < \left(1+ \epsilon\right) ^{\frac{1}{3}} -1$$
        \item switch positions of the fractions
        $$\frac{1}{\left(1+ \epsilon\right) ^{\frac{1}{3}} -1} < n$$
        \item Switch sides:
        $$n > \frac{1}{\left(1+ \epsilon\right) ^{\frac{1}{3}} -1}$$
        Which increases as $\epsilon$ decreases

    \end{enumerate}
    \newpage
    \item Prove the convergence of:
    $$\lim_{n\rightarrow \infty} \frac{\sin n}{n^{2}}$$
        \begin{enumerate}
        \item First, note that this sequence approaches 0. Thus, we need to prove that:
            $$\forall \epsilon > 0,  \exists N s.t. \forall n > N, \left|\frac{\sin n}{n^{2}}\right| < \epsilon$$
            \item Noting that $n^2 > 0$, we can rewrite as:
            $$ \frac{\left|\sin n\right|}{n^{2}} < \epsilon$$
            \item Noting that
            $$\left|\sin n\right| \leq 1 \rightarrow \frac{\left|\sin n\right|}{n^{2}} \leq \frac{1}{n^2} \leq \frac{1}{n} $$
            \item Which means that if we can prove convergence of $1/n$, then we have proven the convergence of the sequence in question. Thus, we want:
            $$\frac{1}{n} < \epsilon$$
            or
            $$\frac{1}{\epsilon} < n$$
            or
            $$n > \frac{1}{\epsilon}$$

    \end{enumerate}
                \newpage
            \item Prove that if $\{b_n\}$ is a sequence of positive terms and $b_n \rightarrow b > 0$, then there is a positive lower bound $m > 0$ such that $b_n \geq m$ for all $n$.
\begin{itemize}
    \item If $b_n \rightarrow b > 0$, then for any $\epsilon$ there exists an $N$ such that for all $n > N$, $|b_n - b| < \epsilon$. In particular, if we let $\epsilon = b/2$, then for all $n > N$, we can ensure that $b_n > b/2$, and since $b > 0$, we know that $b/2 > 0$.
    \item Next, since there are only a finite number of $b_n$ values before N, we can take the minimum of those values. Call this $m'$.  We know that each term is positive, so $m'$ is positive.
    \item Next, we take the minimum of $b/2$ and $m'$, and we can take half of that value as well, and call it m: $\min  \left\{b/2,m'\right\}/2 = m$. This $m$ is one of infinitely many possible lower bounds.
\end{itemize}
\end{enumerate}

\section{Measure Theory Limit}

I really like single sided epsilon balls, and the related idea of single sided limits.  I especially like the deleted versions in which the point about which we're considering is deleted, but I'll explain more about that soon.  Single sided limits great because on either side of a single domain point, a function can behave totally differently. Which is sort of the whole idea. Every point of the continuum is a hinge point between what's on its left and on its right, and the behavior of the two sides can be completely different. So I think single sided limits/neighborhoods are nice because they allow us to treat these two parts of functions in their own right.

Deleted neighborhoods and limits which exclude the individual point are great because they ignore what happens at the single point. And what's also interesting is that they ignore every other point. For example, if you pick a given point, you can make your delta ball small enough to exclude that point. The only situation in which you can include something would be an infinite set in which the point in question is a limit point of that set. So no finite set or individual point is ever considered in a single-sided, deleted neighborhood. And in fact, no point is ever considered in a deleted neighborhood, regardless if it's left or right. And this applies to every single domain point, so under this thinking, no finite set of points or single point ever matters.

I also really like the idea of considering properties that hold almost everywhere, and ignoring properties that hold almost nowhere. The notion of almost everywhere is awesome because it ignores the behavior of sets with zero measure. This means that any behaviors that are oddities can be ignored.

And what's interesting about combining these ideas is that if we consider properties in the "almost everywhere" sense, there is no difference between deleted neighborhoods and non-deleted neighborhoods. The singularity of the neighborhood point is ignored under "almost everywhere".  So this idea of almost everywhere is a souped-up version of the deleted neighborhood.

\subsection{Things that do matter}
Limits that diverge such as $\sin \frac{1}{x}$ at $x=0$ and limits that go to infinity, such as 1/x.

So what we really want to know is how often one of these two things happen. When does the limit diverge, and when does it go to infinity? And we want to know if these happen at all in a function, and if so, how frequently.

As a small point, I'd like to include $\infty$ in the domain in the sense that $\displaystyle \lim_{x \to \infty} x = \infty$, which I'd include in this section of problems, and I'd also say that $\displaystyle \lim_{x \to \infty} \sin x$ does not exist.

\subsection{Definition}

$$\forall I, $$

$$\exists B_{\epsilon} , \mu(B_{\delta} \setminus f^{-1}(B_\epsilon)) = 0$$

\subsection{New Theory}

How often can a function break essential continuity?

Working in the context of real functions:

Let's say a function is essentially continuous at a domain point $c$ iff

$$\exists L, \forall B(L), \exists \delta, B_\delta (c) \subseteq \textrm{Cl}(f^{-1}(B(L)))$$

Which means it breaks essential continuity if:

$$\forall L, \exists B(L), \forall \delta, B_\delta (c) \nsubseteq  \textrm{Cl}(f^{-1}(B(L)))$$

I'm wondering about the size of this set of discontinuities. I'm pretty sure it can at least be countable. But I'm wondering if it can be uncountable or have Lebesgue Measure $> 0$.

\subsection{New Theory From Discontinuity Classification}

I'm going to define an essential discontinuity as the following:

Pick a ball in the domain, and call it $B$.

There exists a nonzero measure set of codomain points, call the set $R$, each of whose elements, $r$, have a ball around them whose inverse image is within $B$.

Some examples of this are $sin(\frac{1}{x})$ and $\frac{1}{x}$ because in both cases, there are a nonzero measure set of codomain points whose surrounding ball has an inverse image inside a ball around the domain point in question.

I think I also need to look into oscillation. The wikipedia page is \url{https://en.wikipedia.org/wiki/Oscillation_(mathematics)/}

We know from \url{https://en.wikipedia.org/wiki/Classification_of_discontinuities/} that the set $D$ of discontinuities is always $F_\sigma$.  I'm pretty sure this can be bigger than countable.  I'm also pretty sure that it can have measure above zero because later in the article, Lebesgue's Theorem is discussed, which says that a function is reimann integrable iff $D$ has measure zero, which makes me think that $D$ can have nonzero measure in general, but it wouldn't be reimann integrable.  But I am pretty sure this includes discontinuities of the first kind such as removable or jumps.  So what I'm wondering is that: we know a function's set of discontinuities can have nonzero measure, but what about if we only discuss discontinuities of the second kind... can the measure of that set be more than zero?

\subsection{Essential Continuity}

In my theory of almost continuity, we can't include jump discontinuities because that would break our properties such as the essential mean value theorem.

\subsection{well-behavedness}

jump discontinuities are pretty well behaved. The do, though, break almost continuity and therefore break mean value theorem, etc.

But they aren't essential discontinuities, so I'd still say they're pretty well-behaved.

\subsection{sets and partitions of the reals}

I was wondering if you could partition the reals in such a way that around a given point, you could have a nonzero meausre amount of each partition (the original way I thought about it was just to have two partitions and to determine if on either side of the point, you could have some of both partition).  It turns out the answer is yes, and there are a couple good examples.

One example is $f(x) = \sin(1/x)$ at zero. To partition the set, we could have $A$ be the set of $x$ such that $f(x)>0$ and the other partition, $B$ be the set of $x$ such that $f(x)\leq 0$.  Then no matter how small you draw your ball around zero, you'll have some nonzero amount of both $A$ and $B$.

I also wondered if you could do this with infinite partitions. In particular, countable, uncountable, or measure $>0$.  It turns out that you can do it with countable partitions, but I'm not sure about uncountable or greater.  Here is an example of the countable:

\includegraphics[width=7in]{set-partitions.jpg}
\centering

\end{document}


\documentclass{article}
\usepackage{amsmath}

\title{A Simple Solver for Matrix Equations}
\author{Andrew Winkler, Ph.~D.}

\begin{document}

\maketitle

\section{Abstract}

In the companion paper to this post, we derive a solver for linear systems \begin{math}{}Ax=b\end{math}, which works for real, complex, rational,
and certain other scalars. It is related to the Cholesky decomposition, but sidesteps creating a factorization
to directly construct the solution.

Here we exhibit a C implementation of the most important case, that where
real numbers are represented as double precision numbers and the matrix \begin{math}A\end{math} is symmetric and positive semi-definite,
that is \begin{math}{}x^tAx >=0 \end{math} for all \begin{math}{}x\end{math}.

\section{Implementation Notes}
The algorithm proceeds recursively. It is not tail-recursive; but at each step
the only information which is retained for post-processing is the current column
of \begin{math}{}A\end{math}, and the current element of
\begin{math}{}b\end{math}.

This allows us to overwrite the not-yet-processed elements of
\begin{math}{}A\end{math} and \begin{math}{}b\end{math}, with the transforms
which the recursion employs. In this way, the memory required for the matrix
never grows. On the other hand, both
\begin{math}{}A\end{math} and \begin{math}{}b\end{math} are destroyed in the process, so must be copied if any further use of them is desired.

Because the matrix \begin{math}A\end{math} is symmetric, we need only an upper or lower triangle; accordingly we allocate a vector of size \begin{math}n-i\end{math} to represent column number \begin{math}i\end{math}, starting with the diagonal element and descending the column.

\begin{verbatim}
$ cat positive_solver.h
extern
double * positive_solver(double ** A, double * b, int n);

$ cat positive_solver.c
/*
 * Andrew Winkler

This code solves the equation Ax=b, where A is a positive
semi-definite matrix, so that x^t A x >= 0 for all x,
provided a solution exists. If A is positive definite,
it will be an isomorphism, but in general conditions
must be imposed on b, for it to lie in the range of A.

It has the virtue of dramatic simplicity - there's no need
to explicitly construct the Cholesky decomposition, no need
to do the explicit back-substitutions.  Yet it's essentially
equivalent to that more labored approach, so its
performance/stability/memory, etc. should be at least as good.

There are two kinds of checks, both of which are disabled.
This means that a solution will be generated; it is up to
the caller to verify that the equation Ax=b is satisfied to
the desired precision. If it does not, then no solution exists.

The first kind of check is that Avec is 0 whenever A[0][0]
is 0, which will always be true if A is positive semi-definite,
but which could become false because of any small perturbation
caused by roundoff error upstream in the computation of A.
The second kind of check is that b[0] is 0 whenever A[0][0] is 0,
which is necessary for b to lie in the range of A. The checks are
disabled because the code does not deal with real numbers,
but rather floating point numbers; it's up to the user to decide
what is "close enough" to zero.

*/

#include <stdlib.h>
#include <stdio.h>
#include "positive_solver.h"

void _consistency_checker(double * v, int n) {
    for (int i=0; i<n; i++) {
        if ( v[i] != 0.0 ) exit(-1);
    }
}

void _positive_solver(
    double ** A,
    double * b,
    double * x,
    int n
    )
{
  if (n < 1) exit(-1);

  if (n == 1) {
    if (A[0][0] == 0.0) {
        /* _consistency_checker(b, 1); */
        x[0] = 0.0;
        return;
    }
    x[0] = b[0] / A[0][0];
    return;
  }

  double * bvec = b + 1;
  double * Avec = A[0] + 1;
  double ** Asub = A + 1;
  double * xvec = x + 1;

  int m = n -1;

  if (A[0][0] == 0.0) {
      /*
      _consistency_checker(b, 1);
      _consistency_checker(Avec, m);
      */
  } else {
      for(int j=0; j < m; j++){
        bvec[j] -= Avec[j] * b[0] / A[0][0];
        for(int i=0; i < m - j; i++)
          Asub[i][j] -= Avec[i] * Avec[i+j] / A[0][0];
      }
  }

  _positive_solver(Asub, bvec, xvec, m);

  if (A[0][0] == 0.0) {
      x[0] = 0.0;
      return;
  }

  double p = 0; for(int k=0; k<m; k++) p += Avec[k] * xvec[k];

  x[0] = (b[0] - p) / A[0][0];

  return;
}

#include <malloc.h>
double * positive_solver(
    double ** A,
    double * b,
    int n
    )
{
  double * x = (double *) malloc(n * sizeof(double));
  _positive_solver(A, b, x, n);
  return x;
}

\end{verbatim}
\end{document}


%!TEX root = ../thesis.tex

\chapter{Isogeometric enhanced SBFEM in 3D}
\input{octree/introduction.tex}

\section{Projection}
\input{octree/surface_division.tex}
\input{octree/projection.tex}
\input{octree/convex_hull.tex}

\section{Intersection}
\paragraph{}
Instead of projecting all points located on the boundary surfaces to the NURBS surfaces, the exact geometry can be retained by calculating the intersection points directly.
By utilizing the work from Sec.~\ref{oct_sc:surface_division}, the number of the surfaces which may contain the intersection can be limited to a few by determine the relationship between their convex hulls and the line.
Only surfaces whose convex hull has intersection with the line will be selected as candidates.
The number of the candidates increase when the mesh becomes coarser as a longer line segment tends to intersect more convex hulls.
On the other hand, when the mesh is refined and the line segment becomes significantly shorter, the number of the candidates decreases at the same time.
A considerable improvement in computational cost can be expected as the time complexity is only meaningful when numerous intersections are calculated.
Only a few intersections need to be determined when the mesh is coarse hence having quite a few candidates at this circumstance may not significantly increase the computational time.
\input{octree/mrep3d.tex}

\section{Introduction of SBFEM in 3D}
\input{octree/sbfem3d.tex}

\section{Numerical examples}
\input{octree/ex_sphere_hole3d.tex}
\input{octree/ex_capsule3d.tex}
\input{octree/ex_lamp.tex}
\input{octree/ex_other_meshes.tex}

\section{Conclusions}
\paragraph{}
In this chapter, a projection is conducted after the octree mesh is generated from the STL file.
Geometric exact is retained by projecting the intersection on the triangular surface back to original NURBS surface or by replacing the distance calculation with finding that between the edge of the element and the NURBS surface directly.
Both of the projecting and the intersection calculating are accelerated by utilizing the strong convex hull property of the NURBS.
The proposed method is able to generate the mesh from an arbitrary geometric input and shows higher accuracy compared to conventional method.


\documentclass[12pt, onside]{article}

\usepackage{fontspec,unicode-math} % Required for using utf8 characters in math mode
\usepackage{parskip}% Extra interlining
\usepackage[margin=0.8in]{geometry}    % Margins
\usepackage[spanish]{babel}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage[binary-units=true]{siunitx}
\usepackage{multirow}
\usepackage{mathtools}
\usepackage{array}
\usepackage{physics}
\usepackage{cleveref}
\usepackage{float}
\usepackage[toc,page]{appendix}

% Header and Footer
\usepackage{fancyhdr}
\setlength\headheight{30pt}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead{}
\fancyhead[L]{
    \textbf{Numerical implementation of the two-body problem}
}
\fancyhead[R]{
    Emilio Domínguez Sánchez
}
\fancyfoot{}
\fancyfoot[C]{\thepage}

\input{docs/arenstorf/listings.tex}

\newcommand{\R}{{\mathbb{R}}}
% \renewcommand{\dv}{\prime}
\newcommand{\ddv}{\dprime}
\renewcommand{\dv}{\prime}
\newcommand{\inner}[2]{\left\langle #1, \; #2 \right\rangle}
\newcommand{\vr}{{\vb{r}}}
\newcommand{\vx}{{\vb{x}}}
\newcommand{\CM}{{\operatorname{CM}}}

\begin{document}
\input{docs/arenstorf/title_page.tex}
\newpage
\tableofcontents
\newpage

\section{Introduction}

    Richard Arenstorf discovered a stable periodic orbit between the Earth and the Moon
which was later used as the basis for Apollo missions.

    In this paper, we work with the initial value problem corresponding to Arenstorf's orbit
and compute the solution with different iterative methods.

\section{Problem description}

    This an instace of the three body problem.
Two of the objects, the Earth and the Moon, are orbiting in a plane.
The third one, our satellite, of negligible mass,
is only affected by the attraction of this two large bodies.

\subsection{Initial Value Problem}

    The system of equations that govern the system are given by
%
\begin{gather*}
    \begin{alignedat}{2}
        x\ddv &= x + 2y\dv - M\frac{x+m}{D_1}& - m&\frac{x-M}{D_2} \\
        y\ddv &= y + 2x\dv - M\frac{y}{D_1}& - m&\frac{y}{D_2} \\
    \end{alignedat} \\
    \begin{alignedat}{1}
        D_1 = &((x+m)^2 + y^2)^{\frac{3}{2}} \\
        D_2 = &((x-m)^2 + y^2)^{\frac{3}{2}} \\
    \end{alignedat} \\
    \begin{alignedat}{1}
        m &= \num{0.012277471} \\
        M &= 1-m \\
    \end{alignedat}
\end{gather*}
%
where $M$ and $m$ are the mass of the Earth and the Moon respectively.

    The initial conditions are taken normalized to the problem
to increase the numerical accuracy.
For example, the distance between the Earth and the Moon is $1$ and
the sum of masses of the Earth and the Moon is also $1$.
%
\begin{alignat*}{2}
    x(0) &= (\num{0.994} & 0) \\
    y(0) &= (0 & \num{-2.001585106}) \\
\end{alignat*}

\section{Objective}

    The objective is tracing the orbit for two cycles
using three fixed step methods:
Euler's method, the modified version of Euler's method and the Runge-Kutta $4$ Method;
as well as two adaptative steps methods: Felhberg's method and
Richardson's extrapolation with variation of the step of the Runge-Kutta $4$ method.
We are interested in obtaining the best possible approximation with each method
and the number of evaluations and the running time of each method.

\section{Implementation}

\subsection{General considerations}

    To determine when the methods should stop
I decided to compare the points with the starting point.
However, we need to stop at the second turn.
Some initial tries with a good method (like RK4) show that
the time taken in a complete turn is approximately $17$ time units.
Hence, the implementations use a lambda function (usually inlined in Julia) that checks
this giving a tolerance called \lstinline!closing_err!.
This value is different depending on the methods,
because some where able to obtain better results than the rest.

\begin{lstlisting}[
    gobble=4,
    caption=Stop condition for the methods,
        basicstyle=\scriptsize
]
    (t, x) ->
        t > 20 && norm(Arenstorf.p0 .- last(x)[1:2]) < closing_err || t > stop_time,
\end{lstlisting}

    It was also necessary to have a means of measuring the error of each solution.
I measured the distance from the method's to the initial points after two iterations
and that same distance when we interpolate the solution based on the last $5$ points
to obtain what would be the exact $x$-coordinate when the $y$-coordinate is $0$,
as in the beginning.
I reused the code for Hermite's interpolation from previous assignments.

\begin{lstlisting}[
    gobble=4,
    caption=Code corresponding to Hermite's interpolation in the tests,
    basicstyle=\scriptsize
]
    n = length(x)
    last5x = [p[1] for p in x[n-4:n]]
    last5y = [p[2] for p in x[n-4:n]]
    closingx = Interpolation.hermite(last5y, last5x, 0)
    print("\tDistance from interpolated closing point to start point: ",
          "$(abs(closingx - Arenstorf.p0[1]))\n")
\end{lstlisting}

\subsection{Plotting and building gifs from the solutions}

    In order to build gifs (or plots) for the solutions, we must use the solutions computed.
However, we have usually computed a lot of points.
This number of points makes it possible to use the solution in other contexts
or draw it smoothly,
but processing a frame for each point would require a lot of computation time.
Furthermore, it would be wrong for mixed step methods,
because each frame would not correspond to the same fraction of time.
Hence, plotting or drawing a frame every $k$ points is also wrong,
and if we do so,
we may be able to see some inconsistencies even in our plots.
Specifically, we could see some polygonals in the more complex parts of our drawing.
Instead, we must iterate over the time (with a fixed step) and plot/frame
all the points seen until that moment.

\begin{lstlisting}[
    gobble=4,
    caption=Creation of gifs in Julia,
    basicstyle=\scriptsize
]
    len = length(t)
    anim = Animation()
    i = 2
    for j = 0:0.05:t[len]
        while i < len && t[i] < j
            i += 1
        end
        # NaNs are used to avoid plotting more points for the Earth
        push!(p1, [(x[i][1], x[i][2]), (cos(2*π*t[i]/t[len]), sin(2*π*t[i]/t[len])), (NaN, NaN)])
        push!(p2, t[i], steps[i-1])
        plot(p1, p2)
        frame(anim)
    end
    gif(anim, "media/arenstorf/richardson.gif", fps = 15)
\end{lstlisting}

\subsection{Euler's Method}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.8\textwidth]{media/arenstorf/euler.png}
    \caption{Graphical results for Euler's method.}
\end{figure}

    Euler's method is the worst of the tested.
We could say it behaved properly for one turn,
but the accumulated error was too big for the second turn in this problem.
I tried reducing the step to $\num{1e-6}$ with just one turn,
but the results were not favorable either.
In this case, there is no point in checking the distance given by interpolation,
because it is very clear that the orbit is deformed with respect to the original one.

\lstinputlisting{outputs/arenstorf/euler.txt}

\subsection{Modified Euler's Method}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{media/arenstorf/mod_euler.png}
    \caption{Graphical results for the modified version of Euler's method.}
    \label{fig:mod_euler}
\end{figure}

    The modified version of Euler's method improved significantly with respect
to the original one.
In this case, I also needed to reduce significantly the step size to obtain better results.
I used the least step among all methods because I could obtain
a better solution with that step size,
which I was not able with Euler's method.
I did not reduce it more because the running time was already over $\SI{30}{\s}$.
This made me think that I needed to consider the performance of the iterative methods.
The conclusions will be presented later (\cref{sec:performance}).

    In this case, we can see in the plot (\cref{fig:mod_euler}) that the second cycle
is slightly different than the first one towards the end.
In the following methods, the differences will not be visible.
The method gave an error when closing the orbit of $\num{4e-3}$.
Because the initial point was almost at distance $1$, this error is also the relative error.
This means that even for an unstable problem like this one,
reducing the step size enough can provide reasonable results.
But then again,
the next methods achieve more in less time.

\lstinputlisting{outputs/arenstorf/mod_euler.txt}

\subsection{Runge Kutta $4$ Method}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.8\textwidth]{media/arenstorf/rk4.png}
    \caption{Graphical results for the RK4 method.}
\end{figure}

    The RK4 is the first method for which we obtain a plot that
visually seems like a single cycle,
meaning that it approximates very well,
and a closing error of $\num{2.8e-8}$ (after interpolation).
However, we still need to use a very small step ($\num{1e-5}$) to obtain good results,
while with the adaptative methods,
the number of iterations, and therefore the running time,
decrease significantly.
The conclusion is that an approximation of order $4$ with steps of $\num{1e-5}$
gives good results for this problem,
but there are parts of the orbit where the system is more stable and we can use bigger steps.

\lstinputlisting{outputs/arenstorf/rk4.txt}

\subsection{Felhberg's method}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.95\textwidth]{media/arenstorf/fehlberg.png}
    \caption{Graphical results for Felhberg's method.}
\end{figure}

    I tweaked the values for Felhberg's method until I saw I could no longer decrease the
approximation error.
I was not able to reduce the closing error below $\num{1e-5}$,
even when I set the tolerance to $\num{1e-16}$,
something that also happened with Richardson's extrapolation.
Felhberg's was the best method for this problem,
the fastest (a little bit above one second in my hardware)
and the one that performed the least number of evaluations of the derivative function.

\lstinputlisting{outputs/arenstorf/fehlberg.txt}

\subsection{Richardson's extrapolation of the RK4 method}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.95\textwidth]{media/arenstorf/richardson.png}
    \caption{Graphical results for Richardson's extrapolation of the RK4 method.}
\end{figure}

    The last method performed similarly to the previous one.
It also falls in the category of mixed step methods,
and as such it was much faster than the fixed step methods,
but it was slower and required more evaluations that Felhberg's method.
A slight improvement with respect to Fehlberg's method
is that the maximum step was slightly bigger, but so was the tolerance of the method.

\lstinputlisting{outputs/arenstorf/richardson.txt}

\section{Access to the code}

    The code at the moment of writting can be checked in my Git repository.
{\small
\url{https://github.com/useredsa/numeric-differential-equations/commit/c0c34ee66c30e19cf622e8280653d83207a0ce89}
}

    I recommend looking at the files
\href{https://github.com/useredsa/numeric-differential-equations/blob/c0c34ee66c30e19cf622e8280653d83207a0ce89/media/arenstorf/fehlberg.gif}{\lstinline{fehlberg.gif}}
and
\href{https://github.com/useredsa/numeric-differential-equations/blob/c0c34ee66c30e19cf622e8280653d83207a0ce89/media/arenstorf/richardson.gif}{\lstinline!richardson.gif!}
which show animations of the satellite, the moon and the step size
for both adaptative methods.

\end{document}


\documentclass[12pt]{article}
\usepackage{amsfonts}

\newcommand{\real}{{\rm Re}}
\newcommand{\imag}{{\rm Im}}

\begin{document}

\section{Mean value of position angle change \\ Mark II}

This memo documents an updated model for estimating the change in
position angle, $\Delta\Psi$, and its uncertainty. Let
\begin{equation}
P_k = Q_k + i U_k = L_k \exp i2\Psi_k
\end{equation}
represent the linear polarization of the $k$th phase bin in one profile and
\begin{equation}
P^\prime_k = Q^\prime_k + i U^\prime_k = L^\prime_k \exp i2\Psi^\prime_k
\end{equation}
the same in the other profile.

Consider the {\bf weighted} cross-correlation,
$\bar{Z}=\bar{C}+i\bar{S}$, where $\bar C$ and $\bar S$ are the
weighted mean values of the real and imaginary components of
\begin{equation}
z_k = c_k + i s_k
    = P^*_k P^\prime_k = L_k L^\prime_k \exp i2(\Psi^\prime_k - \Psi_k).
\end{equation}
That is, where
\begin{equation}
c_k = Q_kQ^\prime_k + U_kU^\prime_k,
\hspace{1cm}
s_k = Q_kU^\prime_k - U_kQ^\prime_k,
\end{equation}
and
\begin{eqnarray}
\sigma_{c_k}^2 & = &
 Q^{\prime2}_k\sigma_{Q_k}^2 + U^{\prime2}_k\sigma_{U_k}^2 +
 Q^2_k\sigma_{Q^\prime_k}^2 + U^2_k\sigma_{U^\prime_k}^2 \\
\sigma_{s_k}^2 & = &
 U^{\prime2}_k\sigma_{Q_k}^2 + Q^{\prime2}_k\sigma_{U_k}^2 +
 U^2_k\sigma_{Q^\prime_k}^2 + Q^2_k\sigma_{U^\prime_k}^2 \\
\sigma_{s_k c_k} & = &
 Q^\prime_k U^\prime_k (\sigma_{Q_k}^2 - \sigma_{U_k}^2) +
 Q_kU_k (\sigma_{U^\prime_k}^2 -\sigma_{Q^\prime_k}^2)
\end{eqnarray}
the weighted means and their variances and covariance are given by
\begin{equation}
\bar C = \sigma_{\bar C}^2 \sum_{k=1}^N { c_k \over \sigma_{c_k}^2 }
\hspace{1cm}
\bar S = \sigma_{\bar S}^2 \sum_{k=1}^N { s_k \over \sigma_{s_k}^2 }
\end{equation}
and
\begin{equation}
\sigma_{\bar C}^2 = \left[ \sum_{k=1}^N{1\over\sigma_{c_k}^2} \right]^{-1}
\hspace{5mm}
\sigma_{\bar S}^2 = \left[ \sum_{k=1}^N{1\over\sigma_{s_k}^2} \right]^{-1}
\hspace{5mm}
\sigma_{\bar S \bar C} = \sum_{k=1}^N
\left[\sigma_{\bar S}\sigma_{\bar C}\over\sigma_{s_k}\sigma_{c_k}\right]^2
                         \sigma_{s_k c_k}
\end{equation}

\newpage

\noindent
The expectation value of $\Delta\Psi=\Psi^\prime_k - \Psi_k$ is given by
\begin{equation}
\langle\Delta\Psi\rangle
={1\over2}\tan^{-1}\left({\bar S\over\bar C}\right).
\end{equation}
and the variance of the expectation value
\begin{equation}
{\mathrm{var}}(\langle\Delta\Psi\rangle) =
{ \bar C^2\sigma_{\bar S}^2 + \bar S^2\sigma_{\bar C}^2
  + 2 \bar S\bar C \sigma_{\bar S \bar C} \over
  \left(\bar C^2 + \bar S^2\right)^2 }
\end{equation}

\end{document}