\documentclass{article}

\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc}    % use 8-bit T1 fonts
\usepackage{hyperref}       % hyperlinks
\usepackage{url}            % simple URL typesetting
\usepackage{booktabs}       % professional-quality tables
\usepackage{amsfonts}       % blackboard math symbols
\usepackage{nicefrac}       % compact symbols for 1/2, etc.
\usepackage{microtype}      % microtypography
\usepackage{natbib}         % bibliography \usepackage{apalike}
\usepackage{xcolor}
\usepackage{graphicx}

\title{REINFORCE Variants}

\author{
Sotetsu KOYAMADA
}

\begin{document}

\maketitle

\tableofcontents

\begin{abstract}
We summarize the famous REINFORCE~\citep{Williams1992-rp} algorithm and its variants.
\end{abstract}

\section{Notation}

\begin{itemize}
	\item Assumes Episodic MDPs.
	\item Discount factor is omitted for the simplicity.
\end{itemize}

The reinforcement learning objective is defined by:

\begin{eqnarray}
J(\theta) := \mathbb{E}_{(s_1, a_1, \ldots, s_T, a_T) \sim p_\theta(s_1, a_1, \ldots, s_T, a_T)} \Biggl[ \sum_{t=1}^{T} r(s_t, a_t) \Biggr].
\end{eqnarray}

\section{REINFORCE Variants}

Policy gradient $\nabla_\theta J(\theta)$ is derived as

\begin{eqnarray}
\nabla_\theta J(\theta)
= \mathbb{E}_{(s_1, a_1, \ldots, s_T, a_T) \sim p_\theta(s_1, a_1, \ldots, s_T, a_T)} \Biggl[ \Biggl(\sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{t^\prime}|s_{t^\prime}) \Biggr) \Biggl( \sum_{t=1}^T r(s_t, a_t) \Biggr) \Biggr].
\end{eqnarray}

\subsection{Vanilla REINFORCE}

\begin{eqnarray}
\nabla_\theta J(\theta)
&\approx& \frac{1}{N} \sum_{n=1}^{N} \Biggl( \sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime}) \Biggr) \Biggl( \sum_{t=1}^T r(s_{n, t}, a_{n, t}) \Biggr) \\
&=& \frac{1}{N} \sum_{n=1}^{N} \mathcal{R}_n \sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime})
\end{eqnarray}
where $\mathcal{R}_{n} := \sum_{t=1}^T r(s_{n, t}, a_{n, t})$.

\subsection{Future Return}

\begin{eqnarray}
\nabla_\theta J(\theta)
&\approx& \frac{1}{N} \sum_{n=1}^{N} \Biggl( \sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime}) \Biggl(\, \sum_{{\color{red}{t=t^\prime}}}^{T} r(s_{n, t}, a_{n, t}) \Biggr) \Biggr)  \\
&=& \frac{1}{N} \sum_{n=1}^{N} \sum_{t^\prime=1}^{T} \mathcal{R}_{n, {\color{red}{t^\prime}}} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime})
\end{eqnarray}
where $\mathcal{R}_{n, t^\prime} := \sum_{t=t^\prime}^T r(s_{n, t}, a_{n, t})$.

\subsection{Average Baselines}

\begin{eqnarray}
\nabla_\theta J(\theta)
\approx \frac{1}{N} \sum_{n=1}^{N} \Biggl(\mathcal{R}_n {\color{red}{- \frac{1}{N} \sum_{n^\prime=1}^N \mathcal{R}_n }} \Biggr)\sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime})
\end{eqnarray}

For the future return case,

\begin{eqnarray}
\nabla_\theta J(\theta)
\approx \frac{1}{N} \sum_{n=1}^{N} \sum_{t^\prime=1}^{T} \Biggl(\mathcal{R}_{n, t^\prime} {\color{red}{- \frac{1}{N} \sum_{n^\prime=1}^N \mathcal{R}_{n^\prime, t^\prime} }} \Biggr)  \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime})
\end{eqnarray}

\subsubsection{Debiasing Factor}
While it is well known that introducing action-independent baseline is unbiased,
estimating the baseline using the same samples used in return estimation may introduce some bias.
Rescaling the gradient can make the estimator unbiased.

\begin{eqnarray}
\nabla_\theta J(\theta)
\approx \frac{1}{{\color{red}{N-1}}} \sum_{n=1}^{N} \Biggl(\mathcal{R}_n - \frac{1}{N} \sum_{n^\prime=1}^N \mathcal{R}_n \Biggr)\sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime})
\end{eqnarray}
See Section 2.4 in \citet{Parmas2020-tr} for the detailed explanation.
For the future return case,
\begin{eqnarray}
\nabla_\theta J(\theta)
\approx \frac{1}{{\color{red}{N-1}}} \sum_{n=1}^{N} \sum_{t^\prime=1}^{T} \Biggl(\mathcal{R}_{n, t^\prime} - \frac{1}{N} \sum_{n^\prime=1}^N \mathcal{R}_{n^\prime, t^\prime} \Biggr)  \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime})
\end{eqnarray}

\subsection{Entropy regularization}
We can add entropy regularization term to enhance the exploration~\citep{Mnih2016-eh}
\begin{eqnarray}
\nabla_\theta J(\theta)
&:=& \frac{1}{N} \sum_{n=1}^{N} \mathcal{R}_n \sum_{t^\prime=1}^{T} \nabla_\theta \log \pi_\theta (a_{n, t^\prime}|s_{n, t^\prime}) +
{\color{red}{\beta \mathcal{H} \bigl(\pi_\theta(\, \cdot \,|\, s_{n, t^\prime}) \bigr)}}.
\end{eqnarray}

\subsection{Maximum Entropy}
Let us define maximum entropy RL objective \citep{Haarnoja2017-xl} (TODO: check other ref) by
\begin{eqnarray}
J(\theta) := \mathbb{E}_{(s_1, a_1, \ldots, s_T, a_T) \sim p_\theta(s_1, a_1, \ldots, s_T, a_T)} \Biggl[ \sum_{t=1}^{T} r(s_t, a_t) {\color{red}{ + \alpha \mathcal{H} \bigl(\pi_\theta(\, \cdot \,|\, s_t) \bigr) }}\Biggr],
\end{eqnarray}
where $\mathcal{H} \bigl(\pi_\theta(\, \cdot \,|\, s_t) \bigr)$ is the entropy term of policy $\pi_\theta$ at state $s_t$ and $\alpha$ is a hyperparameter.
Then, the gradient is derived as
\begin{eqnarray}
\textrm{TBA.}
\end{eqnarray}
For the future return case,
\begin{eqnarray}
\textrm{TBA.}
\end{eqnarray}

\bibliographystyle{apalike}
\bibliography{reference}

\end{document}


\documentclass[12pt, a4paper]{article}
% \usepackage{ctex}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{bm}
\usepackage{cases}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{hyperref}
\usepackage{amsfonts}
\usepackage{authblk}
\usepackage{mathrsfs}
\newcommand{\E}{\mathbb{E}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathcal{N}}
\hypersetup{hidelinks}
\title{PRML Note\\C09 Mixture Models and EM}
\author{Yang Zhao}
\affil{Department of Automation, Tsinghua University}
\date{}

