%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS671: Machine Learning
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 1}

Determine the interior, closure and border of the following sets:

\begin{enumerate}[label=(\alph*)]
\item $S_1 = \{x \in \mathbb{R}^2 | x_1^2 + x_2^2 \leqslant 1, x_2 > 1 \}$

\item $S_2 = \{x \in \mathbb{R}^2 | x_1^2 + x_2^2 \leqslant 1, x_1 + x_2 = 1.4 \}$

\item $S_3 = \{x \in \mathbb{R}^2 | x_1^2 + x_2^2 \leqslant 1, x_1 + x_2 = 1.5 \} $
\end{enumerate}

\subsection*{Solution}

\begin{enumerate}[label=(\alph*)]
\item Set $S_1$ can be represented as the intersection of two sets $S_{\text{sub}_1} = \{x \in \mathbb{R}^2 | x_1^2 + x_2^2 \leqslant 1 \}$ and $S_{\textbf{sub}_2} = \{x \in \mathbb{R}^2 | x_2 > 1 \}$.
Suppose $x_0(t_1, t_2) \in S_{\textbf{sub}_2}$.
This means $t_2 > 1$ and therefore $t_2^2 > 1$.
Hence, regardless of the choice of $t_1$, $t_1^2 + t_2^2 > 1$ and therefore $x_0 \notin S_{\textbf{sub}_1}$.
Consequently, $S_1 = S_{\text{sub}_1} \cap S_{\text{sub}_2} = \emptyset$.
Therefore, $K(S) = I(S) = \partial S = \emptyset$.

\item Set $S_2$ can be represented as the intersection of two sets $S_{\text{sub}_1} = \{x \in \mathbb{R}^2 | x_1^2 + x_2^2 \leqslant 1 \}$ (which represents closure of a circle) and $S_{\textbf{sub}_2} = \{x \in \mathbb{R}^2 | x_1 + x_2 = 1.4\}$ which is a line.
Figure \ref{fig11} shows how the two sets intersect.
We first obtain the points where the circle and line intersect by solving Eq. \ref{eq11} for $x_1$ and $x_2$.

\begin{equation}
\left\{ \begin{array}{ll} x_1^2 + x_2^2 = 1 \\ x_1 + x_2 = 1.4 \end{array} \right.
\label{eq11}
\end{equation}

Eq. \ref{eq11} translates to $2x_2^2 - 2.8x_2 + 0.96 = 0$ and yields to $x = (0.6, 0.8)$ and $x = (0.8, 0.6)$.
Therefore the set $S$ is the set of points on the closed segment $[(0.6,0.8),(0.8,0.6)]$.

As the endpoints of the line segment are included in $S$, $S$ is closed in $\mathbb{R}^2$, therefore, $K(S) = S$.
As well, because $S$ is a line segment in $\mathbb{R}^2$, its interior is $S - \{(0.6,0.8),(0.8,0.6)\}$ and its boundary is $S$.

\begin{figure}[H]\centering
\begin{tikzpicture}
\draw [thick] (0,0) circle (1cm);
\draw [thick] (0,1.4) coordinate (a_1) -- (1.4,0) coordinate (a_2);
\draw [->,thick] (0,-1.5) -- (0,2) node (yaxis) [above] {y};
\draw [->,thick] (-1.5, 0) -- (2, 0) node (xaxis) [right] {x};
\fill[black] (a_1) circle (2pt) node[left] {$(0, 1.4)$};
\fill[black] (a_2) circle (2pt) node[below right] {$(1.4, 0)$};
\fill[black] (0.6,0.8) circle (2pt) node[above right] {$(0.6, 0.8)$};
\fill[black] (0.8,0.6) circle (2pt) node[right] {$(0.8, 0.6)$};
\end{tikzpicture}
\caption{Intersection of $S_{\text{sub}_1}$ and $S_{\text{sub}_2}$}\label{fig11}
\end{figure}

\item Set $S_2$ can be represented as the intersection of two sets $S_{\text{sub}_1} = \{x \in \mathbb{R}^2 | x_1^2 + x_2^2 \leqslant 1 \}$ (which represents closure of a circle) and $S_{\textbf{sub}_2} = \{x \in \mathbb{R}^2 | x_1 + x_2 = 1.5\}$ which is a line.
The two sets are shown in Figure \ref{fig12}.
As is shown, the two sets do not intersect which means $S_1 = S_{\text{sub}_1} \cap S_{\text{sub}_2} = \emptyset$.
Therefore, $K(S) = I(S) = \partial S = \emptyset$.

\begin{figure}[H]\centering
\begin{tikzpicture}
\draw [thick] (0,0) circle (1cm);
\draw [thick] (0,1.5) coordinate (a_1) -- (1.5,0) coordinate (a_2);
\draw [->,thick] (0,-1.5) -- (0,2) node (yaxis) [above] {y};
\draw [->,thick] (-1.5, 0) -- (2, 0) node (xaxis) [right] {x};
\fill[black] (a_1) circle (2pt) node[left] {$(0, 1.5)$};
\fill[black] (a_2) circle (2pt) node[below right] {$(1.5, 0)$};
\end{tikzpicture}
\caption{$S_{\text{sub}_1}$ and $S_{\text{sub}_2}$}\label{fig12}
\end{figure}

We can as well mathematically support our deduction by showing no real solution exists for the following equation.

\begin{equation}
\left\{ \begin{array}{ll} x_1^2 + x_2^2 = 1 \\ x_1 + x_2 = 1.5 \end{array} \right.
\label{eq12}
\end{equation}

Eq. \ref{eq12} translates to $2x_2^2 - 3x_2 + 1.25 = 0$ and yields to $x_2 = 0.75 + 0.25j$ and $x_2 = 0.75 - 0.25j$ which shows the two sets never intersect.

\end{enumerate}


\documentclass{amsart}
\usepackage{amsaddr}
\usepackage[style=numeric,backend=biber]{biblatex}
\addbibresource{../sources.bib}

\input{../packages.tex}
\input{../commands.tex}

\numberwithin{equation}{section}

\graphicspath{{figures/}}

\begin{document}

\title{Fast Simulation of Multistage Clonal Expansion Models}

%    Remove any unused author tags.

%    author one information
\author{Lukas K\"ostler}
\email{lukas.koestler@tum.de}
\address{Technical University of Munich (TUM)}

%\keywords{Multistage Clonal Expansion, Filtered Poisson process}

\date{\today}

\begin{abstract}
We derive and demonstrate a method to simulate Multistage Clonal Expansion (MSCE) models. The method is faster than previous ones while retaining similar accuracy.
\end{abstract}

\maketitle

\section{Derivation}

\subsection{Poisson process with random start time}
We will consider a non-homogeneous Poisson process with rate $\lambda\br{t} > 0$ which has a random start time $\tau \geq 0$ with distribution $p$. For fixed $\tau$ the number of occurrences at time $t \geq \tau$ given start time $\tau$, $N\br{t} \vert \tau$ has the characteristic function \cite[chapter 4, eqn. (2.1)]{parzen1962stochastic}
\begin{align}\label{eqn:nonhompoisson}
\begin{split}
\varphi_{N\br{t} \vert \tau}\br{u} &= \exp\brr{m\br{t - \tau} \left\{e^{i u} - 1\right\}} \\
m\br{t} &= \int_0^t \lambda\br{s} \ds \, .
\end{split}
\end{align}
The by the law of total expectation, the characteristic function of $N\br{t}$ is given by
\begin{align*}
\varphi_{N\br{t}}\br{u} &= \E{\varphi_{N\br{t} \vert \tau}\br{u}} \\
&=\int_0^t \exp\brr{m\br{t - \tau} \left\{e^{i u} - 1\right\}} p \br{\tau} \, \dtau + \int_t^\infty p \br{\tau} \, \dtau \, .
\end{align*}
Comparing this expression to a Poisson process with different rate yields the first theorem.
\begin{theorem}\label{thm:iteratedpoisson}
Let $N\br{t}$ follow a non-homogeneous Poisson process with rate $\lambda\br{t} \geq 0$ starting at a random time $\tau \geq 0$ with probability density function $p\br{\tau}$. Let $M\br{t}$ follow a non-homogeneous Poisson process starting at time $0$ with rate
\begin{equation}
\nu\br{t} = \int_0^t\lambda\br{t-\tau} p\br{\tau} \ds = \br{\lambda \ast p}\br{t} \ .
\end{equation}
Then, for the characteristic functions $\varphi_{N\br{t}}$ and $\varphi_{M\br{t}}$ there holds
\begin{equation}
\max_{t \in \brr{0, \sigma}} \fabs{\varphi_{N\br{t}} - \varphi_{M\br{t}}}
\leq 2 \br{\fexp{2 m\br{\sigma}} - 2m\br{\sigma} - 1}
= O (m\br{\sigma}^2) \,
\end{equation}
where $m$ is the mean value function of $N\br{t}$ as defined in eqn. \eqref{eqn:nonhompoisson}.
\end{theorem}
\begin{proof}
Let $h$ be the mean value function (eqn. \eqref{eqn:nonhompoisson}) of $M\br{t}$, then there holds
\begin{align*}
\frac{d}{dt} \int_0^t m\br{t - \tau} p\br{\tau} \dtau = \int_0^t m'\br{t - \tau} p \br{\tau} \dtau = \int_0^t\lambda\br{t-\tau} p\br{\tau} \dtau = \frac{d}{dt} \nu\br{t} \, .
\end{align*}
We used the Leibniz integration rule and $m\br{0} = 0$ in the first step. Because $h\br{0} = 0$ has to hold, we get
\begin{equation*}
h\br{t} = \int_0^t m\br{t - \tau} p\br{\tau} \dtau = \br{m \ast p}\br{t}
\end{equation*}\\

\noindent For $\varphi_{N\br{t}}\br{u}$, using $k := \left\{e^{i u} - 1\right\}$, we obtain
\begin{align*}
\varphi_{N\br{t}}\br{u}
&=\int_0^t \exp\brr{m\br{t - \tau} k} p \br{\tau} \, \dtau + \int_t^\infty p \br{\tau} \, \dtau \\
&= 1 + k \int_0^t m\br{t - \tau} p \br{\tau} \, \dtau + \sum_{j=2}^\infty \frac{k^j}{j!} \int_0^t m\br{t - \tau}^j p \br{\tau} \, \dtau \, .
\end{align*}\\

\noindent For $\varphi_{M\br{t}}\br{u}$ we obtain
\begin{align*}
\varphi_{M\br{t}}\br{u}
&= \fexp{h\br{t} k}
= 1 + k h\br{t} + \sum_{j=2}^\infty \frac{k^j}{j!} h\br{t}^j \, .
\end{align*}\\

\noindent Let $t \in \brr{0, \sigma}$ then there holds
\begin{align*}
\fabs{\varphi_{N\br{t}} - \varphi_{M\br{t}}}
&= \Big \vert \sum_{j=2}^\infty \frac{k^j}{j!} \int_0^t m\br{t - \tau}^j p \br{\tau} \, \dtau - \sum_{j=2}^\infty \frac{k^j}{j!} h\br{t}^j \Big \vert \\
&\leq \sum_{j=2}^\infty \frac{\fabs{k}^j}{j!} \Big\vert \int_0^t m\br{t - \tau}^j p \br{\tau} \, \dtau - \br{ \int_0^t m\br{t - \tau} p \br{\tau} \, \dtau }^j \Big\vert \\
&\leq \sum_{j=2}^\infty \frac{\fabs{k}^j}{j!} \br{\fabs{m\br{\sigma}}^j + \fabs{m\br{\sigma}}^j} \\
&=2 \sum_{j=2}^\infty \frac{\br{2 m\br{\sigma}}^j}{j!}\\
&=2 \br{\fexp{2 m\br{\sigma}} - 2m\br{\sigma} - 1} \, .
\end{align*}
We used that $\int_0^t p\br{\tau} \dtau = 1$ and that $m\br{\cdot}$ is positive and monotonically increasing.
\end{proof}

\begin{remark}
\autoref{thm:iteratedpoisson} is useful if $m\br{\sigma} \ll 1$ because it then implies that a Poisson process with random start time can be viewed (and simulated) as a Poisson process with rate $h = \br{m \ast p}$. This makes intuitive sense, because if $m\br{\sigma}$, i.e. the expected number of occurrences for $N\br{\sigma}$ starting at $0$, is much smaller than $1$ the correlation that is introduced through the random starting time is negligible.\\

\noindent The formulas for the mean and the variance are
\begin{align*}
\E{N\br{\sigma}} &= h\br{\sigma} \, ,\\
\E{M\br{\sigma}} &= h\br{\sigma} \, ,\\
Var\br{N\br{\sigma}} &= h\br{\sigma} + \int_0^\sigma m^2\br{t-\tau} p\br{\tau} \dtau - \br{\int_0^\sigma m\br{t-\tau} p\br{\tau} \dtau}^2 \, , \\
Var\br{M\br{\sigma}} &= h\br{\sigma} \, .
\end{align*}
While the mean is consistent, the difference in variance is of second order in $m$.
\end{remark}

\subsection{Two Stage Poisson Process}
We will consider a two stage Poisson process. The first process has rate $\nu\br{t}$ and each occurrence of the first process is the starting point of a second-stage process with rate $\lambda\br{t}$. We are interested in the number $N\br{t}$ of occurrences from the first process and the arrival times of the second-stage processes. It is vital that we do not need the arrival times of the first process.

\begin{theorem}\label{thm:twostagepoisson}
Let $N\br{t}$ be the number of occurrences of a non-homogeneous Poisson process with rate $\nu\br{t}$, mean value function $\eta\br{t}$ starting at time $0$. Let $u_1, \dots, u_{N\br{t}}$ denote the arrival times of this process.

For each $j = 1, \dots, N\br{t}$ let $Y\br{t, u_j}$ denote the number of occurrences of a non-homogeneous Poisson process with rate $\lambda\br{t}$, mean value function $m\br{t}$ starting at time $u_j$.

The process
\[
    Y\br{t} = \sum_{j=1}^{N\br{t}} Y\br{t, u_j}
\]{}
is called a filtered Poisson process \cite[chapter 4, eqn. (5.42)]{parzen1962stochastic}.
For $t \in \brr{0, \sigma}$, if we neglect terms of order $O(m\br{\sigma}^2)$, there holds:

\begin{enumerate}
% \item[i)] The process $Y\br{t}$ is a Poisson process with rate
% \[
%     \mu\br{t} = \br{\nu \ast \lambda}\br{t} \, .
% \]
\item[i)] Conditioned on $N\br{\sigma}$ the process $Y\br{t}$ follows a Poisson process with rate
\[
    \mu_{N}\br{t} = \frac{N\br{\sigma}}{\eta\br{\sigma}} \br{\nu \ast \lambda}\br{t} \qquad \forall t \in \brr{0, \sigma} \, .
\]
\end{enumerate}
\end{theorem}
\begin{proof}
By Proposition 2.206 in \cite[p. 147]{intro2015Stoch} (\emph{actually this only guarantees the property for a homogeneous process. I am quite certain that this also holds for the non-homogeneous case but I am lacking a source.}) we have that conditioned on $N\br{\sigma}$ the distribution for $u_j$ (note that the $u_j$ are not ordered) is given by $p\br{u} = \nu\br{u} / \eta\br{\sigma}$ and all $u_j$ are i.i.d.. Then we know by \autoref{thm:iteratedpoisson} that $Y\br{t, u_j}$ can be approximated up to order $2$ by a Poisson process with rate
\begin{equation*}
\mu_j\br{t} = \int_0^t\lambda\br{t-u} \frac{\nu\br{u}}{\eta\br{\sigma}} \mathrm{d}u = \frac{\br{\lambda \ast \nu}}{\eta\br{\sigma}} \br{t} \qquad \forall t \in \brr{0, \sigma} \, .
\end{equation*}
Because the sum of $N\br{\sigma}$ independent Poisson process is again a Poisson process, we know that $Y\br{t}$ can be approximated up to order $2$ by a Poisson process with rate
\[
    \mu_{N}\br{t} = \frac{N\br{\sigma}}{\eta\br{\sigma}} \br{\nu \ast \lambda}\br{t} \, .
\]{}
\end{proof}
\begin{remark}
\autoref{thm:twostagepoisson} is useful if $m\br{\sigma} \ll 1$ because it then implies that a two stage Poisson process can be simulated as follows. a) Draw $N\br{\sigma}$ at random from a Poisson distribution with mean $\eta\br{\sigma}$. b) Simulate a Poisson process with rate
\[
\frac{N\br{\sigma}}{\eta\br{\sigma}} \br{\nu \ast \lambda}\br{t} \, .
\]
The direct solution would be to simulate the first Poisson process fully and obtain arrival times $u_j$. For each arrival time one would simulate a Poisson process with rate $\lambda\br{t-u_j}$. This means on average $\eta\br{\sigma}$ many Poisson process simulations. The method proposed here can, under the circumstances described, generate a very good approximation with only two Poisson process simulations. This advantage comes from marginalizing out (approximately) the arrival times $u_j$.\\

