\documentclass{amsart}

\usepackage{hyperref}
\author{Drew Johnson}

\newcommand{\M}[2]{\overline{\mathcal M}_{{#1},{#2}}}
\newcommand{\Mgn}{\M gn}
\newcommand{\ch}{\text{ch}}
\newcommand{\di}{\delta_{\text{irr}}}

\title{Computing Top Intersections on $\Mgn$ in Sage}

\begin{document}

\begin{abstract}
We describe code written using the open source mathematics project Sage that computes top intersection on the moduli space of stable $n$-pointed genus $g$ curves.  Our implementation has no theoretical upper bound, is written using open source software, and can compute any intersection involving boundary divisors, $\psi$ classes, $\kappa$ classes, $\lambda$ classes, and the chern character classes.
\end{abstract}

\maketitle
\section{Purpose of Algorithm}
Let $\Mgn$ be the Deligne-Mumford compactification of the moduli space of genus $g$ curves with $n$ marked points.  There are some geometrically defined classes that are part of the so-called tautological ring.  We would like to be able to compute any top intersection of these classes, i.e. any intersection with degree equal to the dimension $3g-3+n$.  We want to consider the classes
\begin{align*}
&\psi_i,\; i = 1, \dots, n \\
&\kappa_i,\; i = 1, \dots, 3g-3+n\\
&\lambda_i,\; i = 1, \dots, g \\
&\ch_i,\; i = 1, 3, 5, \dots, 2g-1
\end{align*}
as well as the boundary divisors.  The $\psi$-classes and boundary divisors have codimension 1, while the remaining classes have codimension equal to their subscript.  We refer to reader to \cite{AC} for the definitions of the $\psi$ and $\kappa$-classes.  The $\lambda$ classes are the chern classes of the Hodge bundle, and the $\ch_i$ are the coefficients of the associated chern character.  It is a property of the Hodge bundle that $\ch_i=0$ for $i$ even, so it suffices to consider only $i$ odd.

The boundary divisors are the image of a so called gluing morphism, either
\[
 \xi_{red}: \M{g_1}{N_1 \cup \{\star\}} \times \M{g_2}{N_2 \cup \{\bullet\}} \rightarrow \M{g}{n}
\]
where $g = g_1 + g_2$ and $N_1 \coprod N_2 = \{1, \dots, n\}$ is a partition, or
\[
	\xi_{irr}: \M{g-1}{n + 2}  \rightarrow \M gn
\]
where the two extra points are identified.  We take the convention as in \cite{faber}, that is that the boundary classes are the fundamental class of this image divided by the degree of this map.  The degree is $2$ for $\xi_{irr}$, and is 1 for $\xi_{red}$ except when $n=0$ and $g_1=g_2$. In this case the degree is $2$.  The class associated with $\xi_{irr}$ we denote by $\di$.

Our goal is to take an arbitrary monomial in these classes and compute the integral, or push-forward to a point, of these classes.

\section{Description of Algorithm}
  In order to compute intersections with boundary divisors, it suffices to pull back the remaining classes by the associated map and compute the intersection on the new moduli space or product of moduli spaces.  Recall that the Chow ring of a product of spaces is the tensor product of the respective Chow rings.  If $\alpha$ is $\kappa_i$, $\lambda_i$ or $\ch_i$, then it pulls back nicely:
\[
	\xi_{red}^*(\alpha) = \alpha \otimes 1 + 1 \otimes \alpha
\]
and
\[
	\xi_{irr}^*(\alpha) = \alpha
\]
(notice that the $\alpha$s on the right hand side of the equation are classes on different spaces than the $\alpha$s are the left hand side).

For $\psi$ classes, we have
\begin{align*}
	\xi_{red}^{*}(\psi_i) &= \begin{cases} \psi_i \otimes 1 & \text{if } i \in N_1 \\
																				1 \otimes \psi_i & \text{if } i \in N_2 \end{cases} \\
	\xi_{irr}^*(\psi_i) &= \psi_i.
\end{align*}

For boundary classes, the formulas are more complicated.  They were written out by Faber in \cite{faber}, including the cases of self intersection.

Now we may assume that there are no boundary divisors.  First, we would like to express the $\lambda$ classes in terms of the chern character.  This is accomplished via the formula
\[
 1 + \lambda_1 t + \lambda_2 t^2 + \dots + \lambda_g t^g = \exp \left(\sum_{i = 1}^g (2i-2)!\ch_{2i-1}t^{2i-1}\right).
\]

Next, we can express the chern characters in terms of $\psi$ and $\kappa$ classes via the formula (see \cite{yang}, originally in \cite{mumford})
\[
 \ch_a = \frac{B_{a+1}}{(a+1)!} \left( \kappa_a - \sum_{i = 1}^n \psi_i^a + \frac12 \sum_{i=0}^{a-1} (-1)^i\sum_{\xi} \xi_* (\psi_{\star}^i  \psi_{\bullet}^{a-1-i}) \right)
\]
where the last sum is taken over all possible gluing morphisms $\xi$.

We will often require the use of the projection formula in this step.  For example, the expansion of $\psi_1 \ch_2$ will contain a term of the form
\[
	\psi_1 (\xi_{red})_* (\psi_{\star} \otimes 1).
\]
which by the projection formula is equal to
\[
	(\xi_{red})_*( \psi_\star \otimes 1 \cdot \xi_{red}^*\psi_1)
\]
so it suffices to compute
\[
\int_{\M{g_1}{N_1 \cup \{\star\}} \times \M{g_2}{N_2 \cup \{\bullet\}}}  \psi_\star \otimes 1 \cdot \xi_{red}^*\psi_1.
\]

Now, we may assume that we only have $\psi$ and $\kappa$ classes left.  In \cite{AC}, it is observed that knowing either the intersections of either the $\psi$ classes or the $\kappa$ classes will give you the intersections of \emph{all} mixed intersections in $\psi$s and $\kappa$s.  In \cite{yang}, a practical formula to reduce to just the $\psi$s is (correcting a typographical error)
\[
\int_{\Mgn} \psi_1^{\alpha_1} \cdots \psi_n^{\alpha_n} \kappa_{b_1} \cdots \kappa_{b_m} = \int_{\M{g}{n+1}}  \psi_1^{\alpha_1} \cdots \psi_n^{\alpha_n}\psi_{n+1}^{b_m} \prod_{j = 2}^m (\kappa_{b_j} - \psi_{n+1}^{b_j})
\]
which will eliminate one $\kappa$ class at a time at the cost of introducing a new marked point.

Thus, it suffices now to consider the case of $\psi$ classes alone.  These can be computed by the Witten-Kontsevich conjecture.  A practical way to compute them is in Lui and Xu \cite{liu-xu}, who give an induction on the genus.  Translating their notation into ours, this formula is
\begin{align*}
 (2g +n-1) (2g+n - 2) &\int_{\Mgn} \psi_1^{d_1} \cdots \psi_n^{d_n} = \\
 \frac{2d_1 + 3}{12} &\int_{\M{g-1}{n+4}} \psi_1^{d_1+1} \psi_2^{d_2} \cdots \psi_n^{d_n} - \frac{2g+n-1}{6} \int_{\M{g-1}{n+3}} \psi_1^{d_1} \cdots \psi_n^{d_n} \\
 	+ &\sum_{I \coprod J = \{2, \dots, n\}}  (2d_1 +3) \sum_{g'}\int_{\M{g'}{|I| + 3}} \psi_1^{d_1 + 1} \prod_{i \in I} \psi_i^{d_i} \cdot \int_{\M{g-g'}{|J| + 2}} \prod_{j \in J} \psi_i^{d_i} \\
 	- &\sum_{I \coprod J = \{2, \dots, n\}}  (2g-n-1) \sum_{g'}\int_{\M{g'}{|I| + 2}} \psi_1^{d_1} \prod_{i \in I} \psi_i^{d_i} \cdot \int_{\M{g-g'}{|J| +2}} \prod_{j \in J} \psi_j^{d_j}.
\end{align*}
Using this formula along with the string equation and the dilaton equation, one can reduce all $\psi$ intersections to the well known base cases
\[
 \int_{\M03} \emptyset = 1
\]
and
\[
	\int_{\M11} \psi_1 = \frac{1}{24}.
\]

Computing intersections involving large powers of $\di$ is particularly computationally intensive since the self intersection formula involves summing over all boundary divisors, so we use a trick from \cite{faber} to speed it up.  This trick allows us to reduce the computation of monomials involving only $\psi$, $\kappa$ and $\di$ classes to the computation of intersections on $\overline{\mathcal M}_g$ or $\M11$, which have much fewer boundary divisors.  We give an example to illustrate this explicitly.  Suppose we wish to compute
\begin{equation}
	\int_{\M23} \di^2 \kappa_2 \psi_1^2. \label{eq:compute-trick}
\end{equation}
Let $\pi$ be the forgetful map
\[
	\pi: \M23 \rightarrow \M22
\]
that forgets the third marked point.

Formula (1.7) of \cite{AC} tells us that in general
\[
	\kappa_a = \pi^* \kappa_a + \psi_{n+1}^a,
\]
where $n+1$ is the point forgotten by $\pi$.  In \cite{faber}, it is noted that $\di$ is a pullback of $\di$ under $\pi$, so  \eqref{eq:compute-trick} is equal to
\[
 \int_{\M23} \pi^* \di^2 (\pi^* \kappa_2 + \psi_3^2) \psi_1^2 = \int_{\M23} \psi_1^2 \pi^*(\di^2 \kappa_2) + \psi_1^2\psi_3^2 \pi^*\di^2.
\]
Now, recall that integration is really pushing down to a point.  Thus, we can apply $\pi_*$ to the integrands to get
\[
 \int_{\M22} \pi_*(\psi_1^2 \pi^*(\di^2 \kappa_2)) + \pi_*(\psi_1^2\psi_3^2 \pi^*\di^2)
\]
which by the projection formula is
\[
	\int_{\M22} \pi_*(\psi_1^2) \di^2 \kappa_2 + \pi_*(\psi_1^2\psi_3)\di^2.
\]
We can push down $\psi$ classes using the equations (1.7) and (1.9) of \cite{AC}), yielding
\[
  \int_{\M22} \psi_1 \di^2 \kappa_2 + \psi_1^2 \kappa_1 \di^2.
\]
  We have reduced the number of marked points by one.  Repeating this will eventually reduce the computation to computing intersections on $\M20$.  Notice also that if no $\kappa$ classes are involved, this can be accomplished in one step via the formula (1.12) of \cite{AC}.

\section{User Interface}
One goal of this project is to make a convenient user interface.  We have created a Sage notebook that demonstrates this.  It can be accessed at \url{http://www.sagenb.org/home/pub/3057/}.
\section{Availability}
The source is available from BitBucket at \url{https://bitbucket.org/drew_j/top-intersections-on-mbar_g-n}, or from the author by email at \href{mailto:werd2.718@gmail.com}{\nolinkurl{werd2.718@gmail.com}}.  Please refer to the Sage notebook mentioned in the previous section for usage instructions.  You can run this program from the online Sage notebook server \url{www.sagenb.org}, or you can install Sage on your local machine.  Sage is open source and freely available at \url{www.sagemath.org}.
\bibliographystyle{halpha}

\bibliography{ref}

\end{document}


\section{Properties and Assumptions of Consistent Update}\label{sec:properties}
Recall that the SIP is defined as finding a measure $\PP_\pspace$ such that the push-forward of it matched $\observedP$.
The following assumption guarantees the existence of a solution to the SIP in the form of an update to the initial distribution.
It implies that any event which is assigned a positive probability by the observations must also have a positive predicted probability.

\begin{assumption}[Predictability Assumption (Theoretical Form)]\label{as:predicted-theoretical}
  The measure associated with $\observed$ is absolutely continuous with respect to the measure associated with $\observed$.
\end{assumption}

If this is unsatisfied, one source of information (the data) suggests certain events are probable while another source of information (the model and initial beliefs) have a priori ruled that almost surely these events should not occur.
Therefore, either initial beliefs, the model under consideration, or the description of uncertainty encoded in $\predicted$ should be subjected to a critical reevaluation.

The following establishes a more practical form (from the perspective of numerical implementation), of \ref{as:predicted-theoretical} which states that the predicted measure must dominate the observed.
\begin{assumption}[Predictability Assumption (Practical Form)]\label{as:predicted-practical}
The requirement given in Assumption~\ref{as:predicted-theoretical} is guaranteed if the following is satisfied:
\begin{equation}\label{eq:pred-pract}
  \exists \; C>0 \text{ such that } \observed (\q) \leq C \predicted(\q) \text{ for a.e. } d\in \dspace,
\end{equation}
where it is understood that $\q = \qlam$ for some $\param \in \pspace$.
\end{assumption}

Assumption~\ref{as:predicted-practical} is particularly useful in that it is the same condition required for applying rejection sampling, which we summarize in Algorithm~\ref{alg:rejection}.
Specifically, this allows us to sample from the updated density using the initial density as follows:

\begin{algorithm}[hbtp]
\DontPrintSemicolon
Draw $\nsamps$ independent identically distributed (i.i.d.) initial samples from the initial density
	\For{$\iparam = 1, \hdots, \nsamps$}{
	    Compute $\Qi = \qoi(\param^{(\iparam)})$.\\
	}
	Approximate $\predicted$, the push-forward of $\initial$, by some method such as kernel density estimation.
  \For{$\iparam = 1, \hdots, \nsamps$}{
	    Compute $r\lami = \frac{\observed\Qi}{\predicted\Qi}$.\\
	}
  Normalize $r$ by dividing it by $\max(r)$.
  \For{$\iparam = 1, \hdots, \nsamps$}{
      Draw a sample from a standard uniform distribution.
	    If the value of $r\lami$ exceeds the value of the random sample, keep $r\lami$.\\
	}
 \caption{Rejection Sampling Leveraging Ratio from Density-Based Approach}
 \label{alg:rejection}
\end{algorithm}

Now, assuming \eqref{eq:pred-pract} holds, we state the following theorem from \cite{BJW18a} based upon the disintegration of measures:

\begin{thm}[Existence and Uniqueness]
  For any set $A\in \pborel$, the probability measure $\updatedP$ defined by
  \begin{equation}\label{eq:dci_sol}
    \updatedP (A) = \int_\dspace \left (  \int_{\pspace \in \qoi^{-1}(\q)}  \initial\lam \frac{\observed\Q}{\predicted\Q} \, d\mu_{\pspace, \q} \lam \right ) \, d\dmeas(\q), \; \forall \; A \in \pborel
  \end{equation}
  is a consistent solution to the SIP given in (\ref{eq:inverse-problem}), and is uniquely defined up to the specification of the initial probability measure $\initial$ on $(\pspace, \pborel)$.
  Here, $\mu_{\pspace, d}$ denotes the disintegration of the dominating measure $\mu_\pspace$.
\end{thm}

The updated density \eqref{eq:update} in the iterated integral in \eqref{eq:dci_sol} has no normalization constant because it is in fact a density (i.e., it integrates to $1$), which is summarized in Corollary 3.1 in \cite{BJW18a} and restated in simplified form below:
\begin{cor}\label{cor:int}
$\updatedP(\pspace) = 1$.
\end{cor}

These definitions are combined to identify the form of the \emph{updated density}, originally derived in \cite{BJW18a}:

\begin{defn}[Updated Distribution]\label{defn:updated}
  A solution satisfying \eqref{eq:dci_sol} is referred to as an updated distribution, with an updated density
  \begin{equation}\label{eq:update}
    \updated \lam = \initial \lam \frac{\observed \Q }{\predicted \Q }, \; \forall \; \param \in \pspace.
  \end{equation}
\end{defn}

% Corollary~\ref{cor:int} is critical to understanding some significant differences between the classical Bayesian posterior density~\cite{Smith} and the updated density density given by \eqref{eq:update}, which we discuss in Section~\ref{sec:othermethods} as well as other places throughout this thesis.
% Moreover, this Corollary provides the basis for a useful numerical diagnostic that assesses both the quality of a numerical approximation of $\updated$ based on finite sampling and density estimation as well as any potential violations of the predictability assumption.

% We next turn our attention to attributes of the Data-Consistent framework which make it appealing for use in solving inverse problems.
% Namely, we summarize the stability results first presented in \cite{BJW18} that demonstrate that approximation errors in the updated density are well understood within the measure-theoretic foundation on which the approach is constructed.

%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Stability of the Consistent Solution}\label{sec:stability}
The Total Variation (TV) metric on a space of probability measures, absolutely continuous with respect to a dominating measure $\mu$, is defined as
\begin{equation}\label{eq:tv}
d_{\text{TV}} (\PP_f, \PP_g) := \int \abs{\pp_f - \pp_g} \, d\mu,
\end{equation}
where $\pp_f,\pp_g$ are the densities (Radon-Nikodym derivatives with respect to $\mu$), associated with measures $\PP_f, \PP_g$, respectively.
The stability results below are all with respect to the TV metric, which is widely used in the literature and is also known as \emph{statistical distance}~\citep{GS02, Smith, Silverman}.
We first define stability with respect to perturbations in the data.

\begin{defn}[Stability of Updated Densities I]\label{defn:stableobs}
  Given $\initialP$ and $\observedP$, let $\widehat{\observedP}$ be any perturbation to $\observedP$ on $(\dspace, \dborel)$ satisfying \eqref{eq:pred-pract}.
  Let $\updatedP$ and $\widehat{\updatedP}$ denote the consistent solutions associated with $\observedP$ and $\widehat{\observedP}$, respectively.
  We say that $\updatedP$ is \emph{stable} with respect to perturbations in $\observedP$ if for all $\eps > 0$, there exists a $\delta > 0$ such that
  \begin{equation}
    d_{\text{TV}} (\observedP, \widehat{\observedP}) < \delta \implies d_{\text{TV}} (\updatedP, \widehat{\updatedP}) < \eps.
  \end{equation}
\end{defn}

In \cite{BJW18a}, it is shown that $d_{\text{TV}} (\widehat{\updatedP}, \updatedP) = d_{\text{TV}} (\widehat{\observedP}, \observedP)$, which immediately proves the following:

\begin{thm}
  $\updatedP$ is stable with respect to perturbations to $\observedP$.
  \label{thm:stableobs}
\end{thm}

This next definition and result are useful in analyzing the sensitivity of the updated density with respect to the initial beliefs.

\begin{defn}[Stability of Updated Densities II]\label{defn:stableinitial}
  Given $\initialP$ and $\observedP$, let $\widehat{\initialP}$ be any perturbation to $\initialP$ on $(\pspace, \pborel)$ satisfying \eqref{eq:pred-pract}.
  Let $\updatedP$ and $\widehat{\updatedP}$ denote the consistent solutions associated with $\observedP$ and $\widehat{\observedP}$, respectively.
  Let $\sett{\PP_{\pspace, d}}{d\in\dspace}{}$ and $\sett{\widehat{\PP_{\pspace, d}}}{d\in\dspace}{}$ be the conditional probabilities defined by the disintegration of $\initialP$ and $\widehat{\initialP}$, respectively.
  We say that $\updatedP$ is \emph{stable} with respect to perturbations in $\initialP$ if for all $\eps > 0$, there exists a $\delta > 0$ such that for almost every $d\in\supp(\observedP)$,
  \begin{equation}\label{eq:stableinitial}
    d_{\text{TV}} (\PP_{\pspace, d}, \widehat{\PP_{\pspace, d}}) < \delta \implies d_{\text{TV}} (\updatedP, \widehat{\updatedP}) < \eps.
  \end{equation}
\end{defn}

The following important stability theorem is also proven in \cite{BJW18a}:

\begin{thm}
  $\updatedP$ is stable with respect to perturbations to $\initialP$
  \label{thm:stableinitial}
\end{thm}

Taken together, these stability results provide assurance that the updated density we obtain is accurate up to the level of experimental error polluting $\observedP$ and error in incorrectly specifying initial assumptions using $\initialP$.
Given that specifying the definition of a ``true'' initial density is somewhat nebulous, we are less interested in the consequences of the latter conclusion.
However, generating samples from $\updatedP$ generally requires a numerical approximation to the predicted distribution, which introduces additional errors in $\updatedP$.
In Section~\ref{sec:approx}, the TV metric is used to bound the error in the updated density in terms of the error in the approximation to the push-forward of the initial.

%%%%%%%%% Section 2.3
\subsection{Numerical Approximation and Sampling}\label{sec:approx}
%Since we are given $\initial$ and $\observed$, the computation of $\predicted$ is the only aspect of the Consistent Bayesian framework that needs to be approximated.
%Since there are few restrictions on the structure of the map $\qoi$ that defines $\predicted$, there is in general no explicit expression from which we can generate samples, so we use a numerical approximation to the probability density function.
%
%For simplicity, we simply propagate Monte Carlo samples from the prior and use a kernel density estimate (usually Gaussian\footnote{In this proposal, all results are generated using this kernel, though six kernels common to density estimation are implemented in the ConsistentBayes Python package [TK - cite Silverman and your github].}).
%
%We summarize this in the following algorithm:
%
%\begin{algorithm}[hbtp]
%\DontPrintSemicolon
%Generate a set of samples $\sett{\param_i}{i=1}{N}$
%	\For{$i = 1, \hdots, N$}{
%			Propagate sample $\param_i$ through the QoI map. Set $d_i = \qoi(\param_i)$.
%	}
%Use $\sett{d_i}{i=1}{N}$ and a density estimation method to approximate $\predicted$.
%	\label{alg:sample}
%\caption{Numerical Approximation of the Push-forward of the Prior Density}
%\end{algorithm}
%

%The computational object associated with $\predicted$ is stored for re-use and can be evaluated at locations in $\dspace$ other than $\sett{d_i}{i=1}{N}$.
%This procedure should be thought of as a characterization of the data space given the prior assumptions encoded in $\initial$.

If $\widehat{\predicted}$ denotes a computational approximation to the push-forward of the initial density obtained with $\widehat{\predicted}$ substituted for $\predicted$ in \eqref{eq:dci_sol}, then the conditional densities from the Disintegration Theorem (c.f. Chapter~\ref{chapter:geometry} for more details), are given as
\[
\frac{\widehat{d\PP_{\pspace, d}}}{d\mu_{\pspace, d}\lam} = \frac{\initial\lam}{ \widehat{\predicted\Q} },
\]
where $\widehat{\PP_{\pspace, d}}$ denotes the disintegration of $\widehat{\updatedP}$.

We assume the following for the approximation of the push-forward of the initial density:
\begin{assumption}\label{as:predicted-theoreticalx}
There exists some $C>0$ such that
\[
\observed (d) \leq C \widehat{\predicted(d)} \text{ for a.e. } d\in \dspace.
\]
\end{assumption}

If this assumption is satisfied, then from \cite{BJW18a}, we have the following:
\begin{thm}\label{thm:predicted_bound}
  The error in the approximate updated density is bounded above:
  \begin{equation}\label{eq:predicted_bound}
    d_{\text{TV}} (\updatedP, \widehat{\updatedP}) \leq C d_{\text{TV}} (\predictedP, \widehat{\predictedP}),
  \end{equation}
  where the $C$ is the constant taken from Assumption \ref{as:predicted-theoreticalx}.
\end{thm}

A straightforward approach to construct $\widehat{\predicted}$ is to use a forward propagation of samples from $\pspace$ to $\dspace$ and then apply kernel density estimation (KDE)~\citep{BJW18a}.
Then, we may evaluate $\updated$ directly for any sample of $\pspace$ at the cost of one model solve per sample.
While this allows us to incorporate sophisticated sampling techniques such as Markov-Chain Monte Carlo (MCMC)~\citep{Smith, Tarantola_book} to generate samples according to the updated distribution, we often opt for a simpler route based on rejection sampling by re-using the initial set of propagated samples.
This avoids any additional model evaluations (as would be required by techniques relying on proposal samples such as MCMC).
We leverage the re-use of samples in the results herein extensively.

By Theorem~\ref{thm:predicted_bound}, the accuracy of the computed updated density relies on the accuracy of the approximation of the push-forward of the initial.
Throughout this thesis, we utilize a KDE with a Gaussian kernel to produce the non-parametric estimates of $\predicted$.
Such KDEs are known to converge at a rate of $\mathcal{O}(N^{-4/(4+\dimD)})$ in mean-squared error and $\mathcal{O}(N^{-2/(4+\dimD)})$ in $L^1$-error, where $\dimD$ is the dimension of $\dspace$, and $N$ is the number of samples from $\initial$ propagated through $\qoi$ \citep{Silverman}.

For simplicity, we introduce the following notation to capture the role of the ratio involved in \eqref{eq:dci_sol} to demonstrate properties we can leverage for generating samples from $\updated$.
We let
\[
\updated\lam = \initial \lam r\Q, \text{ where } r\Q = \frac{\observed\Q}{\predicted\Q}.
\]

Many standard calculations about the updated density involve integrals of functions of $r\Q$ with respect to the prior.
For any measurable function $f$, we establish the connection of calculating quantities over $\pspace$ with those over $\dspace$ by leveraging the following identity:
\[
\int_\pspace f\left( r\Q \right ) \, d\initialP = \int_\dspace f\left( r(\q) \right ) \, d\predictedP
\]

We use several throughout this thesis, including the integral of the updated density:
\[
I(\updated ) = \int_\pspace r\Q \, d\initialP = \int_\dspace r(\q) \, d\predictedP ,
\]
which we can use to validate that $I(\updated) = 1$ in order to numerically validate that the predictability assumption given in \eqref{eq:predicted_bound} was not violated.
The sample average of $r(\q)$ can be used to estimate $I(\updated)$.
This convenience is afforded by the fact that the i.i.d. samples provide us with the ability to take a Monte Carlo estimate of the integral.

% Similarly, we follow \cite{BJW18} to write the commonly used metric for Information Gain, the Kullback-Liebler (KL) divergence:
% \begin{equation}\label{eq:KLdiv}
% \text{KL}(\initial : \updated ) = \int_\pspace r\Q \log r\Q \, d\initialP = \text{KL}(\observed : \predicted ),
% \end{equation}
% i.e., the KL-divergence between the initial and updated density is equal to the KL-divergence between the observed and the predicted densities.

Taken together, the results summarized in this section demonstrate that the Data-Consistent framework and density-based solution \eqref{eq:dci_sol} have been rigorously constructed and studied.
They give an experimenter assurance that there are not unexpected consequences for small mistakes in problem formulation.
In the following section, we provide a similar sense of assurance for a central practical consideration involved in this work: that the results depend on computational implementations of the aforementioned theory, i.e., software.
We directly address how to establish a complementary level of rigor to the implementation of the work as was invested in the construction of the theory.
We leverage modern advances in software engineering with an emphasis on results being reproducible in an accessible manner.

\input{software/intro.tex}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Outline of Remaining Chapters}\label{sec:outline}
In Chapter~\ref{chapter:mud}, we propose a way by which parameter identification can be performed in the DCI framework by posing the problem as a SIP and maximizing $\updated$.
Central to how this contribution is accomplished in practice is the definition of a data-constructed QoI map.
The impact of a QoI's inherent geometric properties on our ability to approximate solutions to SIPs using finite sampling is then summarized in Chapter~\ref{chapter:geometry}.
The focus there is on the property called skewness, which is connected to the QoI maps introduced in \ref{chapter:mud} through a case study of a PDE-based example in Chapter~\ref{chapter:vector-valued}.
Finally, we provide some concluding remarks and directions for future research in Chapter~\ref{chapter:future} alongside several examples demonstrating preliminary results for novel extensions of the work presented in this thesis.


\problemname{Great Expectations}

A \emph{speedrun} is a playthrough of a game with the intention to complete it as
quickly as possible. When speedrunning, you usually follow a pre-planned
path through the game. Along this path, there may be some places
where you have to pull off a difficult technique, or \emph{trick}, which may cause a
delay if you fail to pull it off successfully. Luckily you can \textit{reset}
the game at any time: if you have made a few mistakes, you can start a new run,
losing your progress but instantaneously starting over with a clean slate. You
can do this as often as you like.

The game you are currently speedrunning has a record of $r$ seconds, which you
intend to beat. You have discovered a path through the game that, in the best
case, takes $n < r$ seconds. There are some tricks along the way, though: you
know exactly where along the run they occur, what the probability is that you
will pull them off successfully, and how many seconds you have to spend to
recover if they fail.

Given this data, you want to find the optimal strategy for when to reset the
game to minimise the expected time to set a new record. Write a program
to determine what this smallest possible expected time is.

\begin{Input}
The input consists of:

\begin{itemize}
\item One line with three integers $n$, $r$ and $m$ ($2 \leq n < r \leq 5\,000$,
  $1 \le m \le 50$), where $n$ and $r$ are as described above and $m$ is the number of tricks.
\item $m$ lines, each containing three numbers describing a trick:
    \begin{itemize}
        \item An integer $t$ ($1 \le t < n$), the time in the route (assuming no failed tricks before) at which the trick occurs,
        \item a real number $p$ ($0 < p < 1$ and $p$ has at most $6$ digits after the decimal point), the probability that the trick succeeds, and
        \item an integer $d$ ($1 \le d \le 1\,000$), the number of seconds required to recover in case the trick fails.
    \end{itemize}
\end{itemize}

The tricks are given in sorted order by $t$, and no two tricks occur
at the same time $t$ in the route.

You may assume that, without resetting, a single playthrough has a probability
of at least $1$ in $50\,000$ to succeed at improving the record.
\end{Input}

\begin{Output}
Output the expected time you will have to play the game to set a new record,
assuming an optimal strategy is used. Your answer should have an absolute
or relative error of at most $10^{-6}$.
\end{Output}

\subsection*{Explanation of Sample Input 1}

The record for this game is $111$ seconds, and your route takes
$100$ seconds if everything goes right.

After playing for $20$ seconds, there is a trick with a $50\%$ success rate. If it
succeeds, you keep playing. If it fails, you incur a $10$ second time loss: now
the run will take at least $110$ seconds. It is still possible to set a record,
but every other trick in the run has to be successful. It turns out to be faster
on average to reset after failing the first trick.

Thus you repeat the first $20$ seconds of the game until the trick is successful:
with probability $1/2$, it takes $1$ attempt; with probability $1/4$, it takes $2$
attempts; and so on. On average, you spend $40$ seconds on the first $20$ seconds
of the route.

Once you have successfully performed the first trick, you want to finish the run
no matter the result of the other tricks: it takes $80$ seconds, plus on average
$1$ second loss from each of the remaining $4$ tricks. So the expected time until you
set a record is $124$ seconds.


\section{Polyhedrons}
In this section, we give a brief introduction to polyhedrons, and show how to compute their surface area and their volume.

\subsection{Definition}
A polyhedron is a region of space delimited by polygonal faces. Generally, we will describe a polyhedron by listing its faces. Some basic conditions apply:
\begin{itemize}
\item all the faces are polygons that don't self-intersect;
\item two faces either share a complete edge, or share a single vertex, or have no common point;
\item all edges are shared by exactly two faces;
\item if we define adjacent faces as faces that share an edge, all faces are connected together.
\end{itemize}

Here is a polyhedron that respects those conditions:
\begin{center}
    \includeFig{polyhedron-0} \\[.5cm]
    \begin{tabu} to .6\linewidth {X[c]|X[c]}
        %\toprule
        \emph{Face type} & \emph{Face visibility} \\
        \midrule
        8 triangles & 7 visible \\
        4 quadrilaterals & 5 hidden \\
    \end{tabu}
\end{center}

Note that those conditions don't exclude nonconvex polyhedrons (like the example above), but they do exclude self-crossing polyhedrons.

\subsection{Surface area}
To compute the surface area of a polyhedron, we need to compute the area of their faces. Like we do for polygons, we denote a face by listing its vertices in order, like $P_1P_2P_3P_4$. The vertices must all be coplanar and the edges should not intersect except at their ends.

\centerFig{polyhedron-1}

How do we find the area of a face $P_1\cdots P_n$? First let's take the most simple case: a triangle $ABC$.
If we compute cross product $\crossv{AB}{AC}$, its direction is perpendicular to the triangle and its norm is
\[|AB|\,|AC| \sin\theta\]
where $\theta$ is the amplitude of angle between $\vv{AB}$ and $\vv{AC}$.
This is twice the area of triangle $ABC$, as was already noted in section~\ref{ss:polygon-area}.
So the area of triangle $ABC$ is $\frac{1}{2}\big\|\crossv{AB}{AC}\big\|$.

\centerFig{polyhedron-2}

If we rewrite the vectors we can obtain a more symmetric expression:
\begin{align*}
\crossv{AB}{AC}
&= (B-A)\times(C-A) \\
&= B \times C - B \times A - A \times C + A \times A \\
&= A \times B + B \times C + C \times A
\end{align*}

Let's extend this to a quadrilateral $ABCD$. There are two cases:
\begin{itemize}
\item Triangles $ABC$ and $ACD$ are oriented in the same way. In this case, vectors $\crossv{AB}{AC}$ and $\crossv{AC}{AD}$ point in the same direction, and the areas should be added together. So we take the vector sum $\crossv{AB}{AC} + \crossv{AC}{AD}$.
\item Triangles $ABC$ and $ACD$ are oriented in different ways (the angle at either $B$ or $D$ is concave). In this case, vectors $\crossv{AB}{AC}$ and $\crossv{AC}{AD}$ point in opposite directions, and we should take the difference of the areas. So again, taking the vector sum $\crossv{AB}{AC} + \crossv{AC}{AD}$ will produce the desired effect.
\end{itemize}

\centerFig{polyhedron-3}

We can reformulate the sum in the same way:
\begin{align*}
\crossv{AB}{AC} + \crossv{AC}{AD}
&= (A \times B + B \times C + C \times A) \\
&\qquad + (A \times C + C \times D + D \times A) \\
&= A \times B + B \times C + C \times D + D \times A
\end{align*}

If we continue with more and more vertices, we can make the following general conclusion. Given a polygon $P_1\cdots P_n$, we can compute the \emph{vector area}
\[\vv{S} = \frac{P_1 \times P_2 + P_2 \times P_3 + \cdots + P_n \times P_1}{2}\]
This vector is perpendicular to the polygon, and $\big\|\vv{S}\big\|$ gives its area. It is oriented such that when it points towards the observer, the vertices are numbered in counter-clockwise order.

\centerFig{polyhedron-4}

Note that this is very similar to the 2D formula for area we obtained in section~\ref{ss:polygon-area}. The only thing that changes is that the result is a vector. We can implement this straightforwardly:
\begin{lstlisting}
p3 vectorArea2(vector<p3> p) { // vector area * 2 (to avoid divisions)
    p3 S = zero;
    for (int i = 0, n = p.size(); i < n; i++)
        S = S + p[i]*p[(i+1)%n];
    return S;
}
double area(vector<p3> p) {
    return abs(vectorArea2(p)) / 2.0;
}
\end{lstlisting}

\subsection{Face orientation}
When given an arbitrary polyhedron, an important task is to orient the faces properly, so that we know which side is inside the polyhedron and which side is outside. In particular, we will try to order the vertices of the faces so that their vector areas $\vv{S}$ all points towards the outside of the polyhedron. Note that, depending on the way the polyhedron was obtained, this might already be the case.

\begin{center}
    \includeFig{polyhedron-5}

    $\vv{S}$ pointing outside (lengths not to scale)
\end{center}

While it's not clear when looking at individual faces which side is inside the polyhedron, it's easy to deduce the correct orientation by looking at the orientation of an adjacent face. If two faces share an edge $[PQ]$, and the first face lists $P$ then $Q$ in this order, then the other face should list them in the other order, so that they ``rotate'' in the same direction. Note that because of circularity, in $P_1\cdots P_n$, $P_n$ is considered to come before $P_1$, not after.

\centerFig{polyhedron-6}

So to orient the faces in a consistent way, start from an arbitrary face, and perform a graph traversal on faces. Whenever you go from a face to a neighboring face, reverse the new face's vertex order if the common edge is present in the same order on both faces. This will either orient all vector areas towards the outside, or all towards the inside.

Here is a example implementation:
\begin{lstlisting}
// Create arbitrary comparator for map<>
bool operator<(p3 p, p3 q) {
    return tie(p.x, p.y, p.z) < tie(q.x, q.y, q.z);
}
struct edge {
    int v;
    bool same; // = is the common edge in the same order?
};

// Given a series of faces (lists of points), reverse some of them
// so that their orientations are consistent
void reorient(vector<vector<p3>> &fs) {
    int n = fs.size();

    // Find the common edges and create the resulting graph
    vector<vector<edge>> g(n);
    map<pair<p3,p3>,int> es;
    for (int u = 0; u < n; u++) {
        for (int i = 0, m = fs[u].size(); i < m; i++) {
            p3 a = fs[u][i], b = fs[u][(i+1)%m];
            // Let's look at edge [AB]
            if (es.count({a,b})) { // seen in same order
                int v = es[{a,b}];
                g[u].push_back({v,true});
                g[v].push_back({u,true});
            } else if (es.count({b,a})) { // seen in different order
                int v = es[{b,a}];
                g[u].push_back({v,false});
                g[v].push_back({u,false});
            } else { // not seen yet
                es[{a,b}] = u;
            }
        }
    }

    // Perform BFS to find which faces should be flipped
    vector<bool> vis(n,false), flip(n);
    flip[0] = false;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (edge e : g[u]) {
            if (!vis[e.v]) {
                vis[e.v] = true;
                // If the edge was in the same order,
                // exactly one of the two should be flipped
                flip[e.v] = (flip[u] ^ e.same);
                q.push(e.v);
            }
        }
    }

    // Actually perform the flips
    for (int u = 0; u < n; u++)
        if (flip[u])
            reverse(fs[u].begin(), fs[u].end());
}
\end{lstlisting}

\subsection{Volume}
Suppose that the faces are oriented correctly. We will show how to compute the volume of the polyhedron by taking the same approach as in section~\ref{ss:polygon-area} for the 2D polygon area.

Let's choose an arbitrary reference point $O$.
We will compute the volume of the polyhedron face by face: for a face $P_1\cdots P_n$, if the side of the face seen from $O$ is inside the polygon, add the volume of pyramid $OP_1\cdots P_n$, and otherwise subtract it. That way, by inclusion and exclusion, the final result will be the volume inside the polyhedron.

Since $\vv{S}$ always points towards the outside of the polygon, we just have to check whether $\vv{S}$ points away from $O$ (add) or towards $O$ (subtract).

\centerFig{polyhedron-7}

How do we compute the volume of pyramid $OP_1\cdots P_n$? We can use the formula
\[\mathrm{volume} = \frac{\mathrm{area\ of\ base}\times\mathrm{height}}{3}\]

It turns out that dot product $\dotv{S}{OP_1}$ computes exactly $(\mathrm{area\ of\ base}\times\mathrm{height})$ in absolute value. Indeed, by definition
\[\dotv{S}{OP_1} = \big\|\vv{S}\big\|\,|OP_1|\cos\theta\]
where $\theta$ is the angle between $\vv{S}$ and $\vv{OP_1}$. The norm $\big\|\vv{S}\big\|$ is the area of $P_1\cdots P_n$, the base of the pyramid, and we can easily see that $|OP_1|\cos\theta$ is the height of the pyramid (up to sign).

\centerFig{polyhedron-8}

So the absolute value of $\dotv{S}{OP_1}$ is equal to the volume of pyramid $OP_1\cdots P_n$, and the dot product is positive if $\vv{S}$ points away from $O$, and negative if $\vv{S}$ points towards $O$. This is exactly the sign we want.

For the implementation, we take $O$ to be the origin for convenience. We divide by 6 at the end because we need to divide by 2 to get the correct area and then by 3 because of the formula for the volume of a pyramid.
\begin{lstlisting}
double volume(vector<vector<p3>> fs) {
    double vol6 = 0.0;
    for (vector<p3> f : fs)
        vol6 += (vectorArea2(f)|f[0]);
    return abs(vol6) / 6.0;
}
\end{lstlisting}

In case the vector areas $\vv{S}$ all point towards the inside of the polyhedron (which may happen after applying the face orientation procedure in the previous section), \lstinline|vol6| will be correct but will have a negative sign. If this happens, flip all the faces so that all $\vv{S}$ now point outside.


\documentclass[../base.tex]{subfiles}

\begin{document}
\section{Estimating Contagion}
\label{est}

\cite{pesaran2007econometric} (PP) consider a two country framework to capture the insights of \cite{masson1999contagion}. The model can be represented by the relations:

\begin{align}
	s_{1,t} =&~\boldsymbol{\delta}_1^{\prime} \mathbf{x}_{1,t} + \boldsymbol{\phi}_1^{\prime} \mathbf{F}_t + \beta_1 D_{2,t} + u_{1,t} \label{eqn:system}\\
	s_{2,t} =&~\boldsymbol{\delta}_2^{\prime} \mathbf{x}_{2,t} + \boldsymbol{\phi}_2^{\prime} \mathbf{F_t} + \beta_2 D_{1,t} + u_{2,t} \label{eqn:system2}
\end{align}

where $s_{i,t}$ is the spread of country $i$ bond yields over the German equivalent, $\mathbf{x}_{i,t}$ is a vector of country-specific regressors (including any lags of the dependent variable), $\mathbf{F}_t$ is a vector of factors common to all countries, and $D_{j, t}$ is a dummy variable indicating a crisis in country $j \neq i$ at time $t$. $u_{i, t}$ is a serially uncorrelated error term with idiosyncratic conditional variances $\sigma^2_{ui, t-1}, i = 1, 2$ and a non-zero correlation coefficient $\rho$ that is assumed to be time invariant for ease of exposition.

The external crisis variable $D_{j, t}$ takes a value of either zero or unity, and the formulation of this variable is discussed in detail in Section \ref{dating_methodology}. For now, it is sufficient to assume an indicator function is triggered if country $j$ sees spreads rise above a certain threshold $c_j$. Essentially, $D_{j,t}$ is an endogenous variable of the form $D_{j, t} = \mathbf{I}(s_{j, t} - c_j)$ where $\mathbf{I}(A) = 1$ if $A >0$ and zero otherwise.

After some simplifications, PP demonstrate that the analytical solution of the model depends crucially upon the strength of fundamentals - defined as $m_{i,t} = \boldsymbol{\delta}_i^{\prime} \mathbf{x}_{i,t} + \boldsymbol{\phi}_i^{\prime} \mathbf{F}_t + u_{i,t}$ - and the characteristics of the contagion coefficients.

If one of $\beta_1$ or $\beta_2$ is zero, then the model possesses a simple solution. Abstracting from the possibility of time-varying thresholds, the system becomes

\begin{align*}
	s_{1,t} =&~ m_{1,t} + \beta_1 I(s_{2,t} - c_2) \\
	s_{2,t} =&~ m_{2,t}
\end{align*}

when $\beta_2 = 0$. $s_{2,t}$ is determined solely by its own fundamentals, while $s_{1,t}$ is a piecewise function of the realised $s_{2,t}$:
\[
s_{1,t} =
\begin{cases}
m_{1,t} + \beta_1  & \text{if } s_{2,t} > 0 \\
m_{1,t}		       & \text{if } s_{2,t} \leq 0
\end{cases}
\]

With two positive contagion coefficients, the solution is not unique for certain values of the fundamental variables. Normalising by the threshold and contagion coefficients, such that normalised variable $A_{i,t} =~\frac{a_{i,t} - c_i}{\beta_i}$:

\begin{align*}
	S_{1,t} =&~ M_{1,t} + I(S_{2,t})\\
	S_{2,t} =&~ M_{2,t} + I(S_{1,t})
\end{align*}

which gives a reduced formulation for country 1:

\begin{align}
	S_{1,t} =&~ M_{1,t} + I(M_{2,t} + I(S_{1,t}))
	\label{eqn:model_soln}
\end{align}

Given the dichotomous nature of the nested indicator function, there are four possible regimes for the system at time $t$:

\begin{align}
	\label{eqn:regions}
	\text{regime 1:}~ s_{1,t} & \leq	c_1, s_{2,t} \leq c_2; ~~~~ \text{regime 2:}~ s_{1,t} \leq	c_1, s_{2,t} > c_2 \\
	\text{regime 3:}~ s_{1,t} & >	c_1, s_{2,t} \leq c_2; ~~~~ \text{regime 4:}~ s_{1,t} >	c_1, s_{2,t} > c_2
\end{align}

These regimes can help to reach a solution for equation (\ref{eqn:model_soln}). Normalisation means that:

\begin{align*}
	\text{regime 1:}~ S_{1,t} & \leq	0, S_{2,t} \leq 0; ~~~~ \text{regime 2:}~ S_{1,t} \leq	0, S_{2,t} > 0 \\
	\text{regime 3:}~ S_{1,t} & >	0, S_{2,t} \leq 0; ~~~~ \text{regime 4:}~ S_{1,t} >	0, S_{2,t} > 0
\end{align*}

When the realisations of $S_{i,t}, i = 1,2$ are known then we can find the characteristics of the fundamentals. For example, in regime 2, $S_{1,t} = M_{1,t} + 1, M_{1,t} \leq 1; S_{2,t} = M_{2,t} > 0$. The full set of regimes is, in $(M_{1,t}, M_{2,t})$ space:

\begin{align*}
	\text{regime 1:}~ M_{1,t} & \leq	0, M_{2,t} \leq 0; ~~~~ \text{regime 2:}~ M_{1,t} \leq	-1, M_{2,t} > 0 \\
	\text{regime 3:}~ M_{1,t} & >	0, M_{2,t} \leq -1; ~~~~ \text{regime 4:}~ M_{1,t} >	-1, M_{2,t} > -1
\end{align*}

A fifth region, where $-1 < M_{1,t} \leq 0; -1 < M_{2,t} \leq 0$, where fundamentals in both countries are weak, but not so much so that there is no chance of the good outcome, does not directly correspond to a region in $(S_{1,t}, S_{2,t})$ space. In this region of fundamentals, $S_{i,t}$ is randomised between `good' and `bad' states according to some process. PP provide the example of $\mathbf{M}_t = (-\frac{1}{2}, -\frac{1}{3})^\prime$, for which the solutions $\mathbf{S}_t^a = (-\frac{1}{2}, -\frac{1}{3})^\prime$ and $\mathbf{S}_t^b = (\frac{1}{2}, \frac{2}{3})^\prime$ can both hold.

Designating the equilibrium choice by $d_t \sim$ Bernoulli($\pi^E$), where $\pi^E$ is the probability of the `bad' outcome of higher spreads, the solution in this region is characterised as $S_{i,t}^*(d_t) =~d_tM_{i,t} + (1 - d_t)(1 + M_{i,t})$. This generates a bimodal distribution of $s_{i,t}^* = s_{i,t}(d_t)$ that Pesaran and Pick demonstrate becomes more pronounced with larger contagion coefficients. Another feature of the model made clear by the above solution is that the $s_{1,t}$ and $s_{2,t}$ will be correlated through the crisis dummy even when fundamentals are uncorrelated, provided $\beta_i \geq 0, \beta_{j \neq i} > 0;~~ i,j = 1, 2$.

The solution derived in the two-country setting is extended to the N-country formulation:

\begin{align}
	s_{i,t} =&~\boldsymbol{\delta}_1^{\prime} \mathbf{x}_{i,t} + \boldsymbol{\phi}_i^{\prime} \mathbf{F}_t + \sum_{j=1}^{N}\beta_j D_{j,t} + u_{i,t}, ~~~ i = 1,2,...,N ; j \neq i
	\label{eqn:pp_multi}
\end{align}

and his equation will form the foundations of the estimation. The remainder of this section describes the econometric issues with estimating the contagion coefficients, as well as a summary of the techniques used.

Finally, while not a primary concern of this paper, it is worth noting that the structure of the dataset has implications for identification. In the case of a sufficiently large number of countries, i.e. $N \rightarrow \infty$, the country-specific contagion coefficients cannot be identified, but for fixed $N$, $T \rightarrow \infty$, the multi-country model converges to the solution of the two-country system. As the analysis is limited to the Eurozone over several years of daily observations, it is reasonable to assume the latter setting applies. The formal result, derived in section 4 of \cite{pesaran2007econometric}, is therefore not reproduced.

\subsection{Estimation Strategies}
\label{est_strat}

In this study, three estimators will be described. The first two, OLS and Generalised Instrumental Variables, are used to estimate the model, while a Maximum Likelihood estimator is derived in the final subsection, although it is not utilised ue to complexities in implementing it for this set up. Again, for ease of notation, the two-country model will be used to describe the estimators. For the first two methods, they are subsequently extended to the N country case using equation (\ref{eqn:pp_multi}).

\subsubsection{OLS and GIVE}

Using the standard formulations in \cite{hayashi2000econometrics} as a foundation, and the expositions found in \cite{pesaran2007econometric} and \cite{massacci2007identification}, the OLS and Generalised Instrumental Variables (GIVE) estimator are derived below.

If we treat the dates of the crisis as predetermined, the dummy variables $D_{j,t}, ~ j = 1, 2$ are exogenous and accurately capture the two regimes. OLS provides an unbiased and consistent estimator in this setting. However, as discussed below, in section \ref{dating_methodology}, these assumptions are unlikely to hold, and $D_{j,t}$ will be endogenously determined.

\cite{pesaran2007econometric} and \cite{metiu2012sovereign} use Generalised Instrumental Variable Estimation (GIVE) to consistently estimate the contagion coefficient. It is assumed that $D_{j,t},~ j = 1, 2$ is endogenous, while all other variables are predetermined. It is also assumed that the threshold parameters $c_1$ and $c_2$ are known. Vectors $\boldsymbol{\gamma}_i$, $\mathbf{y}_i$, and $\mathbf{h}_{i,t}$, for $i,j = 1,2 ~~ i \neq j$ are defined as

\begin{align*}
 \boldsymbol{\gamma}_i \equiv&~ (\boldsymbol{\phi}_i^{\prime}, \boldsymbol{\delta}_{i}^{\prime}, \beta_i)^{\prime} \\
 \mathbf{y}_i \equiv&~ (y_{i,1},...,y_{i,T})^{\prime}\\
 \mathbf{h}_{i,t} \equiv&~ [\mathbf{F}_t^{\prime}, \mathbf{x}_{i,t}^{\prime}, \mathbf{I}(y_{j,t} - c_j)]^{\prime}
\end{align*}

with the matrix $\mathbf{H}_i \equiv (\mathbf{h}_{i,1}^{\prime},..., \mathbf{h}_{i,T}^{\prime})^{\prime}$ representing the exogenous and endogenous regressors for country $i$ across the whole sample period $T$.

Defining $\mathbf{w}_{i,t}$ as the vector of instruments, $\mathbf{W}_i \equiv (\mathbf{w}_{i,1}^{\prime},..., \mathbf{w}_{i,T}^{\prime})^{\prime}$ is the full sample matrix of instruments, and with associated projection matrix: $\mathbf{P}_{\mathbf{W}_i} \equiv \mathbf{W}_i (\mathbf{W}_i^{\prime} \mathbf{W}_i)^{-1} \mathbf{W}_i^{\prime}$. The system in equations (\ref{eqn:system}) and (\ref{eqn:system2}) is expressed for the full sample as

\begin{align}
	\mathbf{y}_i = \mathbf{H}_i \boldsymbol{\gamma}_i + \mathbf{u}_i \label{eqn:matrix_system}
\end{align}

The OLS and GIVE estimators in this setup are:

\begin{align}
	\hat{\boldsymbol{\gamma}}_{i, OLS} &= (\mathbf{H}_i^{\prime} \mathbf{H}_i)^{-1} \mathbf{H}_i^{\prime} \mathbf{y}_i	\label{eqn:ols}\\
	\hat{\boldsymbol{\gamma}}_{i, GIVE} &= (\mathbf{H}_i^{\prime} \mathbf{P}_{\mathbf{W}_i} \mathbf{H}_i)^{-1} \mathbf{H}_i^{\prime} \mathbf{P}_{\mathbf{W}_i} \mathbf{y}_i \label{eqn:give}
\end{align}

with the theoretical feasible covariance matrices:

\begin{align*}
	\hat{\mathbf{V}}_{i, OLS} &= \hat{\sigma}^2_i (\mathbf{H}_i^{\prime} \mathbf{H}_i)^{-1} \\
	\hat{\mathbf{V}}_{i, GIVE} &= \hat{\sigma}^2_i (\mathbf{H}_i^{\prime} \mathbf{P}_{\mathbf{W}_i} \mathbf{H}_i)^{-1}
\end{align*}

where $\hat{\mathbf{u}}_i = \mathbf{y}_i - \mathbf{H}_i \hat{\boldsymbol{\gamma}}_i$ and $\hat{\sigma}^2_i = \frac{1}{T} (\hat{\mathbf{u}}_i^{\prime} \hat{\mathbf{u}}_i)$.

%need to be precise here
If the parameter values $c_1, c_2$ are known, then the system is linear in parameters even though one of the regressors is a nonlinear function of the endogenous variables from other equations. \cite{pesaran2007econometric} and \cite{massacci2007identification} use the country specific exogenous regressors as instruments for the crisis dummy associated with that country which appears in the other equations. \cite{metiu2012sovereign}, using an alternative identification strategy to the one presented here, uses lagged terms of the dependent variables for the other countries in the system.

Neither of these choices of instrument is ideal. The optimal choice of $w_{i,t}$ would be the conditional probability of a crisis occurring at time $t$, given the information set $\boldsymbol{\Omega}_t = (\mathbf{F}_t^\prime, \mathbf{x}_{i,t}^{\prime}, \mathbf{x}_{j_t}^{\prime})$. As this is a function of unknown parameters $\boldsymbol{\gamma}_i$, it is infeasible. The instruments chosen are likely to suffer from a weak instrument problem, in which case the distribution of the GIVE estimator will not be asymptotically normal, meaning standard statistical inference may be misleading. A \cite{cragg1993testing} statistic is reported to indicate whether this problem is severe, and refer to the critical values given in \cite{stock2005testing}.

\subsubsection{Full Information Maximum Likelihood}
\label{fiml}

\cite{massacci2007identification} takes the model in PP, and derives a Full Information Maximum Likelihood (FIML) estimator, treating the crisis dummies as missing data that are generated by latent variables which are a function of other elements of the dataset. Using Monte Carlo simulations, Massaccci finds that FIML performs better than GIVE, while imposing fewer requirements on the data by not requiring equation specific variables to identify the contagion coefficients. Instead, identification is achieved by exploiting the dichotomous outcomes of the indicator function. This means that the identification condition is that at least one observation of $s_{i,t}$ falls either side of the threshold variable $c_i$.

Though this estimator could not be implemented for the system in question, a short exposition drawing on \cite{massacci2007identification} is presented for reference.

First, the assumptions that the elements of $\Omega_t$ are ergodic stationary variables, and that the errors $\mathbf{u}_t~\sim~\text{IID}(\mathbf{0}, \mathbf{\Sigma_u})$ are imposed.

Let $\boldsymbol{\theta}_k$ denote the vector of coefficients which characterise the joint distribution of $(s_{1,t}, s_{2,t})^{\prime}$ in regime $k$ for $k = 1, 2, 3, 4$ and the regimes correspond to those in (\ref{eqn:regions}):

\begin{align*}
	\label{eqn:fiml_coefs}
	&\boldsymbol{\theta}_1 \equiv (\boldsymbol{\phi}^{\prime}_1, \boldsymbol{\delta}^{\prime}_1,  \boldsymbol{\phi}^{\prime}_2, \boldsymbol{\delta}^{\prime}_2), 	&\boldsymbol{\theta}_2 \equiv ~~~ (\boldsymbol{\phi}^{\prime}_1, \boldsymbol{\delta}^{\prime}_1,
	\beta_1,  \boldsymbol{\phi}^{\prime}_2, \boldsymbol{\delta}^{\prime}_2) \\
	&\boldsymbol{\theta}_3 \equiv (\boldsymbol{\phi}^{\prime}_1, \boldsymbol{\delta}^{\prime}_1,  \boldsymbol{\phi}^{\prime}_2, \boldsymbol{\delta}^{\prime}_2, \beta_2), 	&\boldsymbol{\theta}_4 \equiv ~~~ (\boldsymbol{\phi}^{\prime}_1, \boldsymbol{\delta}^{\prime}_1,
	\beta_1,  \boldsymbol{\phi}^{\prime}_2, \boldsymbol{\delta}^{\prime}_2, \beta_2)
\end{align*}

Denoting the joint probability density function as $f(s_{1,t}, s_{2,t}; \boldsymbol{\theta_k} | \boldsymbol{\Omega}_t)$, then the system in (\ref{eqn:system}) and (\ref{eqn:system2}) can be represented:

\begin{align}	\label{eqn:fiml_decomp}
	f(s_{1,t}, s_{2,t} | \boldsymbol{\Omega}_t) =& [1 - \mathbf{I}(s_{1,t} - c_1)][1 - \mathbf{I}(s_{2,t - c_2})] f(s_{1,t}, s_{2,t}; \boldsymbol{\theta_1} | \boldsymbol{\Omega}_t)\\
	&+ [1 - \mathbf{I}(s_{1,t} - c_1)]\mathbf{I}(s_{2,t - c_2})f(s_{1,t}, s_{2,t}; \boldsymbol{\theta_2} | \boldsymbol{\Omega}_t)\\
	&+ \mathbf{I}(s_{1,t} - c_1)[1 - \mathbf{I}(s_{2,t - c_2})]f(s_{1,t}, s_{2,t}; \boldsymbol{\theta_3} | \boldsymbol{\Omega}_t)\\
	&+ \mathbf{I}(s_{1,t} - c_1)\mathbf{I}(s_{2,t - c_2})f(s_{1,t}, s_{2,t}; \boldsymbol{\theta_4} | \boldsymbol{\Omega}_t) \label{eqn:fiml_decomp2}
\end{align}

\cite{massacci2007identification} shows that, thanks to the presence of multiple equilibria, the sum of the above generates a cdf that exceeds unity. He applies a normalisation factor $\frac{1}{p_t}$, defined such that the sum of probabilities is equal to one.

Applying this, he uses the $f(s_{1,t}, s_{2,t} | \boldsymbol{\Omega}_t)$ decomposed in (\ref{eqn:fiml_decomp}) - (\ref{eqn:fiml_decomp2}) to maximise the following log-likelihood function:

\begin{align}
	L_T = \sum_{t=1}^{T} \text{log}f(s_{1,t}, s_{2,t} | \boldsymbol{\Omega}_t)
\end{align}

\cite{massacci2007identification} provides a proof of the consistency of parameter estimation using this estimator.

The estimator's major strength when estimating contagion coefficients is that it does not require country-specific explanatory variables in order to be identified. This is especially useful in the face of weak instruments which could render GIVE unable to provide reliable inference. It faces challenges regarding the assumptions it requires about the underlying distribution of the determinants of $D_{j,t}$, which in turn require consistent estimates of $c_i$ and $c_j$.

Additionally, extending the estimation technique to the multiple country setting is challenging, and estimation of country-pairs does not lend itself to the task of studying contagious systems. As a result, there are no estimations performed using the estimator, though it would provide fruitful future work.

\subsection{Crisis Dating}
\label{dating_methodology}

A key concern in all the above methods described is the procedure used to determined $D_{j,t}$. There is little consensus on how to solve this problem. \cite{fry2011actually} survey the literature on five crises since the mid-1990s and find over seventy papers which only rarely agree on precise dating of trigger events. Figure 1 in  \cite{dungey2015endogenous} (DMTY) vividly illustrates the lack of consensus on dating the recent U.S. financial crisis.

Though small disagreements in the dating methodology may not seem a major concern, misspecification of crisis periods will almost certainly lead to inconsistent estimation of the contagion coefficients. If the mistakes are systematic, for example thanks to sample selection bias, then this can have serious implications for drawing meaningful inference from the model.

The level of attention paid to this issue varies substantially in the literature. DMTY identify three  features commonly used by scholars to date crises: increases in volatility above a threshold; changes in transmission mechanisms between countries; and institutional action.

Institutional action, such as a devaluation, bailout, policy rate change, is the most common approach in early studies. This is unsurprising, as is it most closely linked to the popular understanding of crises\footnote{For instance, discussing the Asian financial crisis, a contemporary BBC article states clearly that ``It was the devaluation of the Thai baht in July that began a chain of currency devaluations across the region." This is representative of the mainstream popular view.
	\textit{Source:} \texttt{http://news.bbc.co.uk/1/hi/special\_report/1997/asian\_economic\_woes/34487.stm}}. However, policy action is almost certainly endogenous to underlying crisis variables, as policy often responds with a lag to developments in the macroeconomy or currency/bond markets rather than causing those developments. As it is the evolution of the underlying variable that is of interest, using such a strategy will likely misspecify crises periods. This is true both of the start and end of episodes.

Inter-country transmission mechanisms are more valid in terms of econometric theory, but do not remain stable over time even when there is no crisis. Innovations or underlying structural changes to cross-border relationships can confound attempts to use the stability of these linkages as an indicator of contagion. Over longer periods of time, this requires special attention and modelling. However, sudden shifts in the mechanisms, such as those we may observe over short time-spans, hint at a move between equilibria rather than long term trends. This can be used to help with crisis dating.

Finally, volatility tends to increase sharply in crisis periods, and can almost be seen as the definition of crisis. A range of papers (notably \cite{eichengreen1996contagious} and \cite{pesaran2007econometric}) seeking to avoid the sample-selection bias inherent in more subjective approaches, use an indicator function that triggers when the performance measure rises above a certain multiple of its standard deviation. This approach attempts to allow the data to determine crisis periods, and reduce researcher discretion in the selection process.

DMTY combine smooth transition function framework with a structural GARCH model to exploit the latter two factors and endogenously determine the crisis dates. They simultaneously estimate the contagion coefficients using a VAR structure. \cite{metiu2012sovereign} uses a one-step-ahead Value-at-Risk (VaR) model to denote periods of stress, while \cite{massacci2007identification} utilises a grid search process. A final notable example of an attempt to solve the identification problem is found in \cite{brutti2012transmission}, where the authors employ the `narrative approach' of \cite{romer1989does} to select days on which news specifically relating to Greece was broken during 2009-10. By stripping out days on which potentially confounding news about other countries broke, contagion originating solely in Greece can be identified and the effect of financial linkages, the initial research question, can be estimated. \cite{arezki2011sovereign} apply a similar approach, using sovereign credit rating news as the narrative indicator.

The above serves as a demonstration that there is relativity little consensus among scholars as to the optimum strategy for specifying crisis periods. The two most common, and also simplest to implement, are a pseudo narrative approach, where key events are selected as start- and end-points for the crisis, and a data-driven indicator function method. These are the two approaches used in this paper, with the details described below.

\underline{Date Scheme A}

As institutional action is such a popular indicator in the literature, a form of narrative dating approach is presented first. Four Eurozone countries, Greece, Ireland, Spain, and Portugal, have received official bailouts since 2009. Table \ref{tab:bailout_dates} outlines the dates chosen as the start and end dates of each of the associated crises, along with sources and notes on selection. These notes further emphasise the arbitrary nature of subjectively selecting crisis dates after the fact. For instance, should a bailout crisis be declared over at the reaching of an agreement with international creditors, or after that agreement passes through the national parliament? While elements of this issue could be resolved with a more systematic narrative approach, the subjective compilation of timelines - usually by newspapers - required for such an exercise is likely to still introduce selection bias.

The a priori specification of the crisis periods means that the dummies can theoretically be treated as predetermined. However, given that narratives about the Eurozone crisis revolve around bond market behaviour, endogeneity is still likely to be a problem - even if the crisis periods are well specified.

\underline{Date Scheme B}

The second method exploits the heightened volatility evident in crisis times, and uses the indicator function given in \cite{pesaran2007econometric}:

\begin{align}
	D_{j, t} \equiv \mathbf{I}(y_{j,t} - \kappa \sigma^2_{j})
\end{align}

In Section 6, PP estimate the \cite{eichengreen1996contagious} dataset using $\kappa = 2$. It is worth noting that the use of the unconditional standard deviation can again introduce sample selection bias to the procedure. For instance, failure to include previous crises in the sample period will lower the estimated unconditional standard deviation and thus increase the frequency of $D_{j,t} = 1$ in future crises. Conversely, inclusion of much older crisis data may generate $D_{j,t} = 0$ observations even when the realisations of $y_{j,t}$ are abnormally high when considering only the contemporary period.

In this scheme, the PP value of $\kappa_j = 2$ is retained, with the standard deviation calculated over the whole sample period. An alternative scheme, using the \cite{eichengreen1996contagious} value of $\kappa = 1.5$ is retained for robustness checks.

\end{document}


\documentstyle[note]{article}
\begin{document}
\LARGE

\subsection*{$\mid\;\;?-$ example1.}
This problem comes from the London 1978 A level exam.

We are asked to find the value(s) of for which
\[	\log_2(x) + 4\log_x(2) = 5\]

Rewriting equation in terms of $log_2(x)$ gives
\[\log_2(x) + 4\log_2(x)^{-1} = 5\]

Substituting $x_1$ for $\log_2(x)$ gives
\[ 4x_1^{-1} + x_1 = 5\]

Multiply through by $x_1$ to get
\[x_1^2 + -5x_1 + 4 = 0\]

Using quadratic equation formula. Solutions are $x_1 = 4$ and $x_1 = 1$

Applying substitution $x_1 = \log_2(x)$ to: $x_1 = 4\wedge x_1 = 1$ gives:
\[    \log_2(x) = 4 \wedge \log_2(x) = 1\]

Solving disjunct $\log_2(x) = 4$, gives
\[    x = 16\]

(by Isolation)

Solving disjunct $\log_2(x) = 1$, gives
\[    x = 2\]

(by Isolation)

\vspace*{.25in}
Answer is : $(X1 \wedge X2)$; where:
\begin{eqnarray*}
X1 & = & x = 16\\
X2 & = &  x = 2
\end{eqnarray*}

\newpage
\subsection*{$\mid\;\; ?-$ example2}

This problem is from the A.E.B. A level exam of 1971.

We are required to find the value(s) of x such that
\[	\cos(x) + 2\cos(2x) + \cos(3x) = 0\]

Angles are in arithmetic progression

Adding in pairs
\[2\cos(2x) + 2\cos(2x)\cos(x) = 0\]

\[(2 + 2\cos(x))\cos(2x) = 0\]

Solving factor $\cos(2x) = 0$. Letting $n_1$ denote an arbitrary integer
\[x = 45 +\frac{180n_1}{2}\]

     (by Isolation)

Solving factor $2 + 2\cos(x) = 0$. Letting $n_2$ denote an arbitrary
integer:
\[x = 180 + 360n_2 \wedge x = -180 + 360n_2\]

   (by Isolation)

\vspace*{0.25in}
Answer is : $(X1 \wedge (X2 \wedge X3))$ where :
\begin{eqnarray*}
    X1 & = &  x = 45 + 90n_1\\
    X2 & = & x = 180 + 360n_2\\
    X3 & = & x = -180 + 360n_2
\end{eqnarray*}
\newpage
\subsection*{$\mid\;\; ?-$ example3}

This problem is from the A.E.B. 1971 A level paper.

The question asks for the value(s) of $x$ which satisfy
\[4^x - 2^{x+1} - 3 = 0\]

Tidying to
\[4^x + (-1)2^{1 + x} = 3\]

Rewriting equation in terms of $2^x$ gives
\[(2^x)^2 + (-1)(2^{1}2^x) = 3\]

Substituting $x_2$ for $2^x$ gives
\[-2x_2 + x_2^2 = 3\]

Using quadratic equation formula. Solutions are $x_2 = 3$ and $x_2 = -1$.

Applying substitution $x_2 = 2^x$ to: $x_2 = 3 \wedge x_2 = -1$ gives:
\[2^x = 3 \wedge 2^x = -1\]

Solving disjunct $2^x = 3$ we have
\[x = \log_2(3)\]

(by Isolation)

Solving disjunct $2^x = -1$. $2^x = -1$ has no real roots, $2^x$ must be
greater than 0.

Answer is: $X1$, where :
\[X1 =  x = \log_2(3) = 1.584962500721156E+00\]

\newpage
\subsection*{$\mid\;\; ?-$ example4}

This question demonstrates the basic methods of PRESS.

The problem is to find the value(s) of $x$ that satisfy
\[\log_e(x+1) + \log_e(x-1) = 3\]

Tidying to
\begin{eqnarray*}
\log_e(1+x) + \log_e(-1+x) & = & 3\\
\log_e((-1 + x)(1 + x)) & = & 3\\
\log(e, -1 + x^2) & = & 3
\end{eqnarray*}

Finally
\[x = (1 + e^3)^{1/2} \wedge x = (-1)(1 + e^3)^{1/2}\]

(by Isolation)

Answer is : $(X1 \wedge X2)$ where:
\begin{eqnarray*}
X1 & = & x = (1 + e^3)^{1/2}\\
X2 & = & x = (-1)(1 + e^3)^{1/2}
\end{eqnarray*}

\end{document}
    X1 =  x = 4.591899054115592E+00
    X2 =  x = -4.591899054115592E+00

Answer is :
(X1 \wedge X2)
  where :
    X1 =  x = 4.591899054115592E+00
    X2 =  x = -4.591899054115592E+00


\mychapter{18}{Lesson 18} %181128

\subsubsection{Cramer-Shoup scheme construction}

From the above two proof systems we can construct a \pke{} scheme, which is attributed to Cramer and Shoup:

\todo{split definition from correctness}

\begin{itemize}
    \item $(\pk, \sk) \pickUAR \keygen$, where:
    \begin{itemize}
        \item $\pk := (h_1, h_2, h_3) = (g_1^{x_1} g_2^{x_2}, g_1^{x_3} g_2^{x_4}, g_1^{x_5} g_2^{x_6})$
        \item $\sk := (x_1, x_2, x_3, x_4, x5, x_6)$
    \end{itemize}
    \item Encryption procedure:
    \begin{itemize}
        \item $r \pickUAR \integer_q$
        \item $\beta = h_s(c_1, c_2, c_3) = (g_1^r, g_2^r, h_1^r m)$
        \item $\Enc(\pk, m) = (c_1, c_2, c_3, (h_2 h_3^\beta)^r)$
    \end{itemize}
    \item Decryption procedure:
    \begin{itemize}
        \item Check that $c_1^{x_3 + \beta x_5} c_2^{x_4 + \beta x_6} = c_4$. If not, output $\perp$.
        \item Else, output $\widehat{m} = c_3 c_1^{-x_1} c_2^{-x_2}$
    \end{itemize}
\end{itemize}

\todo{All the proofs here...}

\section{Digital signatures}

In this section we explore the solutions to the problem of authentication using an asymmetric key method. Some observations are in order:

\begin{figure}
    \centering

    \begin{tikzpicture}[node distance = 2cm, auto, >=latex']

        \node (s) {};
        \node (a) [box] [right of = s] {Alice};
        \node (k) [above of = a, node distance = 1cm] {$sk$};
        \node (b) [box] [right of = a, node distance = 5cm] {Bob};
        \node (p) [above of = b, node distance = 1cm] {$pk$};
        \node (e) [right of = b] {};

        \path[->] (s) edge node {$m$} (a);
        \path[->] (k) edge (a);
        \path[->] (a) edge node {$(m, \sigma)$} (b);
        \path[->] (p) edge (b);
        \path[->] (b) edge node {$b$} (e);

    \end{tikzpicture}
    \caption{Asymmetric authentication}
    \label{fig:digisign}
\end{figure}

\begin{itemize}
    \item In a symmetric setting, a verifier routine could be banally implemented as recomputing the signature using the shared secret key and the message. Here, Bob cannot recompute $\sigma$ as he's missing Alice's secret key (and for good reasons too...). Thus, the verifying routine must be defined otherwise;
    \item In a vaguely similar manner to how an attacker could encrypt messages by itself in the asymmetric scenario, because the public key is known to everyone, any attacker can verify any signed messages, for free.
\end{itemize}

Nevertheless, proving that a \textsc{ds} scheme is secure is largely defined in the same way as in the symmetric scenario, with the \textsc{uf-cma} property:

\begin{cryptogame}{dsufcma}{Unforgeable digital signatures}{uf-cma}

    \receive{$(\pk, \sk) \pickUAR \keygen(1^\lambda)$}{$\pk$}{}

    \cseqdelay

    \send{$m \in M$}{$m$}{}
    \receive{$\sigma \pickUAR \textsf{\textup{Sign}}(sk, m)$}{$\sigma$}{}

    \cseqdelay

    \send{$m^* \notin M$}{$(m^*, \sigma^*)$}{\textsc{Output} $\textsf{\textup{Verify}}(\pk, m^*, \sigma^*)$}

\end{cryptogame}

\subsection{Public Key Infrastructure}

The problem now is that Alice has a public key, but she wants some sort of ``certificate of validity'' for it, so that Bob will be sure that whenever he receives Alice's public key, he can be sure it's the right one by checking such certificate.

For certificates to be useful, the parties need an universally-trusted third party, called \textit{Certification Authority}. It will provide a special \textit{signature} to Alice for proving her identity to Bob, as exemplified by the sequence in figure \ref{cryptosequence:certissue}.

Whenever Bob wants to check the validity of the Alice's public key, he can query the authority for the certificate, and verify the public key he just received, as shown in figure \ref{cryptosequence:certcheck}

\begin{cryptosequence}
    {certissue}
    {}

    \cseqentity{CA}{CA}
    \cseqentity[2.2]{A}{Alice}

    \cseqmessager{CA}{$(\pk_C, \sk_C) \pickUAR \keygen$}{$\pk_C$}{A}{}

    \cseqdelay

    \cseqmessagel{A}{\shortstack[l]{
        $(\pk_A, \sk_A) \pickUAR \keygen$ \\
        $\textsc{id}_A = \textup{``Alice''} \| \pk_A$
        }}{$\textsc{id}_A$}{CA}{}

    \cseqdelay

    \cseqmessager{CA}{$\textsc{cert}_A \pickUAR \textsf{\textup{Sign}}(\sk_C, \textsc{id}_A)$}{$\textsc{cert}_A$}{A}{}

\end{cryptosequence}

\begin{cryptosequence}
    {certcheck}
    {}

    \cseqentity{CA}{CA}
    \cseqentity[0.8]{A}{Alice}
    \cseqentity[1.4]{B}{Bob}

    \cseqmessager{CA}{\shortstack[r]{
        \small{$(\pk_C, \sk_C) \pickUAR$} \\
        \small{$\pickUAR \keygen$}
    }}{$\pk_C$}{A}{}
    \cseqmessager{CA}{}{$\pk_C$}{B}{}

    \cseqdelay

    \cseqmessagel{A}{\shortstack[l]{
        $(\pk_A, \sk_A) \pickUAR \keygen$ \\
        $\textsc{id}_A = \textup{``Alice''} \| \pk_A$
    }}{$\textsc{id}_A$}{CA}{}

    \cseqdelay

    \cseqmessager{CA}{\shortstack[r]{
        \small{$\textsc{cert}_A \pickUAR$} \\
        \small{$\pickUAR \textsf{\textup{Sign}}(\sk_C, \textsc{id}_A)$}
    }}{$\textsc{cert}_A$}{A}{}

    \cseqdelay

    \cseqmessager{A}{}{$(\textsc{id}_A, \textsc{cert}_A)$}{B}{\small{$b = \textsf{\textup{Auth}}(\pk_C, \textsc{id}_A, \textsc{cert}_A)$}}

\end{cryptosequence}

How can Bob recognize a valid certificate from an expired/invalid one? The infrastructure provides some servers which contain the lists of the currently valid certificates, such as $\textsc{cert}_A$, in the case of Alice.

\begin{theorem}
    Signatures are in \textit{\textbf{Minicrypt}}.
\end{theorem}

This is a counterintuitive result, not proven during the lesson, but very interesting because it implies that we can create valid signatures only with hash functions, without considering at all public key encryption.

Up next:
\begin{itemize}
    \item Digital Signatures from TDP*
    \item Digital Signatures from ID Scheme*
    \item Digital Signatures from CDH
\end{itemize}

Where * appears, something called \textit{Random Oracle Model} is used in the proof. Briefly, this model assumes the existence of an ideal hash function which behaves like a truly random function (outputs a random y as long as x was never queried, otherwise gives back the already taken y).


\input{../../style/preamble}
\input{../../latex-math/basic-math}
\input{../../latex-math/basic-ml}

\newcommand{\titlefigure}{figure_man/biasvariance_scheme.png}
\newcommand{\learninggoals}{
  \item Understand why overfitting happens
  \item Know how overfitting can be avoided
  \item Know regularized empirical risk minimization
}

\title{Introduction to Machine Learning}
\date{}

\begin{document}

\lecturechapter{Introduction to Regularization}
\lecture{Introduction to Machine Learning}

\section{Motivation for Regularization}

\begin{vbframe}{Example: Overfitting}

\begin{itemize}
\item Assume we want to predict the daily maximum \textbf{ozone level} in LA given a data set containing $50$ observations.
\item The data set contains $12$ features describing time conditions (e.g., weekday, month),
the weather (e.g., temperature at different weather stations, humidity, wind speed) or geographic variables (e.g., the pressure gradient).
\item We fit a linear regression model using \textbf{all} of the features

$$
\fxt = \thetab^T\xv = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + ... + \theta_{12} x_{12}
$$

with the $L2$ loss.

\item We evaluate the performance with $10$ times $10$-fold CV.

\end{itemize}

\vfill

\begin{footnotesize}
We use (a subset of) the \texttt{Ozone} data set from the \texttt{mlbench} package. This way, we artificially create a \enquote{high-dimensional} dataset by reducing the number of observations drastically while keeping the number of features fixed.
\end{footnotesize}

\framebreak

While our model fits the training data almost perfectly (left), it generalizes poorly
to new test data (right). We overfitted.

\lz

\begin{figure}
\includegraphics[width=0.8\textwidth]{figure_man/example01.png}\\
\end{figure}

\end{vbframe}

\begin{vbframe}{Avoid Overfitting}

Why can \textbf{overfitting} happen? And how to avoid it?

\begin{enumerate}
\item Not enough data \\
$\to$ collect \textbf{more data}
\item Data is noisy \\
$\to$ collect \textbf{better data} (reduce noise)
\item Models are too complex \\
$\to$ use \textbf{less complex models}
\item Aggressive loss optimization \\
$\to$ \textbf{optimize less}
\end{enumerate}

\framebreak

\textbf{Approach 1: Collect more data}

\lz

We explore our results for increased dataset size by $10$ times $10$-fold CV.
The fit worsens slightly, but the test error decreases.

\begin{figure}
\includegraphics[width=0.7\textwidth]{figure_man/avoid-overfitting01.png}\\
\end{figure}

Good idea, but often not feasible in practice.

\framebreak

\textbf{Approach 3: Reduce complexity}

\lz

We try the simplest model we can think of: the constant model. For the $L2$ loss, the optimal constant model is

$$
\fxt = \frac{1}{n}\sumin \yi
$$

We then increase the complexity of the model step-by-step by adding one feature at a time.

\framebreak

We can control the complexity of the model by including/excluding features.
We can try out all feature combinations and investigate the model fit.

\begin{figure}
\includegraphics[width=0.7\textwidth]{figure_man/avoid-overfitting02.png}\\
\end{figure}

\vfill

\begin{footnotesize}
Note: For simplicity, we added the features in one specific (clever) order, so we cheated a bit. Also note there are $2^{12} = 4096$ potential feature combinations.
\end{footnotesize}

\framebreak

\textbf{Approach 4: Optimize less}

\lz

Now we use polynomial regression with temperature as the only feature to predict the ozone level, i.e.,

$$\fxt = \sum^{d}_{i=0} \theta_i (x_T)^{i} .$$
We choose $d = 15$, for which we get a very flexible model, which can be prone to overfitting for small data sets. \\
\medskip
In this example, we don't solve for $\hat\theta$ directly, but instead, we use the gradient descent algorithm to find $\hat\theta$ stepwise.

\framebreak

We want to stop the optimization early when the generalization error starts to degrade.

\begin{figure}
\includegraphics[width=0.7\textwidth]{figure_man/mean-squ-error.png}\\
\end{figure}

\footnotesize{Note: For polynomial regression, gradient descent usually needs many iterations before it starts to overfit. Hence a very small training set was chosen to accelerate this effect.}

\framebreak

We have contradictory goals

\begin{itemize}
\item \textbf{maximizing the fit} (minimizing the train loss)
\item \textbf{minimizing the complexity} of the model.
\end{itemize}

We need to find the \enquote{sweet spot}.

\begin{center}
\begin{figure}
\includegraphics[width=0.6\textwidth]{figure_man/complexity-vs-fit.png}
\end{figure}
\end{center}

\framebreak

Until now, we can either add a feature completely or not at all.

\lz

Instead of controlling the complexity in a discrete way by specifying the number of features,
we might prefer to control the complexity  \textbf{on a continuum} from simple to complex.

\begin{center}
\begin{figure}
\includegraphics[width=0.6\textwidth]{figure_man/complexity-vs-fit-continuous.png}
\end{figure}
\end{center}

\end{vbframe}

\section{Regularized Empirical Risk Minimization}

\begin{vbframe}{Regularized Empirical Risk Minimization}

Recall, empirical risk minimization with a complex hypothesis set tends to overfit. A major tool to handle overfitting is \textbf{regularization}.

  \lz

In the broadest sense, regularization refers to any modification made to a learning algorithm that is intended to reduce its generalization error but not its training error.

  \lz

Explicitly or implicitly, such modifications represent the preferences we have regarding the elements of the hypothesis set.

  \framebreak

  Commonly, regularization takes the following form:

  $$
  \riskrf = \riskef + \lambda \cdot J(f) = \sumin \Lxyi + \lambda \cdot J(f)
  $$
\begin{itemize}

  \item $J(f)$ is called \textbf{complexity penalty}, \textbf{roughness penalty} or \textbf{regularizer}.
  \item $\lambda > 0$ is called \textbf{complexity control} parameter.
  \item It measures the \enquote{complexity} of a model and penalizes it in the fit.
  \item As for $\riske$, often $\riskr$ and $J$ are defined on $\thetab$ instead of $f$, so $\riskrt = \risket + \lambda \cdot J(\thetab)$.
\end{itemize}

\framebreak

\textbf{Remarks:}

\begin{itemize}
  \item Note that we now face an optimization problem with two criteria:
    \begin{enumerate}
      \item models should fit well (low empirical risk),
      \item but not be too complex (low $J(f)$).
    \end{enumerate}
  \item We decide to combine the two in a weighted sum and to control
  the trade-off via the complexity control parameter $\lambda$.
  \item $\lambda$ is hard to set manually and is usually selected via cross-validation (see later).
  \item $\lambda = 0$: The regularized risk $\riskrf$ reduces to the simple empirical $\riskef$.

  \item If $\lambda$ goes to infinity, we stop caring about the loss/fit and models become as \enquote{simple} as possible.
\end{itemize}

\framebreak

\center
\vspace*{0.5cm}
\includegraphics[width=0.6\textwidth]{figure_man/biasvariance_scheme.png} \\
\footnotesize{Hastie, The Elements of Statistical Learning, 2009 (p. 225)}

\end{vbframe}

\endlecture
\end{document}


\section{Introduction}

\subsubsection{Background}
Systems such as neurons can have different types of input. The authors\supercite{Smyth:1996} propose dividing the input to a neuron into receptive (or driving) and contextual. They further present formulations of how contextual connections can modulate driving input. Finally, they use information theory to compare and contrast contextual modulation in these different formulations.

\subsubsection{Motivation}
This work was published in 1996, but with the advances in computing and information theory, and increased emphasis on the importance of context\supercite{Larkum:2013}, new studies\supercite{Kay:2011,Wibral:2017} have started building on its foundations. Furthermore, there are many ways to write the activation functions and only a few simple formulations were analyzed. Therefore, the significance of this work is twofold: 1. To replicate the study in a modern programming language and make it publicly accessible.   2. Enable new activation functions to be readily tested.

\subsubsection{Replication}
A successful attempt is made to qualitatively and quantitatively replicate the results of the original paper by replicating the three figures that comprise the results. While in all cases the results are qualitatively identical, there are minor quantitative differences in some figures which could in some cases be attributed to the use of a different seed value or the differences between how small or large values are handled in this python implementation and the original authors' implementation in an old programming environment about three decades ago.

\section{Methods}

\subsubsection{Input}
The study is limited to information transmission from a neuron which has only one receptive (R) or contextual (C) connection as input and produces one output (X). There is input at every time step, +1 denotes firing and -1 being silent, which is then multiplied by the weight of their connection. Probability distributions were created in the same way as original authors as described below, where for all figures P(C = 1 | R = 1) was set to 0.889972. This is so that the three components had all the same maximum possible value (0.5 bits). This makes it easier to compare the absolute values.

\begin{equation}
P(R = +1, C = +1) = 0.5P(C = 1 | R = 1)
\end{equation}

\begin{equation}
P(R =+1, C = -1) = 0.5[1 - P(C = 1 | R = 1)]
\end{equation}

\begin{equation}
P(R = -1, C = +1) = 0.5[1 - P(C = 1 | R = 1)]
\end{equation}

\begin{equation}
P(R = -1, C = -1) = 0.5P(C = 1 | R = 1).
\end{equation}

\subsubsection{Activation Functions}
Three activation functions are studied by the authors. Based on how receptive and contextual information are formulated to interact, they are called A\textsubscript{a} (additive), A\textsubscript{m} (modulatory), and A\textsubscript{b} (both additive and modulatory).

\begin{equation}
A_a(r,c) = r + c
\end{equation}

\begin{equation}
A_m(r,c) = 0.5r[1 + e^{rc}]
\end{equation}

\begin{equation}
A_b(r,c) = 0.5r[1 + e^{rc}] + c
\end{equation}

Where r is an instance of  R, and c an instance of C, multiplied by the weight of the connection. The output of each activation function is passed into the sigmoidal activation function to generate a probability of firing:

\begin{equation}
p(X=1 | r,c) = \frac{1}{1 + e^{-A(r,c)}}
\end{equation}

\subsubsection{Information Theory}
They use the three-way mutual information as a measure of the information shared between the output and the inputs which is defined as follows:

\begin{equation}
I(X;R;C) = \sum_{x,r,c}p(x,r,c)log\frac{P(x|r)P(x|c)}{P(x)P(x|r,c)}
\end{equation}

I(X;R|C) is used by the authors as a measure of information that is shared between the R input and the X output but not the C input and is defined as follows:

\begin{equation}
I(X;R|C) = \sum_{x,r,c}P(x,r,c)log\frac{P(x|r,c)}{P(x|c)}
\end{equation}

I(X;C|R) is used by the authors as a measure of information that is shared between the C input and the X output but not the R input and is defined as follows:

\begin{equation}
I(X;C|R) = \sum_{x,r,c}P(x,r,c)log\frac{P(x|r,c)}{P(x|r)}
\end{equation}

\subsubsection{Dependencies} The replication was done using Ubuntu 19.10 with Intel® Core™ i7-7500U CPU with Anaconda Python-3.8.1, numpy-1.18.1, scipy-1.4.1, and matplotlib-3.1.3. There are no external libraries required for calculating the information theoretic terms as everything is provided in the accompanying code.

\section{Results}

\subsubsection{Activation functions and transmitted information}

Figure 1 shows the three-way mutual information and conditional mutual information terms for each of the activation functions and across a range of R input weights while C remains constant at 1. These results are produced in a similar way to the paper by using the exact probability distributions. They are qualitatively identical to the original paper. There are minor quantitative differences. For example, in (c) the maximum value of the $A_b$ function is higher in the original paper.  In order to rule out the possibility of missing the higher maximum value as a result of not having that particular data point, a small step size of 0.1 was used for each data point which leads to a higher number of data points on this figure than the original publication's.

\begin{figure}[H]
    \includegraphics[width=\textwidth]{figure_1.png}
      \caption{Replication of the comparison of the activation functions for (a) I(X;R;C), (b) I(X;R|C) and (c) I(X;C|R). The activation functions are zero context (dashed), additive context (squares), modulatory context (circles), and both additive and modulatory combined (dotted).}
\end{figure}

\subsubsection{Information surface plots for different input weights}

Figure 2 shows the I(X;C|R) for each of the activation functions and across a range of r and c input weights. These results are produced in a similar way to the paper by using the exact probability distributions. They are qualitatively identical to the original paper. There are no notable quantitative differences.

\begin{figure}[H]
    \begin{center}
        \includegraphics[width=12cm,height=15cm]{figure_2.png}
    \end{center}
      \caption{Replication of the comparison of the information surfaces of I(X;C|R) for (a) additive, (b) modulatory and (c) both over all R and C input weights. The conditional probability P(C = 1 |R = 1) = 0.889972.}
\end{figure}

\subsubsection{Sampling biases and variances}

Figure 3 compares the mean and standard deviation of three-way measures sampled against the analytical measures for the modulatory activation function. The mean and deviations were computed from 100 trials per sample size and compared with the analytical measure across a range of sample sizes. As the authors claim, sample size of 200 could be sufficient as the improvements beyond that are not much. The error for small sample sizes is higher in the original paper, this can be attributed to use of a different seed value and their programming environment.

\begin{figure}[H]
    \includegraphics[width=\textwidth]{figure_3.png}
      \caption{Replication of comparison of the analytical (dotted) and sampled (error bars) information measures for (a) I(X;R;C), (b) I(X;R|C) and (c) I(X;C|R). The error bars represent standard deviation about the mean information computed from 100 trials for each sample size with the modulatory activation function. The R and C input weights were set to 2.}
\end{figure}

\section{Discussion}

The goal of the authors was to study contextual modulation in single binary neurons using information theory. To that end they present figures 1-2 as a way to compare and contrast functions with or without forms of contextual modulation. They use figure 3 to suggest how much data is required to get the required accuracy for the analyses presented in figures 1-2. \newline

One example of how the analyses here can be used to study contextual modulation is included as follows: For the modulatory activation function, it is intended that the role of R is to drive the neuron to fire and C is to amplify the firing. Figure 2 (b) shows that increasing C alone does not increase I(X;C|R), but increasing R and C together does and a higher C with a constant R amplifies it. At low R values, C has a higher role in the output so I(X;C|R) begins to diminish at higher R values. This shows that the modulatory function is behaving as intended and expected. \newline

In conclusion, this manuscript and the accompanying code present a successful replication of the authors' work in a modern programming language. Similarly, the code and information-theoretic analyses here could be used to study other activation functions and contextual modulation.

\subsubsection{Acknowledgments}

I would like to thank Bill Phillips and Jim Kay for clarifying my questions about the input. This research was supported by the funding initiative Niedersächsisches Vorab of the Volkswagen Foundation and the Ministry of Science and Culture of Lower Saxony.