\begin{document}
    \maketitle
    \section{K-means Clustering}
    \begin{itemize}
        \item Suppose we have a data set $\{\bm{x}_1,\bm{x}_2,\cdots,\bm{x}_N\}$ and
        our goal is to partition the data set into some number $K$ of clusters, where
        we shall suppose for the moment that the value of $K$ is given.
        \item We might think of a cluster as comprising a group of data points whose
        inter-point distances are small compared with the distances to points outside
        of the cluster. To do this, we introduce a set of D-dimensional vectors
        $\bm{\mu}_k$, where $k=1,\cdots,K$, in which $\bm{\mu}_k$ is a prototype
        associated with the $k^{th}$ cluster.
        \item For each data point, we introduce a corresponding set of binary indicator
        variables $r_{nk}\in\{ 0,1\}$, where $k=1,\cdots,K$ describing which of $K$
        clusters the data point $\bm{x}_n$ is designed to, so that if data point
        $\bm{x}_n$ is designed to cluster $k$, then $r_{nk}=1$ and $r_{nj}=0$ for
        $j\neq k$. This is known as the $1$-of-$K$ coding scheme. We can define an
        objective function, sometimes called a \textit{distortion measure}, given by
        \begin{equation}
            J=\sum_{n=1}^N\sum_{k=1}^K r_{nk}\Vert\bm{x}_n-\bm{\mu}_k\Vert^2
        \end{equation}
        Our goal is to find values for the $r_{nk}$ and the $\bm{\mu}_k$ so as to
        minimize $J$.
        \item First we choose some initial values for the $\bm{\mu}_k$. Then in the
        first phase we minimize $J$ with respect to the $r_{nk}$, keeping the
        $\bm{\mu}_k$ fixed. In the second phase we minimize $J$ with respect to the
        $\bm{\mu}_k$, keeping the $r_{nk}$ fixed. This two-stage optimization is then
        repeated until convergence.
        \item Consider first the detemination of the $r_{nk}$. We can simply assign the
        $n^{th}$ data point to the closest cluster centre.
        \begin{equation}
            r_{nk}=\begin{cases}
                1&\textit{if $k=argmin_j\Vert\bm{x}_n-\bm{\mu}_j\Vert^2$}\\
                0&\textit{otherwise}
            \end{cases}
        \end{equation}
        \item Consider the optimization of the $\bm{\mu}_k$ with the $r_{nk}$ held fixed.
        The objective function can be minimized by setting its derivative with respect to
        $\bm{\mu}_k$ to zero giving
        \begin{equation}
            \bm{\mu}_k=\frac{\sum_nr_{nk}\bm{x}_n}{\sum_nr_{nk}}
        \end{equation}
        \item This $K$-means algorithm may converge to a local rather than global
        minimum of $J$.
        \item In practice, a better initialization procedure would be to choose the
        cluster centres $\bm{\mu}_k$ to be equal to a random subset of $K$ data points.
        \item It is worth noting that the $K$-means algorithm itself is often used to
        initialize the parameters in a Gaussian mixture model before applying the EM
        algorithm.
        \item The $K$-means algorithm can be generalized by introducing a more general
        dissimilarity measure $\nu(\bm{x},\bm{x}')$ instead of the Euclidean distance,
        which gives the $K$-medoids algorithm.
    \end{itemize}
    \section{Mixtures of Gaussians}
    \begin{itemize}
        \item The Gaussian mixture distribution can be written as a linear superposition
        of Gaussians in the form
        \begin{equation}
            p(\bm{x})=\sum_{k=1}^K\pi_k\N(\bm{x}|\bm{\mu}_k,\mathbf{\Sigma}_k)
        \end{equation}
        Let us introduce a $K$-dimensional binary random variable $\bm{z}$ to represent
        the state of the Gaussian distribution using $1$-of-$K$ code.
        \begin{figure}[htbp]
            \centering
            \includegraphics[width=0.75in]{figures/MixtureModel.PNG}
            \caption{Graphical representation of a mixture model}
        \end{figure}
        \item The joint distribution is expressed in the form $p(\bm{x},\bm{z})=p(\bm{z})
        p(\bm{x}|\bm{z})$. The marginal distribution over $\bm{z}$ is specified in terms
        of the mixing coefficients $\pi_k$, such that
        \begin{equation}
            p(z_k=1)=\pi_k
        \end{equation}
        and the conditional distribution of $\bm{x}$ given a particular value for $\bm{z}$
        is a Gaussian
        \begin{equation}
            p(\bm{x}|z_k=1)=\N(\bm{x}|\bm{\mu}_k,\mathbf{\Sigma}_k)
        \end{equation}
        So the joint distribution is given by
        \begin{equation}
            p(\bm{x})=\sum_{k=1}^K\pi_k\N(\bm{x}|\bm{\mu}_k,\mathbf{\Sigma}_k)
        \end{equation}
        \item We use $\gamma(z_k)$ to denote $p(z_k=1|\bm{x})$, whose value can be found
        using Bayes' theorem
        \begin{equation}
            \label{eq:responsibility}
            \gamma(z_k)\equiv p(z_k=1|\bm{x})=\frac{\pi_k\N(\bm{x}|\bm{\mu}_k,
            \mathbf{\Sigma}_k)}{\sum_{j=1}^K\pi_j\N(\bm{x}|\bm{\mu}_j,\mathbf{\Sigma}_j)}
        \end{equation}
        the $\gamma(z_k)$ can be viewed as the \textit{responsibility} that component $k$
        takes for explaining the observation $\bm{x}$. $\gamma(z_{nk})\equiv
        p(z_k=1|\bm{x}_n)$.
        \item Suppose we have a data set of observations $\{\bm{x}_1,\cdots,\bm{x}_N\}$.
        The log of the likelihood function is given by
        \begin{equation}
            \label{eq:MLEforGM}
            lnp(\mathbf{X}|\bm{\pi},\bm{\mu},\mathbf{\Sigma})=\sum_{n=1}^Nln
            \Big\{\sum_{k=1}^K\pi_k\N(\bm{x}_n|\bm{\mu}_k,\mathbf{\Sigma}_k)\Big\}
        \end{equation}
        It is worth emphasizing that there is a significant problem associated with the
        maximum likelihood framework applied to Gaussian mixture models, due to the
        presence of singularities.
        \item These singularities provide another example of the severe over-fitting that
        can occur in a maximum likelihood approach. If we adopt a Bayesian approach, this
        difficulty does not occur.
        \item \textit{EM algorithm}. The expectation-maximization algorithm is an elegant
        and powerful method for finding maximum likelihood solutions for models with
        latent variables. The EM algorithm can be generalized to obtain the variational
        inference framework.
        \item Setting the derivatives of $lnp(\mathbf{X}|\bm{\pi},\bm{\mu},
        \mathbf{\Sigma})$ in equation (\ref{eq:MLEforGM}) with respect to the $\bm{\mu}_k$
        and $\mathbf{\Sigma}_k$ to zero, we obtain
        \begin{align}
            \label{eq:mean}
            \bm{\mu}_k&=\frac{1}{N_k}\sum_{n=1}^N\gamma(z_{nk})\bm{x}_n\\
            \label{eq:corvariance}
            \mathbf{\Sigma}_k&=\frac{1}{N_k}\sum_{n=1}^N\gamma(z_{nk})
            (\bm{x}_n-\bm{\mu}_k)(\bm{x}_n-\bm{\mu}_k)^T
        \end{align}
        where we have defined
        \begin{equation}
            N_k=\sum_{n=1}^N\gamma(z_{nk})
        \end{equation}
        \item If we want to maximize $lnp(\mathbf{X}|\bm{\pi},\bm{\mu},\mathbf{\Sigma})$
        with respect to the $\pi_k$, we must take account of the constriant $\sum_{k=1}
        ^K\pi_k=1$. This can be achieved using a Lagrange multiplier and maximize the
        following quantity
        \begin{equation}
            lnp(\mathbf{X}|\bm{\pi},\bm{\mu},\mathbf{\Sigma})+\lambda\Big(
                \sum_{k=1}^K\pi_k-1\Big)
        \end{equation}
        which gives
        \begin{equation}
            0=\sum_{n=1}^N\frac{\N(\bm{x}_n|\bm{\mu}_k,\mathbf{\Sigma}_k)}
            {\sum_j\pi_j\N(\bm{x}_n|\bm{\mu}_j,\mathbf{\Sigma}_j)}+\lambda
        \end{equation}
        If we multiply both sides by $\pi_k$ and sum over $k$ making use of the
        constriant $\sum_{k=1}^K\pi_k=1$, we can find $\lambda=-N$ and we can obtain
        \begin{equation}
            \label{eq:coefficient}
            \pi_k=\frac{N_k}{N}
        \end{equation}
        \item For EM algorithm, in the expectation step, or E step, we use the current
        values for the parameters to evaluate the posterior probabilities, or
        responsibility given by equation (\ref{eq:responsibility}); in the maximization
        step, or M step, we use the probabilities to re-estimate the means, corvariances
        and mixing coefficients using the equation (\ref{eq:mean}),
        (\ref{eq:corvariance}), (\ref{eq:coefficient}).
    \end{itemize}
    \section{An Alternative View of EM}
    \begin{itemize}
        \item The goal of the EM algorithm is to find maximum likelihood solutions for
        models having latent variables. We denote the set of all observed data by
        $\mathbf{X}$, in which the $n^{th}$ row represents $\bm{x}_n^T$, and similarly
        we denote the set of all latent variables by $\mathbf{Z}$, with a corresponding
        row $\bm{z}_n^T$. The set of all model parameters is denoted by $\bm{\theta}$,
        and so the log likelihood function is given by
        \begin{equation}
            ln p(\mathbf{X}|\bm{\theta})=ln\Big\{\sum_{\mathbf{Z}}p(\mathbf{X},
            \mathbf{Z}|\bm{\theta}) \Big\}
        \end{equation}
        \item The presence of the sum prevents the logarithm from acting directly on the
        joint distribution, resulting in complicated expressions for the maximum likelihood
        solution.
        \item \textit{The General EM Algorithm}. \begin{enumerate}
            \item Choose an initial setting for the parameters $\bm{\theta}^{old}$.
            \item \textbf{E Step}. Evaluate $p(\mathbf{Z}|\mathbf{X},\bm{\theta}^{old})$.
            \item \textbf{M Step}. Evaluate $\bm{\theta}^{new}$ given by
            \begin{equation*}
                \bm{\theta}^{new}=argmax_{\bm{\theta}} Q(\bm{\theta},\bm{\theta}^{old})
            \end{equation*}
            where \begin{equation*}
                Q(\bm{\theta},\bm{\theta}^{old})=\sum_{\mathbf{Z}}p(\mathbf{Z}|\mathbf{X},
                \bm{\theta}^{old})ln p(\mathbf{X},\mathbf{Z}|\bm{\theta})
            \end{equation*}
            \item Check for convergence of either the log likelihood or the parameters values.
            If the convergence criterion is not satisfied, then let
            \begin{equation*}
                \bm{\theta}^{old}\leftarrow\bm{\theta}^{new}
            \end{equation*}
            and return to step 2.
        \end{enumerate}
        \item The EM algorithm can also be used to find MAP solutions for models in which a
        prior $p(\bm{\theta})$ is defined over the parameters. In this case the E step remains
        the same as in the maximum likelihood case, whereas in the M step the quantity to be
        maximized is given by $Q(\bm{\theta},\bm{\theta}^{old})+lnp(\bm{\theta})$. Suitable
        choices for the prior will remove the singularities.
        \item Consider the problem of maximizing the likelihood for the complete data set
        $\{\mathbf{X},\mathbf{Z}\}$. This likelihood function takes the form
        \begin{equation}
            p(\mathbf{X},\mathbf{Z}|\bm{\mu},\mathbf{\Sigma},\bm{\pi})=
            \prod_{n=1}^N\prod_{k=1}^K\pi^{z_{nk}}_k\N(\bm{x}_n|\bm{\mu}_k,
            \mathbf{\Sigma}_k)^{z_{nk}}
        \end{equation}
        where $z_{nk}$ denotes the $k^{th}$ component of $\bm{z}_n$. Taking the logarithm, we
        obtain
        \begin{equation}
            lnp(\mathbf{X},\mathbf{Z}|\bm{\mu},\mathbf{\Sigma},\bm{\pi})=
            \sum_{n=1}^N\sum_{k=1}^Kz_{nk}\{ln\pi_k+ln\N(\bm{x}_n|\bm{\mu}_k,\mathbf{\Sigma}_k)\}
        \end{equation}
        Comparision with the log likelihood function (\ref{eq:MLEforGM}) for the incomplete data
        shows that summation over $k$ and the logarithm have been interchanged.
    \end{itemize}
    \section{The EM Algorithm in General}
    \begin{itemize}
        \item Consider a probabilistic model in which we collectively denote all of the observed
        variables by $\mathbf{X}$ and all of the hidden variables by $\mathbf{Z}$. The joint
        distribution $p(\mathbf{X},\mathbf{Z}|\bm{\theta})$
        is governed by a set of parameters denoted $\bm{\theta}$.
        \item We shall suppose that direct optimization of $p(\mathbf{X}|\bm{\theta})$
        is difficult, but that optimization
        of the complete-data likelihood function $p(\mathbf{X},\mathbf{Z}|\bm{\theta})$
        is significantly easier.
        \item For any choice of $q(\mathbf{Z})$, the following decomposition holds
        \begin{equation}
            lnp(\mathbf{X}|\bm{\theta})=\mathcal{L}(q,\bm{\theta})+KL(q||p)
        \end{equation}
        where we have defined
        \begin{align}
            \mathcal{L}(q,\bm{\theta})=\sum_{\mathbf{Z}}q(\mathbf{Z})ln\Big\{\frac
            {p(\mathbf{X},\mathbf{Z}|\bm{\theta})}{q(\mathbf{Z})}\Big\}\\
            KL(q||p)=-\sum_{\mathbf{Z}}q(\mathbf{Z})ln\Big\{\frac
            {p(\mathbf{Z}|\mathbf{X},\bm{\theta})}{q(\mathbf{Z})}\Big\}
        \end{align}
        Because the Kullback-Leibler divergence satisfies $KL(q||p)\ge 0$, we see that the quantity
        $\mathcal{L}(q,\bm{\theta})$ is a lower bound on the log
        likelihood function $lnp(\mathbf{X}|\bm{\theta})$.
        \item The EM algorithm is a two-stage iterative optimization technique for finding
        maximum likelihood solutions.
        \begin{enumerate}
            \item In the \textbf{E} step, the
            lower bound $\mathcal{L}(q,\bm{\theta}^{old})$ is maximized with respect to
            $q(\mathbf{Z})$ while holding $\bm{\theta}^{old}$ fixed when $q(\mathbf{Z})$ is
            equals to $p(\mathbf{Z}|\mathbf{X},\bm{\theta}^{old})$.
            \item In the subsequent \textbf{M} step, the distribution $q(\mathbf{Z})$ is held
            fixed and the lower bound $\mathcal{L}(q,\bm{\theta})$ is maximized with respect to
            $\bm{\theta}$ to give some new value $\bm{\theta}^{new}$.
        \end{enumerate}
    \end{itemize}
    \section{Appendix}
\end{document}


\title{Model}

{{navbar}}

\subsubsection{Model}

A probabilistic model is a joint distribution $p(\mathbf{x},
\mathbf{z})$ of data $\mathbf{x}$ and latent variables $\mathbf{z}$.
For background, see the \href{/tutorials/model}{Probabilistic Models tutorial}.

In Edward, we specify models using a simple language of random variables.
A random variable $\mathbf{x}$ is an object parameterized by
tensors $\theta^*$, where
the number of random variables in one object is determined by
the dimensions of its parameters.

\begin{lstlisting}[language=Python]
from edward.models import Normal, Exponential

# univariate normal
Normal(loc=tf.constant(0.0), scale=tf.constant(1.0))
# vector of 5 univariate normals
Normal(loc=tf.zeros(5), scale=tf.ones(5))
# 2 x 3 matrix of Exponentials
Exponential(rate=tf.ones([2, 3]))
\end{lstlisting}

For multivariate distributions, the multivariate dimension is the
innermost (right-most) dimension of the parameters.

\begin{lstlisting}[language=Python]
from edward.models import Dirichlet, MultivariateNormalTriL

# K-dimensional Dirichlet
Dirichlet(concentration=tf.constant([0.1] * K))
# vector of 5 K-dimensional multivariate normals with lower triangular cov
MultivariateNormalTriL(loc=tf.zeros([5, K]), scale_tril=tf.ones([5, K, K]))
# 2 x 5 matrix of K-dimensional multivariate normals
MultivariateNormalTriL(loc=tf.zeros([2, 5, K]), scale_tril=tf.ones([2, 5, K, K]))
\end{lstlisting}

Random variables are equipped with methods such as
\texttt{log\_prob()}, $\log p(\mathbf{x}\mid\theta^*)$,
\texttt{mean()}, $\mathbb{E}_{p(\mathbf{x}\mid\theta^*)}[\mathbf{x}]$,
and \texttt{sample()}, $\mathbf{x}^*\sim p(\mathbf{x}\mid\theta^*)$.
Further, each random variable is associated to a tensor $\mathbf{x}^*$ in the
computational graph, which represents a single sample $\mathbf{x}^*\sim
p(\mathbf{x}\mid\theta^*)$.

This makes it easy to parameterize random variables with complex
deterministic structure, such as with deep neural networks, a diverse
set of math operations, and compatibility with third party libraries
which also build on TensorFlow.
The design also enables compositions of random variables
to capture complex stochastic structure.
They operate on $\mathbf{x}^*$.

\includegraphics[width=375px]{/images/random_variable_ops.png}

\begin{lstlisting}[language=Python]
from edward.models import Normal

x = Normal(loc=tf.zeros(10), scale=tf.ones(10))
y = tf.constant(5.0)
x + y, x - y, x * y, x / y
tf.tanh(x * y)
x[2]  # 3rd normal rv in the vector
\end{lstlisting}

In the \href{/api/model-compositionality}{compositionality page}, we
describe how to build models by composing random variables.

\begin{center}\rule{3in}{0.4pt}\end{center}

\begin{itemize}
  \item @{ed.models.RandomVariable}
  \item {{models}}
\end{itemize}


\subsection{Triangles}
\subsubsection{Area of a triangle}

\subsubsection{Circumference of a triangle}

\subsubsection{Sum of angles of a triangle}

Angles in a triangle add to \(\pi \).


\chapter*{Appendix A \\ Effective Window Method Polynomials \label{chap:append1}}
\addcontentsline{toc}{chapter}{Appendices}
\addcontentsline{toa}{appendix}{Appendix A}
\addtocontents{toa}{\addvspace{10pt}}%just to separate the entries in the list

For reference, here we list the first few polynomials $H_{l_> l_<}(x)$ from Eq.~(\ref{foffdiag})
\beqa
H_{20}(x)&=&x^2-1, \\ & & \nonumber \\
H_{40}(x)&=&{7\over 4}x^4-{5\over 2}x^2 +{3\over 4}, \\  & & \nonumber \\
H_{42}(x)&=&x^4-x^2, \\& & \nonumber \\
H_{60}(x)&=&    \frac{33}{8} x^6 - \frac{63}{8}x^4 + \frac{35}{8}x^2 - \frac{5}{8}  , \\ & & \nonumber \\
H_{62}(x) &=&   \frac{11}{4}x^6 - \frac{9}{2}x^4 + \frac{7}{4}x^2, \\ & & \nonumber \\
H_{64}(x) &=&  x^6 -  x^4
\label{Hpoly}
\eeqa


% Preamble.
\documentclass[12pt]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage[fleqn]{amsmath}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{amsfonts}
\usepackage{enumitem}
%\usepackage{tikz}  % Include for figures.
%\usepackage{subfiles}  % Include for subfiles.

%% Title macros.
\newcommand{\HOMEWORKNUM}{18}
\newcommand{\NAME}{D. Choi}
\newcommand{\DATE}{2020-06-19}

\title{\vspace{-2\baselineskip}MATH 225 - Homework \#\HOMEWORKNUM}
\author{\NAME}
\date{\DATE}

%% Formatting options.
%\pagenumbering{gobble}  % Include for single-page document.

% Macros.
%% Contextualized by input/output bases.
\newcommand{\based}[3]{{\{#1\}}_{#2}^{#3}}

% Document.
\begin{document}
\maketitle

\section*{1.}
\textit{$T: \mathbb{R}^2 \rightarrow \mathbb{R}^2$ reflects vectors across
line $y = \tan(30 \degree)x$ in standard basis.}
\begin{gather*}
	\mathcal{A}
	=
	\left\{
		\begin{pmatrix} \cos(20 \degree) \\ \sin(20 \degree) \end{pmatrix},
		\begin{pmatrix} -\sin(20 \degree) \\ \cos(20 \degree) \end{pmatrix}
	\right\}
	\\
	\mathcal{B}
	=
	\left\{
		\begin{pmatrix} \cos(40 \degree) \\ \sin(40 \degree) \end{pmatrix},
		\begin{pmatrix} -\sin(40 \degree) \\ \cos(40 \degree) \end{pmatrix}
	\right\}
\end{gather*}
\begin{enumerate}[label=(\alph*)]
	\item \textit{Find $\based{T}{\mathcal{A}}{\mathcal{B}}$.}
	\\[\baselineskip]
	Let $\mathcal{S}$ be the standard basis. \\
	In standard basis, $T$ can be expressed by the matrix
	\begin{equation*}
		\based{T}{\mathcal{S}}{\mathcal{S}}
		=
		\begin{pmatrix}
			\cos(60 \degree) & \sin(60 \degree) \\
			\sin(60 \degree) & -\cos(60 \degree)
		\end{pmatrix}
		.
	\end{equation*}
	Using $\based{T}{\mathcal{S}}{\mathcal{S}}$,
	\begin{equation*}
		\based{T}{\mathcal{A}}{\mathcal{B}}
		=
		\based{I}{\mathcal{S}}{\mathcal{B}}
		\based{T}{\mathcal{S}}{\mathcal{S}}
		\based{I}{\mathcal{A}}{\mathcal{S}}
		.
	\end{equation*}
	Thus,
	\footnotesize
	\begin{equation*}
		\boxed{
			\based{T}{\mathcal{A}}{\mathcal{B}}
			=
			\begin{pmatrix}
				\cos(40 \degree) & -\sin(40 \degree) \\
				\sin(40 \degree) & \cos(40 \degree)
			\end{pmatrix}
			^{-1}
			\begin{pmatrix}
				\cos(60 \degree) & \sin(60 \degree) \\
				\sin(60 \degree) & -\cos(60 \degree)
			\end{pmatrix}
			\begin{pmatrix}
				\cos(20 \degree) & -\sin(20 \degree) \\
				\sin(20 \degree) & \cos(20 \degree)
			\end{pmatrix}
		}
		.
	\end{equation*}
	\normalsize

	\item \textit{Find $\based{T}{\mathcal{B}}{\mathcal{A}}$.}
	\begin{equation*}
		\based{T}{\mathcal{B}}{\mathcal{A}}
		=
		\based{I}{\mathcal{S}}{\mathcal{A}}
		\based{T}{\mathcal{S}}{\mathcal{S}}
		\based{I}{\mathcal{B}}{\mathcal{S}}
		.
	\end{equation*}
	Thus,
	\footnotesize
	\begin{equation*}
		\boxed{
			\based{T}{\mathcal{B}}{\mathcal{A}}
			=
			\begin{pmatrix}
				\cos(20 \degree) & -\sin(20 \degree) \\
				\sin(20 \degree) & \cos(20 \degree)
			\end{pmatrix}
			^{-1}
			\begin{pmatrix}
				\cos(60 \degree) & \sin(60 \degree) \\
				\sin(60 \degree) & -\cos(60 \degree)
			\end{pmatrix}
			\begin{pmatrix}
				\cos(40 \degree) & -\sin(40 \degree) \\
				\sin(40 \degree) & \cos(40 \degree)
			\end{pmatrix}
		}
		.
	\end{equation*}
	\normalsize

	\item \textit{Find $\based{T}{\mathcal{A}}{\mathcal{A}}$.}
	\begin{equation*}
		\based{T}{\mathcal{A}}{\mathcal{A}}
		=
		\based{I}{\mathcal{S}}{\mathcal{A}}
		\based{T}{\mathcal{S}}{\mathcal{S}}
		\based{I}{\mathcal{A}}{\mathcal{S}}
		.
	\end{equation*}
	Thus,
	\footnotesize
	\begin{equation*}
		\boxed{
			\based{T}{\mathcal{A}}{\mathcal{A}}
			=
			\begin{pmatrix}
				\cos(20 \degree) & -\sin(20 \degree) \\
				\sin(20 \degree) & \cos(20 \degree)
			\end{pmatrix}
			^{-1}
			\begin{pmatrix}
				\cos(60 \degree) & \sin(60 \degree) \\
				\sin(60 \degree) & -\cos(60 \degree)
			\end{pmatrix}
			\begin{pmatrix}
				\cos(20 \degree) & -\sin(20 \degree) \\
				\sin(20 \degree) & \cos(20 \degree)
			\end{pmatrix}
		}
		.
	\end{equation*}
	\normalsize

	\item \textit{Find $\based{T}{\mathcal{B}}{\mathcal{B}}$.}
	\begin{equation*}
		\based{T}{\mathcal{B}}{\mathcal{B}}
		=
		\based{I}{\mathcal{S}}{\mathcal{B}}
		\based{T}{\mathcal{S}}{\mathcal{S}}
		\based{I}{\mathcal{B}}{\mathcal{S}}
		.
	\end{equation*}
	Thus,
	\footnotesize
	\begin{equation*}
		\boxed{
			\based{T}{\mathcal{B}}{\mathcal{B}}
			=
			\begin{pmatrix}
				\cos(40 \degree) & -\sin(40 \degree) \\
				\sin(40 \degree) & \cos(40 \degree)
			\end{pmatrix}
			^{-1}
			\begin{pmatrix}
				\cos(60 \degree) & \sin(60 \degree) \\
				\sin(60 \degree) & -\cos(60 \degree)
			\end{pmatrix}
			\begin{pmatrix}
				\cos(40 \degree) & -\sin(40 \degree) \\
				\sin(40 \degree) & \cos(40 \degree)
			\end{pmatrix}
		}
		.
	\end{equation*}
	\normalsize
\end{enumerate}

\end{document}


% Created 2020-04-01 mié 08:59
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=1610]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{pgfplots}
\usepackage{pdfpages}
\usepgfplotslibrary{groupplots}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{System identification of the pneumatic tank model}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={System identification of the pneumatic tank model},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Intro}
\label{sec:org067222b}
\begin{frame}[label={sec:orge13f9f7}]{Lab experiment}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/tank-lab-setup.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:org9936e26}]{Simulink simulation}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/tank-simscape-model.png}
\end{center}
\end{frame}