\noindent For the Colorectal cancer model from \cite{jeon2008evaluation} the first rate is of order $10^2$, $\sigma=50$ and the second rate is of order $10^{-6}$. The direct approach results in approximately $5000$ Poisson process simulations. The approximate method yields a theoretic speedup factor of 1000. Also $m\br{\sigma} \approx 10^{-4}$ and thus the approximation is extremely accurate.
\end{remark}

\newpage
\section{Numerical Experiments}
In this section we present numerical experiments for all theorems presented in this paper.

\subsection{Poisson process with random start time}
\label{sec:numPoissProcOne}
In Figure \ref{fig:numPoissProcOne} the model as described in \autoref{thm:iteratedpoisson} is simulated with a sample size of $10^7$. For $\lambda = 10^{-2}$ the approximation is already very accurate. The relative error in variance is $\lambda/6 \approx 1.7 \times 10^{-3}$ for $\lambda = 10^{-2}$.
\begin{figure}[ht]
    \centering
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc01_0_1.eps}
        \caption{$\lambda \equiv 1$}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc01_0_2.eps}
        \caption{$\lambda \equiv 1$. Log-Scale.}
    \end{subfigure}
    \\
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc01_1_1.eps}
        \caption{$\lambda \equiv 10^{-1}$.}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc01_1_2.eps}
        \caption{$\lambda \equiv 10^{-1}$. Log-Scale.}
    \end{subfigure}
    \\
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc01_2_1.eps}
        \caption{$\lambda \equiv 10^{-2}$.}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc01_2_2.eps}
        \caption{$\lambda \equiv 10^{-2}$. Log-Scale.}
    \end{subfigure}
    \caption{$10^7$ samples. $\sigma = 1$. $\tau \sim \text{unif}\br{0,1}$. For this example there holds $\E{N} = \lambda / 2$ and $Var\br{N} = \lambda/2 + \lambda^2/12$. The blue histogram represents the values from the direct simulation with $10^7$ samples, i.e. the "ground truth". The red line is a Poisson distribution with parameter $\E{M} = \lambda / 2$, i.e. the approximation. Because the approximation is just a Poisson process, the distribution of $M$ is Poisson and can be obtained without sampling.}
    \label{fig:numPoissProcOne}
\end{figure}

\subsection{Two Stage Poisson Process}\label{sec:numPoissProcTwo}
We consider a two-stage Poisson process with homogeneous rates, i.e. $\nu\br{t} \equiv 10^3$, $\lambda\br{t} \equiv 10^{-3}$. We choose $\sigma = 2$.\\

\noindent Because the two-stage Poisson process is a filtered Poisson process, we can use the formulas from \cite[chapter 4, eqn. (5.43)--(5.45)]{parzen1962stochastic} to analytically calculate the mean and variance of the true process $N$. For the approximation $M$ we use the law of total expectation and obtain
\begin{align}\label{eqn:numPoissProcTwoEVar}
\begin{split}
\E{N\br{\sigma}} &= \frac{\nu \lambda \sigma^2}{2} \,, \\
Var\br{N\br{\sigma}} &= \frac{\nu \lambda \sigma^2}{2} \br{1 + \frac{2 \sigma \lambda}{3}} \,, \\
\E{M\br{\sigma}} &= \frac{\nu \lambda \sigma^2}{2} \,,\\
Var\br{M\br{\sigma}} &= \frac{\nu \lambda \sigma^2}{2} \br{1 + \frac{\sigma \lambda}{2}} \, .
\end{split}
\end{align}
The relative error in variance is thus $\frac{\sigma \lambda}{6} \approx 0.8 \%$. The results for the sampling the real and approximate process with $10^6$ samples are shown in \autoref{tab:numPoissProcTwo} and \autoref{fig:numPoissProcTwo}.\\

\noindent If we would have used a normal Poisson process as approximation, the variance would be exactly the mean and the relative error in Variance would be $\sigma \lambda$, i.e. six times higher. More significant would be that by direct simulation we would not obtain the number of occurrences in the intermediate stage, which is needed for further computation.\\

\noindent The runtime\footnote{This experiment was carried out on one core of a Intel Xeon X5680 at 3.33GHz that was launched in 2010.} is approximately $4.8 \times 10^{-4}$ seconds per sample for the direct method and $1.4 \times 10^{-6}$ seconds per sample for the approximate method. This is a speed up by a factor of ca. 100. It should be noted that for this experiment only the number of occurrences $N$ was computed and not their arrival times, therefore the speedup is probably even more substantial for the real simulation.\\

\noindent From \autoref{eqn:numPoissProcTwoEVar} it can be seen that by simulating the approximation with $\hat{\nu} = \frac{3}{4} \nu$ and $\hat{\lambda} = \frac{4}{3} \lambda$ mean and variance of the approximation will be correct. The numerical experiments (\autoref{tab:numPoissProcTwoChnaged} and \autoref{fig:numPoissProcTwo}) indicate that the effect of this change is small.

\begin{table}
    \begin{tabular}{ |c|c|r|r| }
    \hline
    \textbf{Statistic} & \textbf{Approximation} & \textbf{Type} & \textbf{Value}\\
    \hline\hline
    \input{tables/poissproc02.tex}
    \hline
    \end{tabular}
    \caption{Two stage Poisson process with $\nu \equiv 10^3$, $\lambda \equiv 10^{-3}$, $\sigma = 2$ and a total of $10^6$ samples.}
    \label{tab:numPoissProcTwo}
\end{table}

\begin{table}
    \begin{tabular}{ |c|c|r|r| }
    \hline
    \textbf{Statistic} & \textbf{Approximation} & \textbf{Type} & \textbf{Value}\\
    \hline\hline
    \input{tables/poissproc02_changed.tex}
    \hline
    \end{tabular}
    \caption{Two stage Poisson process with $\nu \equiv 10^3$, $\lambda \equiv 10^{-3}$, $\sigma = 2$ and a total of $10^6$ samples. For the approximate simulation $\hat{\nu} = \frac{3}{4} \nu$ and $\hat{\lambda} = \frac{4}{3} \lambda$ were used. Therefore, the analytic mean and variance are identical for direct and approximate simulation.}
    \label{tab:numPoissProcTwoChnaged}
\end{table}

\begin{figure}[ht]
    \centering
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc02_1.eps}
        \caption{Bar-chart for the number of occurrences.}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc02_2.eps}
        \caption{Bar-chart for the number of occurrences. Log-Scale.}
    \end{subfigure}
    \\
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc02_changed_1.eps}
        \caption{Bar-chart for the number of occurrences. $\hat{\nu} = \frac{3}{4} \nu$ and $\hat{\lambda} = \frac{4}{3} \lambda$.}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.475\textwidth}
        \centering
        \includegraphics[width=\textwidth]{poissproc02_changed_2.eps}
        \caption{Bar-chart for the number of occurrences. $\hat{\nu} = \frac{3}{4} \nu$ and $\hat{\lambda} = \frac{4}{3} \lambda$. Log-Scale.}
    \end{subfigure}
    \caption{Two stage Poisson process with $\nu \equiv 10^3$, $\lambda \equiv 10^{-3}$. For the direct simulation $\nu$, $\lambda$ were used. For the approximate simulation $\nu$, $\lambda$ were used in (A) and (B) and $\hat{\nu} = \frac{3}{4} \nu$ and $\hat{\lambda} = \frac{4}{3} \lambda$ were used for (C) and (D). $\sigma = 2$ and a total of $10^6$ samples was used.}
    \label{fig:numPoissProcTwo}
\end{figure}

\clearpage
\printbibliography
\end{document}


\section{$\bigotimes$}
Welcome to algebraic topology! This is family weekend, so welcome. Today'll be more about algebra, and there'll be very little topology, I'm afraid. Today'll be about tensor products. I got your permission to talk about modules over a commutative ring. We're always going to let $R$ be a commutative ring (they're going to be simple; for example, $\QQ,\FF_p,\Z,\Z/n\Z,\cdots,\text{PIDs}$).

I want to tell you that the category of $R$-modules is what's called a ``categorical ring'', where the addition corresponds to the direct sum, the zero element is the zero module, $1$ is $R$ itself, and multiplication is where you put a circle around a multiplication symbol.

The reason we do this is because of bilinear maps. Let me recall the definition of a bilinear map.
\begin{definition}
If I have $M,N,P$ are $R$-modules, then a bilinear (or if you want to be annoying, $R$-bilinear) map is a map $\beta:M\times N\to P$ such that $\beta(x+x^\prime,y)=\beta(x,y)+\beta(x^\prime,y)$ and $\beta(x,y+y^\prime)=\beta(x,y)+\beta(x,y^\prime)$, and such that $\beta(rx,y)=r\beta(x,y)$ and $\beta(x,ry)=r\beta(x,y)$.
\end{definition}
\begin{example}
$\RR^n\times\RR^n\to\RR$ given by the dot product is a $\RR$-bilinear map. The cross product $\RR^3\times\RR^3\to\RR$ is $\RR$-bilinear. More generally, if $R$ is a ring then the multiplication $R\times R\to R$ is $R$-bilinear, and the multiplication on an $R$-module $M$ given by $R\times M\to M$ is $R$-bilinear. This enters into topology because the map $ H_n(X;R)\times H_n(Y;R)\xrightarrow{\times} H_{m+n}(X\times Y;R)$ is $R$-bilinear.
\end{example}
Wouldn't it be great to reduce stuff about bilinear maps to linear maps? We're going to do this by means of the universal property.
\begin{definition}
Let $M,N$ be $R$-modules. A \emph{tensor product} of $M,N$ is a $R$-module $P$ and a bilinear map $M\times N\xrightarrow{\beta_0}P$ such that for every bilinear map $M\times N\xrightarrow{\beta}Q$ there is a unique factorization.
\begin{equation*}
\xymatrix{M\times N\ar[r]^{\beta_0}\ar[dr]^\beta & P\ar@{-->}[d]^f\\
 & Q}
\end{equation*}
through an $R$-module homomorphism $f$. It's easy to check that $f\circ\beta_0$ is bilinear.
\end{definition}
So $\beta_0$ is universal bilinear map out of $M\times N$. Instead of $\beta_0$ we're going to write $M\times N\xrightarrow{\otimes}P$. This means that $\beta(x,y)=f(x\otimes y)$ in the above diagram. There are lots of things to say about this. When you have something that is defined via a universal property, you first have to check that it exists!
\begin{construction}
I want to construct an $R$-bilinear map out of $M\times N$. I guess I should say it like this. Let $\beta:M\times N\to Q$ be any $R$-bilinear map. This $\beta$ isn't linear. Maybe we should first extend it to a linear map. Consider $R\langle M\times N\rangle$, the free $R$-module generated by $M\times N$. Well, $\beta$ is a map of sets, so there's a unique $R$-linear homomorphism $\overline{\beta}:R\langle M\times N\rangle\to Q$. Then I get a factorization:
\begin{equation*}
\xymatrix{M\times N\ar[rr]^\beta\ar[dr]^{[-]} & & Q\\
& R\langle M\times N\rangle\ar[ur]^{\overline{\beta}} &}
\end{equation*}
The map $[-]$ isn't bilinear. So we should quotient $R\langle M\times N\rangle$ by a submodule $S$ of relations. More precisely, $S$ is the sub $R$-module generated by the relations needed to map $[-]$ a $R$-bilinear map, namely:
\begin{enumerate}
\item $[(x+x^\prime,y)]-[(x,y)]-[(x^\prime-y)]$.
\item $[(x,y+y^\prime)]-[(x,y)]-[(x,y^\prime)]$.
\item $[(rx,y)]-r[(x,y)]$.
\item $[(x,ry)]-r[(x,y)]$
\end{enumerate}
for all $x,x^\prime\in M$ and $y,y^\prime\in N$. Now, this map $[-]$ is bilinear - we've quotiented out by all things that made it false! Now the map $R\langle M\times N\rangle\to Q$ factors through via $R\langle M\times N\rangle\to R\langle M\times N\rangle/S\xrightarrow{f} Q$ because the map $\overline{\beta}$ is linear, and $f$ is unique because the $\overline{\beta}$ is unique, so there's at most one factorization. We just checked that there was one, so we're done. We'll also write the composition $M\times N\xrightarrow{[-]}R\langle M\times N\rangle\to R\langle M\times N\rangle/S$ as $\otimes$.
\end{construction}
You're never going to use this construction to compute anything. If you find yourself using this construction, stop and think about what you're doing.
\begin{remark}
Note that the image of $(m,n)$ in $R\langle M\times N\rangle/S$ generates $R\langle M\times N\rangle/S$ as an $R$-module. The $R$-module $R\langle M\times N\rangle/S$ contains elements of the form $x\otimes y$ with $x\in M$ and $y\in N$ because they generate $R\langle M\times N\rangle$, and $R\langle M\times N\rangle/S$ is a quotient of that.