\section{Models}
\label{sec:org432c17d}
\begin{frame}[label={sec:orgcb96d48}]{System identification}
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[block, right of=input, node distance=20mm] (plant)  {System};
    \node[coordinate, right of=plant, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3, color=blue!80!black] {$u(t)$} (plant);
    \draw[->] (plant) -- node[above, near end, color=orange!80!black] {$p(t)$} (output);
  \end{tikzpicture}
\end{center}

Given input-output data \(\{ \big( \textcolor{blue!80!black}{u(1)}, \textcolor{orange!80!black}{p(1)} \big), \big( \textcolor{blue!80!black}{u(2)}, \textcolor{orange!80!black}{p(2)} \big), \ldots, \big( \textcolor{blue!80!black}{u(N)}, \textcolor{orange!80!black}{p(N)} \big) \}\) find a \alert{good description} of the system that generated the data.
\end{frame}

\begin{frame}[label={sec:org0d412e7}]{Flow through the valve}
\begin{center}
  \begin{tikzpicture}
    \node {\includegraphics[width=0.7\linewidth]{../../figures/valve-volt-opening.png}};
    \node[coordinate] (five) at (0.54,-2.8) {};
    \node[coordinate, below of=five, node distance=1.8cm] (origin) {};
    \draw[color=red!80!black] (five) to (origin);
    \draw[color=blue!80!black, ->, thick] (origin) ++(0,0.4cm) -- node[below, near end] {$u(t)$} ++(1.5cm, 0);
   \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org2499e40}]{Nonlinear model 1}
\begin{block}{Flow into the tank, \(V_{in}(t) > 5\)}
\[ \dot{p}(t) = a_0(\underbrace{V_{in}(t) - 5}_{u(t)})\sqrt{|\underbrace{p_s - p(t)}_{\Delta p(t)}|} \]
\end{block}
\begin{block}{Flow out the tank, \(V_{in}(t) < 5\)}
\[ \dot{p}(t) = a_0u(t)\sqrt{|\underbrace{p(t)-0}_{\Delta p(t)}|} \]
\end{block}
\end{frame}

\begin{frame}[label={sec:orgc8a4915}]{Nonlinear model 2}
\[ \dot{p}(t) = a_0u(t)|\Delta p(t))|^{a_1} \]
\end{frame}

\begin{frame}[label={sec:org5ac1def}]{Converting to a regression model which is linear in the parameters}
\[ \dot{p}(t) = a_0u(t)|\Delta p(t)|^{a_1} \]

Take the logarithm of the equation to get

\[ \log \dot{p} = \log a_0 + \log u + a_1 \log |\Delta p|\]
\end{frame}

\begin{frame}[label={sec:org3d7146d}]{Linear model}
Introduce \(y(t) = p(t)-p_0\), where \(p_0\) is a chosen operating point.

\[ \dot{p}(t) = \dot{y}(t) = -a y(t) + ku(t)\]
\end{frame}

\section{Experiments and results}
\label{sec:orga687e3f}

\begin{frame}[label={sec:org687b35d}]{Input-output data}
\begin{center}
\includegraphics[width=0.88\linewidth]{../../figures/tank-sysid-input-output.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgd55b88c}]{Fitting nonlinear model 1}
\begin{center}
\includegraphics[width=0.9\linewidth]{../../figures/tank-sysid-sqrt-deltaP}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgc3a7756}]{Fitting nonlinear model 2}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/tank-sysid-log}
\end{center}
\end{frame}

\begin{frame}[label={sec:org54e1900}]{Fitting linear model}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/tank-sysid-linear-model}
\end{center}
\end{frame}

\begin{frame}[label={sec:org23ab816}]{Validation}
\begin{center}
\includegraphics[width=0.55\linewidth]{../../figures/sysid-tank-validation-model.png}
\end{center}
\end{frame}