These $x\otimes y$ are called ``decomposable tensors''. (I've heard them called pure tensors.)
\end{remark}
What are the properties of $R\langle M\times N\rangle/S=:P$?
\begin{enumerate}
\item How many maps are there that make the following diagram commute?
\begin{equation*}
\xymatrix{& P\ar@{-->}[dd]\\
M\otimes N\ar[ur]^\otimes\ar[dr]_\otimes & \\
& P}
\end{equation*}
By the uniqueness statement, there's only one map, namely the identity!
\item Suppose that we have two tensor products of $M$ and $N$, say $P$ and $P^\prime$. We have
\begin{equation*}
\xymatrix{& P\ar@{-->}[dd]^b\\
M\otimes N\ar[ur]^\otimes\ar[dr]_\otimes & \\
& P^\prime\ar@{-->}[uu]_{b^\prime}}
\end{equation*}
And $b,b^\prime$ are unique. If you compose $b$ and $b^\prime$, you'll see that you get the identity of $P$ and $P^\prime$, depending on how you compose the maps. More precisely, you have:
\begin{equation*}
\xymatrix{& P\ar@{-->}[d]^b\\
M\otimes N\ar[ur]^\otimes\ar[dr]_\otimes & P^\prime\ar[d]^{b^\prime}\\
& P^\prime}
\end{equation*}
and
\begin{equation*}
\xymatrix{& P^\prime\ar@{-->}[d]^{b^\prime}\\
M\otimes N\ar[ur]^\otimes\ar[dr]_\otimes & P\ar[d]^{b}\\
& P^\prime}
\end{equation*}
Thus $bb^\prime=1$ and $b^\prime b=1$. So $b,b^\prime$ are isomorphisms, i.e., $P\cong P^\prime$. We say that there is a canonical\footnote{This means god given, but here it means that it's naturally constructed.} isomorphism between any two constructions of a tensor products. The universal property defines the object up to canonical isomorphism. This is a general principle.

We can thus write the tensor product as if it just depended on just $M$ and $N$. We write $M\otimes N$. A general element is a finite sum $\sum_i x_i\otimes y_i$. To be really honest, we'll write $M\otimes_R N$. If $R$ is understood, we'll omit it. I'll usually forget to add the $\otimes_R$, and simply write $\otimes$.
\item Functoriality. If I have homomorphisms $M\times N\xrightarrow{f\times g}M^\prime\times N^\prime$. I have:
\begin{equation*}
\xymatrix{M\times N\ar[d]^{f\times g}\ar[r]^\otimes\ar[dr] & M\otimes N\ar@{-->}[d]\\
M^\prime\times N^\prime\ar[r]^\otimes & M^\prime\otimes N^\prime}
\end{equation*}
The dotted map exists because the diagonal map is $R$-bilinear. We write the map $M\otimes N\to M^\prime \otimes N^\prime$ as $f\otimes g$. We need to check stuff though.
\begin{equation*}
\xymatrix{M\times N\ar[d]^{f\times g}\ar[r]^\otimes\ar[dr] & M\otimes N\ar@{-->}[d]^{f\otimes g}\\
M^\prime\times N^\prime\ar[r]^\otimes\ar[d]^{f^\prime\times g^\prime} & M^\prime\otimes N^\prime\ar@{-->}[d]^{f\otimes g}\\
M^{\prime\prime}\times N^{\prime\prime}\ar[r]^\otimes & M^{\prime\prime}\otimes N^{\prime\prime}}
\end{equation*}
And the composite matches up, i.e., $(f^\prime\otimes g^\prime)(f\otimes g)=(f^\prime f)\otimes g^\prime g$.
\item I said that this was gonna be a categorical ring, so we need to check this. Well, $R\otimes_R M$ should be isomorphic to $M$. Let's think about this for a minute. I just need to check the universal property. Suppose I have an $R$-bilinear map $\beta:R\times M\to P$. We already have a universal $R$-bilinear map $\varphi:R\times M\to M$. I have to construct a universal factorization $f:M\to P$. Just let $f(x)=\beta(1,x)$. It's $R$-bilinear. We can check that this diagram commutes now because $f(\varphi(r,x))=f(rx)=\beta(1,rx)=r\beta(1,x)=\beta(r,x)$. Well, this map $R\times M\to M$ is surjective, so there's at most one factorization. So we're done. There are other checks that are extremely boring, but they're part of the toolkit.

I need to check that $L\otimes(M\otimes N)\cong (L\otimes M)\otimes N$ that's compatible with $L\times (M\times N)\cong (L\times M)\times N$. There's a canonical isomorphism. I don't know how to not say that this is trivial. Also, we need to check that $M\otimes N\cong N\otimes M$. (Just do this yourself. It's really easy.)
\item What happens with $M\otimes\left(\bigoplus_{\alpha\in A}N_\alpha\right)$? It might be a finite direct sum, or maybe an uncountable collection. How does this relate to $\bigoplus_{\alpha\in A}(M\otimes N_\alpha)$? Let's construct a map $\displaystyle\bigoplus_{\alpha\in A}(M\otimes N_\alpha)\to M\otimes\left(\bigoplus_{\alpha\in A}N_\alpha\right)$. We just need to define maps $M\otimes N_\alpha\to M\otimes\left(\bigoplus_{\alpha\in A}N_\alpha\right)$ because direct sums are coproducts. Let this map be $1\otimes\text{in}_\alpha$ where $\mathrm{in}_\alpha:N_\alpha\to \bigoplus_{\alpha\in A}N_\alpha$. These give you a map $f:\bigoplus_{\alpha\in A}(M\otimes N_\alpha)\to M\otimes\left(\bigoplus_{\alpha\in A}N_\alpha\right)$

What about a map the other way? This is a bit trickier. An element of $M\otimes\left(\bigoplus_{\alpha\in A}N_\alpha\right)$ is $x\otimes(y_\alpha)_{\alpha\in A}$, where you note that $y_\alpha=0$ for all but finitely many $\alpha\in A$. Define $g:M\otimes\left(\bigoplus_{\alpha\in A}N_\alpha\right)\to \bigoplus_{\alpha\in A}(M\otimes N_\alpha)$ via $x\otimes(y_\alpha)_{\alpha\in A}\mapsto (x\otimes y_\alpha)_{\alpha\in A}$. It's up to you to check that these are inverses and that you can extend to a general nondecomposable tensor by linearity.
\end{enumerate}
We have not done any computations yet. I guess I should end with the statement that $S_\ast(X;M):=S_\ast(X)\otimes_R M$ if $M$ is an $R$-module. We'll discuss on Monday the question we raised last time, namely:
\begin{question}
How is $ H_\ast(X;M)$ related to $ H_\ast(X)= H_\ast(X;\Z)$? This is a reasonable question.
\end{question}


\chapter{Propulsion}

\section{Piston Engine}

Piston engine manifold absolute pressure due to engine revolution speed and normalized throttle position is approximated by the following formula.
\begin{equation}
  p_{MAP}
  =
  p \left( h \right)
  +
  \left(
    4 \cdot 10^5 \hat \delta_{throttle}
    -
    4.05 \cdot 10^5
  \right)
  \frac{n}{n_{max}}
\end{equation}

Fuel to air ratio is approximated by the following expression: \cite{Allerton2009}
\begin{equation}
  FAR = 0.1 \left( 2 - {\hat \delta}_{mixture}^2 \right) \frac{\rho_0}{\rho}
\end{equation}

Engine static power is approximated as:
\begin{equation}
  P_S =
  1.093 \cdot 10^{-5} \left( 1 + C_{\Delta P} \right) P_{max} p_{MAP}
  \left( \frac{n}{n_{max}} - 0.05 \right)
\end{equation}

Power losses can be calculated using following formula:
\begin{equation}
  \Delta P = P_{max} C_{\Delta P} \left( \frac{n}{n_{max}} \right)^2
\end{equation}

Engine net power is given as:
\begin{equation}
  P = P_S - \Delta P
\end{equation}

\section{Propeller}

Thrust generated by the propeller and power required by the propeller are given by the following equations. \cite{Allerton2009, Raymer1992} Propeller revolution speed n is expressed in revolutions per second.
\begin{align}
  T &= \rho n^2 D^4 C_T \\
  P &= \rho n^3 D^5 C_P
\end{align}

Where thrust $C_T$ and power $C_P$ coefficients are functions of advance ratio and blade angle.

Advance ratio is given by the following formula: \cite{Allerton2009, Raymer1992, Torenbeek1982}
\begin{equation}
  J = \frac{V}{nD}
\end{equation}

The propeller torque required is given as: \cite{ResnickHalliday2011}
\begin{equation}
  Q = \frac{P}{2 \pi n}
\end{equation}

\subsection{Propeller Induced Velocity}

The pressure jump across the propeller disk can be expressed as:
\begin{equation}
  \Delta p = \frac{1}{2} \rho \left[ \left( V + V_i \right)^2 - V^2 \right]
\end{equation}

Hence:
\begin{align}
  T &= \Delta p A \\
  \label{eq-prop-induced-velocity-thrust}
  T &= \frac{1}{2} \rho A \left[ \left( V + V_i \right)^2 - V^2 \right]
\end{align}

Induced velocity can be found by solving equation (\ref{eq-prop-induced-velocity-thrust}).

% \section{Jet Engine}

% TODO

% \section{Turboshaft Engine}

% TODO


\chapter{Mixture models and the EM algorithm}
\label{chap:EM-algorithm}

\section{Latent variable models}
In Chapter \ref{chap:DGM} we showed how graphical models can be used to define high-dimensional joint probability distributions. The basic idea is to model dependence between two variables by adding an edge between them in the graph. (Technically the graph represents conditional
independence, but you get the point.)

An alternative approach is to assume that the observed variables are correlated because they arise from a hidden common “cause”. Model with hidden variables are also known as \textbf{latent variable models} or \textbf{LVM}s. As we will see in this chapter, such models are harder to fit than models with no latent variables. However, they can have significant advantages, for two main reasons.
\begin{itemize}
\item{First, LVMs often have fewer parameters than models that directly represent correlation in the visible space.}
\item{Second, the hidden variables in an LVM can serve as a \textbf{bottleneck}, which computes a compressed representation of the data. This forms the basis of unsupervised learning, as we will see. Figure \ref{fig:latent-variable-model} illustrates some generic LVM structures that can be used for this purpose.}
\end{itemize}

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.60]{latent-variable-model.png}
\caption{A latent variable model represented as a DGM. (a) Many-to-many. (b) One-to-many. (c) Many-to-one. (d) One-to-one.}
\label{fig:latent-variable-model}
\end{figure}

\section{Mixture models}
The simplest form of LVM is when $z_i \in \{1,\cdots,K\}$, representing a discrete latent state. We will use a discrete prior for this, $p(zi)=\mathrm{Cat}(\pi)$. For the likelihood, we use $p(\vec{x}_i|z_i =k)=p_k(\vec{x}_i)$, where $p_k$ is the $k$'th \textbf{base distribution} for the observations; this can be of any type. The overall model is known as a \textbf{mixture model}, since we are mixing together the $K$ base distributions as follows:
\begin{equation}
p(\vec{x}_i|\vec{\theta})=\sum\limits_{k=1}^K \pi_kp_k(\vec{x}_i|\vec{\theta})
\end{equation}

Depending on the form of the likelihood $p(\vec{x}_i|\vec{z}_i)$ and the prior $p(\vec{z}_i)$, we can generate a variety of different models, as summarized in Table \ref{tab:popular-directed-latent-variable-models}.

\begin{table}
\centering
\begin{tabular}{llll}
\hline\noalign{\smallskip}
$p(\vec{x}_i|\vec{z}_i)$ & $p(\vec{z}_i)$ & \textbf{Name} & \textbf{Section} \\
\noalign{\smallskip}\svhline\noalign{\smallskip}
MVN & Discrete & Mixture of Gaussians & 11.2.1 \\
Prod. Discrete & Discrete & Mixture of multinomials & 11.2.2 \\
\multirow{2}{*}{Prod. Gaussian} & \multirow{2}{*}{Prod. Gaussian} & Factor analysis/ & \multirow{2}{*}{12.1.5} \\
                                &                                 & probabilistic PCA&  \\
\multirow{2}{*}{Prod. Gaussian} & \multirow{2}{*}{Prod. Laplace} & Probabilistic ICA/& \multirow{2}{*}{12.6} \\
                                &                                & sparse coding     &  \\
Prod. Discrete & Prod. Gaussian & Multinomial PCA & 27.2.3 \\
\multirow{2}{*}{Prod. Discrete} & \multirow{2}{*}{Dirichlet} & Latent Dirichlet & \multirow{2}{*}{27.3}\\
                                &                            & allocation       &  \\
Prod. Noisy-OR & Prod. Bernoulli & BN20/ QMR & 10.2.3 \\
Prod. Bernoulli & Prod. Bernoulli & Sigmoid belief net & 27.7 \\
\noalign{\smallskip}\hline
\end{tabular}
\caption{Summary of some popular directed latent variable models. Here “Prod” means product, so “Prod. Discrete” in the likelihood means a factored distribution of the form $\prod_j \mathrm{Cat}(x_{ij}|\vec{z}_i)$, and “Prod. Gaussian” means a factored distribution of the form $\prod_j \mathcal{N}(x_{ij}|\vec{z}_i)$.}
\label{tab:popular-directed-latent-variable-models}
\end{table}

\subsection{Mixtures of Gaussians}
\begin{equation}
p_k(\vec{x}_i|\vec{\theta})=\mathcal{N}(\vec{x}_i|\vec{\mu}_k,\Sigma_k)
\end{equation}

\subsection{Mixtures of multinoullis}
\begin{equation}
p_k(\vec{x}_i|\vec{\theta})=\prod\limits_{j=1}^D \mathrm{Ber}(x_{ij}|\mu_{jk})=\prod\limits_{j=1}^D \mu_{jk}^{x_{ij}}(1-\mu_{jk})^{1-x_{ij}}
\end{equation}
where $\mu_{jk}$ is the probability that bit $j$ turns on in cluster $k$.

The latent variables do not have to any meaning, we might simply introduce latent variables in order to make the model more powerful. For example, one can show that the mean and covariance of the mixture distribution are given by
\begin{align}
\mathbb{E}[\vec{x}] & = \sum\limits_{k=1}^K \pi_k\vec{\mu}_k \\
\mathrm{Cov}[\vec{x}] & = \sum\limits_{k=1}^K \pi_k(\Sigma_k+\vec{\mu}_k\vec{\mu}_k^T)-\mathbb{E}[\vec{x}]\mathbb{E}[\vec{x}]^T
\end{align}
where $\Sigma_k=\mathrm{diag}(\mu_{jk}(1-\mu_{jk}))$. So although the component distributions are factorized, the joint distribution is not. Thus the mixture distribution can capture correlations between variables, unlike a single product-of-Bernoullis model.

\subsection{Using mixture models for clustering}
There are two main applications of mixture models, black-box density model(see Section 14.7.3 TODO) and clustering(see Chapter 25 TODO).

\textbf{Soft clustering}
\begin{equation}\begin{split}
r_{ik} & \triangleq p(z_i=k|\vec{x}_i,\vec{\theta})=\dfrac{p(z_i=k,\vec{x}_i|\vec{\theta})}{p(\vec{x}_i|\vec{\theta})} \\
       & =\dfrac{p(z_i=k|\vec{\theta})p(\vec{x}_i|z_i=k,\vec{\theta})}{\sum_{k'=1}^K p(z_i=k'|\vec{\theta})p(\vec{x}_i|z_i=k',\vec{\theta})}
\end{split}\end{equation}
where $r_{ik}$ is known as the \textbf{responsibility} of cluster $k$ for point $i$.

\textbf{Hard clustering}
\begin{equation}
z_i^* \triangleq \arg\max_k r_{ik}=\arg\max_k p(z_i=k|\vec{x}_i,\vec{\theta})
\end{equation}

The difference between generative classifiers and mixture models only arises at training time: in the mixture case, we never observe $z_i$, whereas with a generative classifier, we do observe $y_i$(which plays the role of $z_i$).

\subsection{Mixtures of experts}
Section 14.7.3 TODO described how to use mixture models in the context of generative classifiers. We can also use them to create discriminative models for classification and regression. For example, consider the data in Figure \ref{fig:mixture-of-experts}(a). It seems like a good model would be three different linear regression functions, each applying to a different part of the input space. We can model this by allowing the mixing weights and the mixture densities to be input-dependent:
\begin{align}
p(y_i|\vec{x}_i,z_i=k,\vec{\theta}) & =\mathcal{N}(y_i|\vec{w}_k^T\vec{x},\sigma_k^2) \\
p(z_i | \vec{x}_i,\vec{\theta}) & = \mathrm{Cat}(z_i|\mathcal{S}(\vec{V}^T\vec{x}_i))
\end{align}

See Figure \ref{fig:mixture-of-experts2}(a) for the DGM.

\begin{figure}[hbtp]
\centering
\subfloat[]{\includegraphics[scale=.60]{mixture-of-experts-a.png}} \\
\subfloat[]{\includegraphics[scale=.60]{mixture-of-experts-b.png}} \\
\subfloat[]{\includegraphics[scale=.60]{mixture-of-experts-c.png}}
\caption{(a) Some data fit with three separate regression lines. (b) Gating functions for three different “experts”. (c) The conditionally weighted average of the three expert predictions.}
\label{fig:mixture-of-experts}
\end{figure}

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.50]{mixture-of-experts2.png}
\caption{(a) A mixture of experts. (b) A hierarchical mixture of experts.}
\label{fig:mixture-of-experts2}
\end{figure}

This model is called a \textbf{mixture of experts} or MoE (Jordan and Jacobs 1994). The idea is that each submodel is considered to be an “expert” in a certain region of input space. The function $p(z_i | \vec{x}_i,\vec{\theta})$ is called a \textbf{gating function}, and decides which expert to use, depending on the input values. For example, Figure \ref{fig:mixture-of-experts}(b) shows how the three experts have “carved up” the 1d input space, Figure \ref{fig:mixture-of-experts}(a) shows the predictions of each expert individually (in this case, the experts are just linear regression models), and Figure \ref{fig:mixture-of-experts}(c) shows the overall prediction of the model, obtained using
\begin{equation}
p(y_i|\vec{x}_i,\vec{\theta})=\sum\limits_{k=1}^K p(z_i=k | \vec{x}_i,\vec{\theta})p(y_i|\vec{x}_i,z_i=k,\vec{\theta})
\end{equation}

We discuss how to fit this model in Section TODO 11.4.3.

\section{Parameter estimation for mixture models}

\subsection{Unidentifiability}

\subsection{Computing a MAP estimate is non-convex}

\section{The EM algorithm}

\subsection{Introduction}
For many models in machine learning and statistics, computing the ML or MAP parameter estimate is easy provided we observe all the values of all the relevant random variables, i.e., if we have complete data. However, if we have missing data and/or latent variables, then computing the ML/MAP estimate becomes hard.

One approach is to use a generic gradient-based optimizer to find a local minimum of the NLL$(\vec{\theta})$. However, we often have to enforce constraints, such as the fact that covariance matrices must be positive definite, mixing weights must sum to one, etc., which can be tricky. In such cases, it is often much simpler (but not always faster) to use an algorithm called \textbf{expectation maximization},or \textbf{EM} for short (Dempster et al. 1977; Meng and van Dyk 1997; McLachlan and Krishnan 1997). This is is an efficient iterative algorithm to compute the ML or MAP estimate in the presence of missing or hidden data, often with closed-form updates at each step. Furthermore, the algorithm automatically enforce the required constraints.

See Table \ref{tab:summary-of-the-applications-of-EM} for a summary of the applications of EM in this book.

\begin{table}
\caption{Some models discussed in this book for which EM can be easily applied to find the ML/ MAP parameter estimate.}
\label{tab:summary-of-the-applications-of-EM}
\centering
\begin{tabular}{ll}
\hline\noalign{\smallskip}
\textbf{Model} & \textbf{Section} \\
\noalign{\smallskip}\svhline\noalign{\smallskip}
Mix. Gaussians & 11.4.2 \\
Mix. experts & 11.4.3 \\
Factor analysis & 12.1.5 \\
Student T & 11.4.5 \\
Probit regression & 11.4.6 \\
DGM with hidden variables & 11.4.4 \\
MVN with missing data & 11.6.1 \\
HMMs & 17.5.2 \\
Shrinkage estimates of Gaussian means & Exercise 11.13 \\
\noalign{\smallskip}\hline
\end{tabular}
\end{table}

\subsection{Basic idea}
EM exploits the fact that if the data were fully observed, then the ML/ MAP estimate would be easy to compute. In particular, each iteration of the EM algorithm consists of two processes: The E-step, and the M-step.
\begin{itemize}
\item{In the \textbf{E-step}, the missing data are inferred given the observed data and current estimate of the model parameters. This is achieved using the conditional expectation, explaining the choice of terminology.}
\item{In the \textbf{M-step}, the likelihood function is maximized under the assumption that the missing data are known. The missing data inferred from the E-step are used in lieu of the actual missing data.}
\end{itemize}

Let $\vec{x}_i$ be the visible or observed variables in case $i$, and let $\vec{z}_i$ be the hidden or missing variables. The goal is to maximize the log likelihood of the observed data:
\begin{equation}
\ell(\vec{\theta})=\log p(\mathcal{D}|\vec{\theta})=\sum\limits_{i=1}^N \log p(\vec{x}_i|\vec{\theta})=\sum\limits_{i=1}^N \log{\sum\limits_{\vec{z}_i} p(\vec{x}_i,\vec{z}_i|\vec{\theta})}
\end{equation}

Unfortunately this is hard to optimize, since the log cannot be pushed inside the sum.

EM gets around this problem as follows. Define the \textbf{complete data log likelihood} to be
\begin{equation}
\ell_c(\vec{\theta})=\sum\limits_{i=1}^N \log p(\vec{x}_i,\vec{z}_i|\vec{\theta})
\end{equation}

This cannot be computed, since $\vec{z}_i$ is unknown. So let us define the \textbf{expected complete data log likelihood} as follows:
\begin{equation}\label{eqn:auxiliary-function}
Q(\vec{\theta},\vec{\theta}^{t-1}) \triangleq \mathbb{E}_{\vec{z}|\mathcal{D},\theta^{t-1}}\left[\ell_c(\vec{\theta})\right]=\mathbb{E}\left[\ell_c(\vec{\theta})| \mathcal{D},\theta^{t-1}\right]
\end{equation}
where $t$ is the current iteration number. $Q$ is called the \textbf{auxiliary function}(see Section \ref{sec:Derivation-of-the-Q-function} for derivation). The expectation is taken wrt the old parameters, $\vec{\theta}^{t-1}$, and the observed data $\mathcal{D}$. The goal of the E-step is to compute $Q(\vec{\theta},\vec{\theta}^{t-1})$, or rather, the parameters inside of it which the MLE(or MAP) depends on; these are known as the \textbf{expected sufficient statistics} or \textbf{ESS}. In the M-step, we optimize the $Q$ function wrt $\vec{\theta}$:
\begin{equation}
\vec{\theta}^t=\arg\max_{\vec{\theta}} Q(\vec{\theta},\vec{\theta}^{t-1})
\end{equation}

To perform MAP estimation, we modify the M-step as follows:
\begin{equation}
\vec{\theta}^t=\arg\max_{\vec{\theta}} Q(\vec{\theta},\vec{\theta}^{t-1})+\log p(\vec{\theta})
\end{equation}
The E step remains unchanged.

In summary, the EM algorithm's pseudo code is as follows

\begin{algorithm}[htbp]
    \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
    \Input{observed data $\mathcal{D}=\{\vec{x}_1,\vec{x}_2,\cdots, \vec{x}_N$\},joint distribution $P(\vec{x},\vec{z}|\vec{\theta})$}
	\Output{model's parameters $\vec{\theta}$}

	// 1. identify hidden variables $\vec{z}$, write out the log likelihood function $\ell(\vec{x},\vec{z}|\vec{\theta})$ \\
	$\vec{\theta}^{(0)}$ = ... // initialize \\

	\While{(!convergency)} {
	    // 2. E-step: plug in $P(\vec{x},\vec{z}|\vec{\theta})$, derive the formula of $Q(\vec{\theta}, \vec{\theta}^{t-1})$ \\
	    $Q(\vec{\theta}, \vec{\theta}^{t-1})=\mathbb{E}\left[\ell_c(\vec{\theta})| \mathcal{D},\theta^{t-1}\right]$ \\
	    // 3. M-step: find \vec{\theta} that maximizes the value of $Q(\vec{\theta}, \vec{\theta}^{t-1})$ \\
		$\vec{\theta}^t=\arg\max\limits_{\vec{\theta}} Q(\vec{\theta}, \vec{\theta}^{t-1})$ \\
	}

\caption{EM algorithm}
\end{algorithm}

Below we explain how to perform the E and M steps for several simple models, that should make things clearer.

\subsection{EM for GMMs}

\subsubsection{Auxiliary function}
\begin{align}
Q(\vec{\theta}, \vec{\theta}^{t-1}) & =\mathbb{E}_{z|\mathcal{D},\theta^{t-1}}\left[\ell_c(\vec{\theta})\right] \nonumber \\
    & = \mathbb{E}_{z|\mathcal{D},\theta^{t-1}}\left[\sum\limits_{i=1}^N \log p(\vec{x}_i,z_i|\vec{\theta})\right] \nonumber \\
	& = \sum\limits_{i=1}^N \mathbb{E}_{z|\mathcal{D},\theta^{t-1}}\left\{\log\left[\prod\limits_{k=1}^K \left(\pi_kp(\vec{x}_i|\vec{\theta}_k)\right)^{\mathbb{I}(z_i=k)}\right]\right\} \nonumber \\
	& = \sum\limits_{i=1}^N{\sum\limits_{k=1}^K{\mathbb{E}[\mathbb{I}(z_i=k)]\log\left[\pi_kp(\vec{x}_i|\vec{\theta}_k)\right]}} \nonumber \\
	& = \sum\limits_{i=1}^N{\sum\limits_{k=1}^K{p(z_i=k|\vec{x}_i,\vec{\theta}^{t-1})\log\left[\pi_kp(\vec{x}_i|\vec{\theta}_k)\right]}} \nonumber \\
	& = \sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log \pi_k}}+\sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log p(\vec{x}_i|\vec{\theta}_k)}} \label{eqn:Q-miture-model}
\end{align}
where $r_{ik} \triangleq \mathbb{E}[\mathbb{I}(z_i=k)]=p(z_i=k|\vec{x}_i,\vec{\theta}^{t-1})$ is the \textbf{responsibility} that cluster $k$ takes for data point $i$. This is computed in the E-step, described below.

\subsubsection{E-step}
The E-step has the following simple form, which is the same for any mixture model:
\begin{equation}\begin{split}
r_{ik} & =p(z_i=k|\vec{x}_i,\vec{\theta}^{t-1})=\frac{p(z_i=k,\vec{x}_i|\vec{\theta}^{t-1})}{p(\vec{x}_i|\vec{\theta}^{t-1})} \\
  & =\frac{\pi_kp(\vec{x}_i|\vec{\theta}_k^{t-1})}{\sum_{k'=1}^K \pi_{k'}p(\vec{x}_i|\vec{\theta}_k^{t-1})}
\end{split}\end{equation}

\subsubsection{M-step}
In the M-step, we optimize $Q$ wrt $\vec{\pi}$ and $\vec{\theta}_k$.

For $\vec{\pi}$, grouping together only the terms that depend on $\pi_k$, we find that we need to maximize $\sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log \pi_k}}$. However, there is an additional constraint $\sum\limits_{k=1}^K{\pi_k}=1$, since they represent the probabilities $\vec{\pi}_k=P(z_i=k)$. To deal with the constraint we construct the Lagrangian
\begin{equation}
\mathcal{L}(\vec{\pi})=\sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log \pi_k}}+\beta\left(\sum\limits_{k=1}^K{\pi_k}-1\right) \nonumber
\end{equation}
where $\beta$ is the Lagrange multiplier. Taking derivatives, we find
\begin{equation}
\hat{\vec{\pi}}_k=\frac{\sum\limits_{i=1}^N \hat{r}_{ik}}{N}
\end{equation}
This is the same for any mixture model, whereas $\vec{\theta}_k$ depends on the form of $p(\vec{x}|\vec{\theta}_k)$.