\begin{frame}[label={sec:orgc0ea29b}]{Validation results}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/sysid-tank-valudation-results.png}
\end{center}
\end{frame}
\end{document}


\chapter{Sums of Combinatorial Games}
\marginurl{%
    Sum of Games:\\\noindent
    Introduction to Combinatorial Game Theory \#5
}{youtu.be/dRaqJKZh3y0}

Let us consider the following game.
\begin{game}[Take-Away Game]
\label{game:two-pile-take-away-n-m-2-1}
  In this game there are two players.
  \begin{itemize}
    \item They have two piles of $n$ and $m$ chips, respectively.
    \item They make moves in turns with player I starting,
      each move consists of moving one or two chips out of the pile.
    \item The player that removes the last chip wins.
  \end{itemize}
\end{game}
It is not hard to draw the graph corresponding to the game for small $n$ and
$m$.
\begin{figure}
  \centering
  \begin{tikzpicture}[thick]
    \node[circle, draw, minimum size=6pt] (v1) at (0,0) {$(0, 0)$};
    \node[circle, draw, minimum size=6pt] (v2) at (1.5,1.5) {$(0, 1)$};
    \node[circle, draw, minimum size=6pt] (v3) at (-1.5,1.5) {$(1, 0)$};
    \node[circle, draw, minimum size=6pt] (v4) at (0,3) {$(1, 1)$};
    \node[circle, draw, minimum size=6pt] (v5) at (3,3) {$(0, 2)$};
    \node[circle, draw, minimum size=6pt] (v6) at (-3,3) {$(2, 0)$};

    \draw[->] (v2) -- (v1);
    \draw[->] (v3) -- (v1);
    \draw[->] (v4) -- (v2);
    \draw[->] (v4) -- (v3);
    \draw[->] (v5) to[out=-90, in=0] (v1);
    \draw[->] (v6) to[out=-90, in=180] (v1);
    \draw[->] (v6) -- (v3);
    \draw[->] (v5) -- (v2);
  \end{tikzpicture}
  \caption{Part of the graph of \Cref{game:two-pile-take-away-n-m-2-1}}
\end{figure}
Using this graph it is easy to see that all drawn positions, except $(1, 1)$ and
$(0, 0)$ are N-positions.

In the rest of the chapter we will discuss a method to study similar games.
Assume we have two combinatorial games $\mathcal{G}_1$ and $\mathcal{G}_2$.
One may form another game played as follows: the initial position of the new game
consists of the pair of initial positions of $\mathcal{G}_1$ and $\mathcal{G}_2$,
players alternate moves, and on each turn a player make a move in one of the game
leaving the position in the second untouched.  The new game is called
\emph{sum of $\mathcal{G}_1$ and $\mathcal{G}_2$}.

Let us give a formal definition.
\begin{definition}
    Let $G_1 = (V_1, F_1)$ and $G_2 = (V_1, F_2)$ be directed graphs.
    We say that $G$ is the sum of $G_1$ and $G_2$, denoted $G_1 + G_2$, is
    a graph $(V_1 \times V_2, F)$  such that
    \[
        F(x_1, x_2) =
            \set[y_1 \in F_1(x_1)]{(y_1, x_2)} \cup
            \set[y_2 \in F_2(x_2)]{(x_1, y_2)}.
    \]
\end{definition}
\Cref{figure:sum-of-graphs-G1-G2} gives an example of this operation.

\begin{figure}
    \centering
    \subfloat[$G_1$\label{figure:sum-of-graphs-G1}]{
        \begin{tikzpicture}[thick]
          \node[circle, draw, minimum size=6pt] (v1) at (0,0) {$1$};
          \node[circle, draw, minimum size=6pt] (v2) at (1,1) {$2$};
          \node[circle, draw, minimum size=6pt] (v3) at (-1,1) {$3$};

          \draw[->] (v1) -- (v2);
          \draw[->] (v1) -- (v3);
          \draw[->] (v2) -- (v3);
        \end{tikzpicture}
    }
    \qquad\qquad
    \subfloat[$G_2$\label{figure:sum-of-graphs-G2}] {
        \begin{tikzpicture}[thick]
          \node[circle, draw, minimum size=6pt] (v1) at (0,0) {$1$};
          \node[circle, draw, minimum size=6pt] (v2) at (1,1) {$2$};
          \node[circle, draw, minimum size=6pt] (v3) at (-1,1) {$3$};

          \draw[->] (v2) -- (v1);
          \draw[->] (v3) -- (v1);
          \draw[->] (v3) -- (v2);
        \end{tikzpicture}
    }
    \qquad\qquad
    \subfloat[$G_1 + G_2$\label{figure:sum-of-graphs-G1-G2}] {
        \begin{tikzpicture}[thick]
          \node[circle, draw, minimum size=6pt] (v11) at (0, 0)  {$(1, 1)$};
          \node[circle, draw, minimum size=6pt] (v21) at (1.5, 1.5)  {$(2, 1)$};
          \node[circle, draw, minimum size=6pt] (v31) at (-1.5, 1.5) {$(3, 1)$};
          \node[circle, draw, minimum size=6pt] (v12) at (0, -1.5) {$(1, 2)$};
          \node[circle, draw, minimum size=6pt] (v22) at (3, 3)  {$(2, 2)$};
          \node[circle, draw, minimum size=6pt] (v32) at (-3, 3) {$(3, 2)$};
          \node[circle, draw, minimum size=6pt] (v13) at (0,-3)  {$(1, 3)$};
          \node[circle, draw, minimum size=6pt] (v23) at (4.5,4.5)  {$(2, 3)$};
          \node[circle, draw, minimum size=6pt] (v33) at (-4.5,4.5) {$(3, 3)$};

          \draw[->] (v11) -- (v21);
          \draw[->] (v11) -- (v31);
          \draw[->] (v21) -- (v31);
          \draw[->] (v12) to[out = 0, in = -90] (v22);
          \draw[->] (v12) to[out = 180, in = -90] (v32);
          \draw[->] (v22) -- (v32);
          \draw[->] (v13) to[out = 0, in = -90] (v23);
          \draw[->] (v13) to[out = 180, in = -90] (v33);
          \draw[->] (v23) -- (v33);
          \draw[->] (v12) -- (v11);
          \draw[->] (v13) to[out = 45, in = -45] (v11);
          \draw[->] (v13) -- (v12);
          \draw[->] (v22) -- (v21);
          \draw[->] (v23) to[out = -100, in = 0] (v21);
          \draw[->] (v23) -- (v22);
          \draw[->] (v32) -- (v31);
          \draw[->] (v33) to[out = -80, in = 180] (v31);
          \draw[->] (v33) -- (v32);
        \end{tikzpicture}
    }
    \caption{\Cref{figure:sum-of-graphs-G1-G2} depicts sum of graphs
        from \Cref{figure:sum-of-graphs-G1} and \Cref{figure:sum-of-graphs-G2}.
    }
\end{figure}

Another example is given by the game of Nim; it is easy to see that
$2$-pile Nim is a sum of two $1$-pile Nims. This observation leads to a
generalization of Bouton's Theorem (\Cref{theorem:bouton}).
\begin{theorem}[The Sprague--Grundy Theorem]
    Let $G_1$ and $G_2$ be some graphs and $g_1$ and $g_2$ be corresponding
    Sprague--Grundy functions. Then the graph $G_1$ and $G_2$ has a
    Sprague--Grundy function $g$ such that
    $g(x_1, x_2) = g_1(x_1) \bitwisexor g_2(x_2)$.