For $\vec{\theta}_k$, plug in the pdf to Equation \ref{eqn:Q-miture-model}
\begin{equation*}\begin{split}
Q(\vec{\theta}, \vec{\theta}^{t-1}) & =\sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log \pi_k}}-\frac{1}{2}\sum\limits_{i=1}^N  \sum\limits_{k=1}^K  r_{ik}\left[\log |\vec{\Sigma}_k| + \right. \\
 & \quad \left. (\vec{x}_i-\vec{\mu}_k)^T\vec{\Sigma}_k^{-1}(\vec{x}_i-\vec{\mu}_k)\right]
\end{split}\end{equation*}

Take partial derivatives of $Q$ wrt $\vec{\mu}_k$, $\vec{\Sigma}_k$ and let them equal to 0, we can get
\begin{align}
\frac{\partial Q}{\partial \vec{\mu}_k} & = -\frac{1}{2}\sum\limits_{i=1}^N{r_{ik}\left[(\vec{\Sigma}_k^{-1}+\vec{\Sigma}_k^{-T})(\vec{x}_i-\vec{\mu}_k)\right]} \nonumber \\
    &  =-\sum\limits_{i=1}^N{r_{ik}\left[\vec{\Sigma}_k^{-1}(\vec{x}_i-\vec{\mu}_k)\right]}=0 \Rightarrow \nonumber \\
\hat{\vec{\mu}}_k & = \frac{\sum_{i=1}^N \hat{r}_{ik}\vec{x}_i}{\sum_{i=1}^N \hat{r}_{ik}}
\end{align}

\begin{align}
\frac{\partial Q}{\partial \vec{\Sigma}_k} & = -\frac{1}{2}\sum\limits_{i=1}^N{r_{ik}\left[\frac{1}{\vec{\Sigma}_k}-\frac{1}{\vec{\Sigma}_k^2}(\vec{x}_i-\vec{\mu}_k)(\vec{x}_i-\vec{\mu}_k)^T\right]}=0 \Rightarrow \nonumber \\
\hat{\vec{\Sigma}}_k & = \frac{\sum_{i=1}^N \hat{r}_{ik}(\vec{x}_i-\vec{\mu}_k)(\vec{x}_i-\vec{\mu}_k)^T}{\sum_{i=1}^N \hat{r}_{ik}} \\
 & =\frac{\sum_{i=1}^N \hat{r}_{ik}\vec{x}_i\vec{x}_i^T}{\sum_{i=1}^N \hat{r}_{ik}}-\vec{\mu}_k\vec{\mu}_k^T
\end{align}

\subsubsection{Algorithm pseudo code}

\begin{algorithm}[htbp]
    \SetAlgoNoLine
    \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
    \Input{observed data $\mathcal{D}=\{\vec{x}_1,\vec{x}_2,\cdots, \vec{x}_N$\},GMM}
	\Output{GMM's parameters $\vec{\pi},\vec{\mu},\vec{\Sigma}$}

	// 1. initialize \\
	$\vec{\pi}^{(0)}$ = ... \\
	$\vec{\mu}^{(0)}$ = ...  \\
	$\vec{\Sigma}^{(0)}$ = ...  \\
	t = 0 \\
	\While{(!convergency)} {
	    // 2. E-step \\
	    $\hat{r}_{ik}=\frac{\pi_kp(\vec{x}_i|\vec{\theta}_k^{t-1})}{\sum_{k'=1}^K \pi_{k'}p(\vec{x}_i|\vec{\mu}_k^{t-1},\vec{\Sigma}_k^{t-1})}$ \\
	    // 3. M-step \\
        $\hat{\vec{\pi}}_k=\frac{\sum_{i=1}^N \hat{r}_{ik}}{N}$ \\
		$\hat{\vec{\mu}}_k  = \frac{\sum_{i=1}^N \hat{r}_{ik}\vec{x}_i}{\sum_{i=1}^N \hat{r}_{ik}}$ \\
		$\hat{\vec{\Sigma}}_k =\frac{\sum_{i=1}^N \hat{r}_{ik}\vec{x}_i\vec{x}_i^T}{\sum_{i=1}^N \hat{r}_{ik}}-\vec{\mu}_k\vec{\mu}_k^T$ \\
        ++t \\
	}

\caption{EM algorithm for GMM}
\end{algorithm}

\subsubsection{MAP estimation}
As usual, the MLE may overfit. The overfitting problem is particularly severe in the case of GMMs. An easy solution to this is to perform MAP estimation. The new auxiliary function is the expected complete data log-likelihood plus the log prior:
\begin{equation}\begin{split}
Q(\vec{\theta}, \vec{\theta}^{t-1}) & = \sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log \pi_k}}+\sum\limits_{i=1}^N{\sum\limits_{k=1}^K{r_{ik}\log p(\vec{x}_i|\vec{\theta}_k)}} \\
 & +\log p(\vec{\pi})+\sum\limits_{k=1}^K{\log p(\vec{\theta}_k)}
\end{split}\end{equation}

It is natural to use conjugate priors.
\begin{align*}
p(\vec{\pi}) & = \mathrm{Dir}(\vec{\pi}|\vec{\alpha}) \\
p(\vec{\mu}_k,\vec{\Sigma}_k) & = \mathrm{NIW}(\vec{\mu}_k,\vec{\Sigma}_k|\vec{m}_0,\kappa_0,\nu_0,\vec{S}_0)
\end{align*}

From Equation \ref{eqn:Dir-MAP} and Section \ref{sec:Posterior-distribution-of-mu-and-Sigma}, the MAP estimate is given by
\begin{align}
\hat{\pi}_k & = \frac{\sum_{i=1}^N r_{ik}+\alpha_k-1}{N+\sum_{k=1}^K \alpha_k-K} \\
\hat{\vec{\mu}}_k & = \frac{\sum_{i=1}^N r_{ik}\vec{x}_i + \kappa_0\vec{m}_0}{\sum_{i=1}^N r_{ik} + \kappa_0} \\
\hat{\vec{\Sigma}}_k & = \frac{\vec{S}_0+\vec{S}_k+\frac{\kappa_0r_k}{\kappa_0+r_k}(\bar{\vec{x}}_k-\vec{m}_0)(\bar{\vec{x}}_k-\vec{m}_0)^T}{\nu_0+r_k+D+2} \\
\text{where } & r_k \triangleq \sum_{i=1}^N r_{ik}, \bar{\vec{x}}_k \triangleq \frac{\sum_{i=1}^N r_{ik}\vec{x}_i}{r_k}, \nonumber \\
  & \vec{S}_k \triangleq \sum_{i=1}^N r_{ik} (\vec{x}_i-\bar{\vec{x}}_k)(\vec{x}_i-\bar{\vec{x}}_k)^T \nonumber
\end{align}

\subsection{EM for K-means}
\label{sec:K-means}

\subsubsection{Representation}
\begin{equation}
y_j=k \text{ if } \|\vec{x}_j-\vec{\mu}_k\|_2^2 \text{ is minimal}
\end{equation}
where $\vec{\mu}_k$ \ is\ the centroid of cluster k.

\subsubsection{Evaluation}
\begin{equation}
\arg\min\limits_{\vec{\mu}} \sum_{j=1}^N {\sum_{k=1}^K}\gamma_{jk}{\|\vec{x}_j-\vec{\mu}_k\|_2^2}
\end{equation}

The hidden variable is $\gamma_{jk}$, which's meanining is:
\begin{equation} \nonumber
\gamma_{jk}=\begin{cases}
1, & \text{if } \|\vec{x}_j-\vec{\mu}_k\|_2 \text{ is minimal for } \vec{\mu}_k \\
0, & \text{otherwise}
\end{cases}
\end{equation}

\subsubsection{Optimization}
E-Step:
\begin{equation}
\gamma_{jk}^{(i+1)}=\begin{cases}
1, & \text{if } \|\vec{x}_j-\vec{\mu}_k^{(i)}\|_2 \text{ is minimal for } \vec{\mu}_k^{(i)} \\
0, & \text{otherwise}
\end{cases}
\end{equation}

M-Step:
\begin{equation}
\vec{\mu}_{k}^{(i+1)}= \frac{\sum_{j=1}^N{\gamma_{jk}^{(i+1)}\vec{x}_j}}{\sum \gamma_{jk}^{(i+1)}}
\end{equation}

\subsubsection{Tricks}

\textbf{Choosing $k$}

TODO

\textbf{Choosing the initial centroids(seeds)}

\begin{enumerate}
\item  \textbf{K-means++}.

The intuition that spreading out the k initial cluster centers is a good thing is behind this approach: the first cluster center is chosen uniformly at random from the data points that are being clustered, after which each subsequent cluster center is chosen from the remaining data points with probability proportional to its squared distance from the point's closest existing cluster center\footnote{\url{http://en.wikipedia.org/wiki/K-means++}}.

The exact algorithm is as follows:
\begin{enumerate}
\item Choose one center uniformly at random from among the data points.
\item For each data point \vec{x}, compute $D(\vec{x})$, the distance between \vec{x} and the nearest center that has already been chosen.
\item Choose one new data point at random as a new center, using a weighted probability distribution where a point \vec{x} is chosen with probability proportional to $D(\vec{x})^2$.
\item Repeat Steps 2 and 3 until $k$ centers have been chosen.
\item Now that the initial centers have been chosen, proceed using standard k-means clustering.
\end{enumerate}
\item TODO
\end{enumerate}

\subsection{EM for mixture of experts}

\subsection{EM for DGMs with hidden variables}

\subsection{EM for the Student distribution *}

\subsection{EM for probit regression *}

\subsection{Derivation of the $Q$ function}
\label{sec:Derivation-of-the-Q-function}

\begin{theorem}
(\textbf{Jensen's inequality}) Let $f$ be a convex function(see Section \ref{sec:Convexity}) defined on a convex set $\mathcal{S}$ . If $\vec{x}_1, \vec{x}_2, \cdots , \vec{x}_n \in \mathcal{S}$ and $\lambda_1, \lambda_2, \cdots , \lambda_n \geq 0$ with $\sum\limits_{i=1}^n \lambda_i=1$,
\begin{equation}
f\left(\sum\limits_{i=1}^n \lambda_i\vec{x}_i\right) \leq \sum\limits_{i=1}^n {\lambda_i f(\vec{x}_i)}
\end{equation}
\end{theorem}

\begin{proposition}
\begin{equation}
\log\left(\sum\limits_{i=1}^n \lambda_i\vec{x}_i\right) \geq \sum\limits_{i=1}^n {\lambda_i \log(\vec{x}_i)}
\end{equation}
\end{proposition}

Now let's proof why the $Q$ function should look like Equation \ref{eqn:auxiliary-function}:
\begin{align}
\ell(\vec{\theta}) &= \log{P(\mathcal{D}|\vec{\theta})}  \nonumber \\
                &= \log{{\sum\limits_{\vec{z}} P(\mathcal{D},\vec{z}|\vec{\theta})}} \nonumber \\
				&= \log{{\sum\limits_{\vec{z}} P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})}} \nonumber \\
\ell(\vec{\theta})-\ell(\vec{\theta}^{t-1}) &= \log\left[\sum\limits_{\vec{z}} P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})\right] - \log{P(\mathcal{D}|\vec{\theta}^{t-1})} \nonumber \\
                &= \log\left[\sum\limits_{\vec{z}} P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})\dfrac{P(\vec{z}|\mathcal{D},\theta^{t-1})}{P(\vec{z}|\mathcal{D},\theta^{t-1})}\right] \nonumber \\
				& \quad -\log{P(\mathcal{D}|\vec{\theta}^{t-1})} \nonumber \\
				&= \log\left[\sum\limits_{\vec{z}} P(\vec{z}|\mathcal{D},\theta^{t-1})\dfrac{P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})}{P(\vec{z}|\mathcal{D},\theta^{t-1})}\right] \nonumber \\
				& \quad - \log{P(\mathcal{D}|\vec{\theta}^{t-1})} \nonumber \\
				&\geq \sum\limits_{\vec{z}} P(\vec{z}|\mathcal{D},\theta^{t-1})\log\left[\dfrac{P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})}{P(\vec{z}|\mathcal{D},\theta^{t-1})}\right] \nonumber \\
				& \quad - \log{P(\mathcal{D}|\vec{\theta}^{t-1})} \nonumber \\
				&= \sum\limits_{\vec{z}} \left\{P(\vec{z}|\mathcal{D},\theta^{t-1}) \right. \nonumber \\
				& \quad \left. \log\left[\dfrac{P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})}{P(\vec{z}|\mathcal{D},\theta^{t-1})P(\mathcal{D}|\vec{\theta}^{t-1})}\right]\right\} \nonumber
\end{align}
\begin{align}
B(\vec{\theta},\vec{\theta}^{t-1}) & \triangleq \ell(\vec{\theta}^{t-1})+ \nonumber \\
 & \sum\limits_{\vec{z}} P(\vec{z}|\mathcal{D},\theta^{t-1})\log\left[\dfrac{P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})}{P(\vec{z}|\mathcal{D},\theta^{t-1})P(\mathcal{D}|\vec{\theta}^{t-1})}\right] \nonumber \\
\Rightarrow & \nonumber
\end{align}

\begin{align}
\vec{\theta}^t &= \arg\max\limits_{\vec{\theta}} B(\vec{\theta},\vec{\theta}^{t-1})  \nonumber \\
                &= \arg\max\limits_{\vec{\theta}}\left\{ \ell(\vec{\theta}^{t-1})+\right. \nonumber \\
				 & \quad \left. \sum\limits_{\vec{z}} P(\vec{z}|\mathcal{D},\theta^{t-1})\log\left[\dfrac{P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})}{P(\vec{z}|\mathcal{D},\theta^{t-1})P(\mathcal{D}|\vec{\theta}^{t-1})}\right]\right\} \nonumber \\
				& \text{Now drop terms which are constant w.r.t. } \vec{\theta} \nonumber \\
				&= \arg\max\limits_{\vec{\theta}}{\left\{\sum\limits_{\vec{z}} P(\vec{z}|\mathcal{D},\theta^{t-1})\log\left[P(\mathcal{D}|\vec{z},\vec{\theta})P(\vec{z}|\vec{\theta})\right]\right\}} \nonumber \\
				&= \arg\max\limits_{\vec{\theta}}{\left\{\sum\limits_{\vec{z}} P(\vec{z}|\mathcal{D},\theta^{t-1})\log\left[P(\mathcal{D},\vec{z}|\vec{\theta})\right]\right\}} \nonumber \\
				&= \arg\max\limits_{\vec{\theta}}{\left\{\mathbb{E}_{\vec{z}|\mathcal{D},\theta^{t-1}}\log\left[P(\mathcal{D},\vec{z}|\vec{\theta})\right]\right\}} \\
				&\triangleq \arg\max\limits_{\vec{\theta}}{Q(\vec{\theta}, \vec{\theta}^{t-1})}
\end{align}

\subsection{Convergence of the EM Algorithm *}

\subsubsection{Expected complete data log likelihood is a lower bound}

Note that $\ell(\vec{\theta}) \geq B(\vec{\theta},\vec{\theta}^{t-1})$, and $\ell(\vec{\theta}^{t-1}) \geq B(\vec{\theta}^{t-1},\vec{\theta}^{t-1})$, which means $B(\vec{\theta},\vec{\theta}^{t-1})$ is an lower bound of $\ell(\vec{\theta})$. If we maximize $B(\vec{\theta},\vec{\theta}^{t-1})$, then $\ell(\vec{\theta})$ gets maximized, see Figure \ref{fig:EM-algorithm}.

\begin{figure}[hbtp]
\centering
    \includegraphics[scale=.50]{EM-algorithm.png}
\caption{Graphical interpretation of a single iteration of the EM algorithm: The function $B(\vec{\theta},\vec{\theta}^{t-1})$ is bounded above by the log likelihood function $\ell(\vec{\theta})$. The functions are equal at $\vec{\theta} = \vec{\theta}^{t-1}$. The EM algorithm chooses $\vec{\theta}^{t-1}$ as the value of $\vec{\theta}$ for which $B(\vec{\theta},\vec{\theta}^{t-1})$ is a maximum. Since $\ell(\vec{\theta}) \geq B(\vec{\theta},\vec{\theta}^{t-1})$ increasing $B(\vec{\theta},\vec{\theta}^{t-1})$ ensures that the value of the log likelihood function $\ell(\vec{\theta})$ is increased at each step.}
\label{fig:EM-algorithm}
\end{figure}

Since the expected complete data log likelihood $Q$ is derived from $B(\vec{\theta},\vec{\theta}^{t-1})$ by dropping terms which are constant w.r.t. $\vec{\theta}$, so it is also a lower bound to a lower bound of $\ell(\vec{\theta})$.

\subsubsection{EM monotonically increases the observed data log likelihood}

\subsection{Generalization of EM Algorithm *}
EM algorithm can be interpreted as F function's maximization-maximization algorithm, based on this interpretation there are many variations and generalization, e.g., generalized EM Algorithm(GEM).

\subsubsection{F function's maximization-maximization algorithm}
\begin{definition}
Given the probability distribution of the hidden variable $Z$ is $\tilde{P}(Z)$, define \textbf{F function} as the following:
\begin{equation}
F(\tilde{P},\vec{\theta})=\mathbb{E}_{\tilde{P}}\left[\log{P(X,Z|\theta)}\right]+H(\tilde{P})
\end{equation}
Where $H(\tilde{P})=-\mathbb{E}_{\tilde{P}}\log\tilde{P}(Z)$, which is $\tilde{P}(Z)$'s entropy. Usually we assume that $P(X,Z|\theta)$ is continuous w.r.t. $\vec{\theta}$, therefore $F(\tilde{P},\vec{\theta})$ is continuous w.r.t. $\tilde{P}$ and $\vec{\theta}$.
\end{definition}

\begin{lemma}
\label{lemma:F-function}
For a fixed $\vec{\theta}$, there is only one distribution $\tilde{P}_{\theta}$ which maximizes $F(\tilde{P},\vec{\theta})$
\begin{equation}
\tilde{P}_{\theta}(Z)=P(Z|X, \vec{\theta})
\end{equation}
and $\tilde{P}_{\theta}$ is continuous w.r.t. $\vec{\theta}$.
\end{lemma}

\begin{proof}
Given a fixed $\vec{\theta}$, we can get $\tilde{P}_{\theta}$ which maximizes $F(\tilde{P},\vec{\theta})$. we construct the Lagrangian
\begin{equation}\begin{split}
\mathcal{L}(\tilde{P}, \vec{\theta}) & =\mathbb{E}_{\tilde{P}}\left[\log{P(X,Z|\theta)}\right]-\mathbb{E}_{\tilde{P}}\log\tilde{P}_{\theta}(Z) \\
                                     & \quad +\lambda\left[1-\sum\limits_Z{\tilde{P}(Z)}\right]
\end{split}\end{equation}

Take partial derivative with respect to $\tilde{P}_{\theta}(Z)$ then we get
\begin{equation}
\dfrac{\partial \mathcal{L}}{\partial{\tilde{P}_{\theta}(Z)}}=\log{P(X,Z|\theta)}-\log\tilde{P}_{\theta}(Z)-1-\lambda  \nonumber
\end{equation}

Let it equal to 0, we can get
\begin{equation}
\lambda=\log{P(X,Z|\theta)}-\log\tilde{P}_{\theta}(Z)-1 \nonumber
\end{equation}

Then we can derive that $\tilde{P}_{\theta}(Z)$ is proportional to $P(X,Z|\theta)$
\begin{eqnarray}
\dfrac{P(X,Z|\theta)}{\tilde{P}_{\theta}(Z)} &=& e^{1+\lambda} \nonumber \\
\Rightarrow \tilde{P}_{\theta}(Z) &=& \dfrac{P(X,Z|\vec{\theta})}{e^{1+\lambda}} \nonumber \\
\sum\limits_Z{\tilde{P}_{\theta}(Z)}=1 & \Rightarrow & \sum\limits_Z{\dfrac{P(X,Z|\vec{\theta})}{e^{1+\lambda}}}=1 \Rightarrow P(X|\vec{\theta})=e^{1+\lambda} \nonumber \\
\tilde{P}_{\theta}(Z) &=& \dfrac{P(X,Z|\vec{\theta})}{e^{1+\lambda}} = \dfrac{P(X,Z|\vec{\theta})}{P(X|\vec{\theta})}=P(Z|X, \vec{\theta}) \nonumber
\end{eqnarray}
\end{proof}

\begin{lemma}
If $\tilde{P}_{\theta}(Z)=P(Z|X, \vec{\theta})$, then
\begin{equation}
F(\tilde{P},\vec{\theta})=\log P(X|\vec{\theta})
\end{equation}
\end{lemma}

\begin{theorem}
One iteration of EM algorithm can be implemented as F function's maximization-maximization.

Assume $\vec{\theta}^{t-1}$ is the estimation of $\vec{\theta}$ in the $(t-1)$-th iteration, $\tilde{P}^{t-1}$ is the estimation of $\tilde{P}$ in the $(t-1)$-th iteration. Then in the $t$-th iteration two steps are:
\begin{enumerate}
\item for fixed $\vec{\theta}^{t-1}$, find $\tilde{P}^t$ that maximizes $F(\tilde{P},\vec{\theta}^{t-1})$;
\item for fixed $\tilde{P}^t$, find $\vec{\theta}^t$ that maximizes $F(\tilde{P}^t,\vec{\theta})$.
\end{enumerate}
\end{theorem}

\begin{proof}
(1) According to Lemma \ref{lemma:F-function}, we can get
\begin{equation}
\tilde{P}^t(Z)=P(Z|X, \vec{\theta}^{t-1}) \nonumber
\end{equation}

(2) According above, we can get
\begin{eqnarray}
F(\tilde{P}^t,\vec{\theta}) &=& \mathbb{E}_{\tilde{P}^t}\left[\log{P(X,Z|\theta)}\right]+H(\tilde{P}^t) \nonumber \\
    &=& \sum\limits_Z{P(Z|X,\vec{\theta}^{t-1})\log{P(X,Z|\theta)}}+H(\tilde{P}^t) \nonumber \\
	&=& Q(\vec{\theta}, \vec{\theta}^{t-1})+H(\tilde{P}^t)\nonumber
\end{eqnarray}

Then
\begin{equation}
\vec{\theta}^t=\arg\max\limits_{\theta}F(\tilde{P}^t,\vec{\theta})=\arg\max\limits_{\theta}Q(\vec{\theta}, \vec{\theta}^{t-1}) \nonumber
\end{equation}
\end{proof}

\subsubsection{The Generalized EM Algorithm(GEM)}
In the formulation of the EM algorithm described above, $\vec{\theta}^t$ was chosen as the value of $\vec{\theta}$ for which $Q(\vec{\theta}, \vec{\theta}^{t-1})$ was maximized. While this ensures the greatest increase in $\ell(\vec{\theta})$, it is however possible to relax the requirement of maximization to one of simply increasing $Q(\vec{\theta}, \vec{\theta}^{t-1})$ so that $Q(\vec{\theta}^t, \vec{\theta}^{t-1}) \geq Q(\vec{\theta}^{t-1}, \vec{\theta}^{t-1})$. This approach, to simply increase and not necessarily maximize $Q(\vec{\theta}^t, \vec{\theta}^{t-1})$ is known as the Generalized Expectation Maximization (GEM) algorithm and is often useful in cases where the maximization is difficult. The convergence of the GEM algorithm is similar to the EM algorithm.

\subsection{Online EM}

\subsection{Other EM variants *}

\section{Model selection for latent variable models}
\label{sec:Model-selection-for-LVM}
When using LVMs, we must specify the number of latent variables, which controls the model complexity. In particular, in the case of mixture models, we must specify $K$, the number of clusters. Choosing these parameters is an example of model selection. We discuss some approaches below.

\subsection{Model selection for probabilistic models}
The optimal Bayesian approach, discussed in Section \ref{sec:Bayesian-model-selection}, is to pick the model with the largest marginal likelihood, $K^*=\arg\max_k p(\mathcal{D}|k)$.

There are two problems with this. First, evaluating the marginal likelihood for LVMs is quite difficult. In practice, simple approximations, such as BIC, can be used (see e.g., (Fraley and Raftery 2002)). Alternatively, we can use the cross-validated likelihood as a performance measure, although this can be slow, since it requires fitting each model $F$ times, where Fis the number of CV folds.

The second issue is the need to search over a potentially large number of models. The usual approach is to perform exhaustive search over all candidate values ofK. However, sometimes we can set the model to its maximal size, and then rely on the power of the Bayesian Occam’s razor to “kill off” unwanted components. An example of this will be shown in Section TODO 21.6.1.6, when we discuss variational Bayes.

An alternative approach is to perform stochastic sampling in the space of models. Traditional approaches, such as (Green 1998, 2003; Lunn et al. 2009), are based on reversible jump MCMC, and use birth moves to propose new centers, and death moves to kill off old centers. However, this can be slow and difficult to implement. A simpler approach is to use a Dirichlet process mixture model, which can be fit using Gibbs sampling, but still allows for an unbounded number of mixture components; see Section TODO 25.2 for details.

Perhaps surprisingly, these sampling-based methods can be faster than the simple approach of evaluating the quality of eachKseparately. The reason is that fitting the model for each $K$ is often slow. By contrast, the sampling methods can often quickly determine that a certain value of $K$ is poor, and thus they need not waste time in that part of the posterior.

\subsection{Model selection for non-probabilistic methods}
What if we are not using a probabilistic model? For example, how do we choose $K$ for the K-means algorithm? Since this does not correspond to a probability model, there is no likelihood, so none of the methods described above can be used.

An obvious proxy for the likelihood is the \textbf{reconstruction error}. Define the squared reconstruction error of a data set $\mathcal{D}$, using model complexity $K$, as follows:
\begin{equation}
E(\mathcal{D}, K) \triangleq \frac{1}{|\mathcal{D}|}\sum\limits_{i=1}^N \lVert\vec{x}_i-\hat{\vec{x}}_i\rVert^2
\end{equation}

In the case of K-means, the reconstruction is given by $\hat{\vec{x}}_i=\vec{\mu}_{z_i}$, where $z_i=\arg\min_k \lVert\vec{x}_i-\hat{\vec{\mu}}_k\rVert^2$, as explained in Section 11.4.2.6 TODO.

In supervised learning, we can always use cross validation to select between non-probabilistic models of different complexity, but this is not the case with unsupervised learning. The most common approach is to plot the reconstruction error on the training set versus $K$, and to try to identify a \textbf{knee} or \textbf{kink} in the curve.

\section{Fitting models with missing data}
Suppose we want to fit a joint density model by maximum likelihood, but we have “holes” in our data matrix, due to missing data (usually represented by NaNs). More formally, let $O_{ij} =1$ if component $j$ of data case $i$ is observed, and let $O_{ij} =0$ otherwise. Let $\vec{X}_v=\{x_{ij}: \vec{O}_{ij} =1\}$ be the visible data, and $X_h=\{x_{ij}: \vec{O}_{ij} =0\}$ be the missing or hidden data. Our goal is to compute
\begin{equation}
\hat{\vec{\theta}}=\arg\max_{\vec{\theta}} p(\vec{X}_v|\vec{\theta}, \vec{O})
\end{equation}

Under the missing at random assumption (see Section \ref{sec:Dealing-with-missing-data}), we have

TODO

\subsection{EM for the MLE of an MVN with missing data}
TODO


\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=1.0in]{geometry}
\usepackage{xcolor}

\begin{document}

\noindent
Does $\displaystyle \sum_{n=1}^\infty \frac{n^2-1}{n^3+7}$
diverge, converge absolutely, or converge conditionally?

\subsection*{Solution 1}