\end{theorem}
\begin{proof}
    Let $G_1 = (V_1, F_1)$, $G_2 = (V_2, F_2)$, and $G = G_1 + G_2$.
    Consider some $x_1 \in V_1$ and $x_2 \in V_2$.
    Let $a = g_1(x_1) \bitwisexor g_2(x_2)$. To prove the statement we need to
    show that
    \begin{enumerate}
        \item for any $0 \le b < a$, there is $(y_1, y_2) \in F(x_1, x_2)$ such
            that $g(y_1, y_2) = b$;
        \item for any $(y_1, y_2) \in F(x_1, x_2)$, $g(y_1, y_2) \neq a$.
    \end{enumerate}

    We start from proving the first statement.
    Let us fix some $0 \le b < a$ and let $c = a \bitwisexor b$.
    Let $g_i(x_i) = (p_{i, \ell}, \dots, p_{i, 0})$ for each $i \set{1, 2}$ and
    $c = (1, q_{k - 1}, \dots, q_0)$ where $k \le \ell$.
    For some $j \in \set{1, 2}$, $p_{j, k} = 1$ since
    $a = g_1(x_1) \bitwisexor g_2(x_2)$. Without loss of generality $j = 1$.
    Hence, $c \bitwisexor g_1(x_1) < g_1(x_1)$, whence there is $x'_1$ such
    that $g_1(x'_1) = c \bitwisexor g_1(x_1)$. As a result, there is a move in
    $G$ from $(x_1, x_2)$ to $(x'_1, x_2)$ and
    $g(x'_1, x_2) = g_1(x'_1) \bitwisexor g_2(x_2) =
        c \bitwisexor g_1(x_1) \bitwisexor g_2(x_2) =
        c \bitwisexor a = b$.

    To prove the second statement, assume that there is
    $(y_1, y_2) \in F(x_1, x_2)$ so that $g(y_1, y_2) = a$. Without loss of
    generality we may assume that $x_2 = y_2$. Hence,
    $0 = g(y_1, x_2) \bitwisexor g(x_1, x_2) = g_1(y_1) \bitwisexor g_1(x_1)$.
    However, $g_1(y_1) \neq g_1(x_1)$ since there is a move from $x_1$ to $y_1$.
    Therefore $g_1(y_1) \bitwisexor g_1(x_1) \neq 0$ which is a contradiction.
\end{proof}
It is also easy to see that if $G_1$ and $G_2$ satisfy the ending condition,
then $G_1 + G_2$ also satisfies the ending condition. Therefore, if $G_1$ and
$G_2$ satisfy the ending condition and $g_1$, $g_2$ are Sprague--Grundy
functions of them, $G_1 + G_2$ has unique Sprague--Grundy function $g$ such that
$g(x_1, x_2) = g_1(x_1) \bitwisexor g_2(x_2)$.

The simple example of an application of this theorem is the analysis of the
following game.
\begin{game}
\label{game:subtraction-1-2-3-out-of-10-11}
  Alice and Bob have two piles with $10$ and $11$ chips respectively.
  They take turns and remove $1$ or $2$ chips from one of the piles.
  If one of them cannot make a move he/she loses.
\end{game}

To determine who is the winner in this game, we start with a subtraction game
$G$ with the subtraction set $\set{1, 2}$. It is easy to see that
$g : \N_0 \to \N_0$ such that
\[
  g(x) =
  \begin{cases}
    0 & \text{if } x \equiv 0 \pmod{3} \\
    1 & \text{if } x \equiv 1 \pmod{3} \\
    2 & \text{if } x \equiv 2 \pmod{3}
  \end{cases}
\]
is the Sprague--Grundy function for $G$.
It is also clear that \Cref{game:subtraction-1-2-3-out-of-10-11} is equal to
$G + G$. Therefore the function $g' : \N_0^2 \to \N_0$ such that
\[
  g(x, y) =
  \begin{cases}
    0 & \text{if } x \equiv 0 \pmod{3} \text{ and } y \equiv 0 \pmod{3}\\
    1 & \text{if } x \equiv 0 \pmod{3} \text{ and } y \equiv 1 \pmod{3}\\
    2 & \text{if } x \equiv 0 \pmod{3} \text{ and } y \equiv 2 \pmod{3}\\
    1 & \text{if } x \equiv 1 \pmod{3} \text{ and } y \equiv 0 \pmod{3}\\
    0 & \text{if } x \equiv 1 \pmod{3} \text{ and } y \equiv 1 \pmod{3}\\
    3 & \text{if } x \equiv 1 \pmod{3} \text{ and } y \equiv 2 \pmod{3}\\
    2 & \text{if } x \equiv 2 \pmod{3} \text{ and } y \equiv 0 \pmod{3}\\
    3 & \text{if } x \equiv 2 \pmod{3} \text{ and } y \equiv 1 \pmod{3}\\
    0 & \text{if } x \equiv 2 \pmod{3} \text{ and } y \equiv 2 \pmod{3}
  \end{cases}
\]
Therefore, the position $(10, 11)$ is an N-position.

\marginurl{%
    Applications of the Sprague-Grundy Theorem:\\\noindent
    Introduction to Combinatorial Game Theory \#6
}{youtu.be/V4yI_1P1Jcc}
A surprising example of the application of this theorem is the following game.
\begin{game}
\label{game:polygon}
  In this game position is described by a polygon with several diagonals.
  The game starts with a polygon with $n$ sides. On each turn a player draw a
  new diagonal so that it does not intersect with previously drawn diagonals.
  Players take turns and the one who cannot make a move loses.
\end{game}
It is easy to see that we do not care about the shape of the polygon and
diagonals, the only important information is the number of nodes and which nodes
are connected by a diagonal.

Let $g(n)$ be the value of the Sprague--Grundy function at the polygon with $n$
sides. It is easy to see that if we split the polygon, by a diagonal, into two
parts with $\ell$ and $m$ sides, then the resulting position is essentially a
position in the sum of two games;
hence,
$g(n) =
  \mex \set[
    \ell, m \ge 3 \text{ and } \ell + m = n + 2
  ]{g(\ell) \bitwisexor g(m)}$.
Using this observation it is easy to compute the value of $g(n)$ for small $n$.
\begin{table}[h!]
  \centering
  \begin{tabular}{l l l l l l l l l}
      \toprule
      3 & 4 & 5 & 6 & 7 & 8 \\
      \midrule
      0 & 1 & 0 & 1 & 0 & 1 \\
      \bottomrule
  \end{tabular}
  \caption{Sprague--Grundy function for \Cref{game:polygon}}
\end{table}
It is easy to see to make a conjecture that $g(n) = 0$ for odd $n$ and $g(n) =
1$ for even $n$. Let us prove this using induction. The base case follows from
the computation necessary to write the table. Let us prove the induction step
from $1, \dots, n$ to $n + 1$.
\begin{itemize}
  \item Let $n + 1$ be even. It is clear that if $\ell + m = (n + 1) + 2$, then
    $\ell$ and $m$ have the same remainder modulo $2$. Therefore, $g(m) =
    g(\ell)$ by the induction hypothesis. As a result,
    \[
      \set[
        \ell, m \ge 3 \text{ and } \ell + m = n + 2
      ]{g(\ell) \bitwisexor g(m)} = \set{0}
    \]
    and $g(n + 1) = 1$.
  \item Let $n + 1$ be odd. It is clear that if $\ell + m = (n + 1) + 2$, then
    $\ell$ and $m$ have different remainders modulo $2$. Therefore, $g(m) \neq
    g(\ell)$ by the induction hypothesis. As a result,
    \[
      \set[
        \ell, m \ge 3 \text{ and } \ell + m = n + 2
      ]{g(\ell) \bitwisexor g(m)} = \set{1}
    \]
    and $g(n + 1) = 0$.
\end{itemize}
\begin{chapterendexercises}
    \exercise Compute the Sprague--Grundy function for states of the subtraction
      game with two piles of chips where players and the subtraction set
      $\set{1, 2, 5}$.
    \exercise Let $G_1$ be the subtraction game with the subtraction set
      $\set{1, 2}$
      Let $G_2$ be the game of Nim with three piles.
      Find all the moves from $(11, (1, 6, 7))$ to P-positions in
      $G1 + G2$.
\end{chapterendexercises}


\chapter{Introduction}
\emph{“你们CS比CS的强吗？那你们的EE比EE的强吗？”}
\newpage

\section{Basics}
The basic assumption of machine learning is that \textbf{data samples are i.i.d.}.

The goal of training a model is to \textbf{minimize the generalization error of the model}. Since we only have limited amount of data, what we can actually do is to minimize the emprical error.

However, we do not always want the emprical error to be as small as possible due to the risk of overfitting.

\section{Overfitting and Underfitting}
\paragraph{Overfitting.} High variance. The model performs well on training sets but performs poorly on new unseen samples. Using a high-order model to fit low-order distribution of data usually leads to overfitting.
\paragraph{Underfitting.} High bias. The model has not fully captured the underlying structure of the data. Conduct more training or change a more complicated model.

\section{Methods for Splitting data}
To train a model, we first need to divide data into training set and test set. Training set and test set should be disjoint.

\subsection{Hold-Out}
Divide dataset $\mathcal{D}$ into traning set $\mathcal{S}$ and test set $\mathcal{T}$ s.t.
\[ \mathcal{S} \cup \mathcal{T} = \mathcal{D} \quad \mathcal{S} \cap \mathcal{T} = \emptyset \]
Typical proportion of $\mathcal{S}$ and $\mathcal{T}$ is 30\% and 70\%.

\subsection{Cross-Validation}
Divide $\mathcal{D}$ into $k$ disjoint sets of similar size.
\[ \mathcal{D} = \mathcal{D}_1 \cup \mathcal{D}_2 \cup \dots \mathcal{D}_k \quad \text{s.t.} \quad \mathcal{D}_i \cap \mathcal{D}_j = \emptyset \]
Each time use $k-1$ sets for training and the remaining set for testing.
A typical value of $k$ is $10$.

\subsection{Leave-One-Out}
A special case of cross-validation, wehre each set $\mathcal{D}_i$ contains only one sample.

\subsection{Bootstrapping}
Suppose $\mathcal{D}$ has $m$ samples. Randomly pick a sample from $\mathcal{D}$, copy it into some $\mathcal{D}'$ and put it back to $\mathcal{D}$. Repeat the process for $m$ times.
\[ \lim_{m\to\infty}(1-\frac{1}{m})^m = \frac{1}{e} \approx 0.368 \]
About $36.8\%$ samples in $\mathcal{D}$ will not be in $\mathcal{D}'$. So we can use $\mathcal{D}'$ for training and $\mathcal{D}\backslash\mathcal{D}'$ for testing.

\section{Performance Evaluation}
\subsection{Measure}
\paragraph{Regression} Common performance measure for a regression model is \textbf{Mean Squared Error}.
\[ E = \frac{1}{m}\sum_{i=1}^m(f(x^{(i)}) - y^{(i)})^2 \]
\paragraph{Classification} Common measure for a classification model is \textbf{Error Rate}
\[ E = \frac{1}{m}\sum_{i=1}^m\mathbb{I}[f(x^{(i)}) \neq y^{(i)}] \]

\subsection{TPR and FPR}
\begin{definition}[Sensitivity/TPR]
    \[ TPR = \frac{TP}{TP + FN} \]
\end{definition}
\begin{definition}[FPR]
    \[ FPR = \frac{FP}{TN + FP} \]
\end{definition}

\subsection{Receiver Operating Characteristic}
Many classification models output a real value and compare it to a certain threshold.

The \textbf{ROC Curve} uses $FPR$ as its $x$-axis, and $TPR$ as its $y$-axis. It can be plotted by setting different thresholds for dividing positive and negative samples.

The \textbf{Area Under Curve, AUC} is used to evaluate different models. Usually models with a larger AUC is considered to have better performance.

\subsection{Precision and Recall}
\begin{definition}[Precision]
    \[ P = \frac{TP}{TP + FP} \]
\end{definition}
\begin{definition}[Recall]
    \[ R = \frac{TP}{TP + FN} \]
\end{definition}
Similar to the ROC Curve, we can also plot the \textbf{P-R Curve}. And the \textbf{Break-Even Point, BEP}, defined as the value when $P = R$, is used to evaluate different models.

Another more common measure is the $F1$ rate
\begin{definition}[$F1$ Rate]
    \[ F1 = \frac{2 \times P \times R}{P + R} = \frac{2 \times TP}{\#Samples + TP - TN} \]
\end{definition}
\begin{remark}
    The $F1$ rate is defined by the harmonic mean of Precision and Recall.
\end{remark}

\begin{definition}[$F_{\beta}$ Rate]
    \[ F_{\beta} = \frac{(1+\beta^2)\times P \times R}{(\beta^2 \times P)+R} \]
\end{definition}
\begin{remark}
    $F_{\beta}$ is the weighted harmonic mean. When $\beta > 1$, precision has a higher weight. When $0 < \beta < 1$, recall has a higher weight.
\end{remark}

\section{Error Analysis}
\paragraph{Bias.} The \textbf{bias} is the difference between model prediction and ground truth. This is usually because the model is not well-trained, or because the model is not complex enough to fit the data distribution.
\paragraph{Variance.} The \textbf{variance} is the variance of outputs of the same model fitted different times. This is usually because the model is too complex and mistakenly fits the noise or specific features in the dataset.
\paragraph{Noise.} Noise.

High variance $\to$ Overfitting.

High bias $\to$ Underfitting.

\subsection{Bias-Variance Decomposition}
Let
\[ bias(x) = f(x) - y \]
\[ var(x) = \mathbb{E}_{\mathcal{D}}[(f(x;\mathcal{D}-f(x)))^2] \]
The generalization error of a model $f$ trained on $\mathcal{D}$ can be represented by
\[ E(f;\mathcal{D}) = bias^2(x) + var(x) + \varepsilon^2 \]


\section{Controllers}
\label{sec:controllers}

In an imitation learning setting, there are two controllers involved: an \emph{omniscient} controller, which performs the desired task with perfect knowledge of the environment, and a \emph{learned} controller, which is trained to imitate the behaviour of the omniscient controller.

\subsection{Omniscient controller}

We implemented a controller from the literature that simultaneously controls position and orientation of the robot toward a certain goal pose~\cite{park2011smooth}.

We chose this particular controller because it promised smooth and intuitive
trajectories, which globally converge to arbitrary goal poses without
singularities, from any initial pose. Furthermore, this specific formulation
makes it easy to impose limits on the velocity, acceleration and jerk of the
resulting paths, ensuring that they would be physically realizable if executed
on a real robot.

The control law is described in an egocentric polar coordinate system, relative to the current pose of the robot. The control variables are the linear $v$ and angular $\omega$ velocities. Given a target pose $T$, the state of the robot is expressed as the triple $(r, \theta, \delta)$, where $r$ is the Euclidean distance from the target position; $\theta \in (-\pi, \pi]$ the target orientation with respect to the line of sight from the robot to the target position; $\delta \in (-\pi, \pi]$ the vehicle orientation with respect to the line of sight, as shown in Figure~\ref{fig:egocentric-coordinates}.

It can be seen that $(r, \theta)$ completely identify the position of the robot, while $\delta$ identifies its orientation. In this formulation, moving the robot to the target pose corresponds to bringing the state to the origin, $(r, \theta, \delta) = (0, 0, 0)$.

\begin{figure}[htbp]
	\centerline{\includegraphics[width=\columnwidth]{controller/egocentric-coordinates}}
	\caption{Egocentric polar coordinate system, relative to the current pose of the robot.}
	\label{fig:egocentric-coordinates}
\end{figure}

Assuming initially the linear velocity $v$ is nonzero positive and given (although not constant), the authors show that the angular velocity $\omega$ only influences $\delta$ directly, which in turn influences $(r, \theta)$. As such, the control problem is decomposed in a slow and a fast subsystem.

The slow subsystem first computes a reference orientation $\hat{\delta}$ to steer the robot toward the origin:
\begin{IEEEeqnarray}{l}
	\hat{\delta} = \arctan(-k_1 \theta)
\end{IEEEeqnarray}

The fast subsystem then controls the angular velocity $\omega$ to bring the current orientation $\delta$ toward the reference orientation $\hat{\delta}$ computed by the slow subsystem (somewhat confusingly, the original paper uses $\delta$ for both the current and reference orientations):
\begin{IEEEeqnarray}{l}
	\omega = -\frac{v}{r} [
		k_2 (\delta - \underbrace{\arctan(-k_1 \theta)}_{\hat{\delta}}) +
		(1 + \underbrace{\frac{k_1}{1 + (k_1\theta)^2}}_{-\dot{\hat{\delta}}})\sin\theta
	] \notag \\
	\label{eq:angular-vel}
\end{IEEEeqnarray}

It can be seen from \eqref{eq:angular-vel} that there is a linear relation between $v$ and $\omega$. In particular, $\, \omega = \kappa(r, \theta, \delta) \, v$ where $\kappa$ is the curvature of the resulting path. It is possible to rewrite \eqref{eq:angular-vel}, such that
\begin{IEEEeqnarray}{l}
	\kappa = -\frac{1}{r} [k_2 (\delta - \hat{\delta}) + (1 - \dot{\hat{\delta}})\sin\theta]
	\label{eq:curvature}
\end{IEEEeqnarray}
which implies that the shape of the path does not depend on the choice of $v$. To ensure a smooth and comfortable trajectory, the authors suggest to choose $v$ so that $v \rightarrow 0$ as $\kappa \rightarrow \pm\infty$ and $v \rightarrow v_\text{max}$ as $\kappa \rightarrow 0$:
\begin{IEEEeqnarray}{l}
	v = \frac{v_\text{max}}{1 + \beta |\kappa(r, \theta, \delta)|^\lambda}
	\label{eq:linear-vel}
\end{IEEEeqnarray}

As written, the control law has a singularity as $r \rightarrow 0$, in other words when the robot approaches the target. We address this problem as suggested in the original paper, by setting $v = k_3 r$ in the neighbourhood of $r = 0$:
\begin{IEEEeqnarray}{l}
	v' = \min(v, k_3 r)
	\label{eq:final-linear-vel}
\end{IEEEeqnarray}

In the equations above $k_1 > 0$, $k_2 > 0$, $k_3 > 0$, $\beta > 0$ and $\lambda > 1$ are design parameters. Figure~\ref{fig:omniscient-trajectories} shows some trajectories generated by this controller, obtained with parameters $k_1 = 1$, $k_2 = 3$, $k_3 = 2$, $\beta = 0.4$ and $\lambda = 2$.

\begin{figure}[htbp]
	\centerline{\includegraphics[width=.8\columnwidth]{controller/demo-circle-omniscient-trajectories}}
	\caption{Trajectories of the omniscient controller from 9 initial poses.}
	\label{fig:omniscient-trajectories}
\end{figure}

Finally, we implemented support for reverse gear as suggested by the authors: by simultaneously flipping the orientation of both the robot and the target when $v$ is negative.

We use the reverse gear to teach the learned controller how to behave when it overshoots the target. This normally never happened with the omniscient controller, so the neural network wouldn't know what to do if it didn't stop precisely over the goal. We used an augmentation technique to ensure that this situation would be present in the training set: we made the robot move in reverse if its initial position was inside a region in front of the goal (i.e. between the arms of the object in Figure~\ref{fig:omniscient-trajectories}).

\subsection{Learned controller}

Our learned controller is implemented as an end-to-end neural network, which
receives the sensor inputs and produces commands for the motors.
Section~\ref{sec:models} will describe the specific architectures that we used.


\chapter{Type theory (old computation book chapter draft)}

The type theory described here is inspired by
Martin-L\"of type theory
and the Haskell programming language.
The type theory is constructive (intuitionistic) and dependent.

\section{Notation}

Iff \(a\) is a type then \(|a|\) is the \emph{cardinality} of \(a\).
It is the number of inhabitants of that type.

\section{Common types}

\begin{itemize}
    \item
        $\Void$ is the empty type.
        It has no inhabitant.
    \item
        $\Bool$ is the type of boolean values.
        This type has two inhabitants: $\true$ and $\false$.
    \item
        $\Bit$ has two inhabitants: $0$ and $1$.
    \item
        $\Fun{a}{b}$ is the type of functions from $a$ to $b$.
        The function can be partial.
        This type is also be written $a \to b$.
    \item
        $\Pred{a}$ is the type of
        logical predicates about objects of type $a$.
        We define
        \[ \Pred{a} = \Fun{a}{\Bool}. \]
    \item
        $\Nat$ is the type of natural numbers.
        This type is defined using Peano axioms.
    \item
        $\Set{a}$ is the type of the set of elements of type $a$.
    \item $\List{a} = \Kleene{a}$ is the Kleene closure of $a$.
        A list of $a$ is an ordered collection of elements of type $a$ with duplicates allowed.
        so we can write $[x,y,z] 1 = y$.
    \item $\Bits$ is the type of bitstrings.
        \[
            \Bits = \Kleene{\Bit}
        \]
    \item $\Either{a}{b}$ is the sum type or the union type
        that consists of $\Left{x}$ for all $x : a$
        and $\Right{y}$ for all $y : b$.
    \item $\Pair{a}{b}$ is the product type
        that consists of $(x,y)$ for all $x : a$ and $y : b$.
    \item
        $\Vect{a}{n}$ is the type of vectors
        whose element type is $a$ and whose length is $n : \Nat$:
        \[ \Vect{a}{n} = \Fun{(I n)}{a} \]
        where
        \[ I n = \{ 0, 1, 2, \ldots, n - 1 \} \]
        or alternatively using predicate logic
        \[ \Fa{x} (x : \Nat \wedge x < n \iff x : I n) \]
    \item $\Typ$ is the type of types.
        This implies
        \[ \Typ : \Typ \]
        (Does $\Typ : \Typ$ imply inconsistency?
        Russell's paradox?
        Unrestricted comprehension?)

        This means that $\sfSet$ and $\sfRel$ can be thought as the \emph{type functions}
        \begin{align*}
            \sfSet &: \Typ \to \Typ,
            \\
            \sfRel &: \Typ \to \Typ \to \Typ.
        \end{align*}
\end{itemize}

The cardinality of $\Nat$ is $\aleph_0$ (aleph-null).

\section{Cardinality of types}

$|\List{a}| = |\Fun{\Nat}{a}|$.

\(|a \to b| = |b|^{|a|}\)

If a type has finitely many inhabitants,
then the cardinality of that type is the number of its inhabitants.

If there is a bijection between two types,
then those types have the same cardinality.

Type-theoretic restatement of Cantor's theorem?
There is no bijection between $a$ and $\Set{a}$.
$|a| < |\Set{a}|$.

Kind of ordering on cardinalities:
\begin{itemize}
    \item Iff there is an injection from $a$ to $b$, then $|a| \le |b|$.
    \item Iff there is an surjection from $a$ to $b$, then $|a| \ge |b|$.
    \item Iff there is a bijection between $a$ and $b$, then $|a| = |b|$.
\end{itemize}

Two sets are equinumerous (have the same cardinality) if and only if there is a bijection between them.

$|\sfT a| = |\sfT (\Set{a})|$?

\section{Cardinality theorems}

\begin{mthm}
    \[
        |a| \lneq |\Set{a}|
    \]
\begin{proof}
    Has been proved by Georg Cantor using Cantor's diagonal argument
    that the cardinality of a set is strictly less than its power set.
    Beth numbers.
    $\beth_n \lneq \beth_{n+1}$ for each natural number $n$.
\end{proof}
\end{mthm}

\begin{mthm}[Equinumerosity among one-parameter types]
    For each $a$, all these types have the same cardinality:
    $\Pred{a}$, $\Set{a}$.
    \begin{proof}
        Let $p : \Pred{a}$ be a predicate and $s : \Set{a}$ be a set.
        We define $p$ and $s$ such that each object that satisfies the predicate $p$ is an element of the set $s$
        and also such that each element of the set $s$ satisfies the predicate $p$.
        \begin{align*}
            F p &= \{ x \,|\, p x \}
            \\
            G s &= \lambda x \to x \in s
        \end{align*}
        The relationship is
        \[ \FA{x} (p x \iff x \in s) \]

        But what if $p x = x \not\in S$.
        Or what if $p x = \neg\exists S ~ x \in S$?
        Or what if $p x = \Fa{S} x \in S$?
        Or what if $p x = x \in x$?
        What if $p x = \neg (p x)$?
        Isn't this prone to Russell's paradox?
        Unrestricted comprehension?
        FIXME?

        Or is this not prone?
        $p$ cannot refer to $s$?
        Can it?
    \end{proof}
\end{mthm}

Thus a predicate is a set and a set is a predicate.
It turns out that there is a name for this concept:
that set is the \emph{extension} of that predicate.
If $p$ is a predicate, then $p$ is also a set,
so we can write $x \in p$ to mean that $p x$ is true.
What if we assume that a predicate is equal to its own extension?
Now we make a bold but reasonable claim:
a predicate \emph{is} a set and a set \emph{is} a predicate.
This has some interesting consequences.

If we assume the equality, then $p$ becomes a fixed point of $\phi \mu$.
To see this, we have to define several functions.
Let $\phi$ be the flip combinator, that is $\phi f x y = f y x$.
Let $\mu$ be the set membership function, that is $\mu x y = x \in y$.
Recall that the $\eta$-reduction transforms $p x = q x$ to $p = q$.
\begin{align*}
    p x &= x \in s
    \\
    &= \mu x s
    \\
    &= \phi \mu s x
    \\
    p &= \phi \mu s
    \\
    p &= s
    \\
    p &= \phi \mu p
    \\
    p &= \phi \mu (\phi \mu p)
    \\
    &= \phi \mu (\phi \mu (\phi \mu p))
    \\
    &= \ldots
\end{align*}
That implies that we can write strange but provable things like these:
\begin{align*}
    1 \in \{0,1,2\} &= \{0,1,2\} 1 = \true
    \\
    3 \in \{0,1,2\} &= \{0,1,2\} 3 = \false
    \\
    (\lambda x \to x = 1) 1 &= 1 \in (\lambda x \to x = 1) = \true
\end{align*}
but this can be confusing at first.
Should we distinguish predicate and set?
Should we treat them as the same thing?
The membership operator $\in$ becomes swapped function application.

We can even generalize the notation $f x = x \in f$ to every function $f : a \to b$, not just predicates.
Let $f x = x + 1$. Then $f 0 = 0 \in f = 1$.
This may need some effort and time to get accustomed to,
but once you master it, you will be another mathematician.

\begin{mthm}[Equinumerosity among two-parameter types]
    All these types have the same cardinality:
    \begin{itemize}
        \item $\Relab{a}{b}$, $\Relab{b}{a}$
        \item $\Pred(a,b)$, $\Pred(b,a)$
        \item $\Set{(a,b)}$, $\Set{(b,a)}$
        \item $\Fun{a}{(\Set{b})}$, $\Fun{(\Set{a})}{b}$
    \end{itemize}
    \begin{proof}
        Proving $|\Relab{a}{b}| = |\Relab{b}{a}|$ is simple.

        Proving $|\Pred(a,b)| = |\Pred(b,a)|$ is simple.

        $r : \Relab{a}{b}$ and $p : \Pred(a,b)$ and $f : a \to b \to \Bool$.
        $r$ relates $x$ to $y$ if and only if $p(x,y)$ is true.
        \begin{align*}
            p z &= z \in r
             \\ &= \mu z r
             \\ &= \phi \mu r z
            \\
            p &= \phi \mu r
        \end{align*}
        Then let $p = r$.

        Since there is a bijection between $\Pred{(a,b)}$ and $\Relab{a}{b}$
        and between $\Pred{a}$ and $\Set{a}$,
        there is a bijection between $\Relab{a}{b}$ and $\Set{(a,b)}$.

        To prove that there is a bijection between $\Relab{a}{b}$ and $\Fun{a}{(\Set{b})}$,
        we choose any $r : \Relab{a}{b}$ that is a relation
        from objects of type $a$ to objects of type $b$.
        Define the \emph{image of $x$ in $r$} as
        $i r x = \{ y \,|\, \text{$r$ relates $x$ to $y$} \}$
        where the type of $i$ is $\Relab{a}{b} \to a \to \Set{b}$.
        We define the \emph{relation functionization} function $F$ as
        \[ F r = \{ (x,Y) \,|\, i r x = Y \} \]
        we capitalize $Y$ to highlight the fact that it is a set.
        $G : \Fun{a}{(\Set{b})} \to \Relab{a}{b}$ is the \emph{function relationization} function.
        \[ G f = \{ (x,y) \,|\, y \in f x \} \]
        $G f$ relates $x$ to $y$ iff $y \in f x$.
        We can see that $F(G f) = f$ and $G(F r) = r$.
        Thus $F \circ G$ is the identity of $\Fun{a}{(\Set{b})}$
        and $G \circ F$ is the identity of $\Relab{a}{b}$.
        Thus $F$ and $G$ are inverses of each other.

        ???
    \end{proof}
\end{mthm}

There is a mapping from $\Fun{a}{b}$ to $\Relab{a}{b}$.
There is a bijection between $\Relab{a}{b}$ and $\Relab{b}{a}$.
There is a bijection between $\Relab{b}{a}$ and $\Fun{b}{(\Set{a})}$.
This means that there is a bijection between $\Fun{a}{(\Set{b})}$ and $\Fun{b}{(\Set{a})}$.


\documentclass[main.tex]{subfiles}

\begin{document}
%\section{*Abstract Integration}
%\towrite{Lebesgue-Stieltjes integral}
%\todo{See Rynne or Garling or Analyse de Hilbert et de Fourier course notes}
%\section{*Probability Measure}
%\todo{See Pestman or MIT course notes or...}
%\towrite{Should cover: p. measure, cont. distr., stochastic vectors, E, moment gen?, CLT, multivar norm dist}
%\section{Large Deviation Theory}
%\todo{See Hugo or MIT course notes or ...}
%\towrite{Should cover: application to multivariate normal dist}

\section{Multivariate Gaussian distribution}
\emph{This presentation of the multivariate Gaussian distribution is heavily based on Chapter VIII of \emph{Mathematical Statistics} by \cite{Pestman1998}, which provides proofs to all theorems listed below.}

One distribution that will be particularly useful in our analysis is the \emph{multivariate Gaussian distribution}, which generalises the (one-dimensional) normal distribution. In the simplest case, we have a stochastic vector $\mat{E}=(\mel{E}_1, \dots, \mel{E}_p)$, which is the combination of $p$ Gaussian distributed, \emph{independent} scalar variables. In general, however, we wish to study stochastic vectors produced by applying a \emph{linear transformation} $\mat{L} \in \mathcal{L}(\mathbb{R}^p, \mathbb{R}^q)$ to $\mat{E}$. In this case, the coordinate variables $(\mel{LE})_1,\dots,(\mel{LE})_q$ are not always independent! For example, the map $(\mel{E}_1, \mel{E}_2) \mapsto (\mel{E}_1, \mel{E}_1 + \mel{E}_2)$ transforms two independent normals into two dependent ones.
%In particular, we answer the following question: ``For $i,j \in \range{q}$ and $x \in \mathbb{R}$, what is the distribution of $(\mel{LE})_i$, \emph{given that} $(\mel{LE})_j \geq x$?''
\subsection{Normal and Gaussian}
Although they are often used interchangeably, we make a clear distinction between a \emph{normal} distribution and a \emph{Gaussian} distribution. The former should be familiar:

\begin{definition}
A scalar variable $E$ is said to be \emph{normally distributed} with parameters $\mu$ and $\sigma$ if
\begin{equation}
x \mapsto \frac{1}{\sigma \sqrt{2\pi}} \exp\left[\frac{-(x-\mu)^2}{2\sigma^2}\right]
\end{equation}
is the probability density of $E$.
\end{definition}

\begin{definition}
A scalar variable $E$ is said to be \emph{Gaussian distributed} if it is either normally distributed or constant.
\end{definition}

One could interpret a constant $E$ with value $\mu$ as a normally distributed variable with mean $\mu$ and `standard deviation $0$'. A linear combination of normally distributed \emph{scalar} variables is also normally distributed, and the same is true for Gaussian distributed scalars.

\begin{definition}
A stochastic vector $\mat{E}=(\mel{E}_1, \dots, \mel{E}_p)$ is \emph{elementary normally distributed}\index{distribution!elementary normal} if the scalar variables $\mel{E}_i$ are independent and normally distributed ($i \in \range{p}$).
$\mat{E}$ is \emph{elementary Gaussian distributed}\index{distribution!elementary Gaussian} if the scalar variables $\mel{E}_i$ are independent and Gaussian distributed.
\end{definition}

\begin{definition}
A stochastic vector $\mat{X}=(\mel{X}_1, \dots, \mel{X}_p)$ is \emph{normally distributed}\index{distribution!normal} if there exists an orthogonal operator $\mat{Q}$ such that $\mat{QX}$ is elementary normally distributed.
$\mat{X}$ is \emph{Gaussian distributed}\index{distribution!Gaussian} if there exists an orthogonal operator $\mat{Q}$ such that $\mat{QX}$ is elementary Gaussian distributed.
\end{definition}
%
%\towrite{Plaatjes vullen gaatjes}
%
We state, without proof, the following properties of Gaussian distributed vectors:
\begin{proposition}\label{thm:normaliffinvertible}
The distribution of a Gaussian distributed vector $\mat{X}$ is uniquely determined by its expectation $\mat{\mu} =\EXP\left[\mat{X}\right]$ and covariance matrix $\mat{\Sigma}=\COVMAT(\mat{X})$, and we write $\mat{X} \sim \gaussdistr(\mat{\mu}, \mat{\Sigma})$. $\mat{X}$ is normally distributed if and only if $\mat{\Sigma}$ is invertible.
\end{proposition}

Translating or applying a linear map to a Gaussian distribution results in a new Gaussian distribution. Note that this can be \emph{any} linear map, not necessarily a bijective, orthogonal one.

\begin{theorem}\label{thm:linearmapofgaussian}
Suppose $\mat{X}$ is a $\gaussdistr(\mat{\mu}, \mat{\Sigma})$-distributed $p$-vector.
\begin{align}
\intertext{For any $\mat{a} \in \mathbb{R}^p$:}
\mat{X}+\mat{a} \, &\sim \, \gaussdistr(\mat{\mu}+\mat{a}, \mat{\Sigma}), \\
\intertext{and for any linear map $\mat{L} \in \mathcal{L}(\mathbb{R}^p, \mathbb{R}^q)$:}
\mat{L}\mat{X} \, &\sim \, \gaussdistr(\mat{L}\mat{\mu}, \mat{L}\mat{\Sigma}\mat{L}^*).
\end{align}
\end{theorem}
\begin{remark}
If $\mat{X}$ is normally distributed, and $\mat{L} \in \mathcal{L}(\mathbb{R}^p, \mathbb{R}^q)$, then $\mat{L}\mat{X}$ is Gaussian distributed, but not necessarily normally distributed! A trivial example is the zero map: any normally distributed scalar is mapped to a constant (0), which does not exhibit a probability density function.
\end{remark}
\begin{corollary}\label{cor:innerproductisgaussian}
For any $\mat{b} \in \mathbb{R}^p$, the mapping
\[
\left\langle\mat{b}, \,\cdot\,\right\rangle : \mathbb{R}^p \rightarrow \mathbb{R} : \mat{x} \mapsto \left\langle\mat{b}, \mat{x}\right\rangle
\]
is \emph{linear}, and therefore the scalar variable $\left\langle\mat{b}, \mat{X}\right\rangle$ is Gaussian distributed.
\end{corollary}
In particular, when applying Corollary \ref{cor:innerproductisgaussian} to each element of the standard basis $(\mat{e}_1, \dots, \mat{e}_p)$ of $\mathbb{R}^p$, we find that each of the \emph{coordinates} of $\mat{X}$ is Gaussian distributed:
\begin{proposition}\label{prop:gaussianmarginaldistr}
Suppose $\mat{X}=(\mel{X}_1,\dots,\mel{X}_p)$ is a $\gaussdistr(\mat{\mu}, \mat{\Sigma})$-distributed $p$-vector. Then each for each $i \in \range{p}$, the marginal distribution is given by:
\[
\mel{X}_i \, \sim \, \gaussdistr(\mel{\mu}_i, \mel{\Sigma}_{ii}).
\]
\end{proposition}

For normally distributed vectors, a probability density function exists:
\begin{theorem}
Suppose $\mat{X}$ is a \emph{normally} distributed $p$-vector with expectation $\mat{\mu} =\EXP\left[\mat{X}\right]$ and covariance matrix $\mat{\Sigma}=\COVMAT(\mat{X})$. Then $\mat{X}$ has a \emph{probability density function} given by
\begin{equation}\label{eq:multivarnormaldensity}
\mat{x} \mapsto \frac{1}{\sqrt{\det(\mat{\Sigma})} \left(2\pi\right)^{p/2}} \exp\left[ -\frac{1}{2} (\mat{x} - \mat{\mu})^* \mat{\Sigma}^{-1} (\mat{x} - \mat{\mu}) \right]
\end{equation}
\end{theorem}
\begin{remark}
The condition that $\mat{X}$ is normally distributed is necessary: if $\mat{X}$ is Gaussian, but not normal, it will take values in an \emph{affine subspace of $\mathbb{R}^p$} (such as a plane, as subspace of $\mathbb{R}^3$). If a density function were to exist, it would have support of zero measure, and integrating over $\mathbb{R}^p$ would yield $0$, instead of $1$.
\end{remark}
%\begin{theorem}
%The mode of a Gaussian distribution is its mean.\todo{(waar) moet dit?}
%\end{theorem}
\section{One linear condition}
\begin{definition}
Given a $\mat{r} \in \mathbb{R}^p$ and $b \in \mathbb{R}$, the set $A\subseteq \mathbb{R}^p$ of solutions to the equation
\[
\left\langle \mat{r}, \mat{x} \right\rangle = b
\]
is called a \emph{plane in $\mathbb{R}^p$}\index{plane}. The set $B \subseteq \mathbb{R}^p$ of solutions to
\[
\left\langle \mat{r}, \mat{x} \right\rangle \leq b
\]
is a \emph{half-space in $\mathbb{R}^p$}.
When $b=1$, we write $\mat{r}_A$ (or $\mat{r}_B$), and we say that $\mat{r}_A$ ($\mat{r}_B$) is a \define{pillar} for $A$ (or $B$). Two planes are called \emph{parallel} if they do not intersect.
\end{definition}
\begin{remark}
When $b \neq 1$, we can scale $\mat{r}$ to create a pillar for $A$ or $B$, unless $b=0$ (which is the case if and only if $\mat{0} \in A$).
\end{remark}
\begin{remark}
$A$ is the \emph{boundary} of $B$, \ie $A = \partial B$.
\end{remark}

Geometrically, a pillar can be interpreted as a vector from the origin that crosses the plane orthogonally. Its length is \emph{not} the distance between the origin and the plane, but rather the \emph{inverse} of this distance.
%
%\towrite{Orthogonal projection onto a plane}

\begin{definition}
A \emph{convex polyhedron in $\mathbb{R}^p$}\index{polyhedron} is the intersection of finitely many half-spaces in $\mathbb{R}^p$, and can be written as the set of solutions to
\[
\mat{R}\mat{x} \leq \mat{b},
\]
where each $i$th row of $\mat{R}$, together with $\mel{b}_i$, defines one of the intersecting half-spaces.
\end{definition}
The boundary of a convex polyhedron is contained in the union of planes corresponding to the half-spaces. This relation is strict, in general.

The convex polyhedra in $\mathbb{R}$ are exactly all (possibly infinite) closed intervals. Convex polyhedra in $\mathbb{R}^3$ are familiar shapes, like a cube or a pyramid, but they might also be unbounded, like a pyramid with infinitely deep foundations.
Objects that are \emph{not} convex polyhedra are spheres (no finite intersection of half-spaces) and donuts (not convex, which as the name suggest, is a property of any convex polyhedron\footnote{Using the linearity of the inner product, one easily finds that half-spaces are convex, and so are their intersections.}).

\subsection{Feasibility region}
In this thesis, we model the \emph{power injection} of a grid as a normally distributed stochastic vector, where each coordinate corresponds to the amount of power injected at a node of the network. Positive values denote net generation (injection) and negative values are assigned to net consumption. The stochastic behaviour originates from \emph{renewable sources}: wind and solar, and their correlations arise from correlated weather.

The transmission network was built to transfer power from one node to another, \ie have a non-zero power injection.
In Chapter \ref{chap:grid}, we will learn that not all power injections are \emph{feasible}, as some might cause one of the transmission lines to overload. The \emph{feasibility region} (the set of power injections that can be used) is, to some approximation,\footnote{We make this \emph{DC approximation} more precise in Chapter \ref{chap:model}.} a \emph{convex polyhedron}.

Using historical generation series, we can estimate the covariance of this power injection. Because the amount of current flowing through a line is a linear function of the power injection (by the same approximation), we can use Corollary \ref{cor:innerproductisgaussian} to determine the marginal distribution of line current, which gives the probability of an overload failure. A failure of this kind (\ie caused by a fluctuation of renewable injection) is called an \define{emergent failure}.

Current studies (notably \cite{Nesti2018emergentfailures} and \cite{Chertkov2011}) take this one step further, by determining \emph{the most probable power injection that caused the emergent failure to occur}. We follow the approach of \cite{Nesti2018emergentfailures}, which is generalised in the following Theorem. Here, the plane $A$ can be taken to be one of the \emph{boundary planes of the feasibility region, corresponding to one of the lines}, and the event $\mat{X} \in A$ is the \emph{event that this line overloads}.

\begin{theorem}\label{thm:modeofaplaneconditional}
Let $\mat{X} \, \sim \, \gaussdistr(\mat{\mu}, \mat{\Sigma})$ be a normally distributed $p$-vector with density function $f_{\mat{X}}$, and let $A\subseteq \mathbb{R}^p$ be a plane, given by a pillar $\mat{r}_A \in \mathbb{R}^p$. Then $\mat{X} \mid \mat{X} \in A$ is Gaussian distributed, and has mode
\begin{equation}
\tilde{\mat{x}}_{A} = \argmax_{\mat{x} \in A} f_{\mat{X}}(\mat{x}) = \mat{\mu}  + \frac{1 - \left\langle \mat{\mu}, \mat{r}_A \right\rangle}{\left\langle  \mat{\Sigma} \mat{r}_A, \mat{r}_A \right\rangle} \mat{\Sigma} \mat{r}_A.\label{eq:modeofaplaneconditional}
\end{equation}
\end{theorem}
\begin{proof}%\todo{inproduct rx is verkeerd om}
We will start with the special case of $\mat{\Sigma} = \mat{I}$, and work our way towards the general case.\footnote{This process uses the so-called the \emph{standardised}\index{distribution!standardised normal} form of $\mat{X}$.}

\emph{Step 1. The case of unit covariance.}\\
Suppose $\mat{X}$ is \emph{elementary} normally distributed, with all marginal variances equal to one, \ie $\mat{\Sigma} = \mat{\Sigma}^{-1} = \mat{I}$. The probability density function of $X$ (see Equation (\ref{eq:multivarnormaldensity})) then reduces to:
\[
\mat{x} \mapsto \frac{1}{\left(2\pi\right)^{p/2}} \exp\left[ \frac{-\norm{\mat{x} - \mat{\mu}}^2}{2}  \right].
\]
This is a \emph{decreasing} function of the \emph{distance between $\mat{x}$ and $\mat{\mu}$}, and so its mode is obtained when this distance is minimal.

Because $A$ is an affine subspace of $\mathbb{R}^p$, the distance between $\mat{x} \in A$ and $\mat{\mu}$ is minimal when $\mat{x}$ is the \emph{orthogonal projection} of $\mat{\mu}$ onto $A$, which is given by:
\[
\tilde{\mat{x}}_{A} = \mat{\mu} + \frac{1 - \left\langle \mat{\mu}, \mat{r}_A \right\rangle}{\left\langle \mat{r}_A, \mat{r}_A \right\rangle} \mat{r}_A.
\]

\emph{Step 2. The case of an arbitrary elementary normal distribution.}\\
Let us drop the assumption that all marginal variances are equal to one. There exist $\lambda_1, \dots, \lambda_p \in \mathbb{R}_{>0}$ such that $\mat{\Sigma} = \mat{\Lambda} = \diag(\lambda_1, \dots, \lambda_p)$, and $\mat{\Lambda}^{t} = \diag(\lambda_1^{t}, \dots, \lambda_p^{t})$ for any $t \in \mathbb{R}$.%\todo{Dit zijn de varianties, gebruik simgasquared?}
With $t=-\frac{1}{2}$, we find that $\mat{\Lambda}^{-\frac{1}{2}}\mat{X}$ is elementary normally distributed, with mean $\mat{\Lambda}^{-\frac{1}{2}}$ and unit covariance matrix (Theorem \ref{thm:linearmapofgaussian}).
Applying the same transformation $\mat{\Lambda}^{-\frac{1}{2}}$ to $A$ yields a new plane, which has pillar $\mat{\Lambda}^{\frac{1}{2}}\mat{r}_A$ (not $\mat{\Lambda}^{-\frac{1}{2}}\mat{r}_A$!):

For each $\mat{x} \in \mathbb{R}^p$, we have

$\mat{x} \in A \iff $
\begin{gather*}
\left\langle \mat{x}, \mat{r}_A \right\rangle = 1
\iff \sum_{i=1}^p \mel{x}_i \mel{r}_{A\,i} = 1
\iff \sum_{i=1}^p \lambda_{i}^{-\frac{1}{2}}\mel{x}_i \lambda_{i}^{\frac{1}{2}} \mel{r}_{A\,i} = 1
\iff \\
\left\langle \mat{\Lambda}^{-\frac{1}{2}}\mat{x}, \mat{\Lambda}^{\frac{1}{2}}\mat{r}_A \right\rangle = 1
\iff
\left\langle \mat{\Lambda}^{-\frac{1}{2}}\mat{x}, \mat{r}_{\mat{\Lambda}^{-\frac{1}{2}}(A)} \right\rangle = 1
\end{gather*}

\hfill $\iff \mat{\Lambda}^{-\frac{1}{2}}\mat{x} \in \mat{\Lambda}^{-\frac{1}{2}}(A).$

This means that the plane $\mat{\Lambda}^{-\frac{1}{2}}(A)$ is defined by the pillar $\mat{\Lambda}^{\frac{1}{2}}\mat{r}_A$.

We can now apply our earlier result, and we find:

\begin{gather*}
\tilde{\mat{x}}_{A} = \mat{\Lambda}^{\frac{1}{2}}
\oversortoftilde{\left(
\mat{\Lambda}^{-\frac{1}{2}} \mat{x}\right)_{\mat{\Lambda}^{-\frac{1}{2}}(A)}
}
=
\mat{\Lambda}^{\frac{1}{2}}
\left(
\mat{\Lambda}^{-\frac{1}{2}} \mat{\mu}  + \frac{1 - \left\langle \mat{\Lambda}^{-\frac{1}{2}} \mat{\mu}, \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A \right\rangle}{\left\langle \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A, \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A \right\rangle} \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A
\right) \\
=
\mat{\mu}  + \frac{1 - \left\langle \mat{\Lambda}^{-\frac{1}{2}} \mat{\mu}, \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A \right\rangle}{\left\langle \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A, \mat{\Lambda}^{\frac{1}{2}} \mat{r}_A \right\rangle} \mat{\Lambda} \mat{r}_A
=
\mat{\mu}  + \frac{1 - \left\langle \mat{\mu}, \mat{r}_A \right\rangle}{\left\langle \mat{\Lambda}  \mat{r}_A,\mat{r}_A \right\rangle} \mat{\Lambda} \mat{r}_A.
\end{gather*}

\emph{Step 3. The general case.}\\
Finally, we consider the general case where $\mat{X}$ is normally distributed. If so, there exists orthogonal $\mat{Q}$ such that $\mat{\Sigma}=\mat{Q}\mat{\Lambda}\mat{Q}^{-1}$, with $\mat{\Lambda}$ a diagonal matrix. In other words, the orthogonal map $\mat{Q}^{-1}$ maps $\mat{X}$ into an \emph{elementary} normally distributed vector.\footnote{$\mat{Q}^{-1}\mat{X}$ is elementary normally distributed, but it does not necessarily have a unit covariance matrix.}
Applying the same transformation $\mat{Q}^{-1}$ to $A$ yields a new plane, which is defined by the pillar $\mat{Q}^{-1}\mat{r}_A$. This derivation is more straightforward, since $\mat{Q}^{-1}$ is orthogonal:

For each $\mat{x} \in \mathbb{R}^p$, we have

$\mat{x} \in A \iff $
\begin{gather*}
\left\langle \mat{x}, \mat{r}_A \right\rangle = 1
\iff \left\langle \mat{Q}^{-1} \mat{x}, \mat{Q}^{-1} \mat{r}_A \right\rangle = 1
\iff \left\langle \mat{Q}^{-1} \mat{x}, \mat{r}_{\mat{Q}^{-1}(A)} \right\rangle = 1
\end{gather*}

\hfill $\iff \mat{Q}^{-1}\mat{x} \in\mat{Q}^{-1}(A).$

Using our previous result, we find:

\begin{gather*}
\tilde{\mat{x}}_{A}
=
\mat{Q}
\oversortoftilde{\left(
\mat{Q}^{-1}\mat{x}\right)_{\mat{Q}^{-1}(A)}
}
=
\mat{Q}
\left(
\mat{Q}^{-1}\mat{\mu}  + \frac{1 - \left\langle \mat{Q}^{-1} \mat{\mu}, \mat{Q}^{-1}\mat{r}_A \right\rangle}{\left\langle \mat{\Lambda} \mat{Q}^{-1} \mat{r}_A,\mat{Q}^{-1} \mat{r}_A \right\rangle} \mat{\Lambda} \mat{Q}^{-1} \mat{r}_A
\right) \\
=
\mat{\mu}  + \frac{1 - \left\langle \mat{Q}^{-1} \mat{\mu}, \mat{Q}^{-1}\mat{r}_A \right\rangle}{\left\langle \mat{\Lambda} \mat{Q}^{-1} \mat{r}_A,\mat{Q}^{-1} \mat{r}_A \right\rangle} \mat{Q} \mat{\Lambda} \mat{Q}^{-1} \mat{r}_A
=
\mat{\mu}  + \frac{1 - \left\langle \mat{\mu}, \mat{r}_A \right\rangle}{\left\langle  \mat{Q} \mat{\Lambda} \mat{Q}^{-1} \mat{r}_A, \mat{r}_A \right\rangle} \mat{Q} \mat{\Lambda} \mat{Q}^{-1} \mat{r}_A \\
=
\mat{\mu}  + \frac{1 - \left\langle \mat{\mu}, \mat{r}_A \right\rangle}{\left\langle  \mat{\Sigma} \mat{r}_A, \mat{r}_A \right\rangle} \mat{\Sigma} \mat{r}_A.
\end{gather*}

\end{proof}

When we replace the \emph{plane} $A$ by a \emph{half-space} $B$ in Theorem \ref{thm:modeofaplaneconditional}, there are two cases to consider. If $\mat{\mu} \in B$, then $\tilde{\mat{x}}_{B} = \mat{\mu}$, because the mode of $\mat{X}$ is $\mat{\mu}$, which is contained in $B$.

In the non-trivial case of $\mat{\mu} \notin B$, we find that $\tilde{\mat{x}}_{B} = \tilde{\mat{x}}_{A}$, where $A \defeq \partial B$ is the \emph{boundary} of $B$, which is a plane, given by the same pillar: $\mat{r}_A = \mat{r}_B$. A geometrical argument is as follows: by reducing to the case of an elementary normally distributed $\mat{X}$ with a unit covariance matrix, the mode is the point of $B$ that minimises the distance to $\mat{\mu}$. Since $\mat{\mu} \notin B$, this point must lie on the boundary of $B$, \ie $\tilde{\mat{x}}_{B} \in A$. Because $A$ is a subset of $B$, we can proceed to compute $\tilde{\mat{x}}_{A}$, which is then necessarily the mode of $\mat{X} \, \mid \, B$.

For a half-plane $B$, we now have an explicit expression for $\tilde{\mat{x}}_{B}$, \ie the \emph{most likely value} of $\mat{X}$, given that $\mat{X} \in B$. What can we say about the \emph{expected value} of $\mat{X} \, \mid \, B$? Geometrically, one can imagine that $\EXP [\mat{X} \, \mid \, \mat{X} \in B]$ lies close to $\tilde{\mat{x}}_{B}$, but `shifted inwards', away from $\partial B$ (in the direction of its pillar). We have not found a direct method to compute this expected value. \cite{Nesti2018emergentfailures} introduce a scaling factor $\epsilon>0$ to the covariance of $\mat{X}$ (\ie $\mat{X}_{\epsilon} \, \sim \, \gaussdistr(\mat{\mu}, \epsilon\mat{\Sigma})$), and show that the expected value \emph{converges} to $\tilde{\mat{x}}_{B}$, the mode,\footnote{The factor $\epsilon$ disappears in (\ref{eq:modeofaplaneconditional}).} as $\epsilon \rightarrow 0$, using the tools of Large Deviations Theory.

One can interpret this result as saying that the probability density of $\mat{X}_{\epsilon} | B$ gets \emph{concentrated close to the boundary of $B$}. In the one-dimensional case, this statement\footnote{\ie the \emph{tail distribution} of $X \, \sim \, \gaussdistr(0,\epsilon^2)$, given $X \geq 1$, becomes \emph{steeper} as $\epsilon \rightarrow 0$.} is easy to demonstrate, see \eg \cite{Touchette2009}.
%
%\todo[inline]{Is de marginale verdeling gaussiaans verdeeld? (Ze is niet normaal verdeeld.) Wat is de covariantiematrix? Je zou als Stap 0 kunnen transformeren naar een $\mathbf{r}_A$ die parallel aan een van de assen loopt, en dan dat coordinaat gelijk aan $\norm{\mathbf{r}_A}$ stellen? Maar dan klopt de verwachtingswaarde niet meer?}
%
%\todo[inline]{Het zou mooier zijn om dit resultaat toe te passen op een algemene Gauss-verdeling, die niet per se normaal is. De marginele verdeling bestaat dan alleen niet altijd, namelijk wanneer $A \cap \Ima(\mathbf{X}) = \emptyset$. Als ze wel bestaat, dan gok ik dat het Gaussiaans is.
%
%Het is nu niet toe te passen op line flows (want die zijn normaal, wel Gaussiaans), maar wel op de injectie. Wanneer je dan de meest likely injectie hebt, kan je afleiden wat de flow is. Dit komt overeen met elkaar?
%
%De covariantiematrix van de flow is ook bijna singulair (numeriek gezien singulair), omdat de correlaties zo sterk zijn. Maar ik denk dat je kan beargumenteren dat de kans op singulaire covariantiematrix $0$ is, en dan kan de stelling dus wel worden toegepast. Numeriek is de bijna-singulariteit ook geen probleem (denk ik), omdat in de uiteindelijke uitdrukking de inverse van sigma niet voorkomt.}
\end{document}