$\displaystyle \sum_{n=1}^\infty \frac1{n}$ is a $p$-series which diverges by the $p$-series test. If we use $a_n = \frac{n^2-1}{n^3+7}$ and $b_n = \frac1{n}$, then
\begin{align*}
\lim_{n \to \infty} \frac{a_n}{b_n}
&= \lim_{n \to \infty} \frac{n^2-1}{n^3+7} \cdot \frac{n}{1}\\
&= \lim_{n \to \infty} \frac{n^3-n}{n^3+7} \\
&= \lim_{n \to \infty} \frac{3n^2-1}{3n^2} \text{ using L'hopital}\\
&= \lim_{n \to \infty} \frac{6n}{6n} \text{ using L'hopital}\\
&= \lim_{n \to \infty} 1\\
&= 1
\end{align*}
So by the Limit Comparison Test, the series $\displaystyle \sum_{n=1}^\infty \frac{n^2-1}{n^3+7}$ diverges.

\subsection*{Solution 2 (comment of method)}

The Direct Comparison Test will also work, but will involve finding first finding a fixed value of $K > 0$ such that
\[ \frac{n^2-1}{n^3+7} \geq K \cdot \frac1n.\]
As the previous several series have shown, this will involve a bit of work to the point that the Limit Comparison Test (solution 1) will be easier/quicker.

\end{document}%%%%%%%%%%%%%%%%%

\begin{align*}
L&=\lim_{n \to \infty} \sqrt[n]{|a_n|}\\
&= \lim_{n \to \infty} \sqrt[n]{\left| \right|}\\
\end{align*}

\begin{align*}
L&=\lim_{n \to \infty} \left|\frac{a_{n+1}}{a_n}\right|\\
&= \lim_{n \to \infty} \left| \right|\\
\end{align*}

\begin{align*}
\lim_{n \to \infty} a_n
&= \lim_{n \to \infty} \\
\end{align*}

Since $\sum |a_n| = \sum a_n$, the series $\displaystyle \sum_{n=1}^\infty AAAAAAAAAAAAAA$ converges absolutely.

Since $|r| < 1$, the series ...  converges by the Geometric Series Test.

Since $|r| \geq 1$, the series ...  diverges by the Geometric Series Test.

The function $f(x)=\frac{}{}$ is continuous, positive, and decreasing on $[1,\infty)$.

\subsection*{Solution}


\subsection{Integrated Y Estimates}

Given a 3D radial model, we can also compute the spherically integrated Compton-y parameter, $Y_{\it sph}(R)$

\begin{eqnarray}
Y_{\it sph}(R) &=& {{\sigma_T}\over{m_e c^2}}\integral{R}{0}{P(r)}{V}\\\nonumber
\label{eq:ysph}
     &=& {{\sigma_T}\over{m_e c^2}}\integral{R}{0}{P(r) 4\pi r^2}{r}\\\nonumber
     &=& {{\sigma_T P_0}\over{m_e c^2}}\integral{R}{0}{p(r)4\pi r^2}{r}\\
     &=& y(0){1\over{D_A\theta_c}}{1\over{I(0)}}\integral{R}{0}{p(r)4\pi r^2}{r}
\end{eqnarray}

if we substitute $P_e(0)$ from Eq.~\ref{eq:ytop}.  If we make the transformation $x = r/r_c$, to transform to dimensionless coordinates, we have:

\begin{eqnarray}
Y_{\it sph}(R) &=& y(0){1\over{D_A\theta_cI(0)}}r^3_c\integral{x_R}{0}{p(x)4\pi x^2}{x}\\\nonumber
               &=& y(0){1\over{D_A\theta_cI(0)}}D^3_A\theta^3_c\integral{x_R}{0}{p(x)4\pi x^2}{x}\\\nonumber
               &=& y(0)D^2_A\theta^2_c{1\over{I(0)}}\integral{x_R}{0}{p(x)4\pi x^2}{x}\\\nonumber
\end{eqnarray}

for which we require the cosmology (i.e., $D_A$).

\subsection{Mass estimates}

Similarly, we see that

\begin{eqnarray}
Y_{\it sph}(R) &=& {{\sigma_T}\over{m_e c^2}}\integral{R}{0}{P_e(r)}{V}\\\nonumber
     &=& {{\sigma_T}\over{m_e c^2}}\integral{R}{0}{P_e(r) 4\pi r^2}{r}\\\nonumber
     &=& {{\sigma_T P_e(0)}\over{m_e c^2}}\integral{R}{0}{p(r)4\pi r^2}{r}\\\nonumber
     &=& {{k_B\sigma_T T_e(0)}\over{m_e c^2}}{1\over{m_p\mu_e}}\integral{R}{0}{n_e(0)m_p\mu_ep(r)4\pi r^2}{r}\\\nonumber
     &=& {{k_B\sigma_T T_e(0)}\over{m_e c^2}}{1\over{m_p\mu_e}}\Mgas(R)\\\nonumber
\end{eqnarray}

from which we have

\begin{eqnarray}
\Mgas(R) &=& m_p\mu_e \left({{m_e c^2}\over{k_B T_e(0)}}\right)\left({{1}\over{\sigma_T}}\right)Y_{\it sph}(R)\\\nonumber
\end{eqnarray}

or

\begin{eqnarray}
\Mgas(R) &=& m_p\mu_e {{y(0)}\over{I(0)}}\left({{m_e c^2}\over{k_B T_e(0)}}\right)\left({{D^2_A\theta^2_c}\over{\sigma_T}}\right)\integral{x_R}{0}{p(x)4\pi x^2}{x}
\label{eq:mgas}
\end{eqnarray}

for which we require the cosmology (i.e., $D_A$) and the temperature normalization ($T_e(0)$).  Here $\mu_e$ is the mean molecular weight per electron, defined as the ratio of the mean particle mass per electron to the mass of the hydrogen atom.


\problemname{Stand Behind Braum!}

Braum is a tank character in the Multiplayer Online Battle Arena game League of Legends. Braum’s task is to absorb as
much damage from the enemy so that his allies may win the fight. Recently, Braum's performance has fallen off and he just isn't as
dominant as he once was. Refusing to be upset, Braum instead decides to hunker down and figure out some strategies so that he can
be a better tank. He decides that if he can calculate the amount of incoming damage, he can adjust his item purchases to
better protect his allies.\\

Given an incoming onslaught of attacks, Braum wants to calculate the Total Damage he’ll take.
Attacks come in three damage types, \textbf{Physical Damage}, \textbf{Magical Damage}, and \textbf{True Damage}.
We can calculate the Total Damage Taken as a sum of the Physical, Magical, and True Damage Taken:

\begin{align*}
    \text{Physical Damage Taken} &= \text{Physical Damage} \cdot \left( \frac{100}{100 + \text{Armor}}\right) \\
    \text{Magical Damage Taken} &= \text{Magical Damage} \cdot \left( \frac{100}{100 + \text{MR}}\right) \\
    \text{True Damage Taken} &= \text{True Damage}
\end{align*}

Braum notices that the total damage posseses the following relationship, $\alpha + \beta + \tau = 1$ . And furthermore
where the values of $\alpha, \beta, \tau$ possess the following relationships:
\begin{align*}
    \alpha &= \frac{\text{Physical Damage}}{\text{Total Damage}} \\
    \beta &= \frac{\text{Magical Damage}}{\text{Total Damage}} \\
    \tau  &= \frac{\text{True Damage}}{\text{Total Damage}}\\
\end{align*}

\section*{Input}
You will be given two lines of input. The first line contains $\alpha$, $\beta$, $\tau$, and $x$ representing the
percent of Physical, Magical, True, and Total damage. The second line contains the Armor and MR values that Braum has.

\section*{Output}
Output the Total Damage Taken by Braum from the incoming attacks, rounded down to the nearest integer value.


\documentclass[10pt]{article}

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

\usepackage{blkarray}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{eurosym}
\usepackage{enumerate}

\title{\textbf{Week 5.} Sequential games with complete information I: Backward Induction}
\date{}

\begin{document}
\maketitle

\subsection*{Exercise 1: Sequential prisoner's dilemma with remorse}

Revisit the prisoner's dilemma with remorse (Example 2.13 from the lecture). The
payoff matrix is:

\begin{equation*}
    \begin{blockarray}{ccc}
        & \text{Silence} & \text{Confess} \\
        \begin{block}{c(cc)}
            \text{Silence} & (3, 3) & (0, 0) \\
            \text{Confess} & (4, 0) & (1, 1) \\
        \end{block}
    \end{blockarray}
\end{equation*}

We already know that if both players move simultaneously, then the only Nash
equilibrium is (Confess, Confess).

\textbf{Now solve this game with backward induction, assuming that}

\begin{enumerate}
    \item the row player moves first,
    \item the column player moves first,
\end{enumerate}

\textbf{and interpret the result.}

\subsection*{Exercise 2: Ultimatum bargain in 2 rounds}

There is a good that is worth 1\euro~to the buyer and 0\euro~to the seller.
Sequence of moves:

\begin{enumerate}
    \item Seller names a price \(p \in \{0.01, 0.02, \dots, 0.99\}\).
    \item Buyer accepts or rejects the offer. If buyer accepts, the payoffs are
    \(p\) for the seller, \(1 - p\) for the buyer and the game is over.
    \item Otherwise, the buyer names a price \(p \in \{0.01, 0.02, \dots,
    0.99\}\).
    \item Seller accepts or rejects the offer. If the seller accepts, the
    payoffs are \(p - \delta\) for the seller, and \(1 - p - \delta\) for the buyer
    where \(\delta=0.045\) reflects the cost of having to go through a long
    negotiation. If the seller rejects, the payoff of both players is
    \(-\delta\).
\end{enumerate}

\textbf{Solve by backward induction, and interpret the result.}

\subsection*{Exercise 3: Stackelberg Duopoly}

Similar to the Cournot duopoly game consider the following situation:

\begin{itemize}
    \item Players: \(N = \{\text{Firm } 1, \text{Firm } 2\}\)
    \item Actions: Amount of good produced, \(x^{(i)} \in [0, \infty)\) for \(i \in \{1, 2\}\)
    \item Payoffs: \(\pi^{(i)}(x^{(1)}, x^{(2)}) = [a - b (x^{(1)} + x^{(2)})] x^{(i)} - c x^{(i)}\)
\end{itemize}

However, now assume that Firm 1 decides first, and Firm 2 observes Firms 1's
decision before choosing an action.

\textbf{Solve the game by backward induction (for \(a=10, b=1, c=1\)) and
compare the result to the Nash equilibrium of the Cournot duopoly
(\(\hat x^{(1)} = \hat x ^{(2)}  = 3)\)}.

[Hint: First you should find the best response of Firm 2. That is, for any given
output \(x^{(1)}\) of Firm 1, compute how Firm 2 would react if it wants to maximize
its payoffs. Using this, compute how Firm 1 can maximize its own payoff when it
takes into account that Firm 2 will react according to its best response.]

\subsection*{Bonus Exercise 1: Properties of backward induction}

\textbf{
Prove that for any finite game with perfect information (i.e. in any game in which
backward induction can be applied) the solution defined by backward induction
is a Nash equilibrium.}

\end{document}


\section{Model Description}

The eclipse module is responsible for determining whether or not a spacecraft is within the shadow of a solar eclipse and if so, how much. The module finds the states of the sun, spacecraft and planets of interest, allowing each body's postition to be related and the construction of the conical shadow model. This provides the means for computing what percent of the spacecraft is illuminated where a shadow factor of 0.0 represents a total eclipse and 1.0 represents no eclipse.

To determine the states of the bodies in question, messages are passed into the code. For the spacecraft, Cartesian vectors provide the position and velocity of its center of mass. For the sun and planets, a time is chosen and input into the module along with SPK, LSK, and PCK files, indicating ephemeris data, leapsecond information, and reference frame orientation. The planets desired to be used in the module are specified through the Basilisk messaging system, where corresponding strings (e.g. "venus", "earth", or "mars barycenter") are input as Spice Objects. This allows the state data to be obtained at the given time, using Spice and the kernel files. The kernels used when testing are given in the Test Parameters section. Fig. \ref{fig:ConShad} illustrates how the states are represented and will be identified in the mathematical model. Calculations in this model are taken from Montenbruck and Gill's \textit{Satellite Orbits Models, Methods and Applications} text \cite{bib:1}.
\begin{figure}[ht]
	\centering
	\captionsetup{justification=centering}
	\includegraphics[width=0.9\textwidth]{Figures/conical_shadow.png}
	\caption{Representation of a Conical Shadow}\label{fig:ConShad}
\end{figure}
\subsection{Mathematical model}

\subsubsection{Determining States}
The initial step in the eclipse module is to obtain the celestial bodies' state data and transform them into usable terms. The relationships shown below remove the dependancy on relating position to the inertial frame \textit{N} and instead utilize the planet \textit{P}, spacecraft body \textit{B}, and helio \textit{H} frames.
\begin{equation} \label{eq:1}
\bm{s}_{P/H} = \bm{r}_{N/H} - \bm{r}_{N/P}
\end{equation}
\begin{equation} \label{eq:2}
\bm{r}_{B/H} = \bm{r}_{N/H} - \bm{r}_{N/B}
\end{equation}
\begin{equation} \label{eq:3}
\bm{s}_{P/B} = \bm{r}_{N/B} - \bm{r}_{N/P}
\end{equation}

The previous three equations provide coordinates for the sun with respect to both the occulting planet and occulted spacecraft as well as the spacecraft's position with respect to the planet, respectively. The parameters on the right side of these equations come from the input state data where $\bm{r}_{N/H}$, $\bm{r}_{N/P}$, and $\bm{r}_{N/B}$ are the sun, planet, and spacecraft positions in the inertial frame.

This module supports the use of multiple occulting bodies, so it is important to analyze only the planet with the highest potential to cause an eclipse. Thus, the closest planet is determined by comparing the magnitude of each planet's distance to the spacecraft, $|\bm{s}_{P/B}|$. Note that if the spacecraft is closer to the sun than the planet, i.e. $|\bm{r}_{B/H}| < |\bm{s}_{P/H}|$, an eclipse is not possible and the shadow fraction is immediately set to 1.0.

\subsection{Eclipse Conditions}
When analyzing the conical shadow model, there are critical distances and conical dimensions that must be considered. These parameters are determined by first knowing the planet's equatorial radius $r_P$, which is used to solve for the angles of the shadow cones. Angles $f_1$ and $f_2$ are computed as shown below, where the subscript 1 relates to the cone of the penumbra and 2 relates to the umbra.
\begin{equation} \label{eq:7}
f_1 = \frac{\arcsin(r_H + r_P)}{|\bm{s}_{P/H}|}
\end{equation}
 \begin{equation} \label{eq:8}
 f_2 = \frac{\arcsin(r_H - r_P)}{|\bm{s}_{P/H}|}
 \end{equation}

Here $r_H$ indicates the equatorial radius of the sun, which is 695000 km. Both the sun and planet radii must be input in terms of meters.

As shown by Fig. \ref{fig:ConShad}, the fundamental plane is perpendicular to the shadow axis and coincident with the spacecraft body. The distance between the plane-axis intersection and the center of the planet is given by $s_0$ as shown by Eq. \ref{eq:9}.
\begin{equation} \label{eq:9}
s_0 = \frac{-\bm{s}_{P/B} \cdot \bm{s}_{P/H}}{|\bm{s}_{P/H}|}
\end{equation}

This distance and the shadow cone angles can now be used to determine the distances, $c_1$ and $c_2$, between the fundamental plane and the cones' vertices $V_1$ and $V_2$. These are calculated as follows:
\begin{equation} \label{eq:10}
c_1 = s_0 + \frac{r_P}{\sin(f_1)}
\end{equation}
\begin{equation} \label{eq:11}
c_2 = s_0 - \frac{r_P}{\sin(f_2)}
\end{equation}

As shown in Eq. \ref{eq:12} and \ref{eq:13}, these are then used to find the radii, $l_1$ and $l_2$, of the shadow cones in the fundamental plane.
\begin{equation} \label{eq:12}
l_1 = c_1 \tan(f_1)
\end{equation}
\begin{equation} \label{eq:13}
l_2 = c_2 \tan(f_2)
\end{equation}

Finding these parameters provides insight into the type of eclipse that the spacecraft is experiencing. To determine the type, it is useful to compare the cone radii to the distance between the spacecraft and the shadow axis, which is given by $l$.
\begin{equation}\label{eq:14}
l = \sqrt{|\bm{s}_{P/B}|^2 - s^2_0}
\end{equation}
Total and annular eclipses both require the spacecraft to be relatively close to the shadow axis, where $|l|<|l_2|$. The difference between these two types is that the planet is closer to the spacecraft for a total eclipse ($c_2 < 0$) than during an annular eclipse ($c_2 > 0$). If the spacecraft is further from the shadow axis but still within a cone radius ($|l|<|l_1|$), it is experiencing a partial eclipse.

\subsection{Percent Shadow}
With the eclipse type determined, the shadow fraction can now be found. To find the shadow fraction, the apparent radii of the sun and planet and the apparent seperation of both bodies are needed. These are given, respectively, by $a$, $b$, and $c$ in the equations below.
\begin{equation} \label{eq:15}
a = \arcsin(\frac{r_H}{|\bm{r}_{B/H}|})
\end{equation}
\begin{equation} \label{eq:16}
b = \arcsin(\frac{r_P}{|\bm{s}_{P/B}|})
\end{equation}
\begin{equation} \label{eq:17}
c = \arccos(\frac{-\bm{s}_{P/B} \cdot \bm{r}_{B/H}}{|\bm{s}_{P/B}| |\bm{r}_{B/H}|})
\end{equation}
Fig. \ref{fig:disk} below illustrates the overlapping disk model that represents the occultation, where the solid orange line indicates the sun and the dotted blue line indicates the planet.
\begin{figure}
		\centering
	\captionsetup{justification=centering}
	\includegraphics[width=0.9\textwidth]{Figures/diskModel.png}
	\caption{Occultation Disk Model}\label{fig:disk}
\end{figure}
\subsubsection{Total Eclipse ($c < b-a$)}

This type assumes that the apparent radius of the planet is larger than that of the sun ($b>a$). A total eclipse produces a total shadow, so the shadow fraction is 0.0.
\subsubsection{Annular Eclipse ($c < a-b$)}

This type assumes the apparent radius of the sun is larger than that of the planet ($a>b$). Use the equation for a circular area, $A = \pi r^2$, to find the area of the sun and planet faces, replacing $r$ with the corresponding apparent radius. The shadow fraction is then just the ratio of the planet's area to the sun's area.
\begin{equation} \label{eq:18}
Shadow Fraction = \frac{A_P}{A_H}
\end{equation}
\subsubsection{Partial Eclipse ($c < a+ b$)}

For a partial eclipse, the occulted area is given by Eq. \ref{eq:19}.
\begin{equation} \label{eq:19}
A = a^2 \arccos(\frac{x}{a}) + b^2 \arccos(\frac{c-x}{b}) - cy
\end{equation}
Parameters a, b, and c are those calculated previously in Eq. \ref{eq:15}, \ref{eq:16}, and \ref{eq:17}. The values x and y are given by the following equations.
\begin{equation}
x = \frac{c^2 + a^2 - b^2}{2c}
\end{equation}
\begin{equation}
y = \sqrt{a^2 - x^2}
\end{equation}

Like with the annular partial eclipse, the shadow factor for this type is the ratio between the occulted area and the sun's apparent area. This is given by the equation below.
\begin{equation}
Shadow Fraction = 1 - \frac{A}{\pi a^2}
\end{equation}


\subsection{Group action}

We have a group \(G\) and a set \(S\).

We have a function \(g.s\) which maps onto \(S\) such that:

\begin{itemize}
\item \(I.s=s\)
\item \((gh).s=g(h.s)\)
\end{itemize}


\subsection{Length of paths in Riemann manifolds}

We can work out the length of a path through a Riemann manifold.

The geodesic is the shortest such path.

The Riemann metric between two points is the length of the geodesic.


\documentclass{amsart}
\usepackage{hyperref,amsmath,amsfonts}

\begin{document}
\title{Library for Quadratic Programming}
\author{Vojt\v{e}ch Franc\\ xfrancv at cmp.felk.cvut.cz}
\maketitle
%\tableofcontents

\def\x{\mathbf x}
\def\f{\mathbf f}
\def\a{\mathbf a}
\def\H{\mathbf H}
\def\Sequ{\rm S_{\rm equ}}
\def\Sneq{\rm S_{\rm neq}}
\renewcommand{\Re}{\mathbb{R}}

\section*{Introduction}

LIBQP is C library which implements algorithms for solving two special instances of
convex Quadratic Programming (QP):

%\begin{enumerate}
 % \item

\large
\subsection*{QP task with simplex constraints} This QP task is defined as follows
\[
   \begin{array}{lrcll}
     \mbox{minimize} &\multicolumn{4}{l}{\displaystyle\frac{1}{2}\x^T \H \x +
       \x^T \f} \\ \\
     \mbox{subject to}
     & \displaystyle\sum_{i\in I_k} x_i & = & b_k\:, & k\in \Sequ \\
     & \displaystyle\sum_{i\in I_k} x_i & \leq & b_k\:, & k\in \Sneq \\
     & x_i & \geq & 0\:, & i\in I \:.
   \end{array}
\]
where \(\x = (x_1,\ldots,x_n)\in\Re^n\) is the optimized vector, \(\H
\in\Re^{n\times n}\) is a symmetric positive semi-definite matrix,
\(\f\in\Re^n\) is a vector, \(I =
\{1,\ldots,n\}\) is an index set, \(\{I_1,\ldots,I_m\}\) are subsets of \(I\)
such that  \(I_1\cup \ldots \cup I_k = I\) and \(I_1 \cap \ldots \cap I_k =
\emptyset\), \(\Sequ\) and \(\Sneq\) are index sets such that
\(\Sequ \cup \Sneq = \{1,\ldots,m\}\) and \(\Sequ \cap \Sneq = \emptyset\),
\((b_1,\ldots,b_m)\in\Re^m\) are positive numbers.

The implemented solver (\verb|libqp_splx.c|) is a generalization of the method
proposed in~\cite{Franc-TR-2006-04,Fan-JMLR05}. It is based on the Sequential Minimal
Optimization (SMO) algorithm with an improved working set selection
strategy. Solving instances of this QP task is required, for example, in machine
learning methods like Structured SVM learning, Bundle Methods for Risk
Minimization, binary SVM with L2-soft margin, etc.

\subsection*{QP task with box constraints and a single linear equality constraint}
This QP task is defined as follows
\[
  \begin{array}{lrcl}
    \mbox{minimize} &\multicolumn{3}{l}{\displaystyle\frac{1}{2}\x^T \H \x +
      \x^T \f} \\ \\
    \mbox{subject to}
    & \displaystyle\x^T \a & = & b\:, \\
    & \multicolumn{3}{c}{\displaystyle l_i \leq x_i \leq u_i\:, \qquad i=1,\ldots,n}\:,
  \end{array}
\]
where \(\x = (x_1,\ldots,x_n)\in\Re^n\) is the optimized vector, \(\H
\in\Re^{n\times n}\) is a symmetric positive semi-definite matrix,
\(\f\in\Re^n\) is a vector, $\a\in\Re^n$ is a vector with non-zero entries,
$b\in\Re$ is a scalar, $(l_1,\ldots,l_n)\in(\Re \cup \{-\infty\})^n$ and
$(u_1,\ldots,u_n)\in(\Re\cup\{\infty\})^n$ are lower and upper bounds,
respectively.

The solver (\verb|libqp_gsmo.c|) is the exact implementation of the
Generalized Sequential Minimal Optimizer proposed in~\cite{Keerthi-00}.
Solving this QP task is required, for example, when training binary
SVM with L1-soft margin.

\section*{Interfaces}

LIBQP is implemented in C language and interfaces to Matlab.

\section*{Platforms}

GNU/Linux. It should run also under Windows though not tested.

\section*{Installation}

LIBQP can be downloaded from \url{http://cmp.felk.cvut.cz/~xfrancv/libqp/libqp.zip}.

\subsection*{MATLAB}

\begin{enumerate}
  \item Run Matlab and go to the folder \verb|libqp_root/matlab|
    \begin{verbatim}
      cd libqp_root/matlab
    \end{verbatim}
  \item Compile mex files by running
    \begin{verbatim}
      libqp_compile
    \end{verbatim}
\end{enumerate}
Now you can use \verb|libqp_splx| and \verb|libqp_gsmo| solvers located in
\verb|libqp_root/matlab|. To make these function visible from Matlab you
need to add
\begin{verbatim}
    addpath('libqp_root/matlab')
\end{verbatim}
to your \verb|startup.m| file.

\noindent
To test the solvers run scripts
\begin{verbatim}
  libqp_splx_test
  libqp_gsmo_test
\end{verbatim}

\subsection*{Example application}

\begin{enumerate}
  \item Go to the folder \verb|libqp_root/examples|
    \begin{verbatim}
      cd libqp_root/examples
    \end{verbatim}
  \item Issue make
    \begin{verbatim}
      make
    \end{verbatim}
\end{enumerate}
Now you can run test script
\begin{verbatim}
    ./run_test
\end{verbatim}

\section*{License}

LIBQP is licensed under the GPL version 3 (\url{http://gplv3.fsf.org/}).

\begin{thebibliography}{00}
\bibitem[1]{Franc-TR-2006-04}
V. Franc, V. Hlavac. A Novel Algorithm for Learning Support Vector Machines
with Structured Output Spaces. Research Report K333 22/06, CTU-CMP-2006-04.
May, 2006.
\url{ftp://cmp.felk.cvut.cz/pub/cmp/articles/franc/Franc-TR-2006-04.ps}
\bibitem[2]{Fan-JMLR05}
R.-E. Fan, P.-H. Chen, C.-J. Lin. Working Set Selection Using Second Order
Information for Training SVM. JMLR. vol 6. 2005.
\url{TBA}
\bibitem[3]{Keerthi-00}
S.-S. Keerthi, E.G.Gilbert. Convergence of a Generalized SMO Algorithm for SVM
Classifier Design. Technical Report CD-00-01, Control Division, Dept. of Mechanical
and Production Engineering, National University of Singapore, 2000.
\url{http://citeseer.ist.psu.edu/keerthi00convergence.html}
\end{thebibliography}
\end{document}


\section{ReLU multigrid method for nonnegative solution}
Considering $f = (1, 1, ..., 1)^\top$,
\begin{breakablealgorithm}%[!htb]
	\caption{$(u^{1}, u^2, \cdots, u^J) = {\text{MG0}}(f; u^0; J,\nu_1, \cdots, \nu_J)$}
	\label{alg:L-ReLUSlash0}
	\begin{algorithmic}
		\State Set up
		$$
		f^1 = f, \quad u^{1}=u^0.
		$$
		\State Smoothing and restriction from fine to coarse level (nested)
		\For{$\ell = 1:J$}
		\For{$i = 1:\nu_\ell$}
		\State
		\begin{equation}\label{eq:smoothing}
		u^{\ell} \leftarrow u^{\ell} + S^\ell \ast \text{Relu}( (f^\ell - A_\ell \ast u^{\ell})).
		\end{equation}
		\EndFor
		\State Form restricted residual and set initial guess:
		$$
		u^{\ell+1,0} \leftarrow 0, \quad f^{\ell+1} \leftarrow R \ast_2 (f^\ell -  A_\ell \ast u^{\ell}), A_{\ell+1} = R \ast_2 A_\ell \ast (R\ast_2^\top).
		$$
		\EndFor
	\end{algorithmic}
\end{breakablealgorithm}
Algorithm 5 is not convergent.

\newpage
\begin{breakablealgorithm}%[!htb]
	\caption{$(u^{1}, u^2, \cdots, u^J) = {\text{MG0}}(f; u^0; J,\nu_1, \cdots, \nu_J)$}
	\label{alg:L-ReLUSlash1}
	\begin{algorithmic}
		\State Set up
		$$
		f^1 = f, \quad u^{1}=u^0.
		$$
		\State Smoothing and restriction from fine to coarse level (nested)
		\For{$\ell = 1:J$}
		\For{$i = 1:\nu_\ell$}
		\State
		\begin{equation}\label{eq:smoothing}
		u^{\ell} \leftarrow u^{\ell} +\text{Relu}( S^\ell \ast  (f^\ell - A_\ell \ast u^{\ell})).
		\end{equation}
		\EndFor
		\State Form restricted residual and set initial guess:
		$$
		u^{\ell+1,0} \leftarrow 0, \quad f^{\ell+1} \leftarrow R \ast_2 (f^\ell -  A_\ell \ast u^{\ell}), A_{\ell+1} = R \ast_2 A_\ell \ast (R\ast_2^\top).
		$$
		\EndFor
	\end{algorithmic}
\end{breakablealgorithm}

Algorithm 6 is convergent, the iterative steps is list below. $l = 2$
\\
\begin{tabular}{c|c|c|}
	J &MG& Algorithm 6\\ \hline
	2 & 15 & 27 \\\hline
	3 & 16 & 35 \\\hline
	4 & 17 & 38 \\\hline
\end{tabular}

\subsection{$\Pi$ is interpolation}
\textbf{Not Convergent}
		\begin{equation}
		u^{\ell,i} \leftarrow u^{\ell,i-1} + \text{Relu}\circ B_{\ell,i}  ({f^\ell -  A^{\ell} (u^{\ell,i-1})}).
		\end{equation}
	 \begin{equation}
	 u^{\ell,i} \leftarrow u^{\ell,i-1} + B_{\ell,i}\circ\text{Relu}({f^\ell -  A^{\ell} (u^{\ell,i-1})}).
	 \end{equation}
\textbf{Almost the same as without Relu}
	 \begin{equation}
	 u^{\ell,i} \leftarrow u^{\ell,i-1} +B_{\ell,i}  ({f^\ell -   \text{Relu}\circ A^{\ell} (u^{\ell,i-1})}).
	 \end{equation}
	 \begin{equation}
	 u^{\ell,i} \leftarrow u^{\ell,i-1} + B_{\ell,i}  ({f^\ell -  A^{\ell} \circ\text{Relu}(u^{\ell,i-1})}).
	 \end{equation}

497-6DL/8 Convolutional Multigrid Method/8.13-ReLU-multigrid.tex