%================================================
\section{Introduction to TDA}
%================================================
	%----------------------------------------------
	\subsection{Mathematics of Machine Learning}
	%----------------------------------------------
		\begin{frame}
		\frametitle{Mathematics of Machine Learning: Problem Statement}

		\begin{definition}
			Given an ideal function $f: X\rightarrow Y$, which associates \textbf{data} in $X\neq\phi$ with \textbf{measurements} in $Y\neq\phi$,
			the problem of \textbf{supervised learning} is to \textbf{learn} a \textbf{target function} $\hat{f}:X\rightarrow Y$, which approximates $f$,
			using a finite sample $S=\{(x_i,f(x_i))\}_{i=1}^n$, a \textbf{training set}.
		\end{definition}

		\begin{itemize}
			\item When $Y$ is finite, we say that this is a \textbf{supervised classification problem},
			otherwise, we say this is a \textbf{supervised regression problem}.
			\medskip
			\item In practice we often encode $Y$ to be isomorphic to $\rr^n$ or $\zz_n$ for some $n\in\nn$.
			(e.g., if the possible measurements are ``red'' and ``not red'', we encode this as $Y=\{0,1\}$).
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Mathematics of Machine Learning: Generalization}

		\begin{itemize}
			\item There are infinitely many $\hat{f}$ for which $\hat{f}(x_i) = f(x_i)$ for all $x_i\in S$.
			\medskip
			\item For the purpose of machine learning, we are more interested not in
			fitting $\hat{f}$ to $S$, but in generalizing $\hat{f}$ to $X$; i.e., approximating $f$ on $X$.
			\medskip
			\item For this reason, we often partition $S$ into two sets $S_{\text{train}}$ and $S_{\text{test}}$.
			\medskip
			\item We first find $\hat{f}$ which performs well on $S_{\text{train}}$, then we validate this on $S_{\text{test}}$.
			\medskip
			\item This does not guarantee accuracy on $X$. It is an open problem of research to establish useful conditions under
			which training and test set performance will generalize to the entire set $X$.
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Mathematics of Machine Learning: Hypothesis Space}
		\begin{itemize}
			\item The \textbf{hypothesis space} for the supervised learning problem is $\cH = \{f:X\rightarrow Y\}$.
			\medskip
			\item It is not computationally feasible to search over the space
			of all such functions for $f$, especially when the $X$ is infinite or extremely large. Instead we choose a class of target functions $\hat{H}$.
			\medskip
			\item Once a class of target functions has been selected, a \textbf{loss function} $J:\hat{\cH}\times \cH\rightarrow \rr$ is selected.
			\medskip
			\item A popular example of this is the mean squared error:
				$$J(\hat{f},f) = \frac{1}{n} \sum_{i=1}^n (f(x_i)-\hat{f}(x_i))^2.$$
			\item In the case where $Y=\{0,1\}$, a popular loss function is \textbf{logarithmic loss}:
				$$J(f,\hat{f}) = -f(x_i)\log(\hat{f}(x_i))+(1-f(x_i))\log(1-\hat{f}(x_i)).$$
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Mathematics of Machine Learning: Optimization}

			\begin{itemize}
			\item The task of learning $\hat{f}$ is to minimize $J$.
			\item There is copious literature on the subject of optimization and loss functions in a machine learning context.
			\item One of the most popular methods of optimization for machine learning is Adam, or adaptive moment estimation.
			\item It is beyond the scope of this talk to delve into the details of Adam.
			\item The selection of an optimization algorithm can impact the performance of machine learning significantly.
			\item See details of Adam here: \url{https://arxiv.org/pdf/1412.6980.pdf}
			\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Mathematics of Machine Learning: Pipeline}
		\begin{itemize}
			\item Define the problem in terms of an ideal function $f$ and a dataset $S$.
			\medskip
			\item Select a target function class; i.e., $\hat{H}$.
			\medskip
			\item Select a loss function $J$; e.g., mean squared error.
			\medskip
			\item Select an optimization method; e.g., Adam.
			\medskip
			\item Learn $\hat{f}$ by optimizing $J$ over $S$ (i.e., implement the machine learning architecture design).
			\medskip
			\item Analyze the results usually with statistics-based methods such as area under curve (AUC) and iterate on the design.
		\end{itemize}

		Note: if enough computational resources are available, hyperparameter search can be used to automate some of the iteration.

		\end{frame}
	%----------------------------------------------
	\subsection{Topological Data Analysis Motivations}
	%----------------------------------------------
		\begin{frame}
		\frametitle{Traditional Assumptions of Data Analytics}
		\begin{itemize}
			\item Qualitative information is required - we wish to classify data/datasets by describing global properties (i.e., features).
				\begin{itemize}
					\item Loss/error functions are quantitative in nature.
				\end{itemize}
			\item Metrics usually have no basis in physics (counterexample to this would be Physics-informed Neural Networks).
				\begin{itemize}
					\item Optimization of machine learning algorithms is sensitive to metric choice (e.g., mean squared error).
				\end{itemize}
			\item Coordinates are not usually natural (unlike state vector coordinates from physics).
				\begin{itemize}
					\item The representation of data matters significantly, especially in terms of coordinates (normalized data vs. raw data).
				\end{itemize}
			\item Preference of summaries over individual parameter selection.
				\begin{itemize}
					\item Parameter selection and hyperparameter search look for optimal parameters, but this is computationally expensive.
				\end{itemize}
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Topological Data Analysis}
		\begin{itemize}
			\item Qualitative information is required - we wish to classify data/datasets by describing global properties.
				\begin{itemize}
					\item Topology captures global qualitative information via connectivity information about
					the underlying surface (e.g., manifold) that data resides within.
				\end{itemize}
			\item Metrics usually have no basis in physics.
				\begin{itemize}
					\item Less sensitivity to metric choice in topology since spatial information is not dependent on true distance, but relative placement.
				\end{itemize}
			\item Coordinates are not usually natural.
				\begin{itemize}
					\item Topology is coordinate free by definition. We place topological structure on a coordinate space, but that structure is not the significant factor.
				\end{itemize}
			\item Preference of summaries over individual parameter selection.
				\begin{itemize}
					\item The powerhouse of topological data analysis is persistent homology, which looks at all possible parameter choices
					as a summary versus endlessly searching a large hyperparameter space.
				\end{itemize}
		\end{itemize}
		\end{frame}
	%----------------------------------------------
	\subsection{Basic Topological Structures}
	%----------------------------------------------
		\begin{frame}
		\frametitle{Spaces Under Study}

		\begin{defn}
		Let $X\neq\phi$ and $d:X\times X\rightarrow [0,\infty)$. Then $d$ is said to be a \textbf{metric} on $X$, and the pair
		$(X,d)$ a \textbf{metric space} if for all $x,y,z\in X$:
			\begin{enumerate}
				\item $d(x,y) = 0$ iff $x=y$
				\item $d(x,y) = d(y,x)$
				\item $d(x,z) \leq d(x,y) + d(y,z)$
			\end{enumerate}
		\end{defn}

		\begin{defn}
		Let $X\neq\phi$ and $\cT\subseteq X$. Then $\cT$ is a \textbf{topology} on $X$, and the pair $(X,\cT)$ is said to be a \textbf{topological space}, if
			\begin{enumerate}
				\item $X,\phi\in\cT$
				\item $\cT$ is closed under arbitrary unions
				\item $\cT$ is closed under finite intersections.
			\end{enumerate}
		The members of $\cT$ are said to be \textbf{open}.
		\end{defn}
		\end{frame}

		\begin{frame}
		\frametitle{Continuity}

		\begin{defn}
		Let $(X,\cT_X)$ and $(Y,\cT_Y)$ be topological spaces and $f:X\rightarrow Y$. Then $f$ is \textbf{continuous} at $x_0$ if for every $V(f(x))\in\cT_Y$, there exists $U(x)\in \cT_X$ such that $f(U)\subseteq V$.
		\end{defn}

		\begin{defn}
		Let $(X,d)$ be a metric space and $\epsilon>0$, then the open ball $B(x;\epsilon)=\{y: d(x,y)<\epsilon\}$
		\end{defn}

		\begin{defn}
		Let $(X,d)$ and $(Y,d_Y)$ be metric spaces. Then $f:X\rightarrow Y$ is continuous at $x_0\in X$ if for every
		$\epsilon>0$, there exists $\delta>0$ such that $f(B(x_0;\delta))\subseteq B(y;\epsilon)$.
		\end{defn}
		\end{frame}

		\begin{frame}
		\frametitle{Point Clouds and Homeomorphism}

		\begin{defn}
		A \textbf{point cloud} is a finite metric space (i.e., $(X,d)$ such that $X=\{x_1,\ldots,x_n\}$). As such all datasets encoded in computers can be seen as mathematical point clouds.
		\end{defn}

		\begin{defn}
		Let $f:X\rightarrow Y$ be bicontinuous (continuous $f$ and $f^{-1}$). Then $f$ is a \textbf{homeomorphism} and we say $X$ is homeomorphic to $Y$; $X\cong Y$. If a property holds for $X$ and for any homeomorphism $f(X)$, then we say that property is a \textbf{topological invariant}.
		\end{defn}

		\begin{itemize}
			\item Since every dataset is a point cloud (i.e., a metric space), every dataset has a topology, dependent on the choice of metric.
			\item E.g., $32\times 32$ black and white images can be seen as lying in $\cM^{32\times 32}([0,1])$. We can endow this space
			with a metric (e.g., Euclidean distance), which makes it a point cloud. Since it is also a point cloud, it has an underlying topology.
			\item $\cM^{32\times 32}([0,1]) \cong [0,1]^{1024}$.
		\end{itemize}
		\end{frame}
	%----------------------------------------------
	\subsection{Algebraic Topology: Connectivity Information}
	%----------------------------------------------
		\begin{frame}
		\frametitle{Homotopy}

		\begin{definition}
			If $f,g:X\rightarrow Y$ are continuous, we say that they are \textbf{homotopic} if $H:X\times[0,1]\rightarrow Y$ is such that $H(x,0)=f(x)$
			and $H(x,1)=g(x)$. Furthermore, we say that $f:X\rightarrow Y$ is a \textbf{homotopy equivalence} if there exists $G:Y\rightarrow X$ such
			that $f\circ g$ is homotopic to $\mathrm{id}_X$ and $g\circ f$ is homotopic to $\mathrm{id}_Y$. If $X$ is homotopy equivalent to $Y$ then
			we say they are \textbf{homotopic} spaces.
		\end{definition}

		\begin{itemize}
			\item See \textit{Algebraic Topology} by Hatcher for a more thorough treatment of this topic.
			\item It is enough for us to note with some hand-waving that there is a group $H_k(X,A)$ for any commutative group $A$ and non-negative
			integer $k$, such that when $X$ and $Y$ are homotopic, $H_k(X,A)$ is \textbf{isomorphic} (i.e., operation-preserving) to $H_k(Y,A)$.
			\item We call $H_k(X,A)$ the \textbf{homotopy group} of $X$.
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Homotopy}
		With respect to $H_k(X,A)$:
		\begin{itemize}
			\item If we require $A$ to be a field, then $H_k(X,A)$ is a vector space.
			\item We denote the dimension of this vector space as $\beta_k(X,A)$, which will be referred to as the $k-$th Betti number.
			\item Informally, the $k-$th Betti number corresponds to the number of independent $k-$dimensional surfaces.
			\item If two spaces are homotopy equivalent, then all their Betti numbers are equal.
			\item The profound observation of TDA, is that data may be studied by studying the inherent independent $k-$dimensional surfaces.
			\item This structure is not one that has been engineered, but one that is inherent in the data's topological structure.
			\item Computationally, it is not feasible/efficient to compute $H_k(X,A)$.
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Homology}

		\begin{itemize}
			\item Homology is a computationally feasible analog for homotopy equivalences.
			\item The rigorous definition of homology for general topology relies on infinitely generate modules over $\zz$.
			\item This definition is not useful from a data analytics perspective because it is computationally impossible to guarantee
			we could feasibly compute an approximation in general.
			\item However, using a simple combinatorial structure called \textbf{simplicial complexes}, we can determine the homology of a given point cloud efficiently.
			\item We will see that we can actually extract Betti numbers from simplicial complexes.
		\end{itemize}
		\end{frame}

		\begin{frame}
		\frametitle{Complexes}
		\begin{defn}
		An \textbf{abstract simplicial complex} is a pair $(V,\Sigma)$ where $V$ is finite and $\Sigma$ is a family of non-empty subsets of $V$ such that:
		$$\sigma\in\Sigma \;\;\;\text{and}\;\;\; \tau\subseteq\sigma \;\;\;\text{implies}\;\;\; \tau\in\Sigma$$
		\end{defn}

		\begin{itemize}
			\item Intuitively, simplicial complexes express a space of points, segments, triangles, tetrahedrons, and their higher dimensional analogues.
			\item These provide a particularly simple way to approximate topological spaces (in terms of homotopy/homology).
			\item Simplicial complexes admit a topology as well as an associated vector space (beyond the scope for this talk).
			\item It is computationally efficient to determine the homology of simplicial complexes compared to the original topological space.
			\item Rigorously, this is done by computing $H_k^{\text{simp}}(X,\zz)$, associated with the simplicial complex $X=(V,\Sigma)$.
			\item $H_k^{\text{simp}}(X,\zz)$ is isomorphic to the homology of $X$, which can be generate for a given point cloud.
			\item See Gunnar Carlsson's ``Topology and Data.''
		\end{itemize}

		\end{frame}

		\begin{frame}
		\frametitle{Persistent Homology}
		\begin{itemize}
			\item In order to guarantee that the homology of the point cloud $X$ corresponds with the homology of a simplicial complex $(V,\Sigma)$
			is to build this complex in such a way that there is a homotopy equivalence between $X$ and $(V,\Sigma)$.
			\item One such complex is the Vietoris-Rips simplicial complex.
			\end{itemize}

			\begin{defn}
			For a metric space $(X,d)$, the Vietoris-Rips simplicial complex associated with $\epsilon>0$, whose vertex set is $X$ and where $\{x_0,\ldots,x_k\}$ spans a $k-$dimensional subset iff $d(x_i,x_j)\leq\epsilon$ for all $i,j\leq k$.
			\end{defn}

			By varying $\epsilon$, we can study the homology of a point cloud at varying scales. We thus do not tie ourselves to one homology, but study the \textbf{persistent homologies} across sufficient choices of $\epsilon$ to summarize the topological information within the data.
		\end{frame}

				\begin{frame}
		\frametitle{VR Complexes}

				\begin{figure}
				\centering
				\includegraphics[scale=0.5]{images/complex.png}
				\caption{Sample simplicial complex structure using the Vietoris-Rips construction. As $\epsilon$ varies, the homology groups of the complex change.}
		\end{figure}
		\end{frame}

	%----------------------------------------------
	\subsection{Persistent Homology Representations}
	%----------------------------------------------
		\begin{frame}
		\frametitle{Persistence Diagrams}
		\begin{itemize}
			\item This is a representation of the persistent homology information.
			\item Persistence Diagrams (PDs) can be encoded in terms of two-dimensional vectors $(b_i,d_i)$, where $b_i$ is the birth and $d_i$ is the death of the $i^{\text{th}}$ homology feature.
			\item PDs have a natural metric associated with them called the \textbf{bottleneck distance}, which is numerically stable and computationally efficient to compute.
			\item We often design algorithms for TDA on PDs under the bottleneck distance.
		\end{itemize}

		\end{frame}

		\begin{frame}
		\frametitle{Persistence Diagram Example}

						\begin{figure}
				\centering
				\includegraphics[scale=0.5]{images/pd.png}
				\caption{Sample persistence diagram visualized in $\rr^n$ as a set of (birth,death) pairs.}
		\end{figure}
		\end{frame}

		\begin{frame}
		\frametitle{Persistence Barcodes}
		\begin{itemize}
			\item Persistent Barcodes are an alternative representation to PDs, mostly used for visualization.
			\item Instead of considering $(b_i,d_i)$ pairs as points, we look at them as segments/intervals and organize them with respect to the $k^{\text{th}}$ Betti number ($y$-axis).
		\end{itemize}

						\begin{figure}
				\centering
				\includegraphics[scale=0.5]{images/barcode.png}
				\caption{Sample barcode. As $\epsilon$ varies, the homology groups of the complex change and this is captured by line segments representing $[$birth, death$]$ intervals.}
		\end{figure}

		\end{frame}

		\begin{frame}
		\frametitle{Persistence Images}
		\begin{itemize}
			\item Persistence Images (PIs) provide a vector representation of PDs stable with respect to input noise, which are efficient to compute, and whose resolution can be adjusted.
			\item The user makes three choices for this representation:
			\begin{itemize}
				\item Resolution of the output PI.
				\item Probability distribution which affects noise stability (e.g., Gaussian, Rayleigh).
				\item Weighting function, which controls the relative importance of persistence coordinates (e.g., sigmoidal functions).
			\end{itemize}
			\item PIs lend themselves to being studied under traditional image processing techniques as well as convolutional neural networks.
		\end{itemize}

		\end{frame}

		\begin{frame}
		\frametitle{Persistence Image Example}

						\begin{figure}
				\centering
				\includegraphics[scale=0.35]{images/persimg.png}
				\caption{Sample persistence image pipeline from PD to Persistence Surface to PI at varying resolutions.}
		\end{figure}
		\end{frame}

		\begin{frame}
		\frametitle{Open Source Tools for Computing Persistent Homology}
		\begin{itemize}
			\item There have been a number of open source software packages developed for the efficient computation of persistent homology of data.
			\item These have been developed mostly by academic mathematicians in Python, C++, R, and Julia.
			\item Most popular ones include dionysus, scikit-tda, gudhi, ripser, and mapper.
			\item A full list of these tools has been compiled by \href{https://www.math.colostate.edu/~adams/advising/appliedTopologySoftware/}{Henry Adams}.
		\end{itemize}

		\end{frame}


% !TEX root = ../main.tex

In this chapter, we sketch some adaptations of the set reconciliation algorithm to related problems. \Cref{general-partitions} considers more general partitioning strategies than one-dimensional ranges. \Cref{maps} presents how to reconcile key-value mappings. \Cref{set-mirror} shows an asymmetric variant of the protocol, mirroring the contents of a set or map held at a primary node to a replica node. \Cref{authenticated} demonstrates how our data structures for fingerprint computations can also serve as simple authenticated data structures.

\section{Higher-Dimensional Ranges}
\label{general-partitions}

The set reconciliation algorithm recursively partitions the item sets of both nodes by splitting the sets into successive ranges. But for the correctness of the algorithm, the exact mechanism by which partitions are chosen is irrelevant. All that is necessary is that in each communication round, when receiving a mismatching fingerprint for some non-empty $S \subseteq U$, the node chooses a partition $\disjointunion{S_0}{\disjointunion{S_1}{\disjointunion{\ldots}{S_k}}} = S$ of $S$, and then communicates to the other node the choice of the partition and how its items are distributed among the individual sets.

Partitioning into successive ranges over a linearly ordered set has some nice properties: It can be applied to virtually any set since one can always arbitrarily define a linear order. The choice of the partition and the way the items are distributed among the sets can be encoded and transmitted efficiently. And finally, when receiving an arbitrarily chosen partition into successive ranges, a node can always efficiently compute the items it holds within that partition as well as the fingerprint over these items.

As long as nodes always want to synchronize the complete sets they hold, this is completely sufficient. But more elaborate partitioning strategies can become attractive if nodes might wish to only synchronize certain subsets. The partitioning mechanism effectively dictates which such subsets can be described, taking on the role of a query language. A natural next step is to look for more powerful partitioning mechanisms that still admit efficient fingerprint computation.

We now give one such partitioning scheme by generalizing the one-dimensional ranges of the original presentation of the algorithm to $k$-dimensional ranges. Let $U$ be a finite set of items, and let $\preceq_0, \preceq_1, \ldots, \preceq_{k-1}$ be linear orders over $U$. A \defined{$k$-dimensional range} is a $k$-tuple $((x_0, y_0), (x_1, y_1), \ldots, (x_{k-1}, y_{k-1}))$ with $x_i \preceq_i y_i$. Given some $S \subseteq U$, the items from $S$ in this range are all items $v \in S$ such that $x_i \preceq_i v \preceq_i y_i$ for all $0 \leq i < k$. Note that for $k = 1$ this is equivalent to the ranges used in previous chapters. The reconciliation protocol needs to be adapted so that messages include the $k$-dimensional range boundaries, but otherwise no changes are required.

Fingerprints can be computed efficiently by storing the set as a (balanced) $k$-d tree~\cite{bentley1975multidimensional}. As $k$-d trees are binary trees, the labels from \cref{fingerprints} can be used without any modification. Fingerprint computation still works by traversing the tree, alternating between the dimensions just as in e.g. a $k$-d tree item lookup. For $k = 1$, this yields exactly the algorithms of \cref{fingerprints}.

Generalized partitioning schemes, including $k$-dimensional ranges, can not only be applied to the regular set reconciliation protocol, but also to all modifications discussed in the further sections.

\section{Map Reconciliation}
\label{maps}

A key-value mapping, i.e. a partial function $\partialfun{\m}{K}{V}$ with a finite domain from some set of keys $K$ to a set of values $V$, can be reconciled by reconciling the set $\set{(k, v) \mid \text{$k \in \domain(\m)$ and $v = \m(k)$}}$. After this reconciliation, a node may have obtained two pairs $(k, v), (k, v')$ if the two nodes mapped the same key $k$ to distinct values $v, v'$, so the resulting set would not correspond to an updated map. In those cases, both nodes compute the single new image of $k$ as $\f(v, v')$, where $\fun{\f}{V \times V}{V}$ is some function known to all participating nodes.

Particularly interesting are cases where $\f$ can be computed via another interactive protocol. If for example $V$ consists of the fingerprints of finite subsets of some universe $U$, and $\f(v, v')$ is defined to be the fingerprint of the union of the two sets whose fingerprints are $v$ and $v'$, then the two nodes can run a set reconciliation session to efficiently obtain the union. Viewed more abstractly, when reconciling a map, the values can be reconciled via arbitrary nested protocol invocations.

\section{Set and Map Mirroring}
\label{set-mirror}

We based the presentation of our synchronization approach on set reconciliation because it is a symmetric problem where both nodes use identical algorithms to compute identical types of messages. A related, asymmetric problem is that of a \defined{replica} node setting its locally stored set to that of a \defined{primary} node, utilizing similarity between the two initial sets to minimize the communication complexity. The approach of recursively exchanging fingerprints for subsets of decreasing size can be modified to solve this \defined{set mirroring} problem.

The primary node can run exactly the same protocol as that for such reconciliation. The replica node uses a slightly modified version. Whenever it receives a range item set, it adds the received items to its local set as usual, but then it deletes all item it holds within that range which were not part of the received range item set. Whenever the replica node sends a range item set, it sends an empty one.

The complexity analysis is identical to that of the set reconciliation protocol, the correctness argument is analogous: ranges with equal fingerprints are already correctly mirrored if no fingerprint collisions occurred, exchanging range item sets correctly mirrors all items within that range, and large ranges with non-equal fingerprints can be handled recursively because mirroring the partitions of a set results in mirroring the whole set.

Just as for reconciliation, maps can be mirrored by interpreting them as sets of key-value pairs. Mirroring maps has some interesting use cases, for example filesystems can be regarded as maps from paths to strings. The efficient creation of backups then becomes the problem of mirroring such a map onto a backup server that may already hold a similar, older backup. An equivalent problem is that of efficiently distributing source code updates from a server to clients which may hold old versions of the source code.

\section{Authenticated Data Structures}
\label{authenticated}

Authenticated data structures solve the problem of outsourcing data structure membership queries processing to untrusted replicas rather than processing all queries at the original trusted data source. The trusted source publishes a short digest to a client. The client can then send a query to a replica, which answers with the result and a small certificate which together with the digest proves that the answer is indeed correct. For more details on this three-party model and pointers to the rich literature on the topic, we refer to~\cite{martel2004general}.

Merkle trees form a simple authenticated set. The root label is the digest, and the certificate for an affirmative membership query consists of the labels of the children of the vertices on the path from the root to the item in question. These labels can be used to recompute the root label, proving that the item is part of the original tree. Fabricating a sequence of labels to fake inclusion of an item amounts to breaking the hash function. Non-membership queries can be authenticated by providing certificates for tree membership of items stored in adjacent leaves such that one is less than and one is greater than the item in question.

Our examination of randomized data structures was driven by the need for Merkle-like properties, so it is not very surprising that they can also be used as authenticated data structures. Indeed~\cite{naor2000certificate} mentions treaps as an alternative to regular Merkle trees. Monoidal labels based on Cayley hash functions remove the need for a specific tree shape to be maintained. Skip lists as authenticated data structures have been studied in~\cite{goodrich2000efficient}, but they require the hash function to be commutative. Our construction removes this assumption.


\chapter{Expectation values}

\section*{3.1. Flip until heads}
\addcontentsline{toc}{section}{3.1. Flip until heads}
In Example 2 on page 136, we found that if you flip a coin until you get a Heads, the
expectation value of the total number of coins is
\begin{equation*}\tag{3.89}
    \frac{1}{2} \cdot 1 + \frac{1}{4} \cdot 2 +
    \frac{1}{8} \cdot 3 + \frac{1}{16} \cdot 4 + \frac{1}{32} \cdot 5 \ldots
\end{equation*}

We claimed that this sum equals 2. Demonstrate this by writing the sum as a
geometric series starting with 1/2, plus another geometric series starting
with 1/4, and so on. You can use the fact that the sum of a geometric series
with first term $a$ and ratio $r$ is $a/(1-r)$.

\vspace{1em}

\begin{proof}
    It can be easily seen that the general term of the sum is $\frac{n}{2^n}$, so
    \[
        S_n = \frac{1}{2} \cdot 1 + \frac{1}{4} \cdot 2 + \frac{1}{8} \cdot 3 + \frac{1}{16} \cdot 4 +
        \frac{1}{32} \cdot 5 + \ldots + \frac{1}{2^n} \cdot n
        = \sum_{k = 1}^{n} \frac{n}{2^n}
    \]

    We rewrite the sum and observe the suggested pattern:
    \begin{align*}
        \frac{1}{2} + \frac{2}{4} + \frac{3}{8} + \ldots + \frac{n}{2^n}
        &= \bigg(\frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \ldots + \frac{1}{2^n}\bigg)
            + \bigg(\frac{1}{4} + \frac{1}{8} + \ldots + \frac{1}{2^n}\bigg) + \ldots
            + \bigg(\frac{1}{2^{n - 1}} + \frac{1}{2^n}\bigg) + \frac{1}{2^n} \\
        &= \sum_{k = 1}^{n}\bigg(\frac{1}{2}\bigg)^k + \sum_{k = 2}^{n}\bigg(\frac{1}{2}\bigg)^k + \ldots
            + \sum_{k = n - 1}^{n}\bigg(\frac{1}{2}\bigg)^k + \sum_{k = n}^{n}\bigg(\frac{1}{2}\bigg)^k
    \end{align*}

    By observing the fact that the sums are actually geometric series with the ratio $r = \frac{1}{2}$
    and the first term $a = \frac{1}{2^k}$, we obtain:
    \[
        \sum_{k = p}^{n} \bigg(\frac{1}{2}\bigg)^k = \frac{\big(\frac{1}{2}\big)^p}{1 - \frac{1}{2}}
        = \bigg(\frac{1}{2}\bigg)^{p - 1}
    \]

    Therefore, by rewriting the sum using the last expression and then applying the geometric
    series result again, we get our desired result:
    \[
        S = \bigg(\frac{1}{2}\bigg)^0 + \bigg(\frac{1}{2}\bigg)^1 + \ldots + \bigg(\frac{1}{2}\bigg)^{n - 1}
        = \sum_{k = 0}^{n - 1} \bigg(\frac{1}{2}\bigg)^k
        = \frac{1}{1 - \frac{1}{2}} = 2
    \]
\end{proof}

\section*{3.2. HT waiting time}
\addcontentsline{toc}{section}{3.2. HT waiting time}
We know from Example 2 on page 136 that the expected number of flips required to obtain a Heads is 2.
What is the expected number of flips required to obtain a Heads and a Tails in succession (in that order)?

\vspace{1em}

\begin{proof}
    Since the first flip in the succession has to be Heads, we flip the coin until we obtain a Heads.
    It is known that the expected number of flips for that to happen is 2. Now, we need to obtain a
    Tails. Since Heads and Tails are equally likely in a fair coin flip, the expected number of flips
    needed to obtain Tails is also 2. Therefore, the expected number of flips needed to obtain a
    Heads and Tails succession (in this order) is $2 + 2 = 4$.
\end{proof}

\section*{3.3. Sum of dependent variables}
\addcontentsline{toc}{section}{3.3. Sum of dependent variables}
Consider the example on page 137, but now let $X$ and $Y$ be dependent in the following
manner: If $Y = 1$, then it is always the case that $X = 1$. If  $Y = 2$, then it is always the
case that $X = 2$. If $Y = 3$, then there are equal chances of $X$ being 1 or 2. If we assume that
$Y$ takes on the values 1, 2, and 3 with equal probabilities of 1/3, then you can quickly show
that $X$ takes on the values 1 and 2 with equal probabilities of 1/2. So we have reproduced
the probabilities in the original example. Show (by explicitly calculating the probabilities of
the various outcomes) that in the present scenario where $X$ and $Y$ are dependent, the relation
$E(X + Y) = E(X) + E(Y)$ still holds.

\vspace{1em}

\begin{proof}
    Since we know that the $X$ takes on the values 1, 2 with equal probabiliy, we can easily find that:
     \[
         E(X) = p(X = 1) \cdot 1 + p(X = 2) \cdot 2 = \frac{1}{2} + \frac{1}{2} \cdot 2 =\frac{3}{2}
    \]

    We do the same for $Y$ to obtain:
    \[
        E(Y) = p(Y = 1) \cdot 1 + p(Y = 2) \cdot 2 + p(Y = 3) \cdot 3
        = \frac{1}{3} + \frac{2}{3} + \frac{3}{3} = 2
    \]

    Therefore, it is straightforward that:
    \[
        E(X) + E(Y) = \frac{3}{2} + 2 = \frac{7}{4}
    \]

    We continue by computing $E(X + Y)$. We'll do that by analyzing the obtained cases from the perspective
    of  $Y$.
    \begin{enumerate}[(1)]
        \item We know that there is a  $\frac{1}{3}$ probability that $Y = 1$ and that if $Y = 1$, then $X = 1$.
            As a result, there is a $\frac{1}{3}$ probability that $Y = 1 \text{ and } X = 1$.

        \item Analogously, we find that there is a $\frac{1}{3}$ probability that $Y = 2$ and $X = 2$.

        \item For $Y = 3$, $X$ takes on the values $1, 2$ with equal probabilities of $\frac{1}{2}$. Hence,
            ($Y = 3$ and $X = 1$) and ($Y = 3$ and $X = 2$) are equally likely with a probability
            of $\frac{1}{6}$.
    \end{enumerate}

    By looking at the described cases, we obtain the outcomes of $X + Y$ and their probabilities:
    \begin{enumerate}[(i)]
        \item $\frac{1}{3}$ probability that $X + Y = 2$, for ($X = 1$ and $Y = 1$)

        \item $\frac{1}{3} + \frac{1}{6} = \frac{1}{2}$ probability that $X + Y = 4$, for ($Y = 2$ and $X = 2$)
    and ($Y = 3$ and  $X = 1$)

    \item $\frac{1}{6}$ probability that $X + Y = 5$, for ($Y = 3$ a and $X = 2$)
    \end{enumerate}

    Finally, we compute the expectation of the sum and prove the linearity of expectation:
    \begin{align*}
        E(X + Y) =& P(X + Y = 2) \cdot 2 + P(X + Y = 4) \cdot 4 + P(X + Y = 5) \cdot 5 \\
        =& \frac{1}{3} \cdot 2 + \frac{1}{2} \cdot 4 + \frac{1}{6} \cdot 5 = \frac{7}{4} = E(X) + E(Y)
    \end{align*}
\end{proof}

\section*{3.4. Playing "unfair" games}
\addcontentsline{toc}{section}{3.4. Playing "unfair" games}
\begin{enumerate}[(a)]
    \item Assume that later on in life, things work out so that you have more than enough money
        in your retirement savings to take care of your needs and beyond, and that you truly
        don't have a need for any more money. Someone offers you the chance to play a one-time
        game where you have a 3/4 chance of doubling your money, and a 1/4 chance of losing
        it all. If you initially have $N$ dollars, what is the expectation value of your
        resulting amount of money if you play the game? Would you want to play it?

    \item Assume that you are stranded somewhere, and that you have only \$10 for a \$20 bus
        ticket. Someone offers you the chance to play a one-time game where you have a 1/4
        chance of doubling your money, and a 3/4 change of losing it all. What is the expectation
        value of your resulting amount of money if you play the game? Would you want to play it?
\end{enumerate}

\vspace{1em}

\begin{proof}
    \hfill
    \begin{enumerate}[(a)]
        \item Since there is a 3/4 chance of doubling the money and a 1/4 chance of losing it all,
            the expectation value of the resulting money if playing the game is:
            \[
                E(X) = \frac{3}{4} \cdot 2N + \frac{1}{4} \cdot 0 = \frac{3N}{2}
            \]

            Even if the expected return looks favorable, I would not play the game in the given context.
            If I don't have a need for more money, a potential of doubling the money is dwarfed by the
            devastating result of losing it all, so the 1/4 probability of losing the money doesn't
            make the game appealing enough.

        \item The expected value of the resulting amount of money if playing the second game is given
            by:
            \[
                E(Y) = \frac{3}{4} \cdot \$0 + \frac{1}{4} \cdot \$20 = \$5
            \]

            Here, even if the expected return doesn't look favorable, I would play the game. The price
            is small enough that it would be worth losing the money for a $\frac{1}{4}$ chance of being
            able to get the bus ticket and get home.
    \end{enumerate}
\end{proof}

\section*{3.5. Simpson's paradox}
\addcontentsline{toc}{section}{3.5. Simpson's paradox}
During the baseball season in a particular year, player A has a higher batting average than player B.
In the following year, A again has a higher average than B. But to your great surprise when you
calculate the batting averages over the combined span of the two years, you find that A's average is
$\emph{lower}$ than B's! Explain, by giving a concrete example, how this is possible.

\vspace{1em}

\begin{proof}
    The Simpson's paradox is a phenomenon in which a trend appears in several different groups of data
    but disappears when these groups are combined. Let's consider the same setup as in the description of
    the problem. Suppose that the batting averages in the first year are 5/10 for player A and 15/35 for
    player B, respectively 10/20 for player A and 20/30 for player B in the second year. We can easily
    see that if we take years individually, the averages of player A are higher than averages of player B.
    However, if we combine the data of the two years, we get that the overall batting average
    of player A will be lower than the overall average of player B:
    \[
        \frac{5 + 10}{10 + 20} = \frac{1}{2} < \frac{7}{13} = \frac{15 + 20}{35 + 30}
    \]
\end{proof}

\section*{3.6. Variance of a product}
\addcontentsline{toc}{section}{3.6. Variance of a product}
Let $X$ and $Y$ each be the result of independent (and fair) coin flips where we assign the value
1 to Heads and 0 to Tails. Show that Var($XY$) is not equal to Var($X$)Var($Y$).

\vspace{1em}

\begin{proof}
    Since we know that $X$ takes on the values $1, 2$ with equal probability, we easily compute the
    variance of $X$:
    \[
        \text{Var}(X) = E[(X - \mu_x)^2] = E\bigg[\bigg(X - \frac{1}{2}\bigg)^2\bigg]
        = \frac{1}{2}\bigg(0 - \frac{1}{2}\bigg)^2  + \frac{1}{2}\bigg(1 - \frac{1}{2}\bigg)^2
        = \frac{1}{2} + \frac{1}{2}
        = \frac{1}{4}
    \]

    Analogously, we get the same result for Var($X$), so:
    \[
        \text{Var}(X)\text{Var}(Y) = \frac{1}{4} \cdot \frac{1}{4} = \frac{1}{16}
    \]

    X and Y can be chosen in 4 ways, 3 give $XY = 0$ and one gives $XY = 1$, so $\mu_{XY} = \frac{1}{4}$.
    Since all pairings are equally likely, we have that $P(XY = 0) = \frac{3}{4}$ and
    $P(XY = 1) = \frac{1}{4}$. Now, the variance of the product is given by:
    \[
        \text{Var}(XY) = E[(XY - \mu_{XY})^2] = E\bigg[\bigg(XY - \frac{1}{4}\bigg)^2\bigg]
        = \frac{3}{4} \bigg(0 - \frac{1}{4}\bigg)^2 + \frac{1}{4} \bigg(1 - \frac{1}{4}\bigg)^2
        = \frac{3}{64} + \frac{9}{64}
        = \frac{3}{16}
    \]

    In conclusion, we proved that in this setup Var($XY$) $\neq$ Var($X$)Var($Y$).
\end{proof}

\section*{3.7. Variances}
\addcontentsline{toc}{section}{3.7. Variances}
For each of the three examples near the beginning of Section 3.2., show that the alternative
$E(X^2) - \mu^2$ form of the variance given in Eq. (3.34) leads to the same results we obtained
in the examples.

\begin{proof}
    \hfill
    \begin{itemize}
        \item \textbf{Example 1 (Die roll):} The expectation value of the six equally likely outcomes
            of a dire roll is $\mu = \frac{21}{6}$, therefore $\mu^2 = \frac{441}{36}$. The expected value
            of $X^2$ is:
            \[
                E(X^2) = \frac{1}{6}(1 + 4 + 9 + 16 + 25 + 36) = \frac{91}{6}
            \]

            Hence, the variance result is the same as using the standard formula in (3.20):
            \[
                \text{Var}(X) = E(X^2) - \mu_X^2 = \frac{91}{6} - \frac{441}{36} = \frac{105}{36} \approx 2.92
            \]

        \item \textbf{Example 2 (Coin flip):} Consider a coin flip where we assign the value 1 to Heads
            and 0 to Tails. The expectation value of these two equally likely outcomes is $\mu = \frac{1}{2}$,
            so $\mu^2 = \frac{1}{4}$. The expected value of $X^2$ is:
            \[
                E(X^2) = \frac{1}{2} (0 + 1) = \frac{1}{2}
            \]

            As a result, the variance is the same as using the standard variance form in (3.21):
            \[
                \text{Var}(X) = E(X^2) - \mu^2 = \frac{1}{4}
            \]

        \item \textbf{Example 3 (Biased coin):} Consider a biased coins, where the probability of getting
            Heads is $p$ and the probability of getting Tails is $1 - p \equiv q$. If we again
            assign the value 1 to Heads and 0 to Tails, then the expectation value is
            $\mu = p \cdot 1 + (1 - p) \cdot 0 = p$, so $\mu^2 = p^2$. The expected value
            of $X^2$ is:
             \[
                 E(X^2) = p \cdot 1 + q \cdot 0 = p
            \]

            Once again, the variance is the same as in (3.22):
            \[
                \text{Var}(X) = E(X^2) - \mu^2 = p - p^2 = p(1 - p) = pq
            \]
    \end{itemize}
\end{proof}

\section*{3.8. Random walk}
\addcontentsline{toc}{section}{3.8. Random walk}
Consider the following one-dimensional random walk. A person starts at the origin and
then takes $n$ successive steps. Each step is equally likely to be to the right or to the
left. All steps have the same length.
\begin{enumerate}[(a)]
    \item What is the probability that the person is located back at the
        origin after the $n$th step?

    \item After $n$ steps, what is the standard deviation of the person's position
        relative to the origin? (Assume that the length of each step is, say, one
        foot).
\end{enumerate}

\vspace{1em}

\begin{proof}
    \hfill
    \begin{enumerate}[(a)]
        \item We see from the beginning that a person can end up in the origin only
            if he made the same number of steps in both directions. Therefore, it's
            impossible to end up in the origin if $n$ is odd, so:
            \[
                P(O | n = \text{odd}) = 0
            \]

            If $n$ is even, we
            count the favorable cases by considering the sequence of performed steps
            and seeing in how many ways can the left steps be placed in the sequence,
            the right steps taking the remaining positions. So, the number
            of ways in which the person ends up in the origin for an even $n$ is:
             \[
                 \binom{n}{\frac{n}{2}} = \frac{n!}{\big(\frac{n}{2}\big)!\big(\frac{n}{2}\big)!}
            \]

            The number of possible step sequences is obviously $2^n$, giving us the
            probability that is located back at the origin after the $n$th step:
            \[
                P(O | n = \text{even}) = \frac{n!}{2^n\big(\frac{n}{2}\big)!\big(\frac{n}{2}\big)!}
            \]

            By using the Law of Total Probability:
            \[
                P(O) = P(O | n = \text{even})P(n = \text{even}) + P(O | n = \text{odd})P(n = \text{odd})
                = \frac{n!}{2^{n+1}\big(\frac{n}{2}\big)!\big(\frac{n}{2}\big)!}
            \]

        \item Let $X$ be the distance from the origin after n steps, where steps are represented
            by the random variables $X_i$ which take on the values $-1$ and $1$ and 2 equally likely. Then,
            \[
                X = \sum_{i = 1}^{n} X_i
            \]

            We compute the expectation of $X^2$:
            \[
                E[X^2] = E\bigg[\bigg(\sum_{i = 1}^n X_i\bigg)^2\bigg]
                = E\bigg[\bigg(\sum_{i = 1}^n {X_i}^2 + \sum_{j = 1}^n\sum_{k = j+1}^n X_jX_k\bigg)\bigg]
            \]

            Since $X_i$ takes on -1 and 1, then $X_i^2 = 1$, so $\displaystyle \sum_{i = 1}^n {X_i}^2 = n$.
            Using this and the linearity of expectation, our expression becomes:
            \[
                E[X^2] = E[n] + E\bigg[\sum_{j = 1}^n\sum_{k = j+1}^n X_jX_k\bigg]
                = n + \sum_{j = 1}^n\sum_{k = j+1}^n E[X_jX_k]
            \]

            The events $X_i$ and $X_j$ of choosing a step are independent for $i \neq j$. From
            this and the fact that the expected value of $X_i$ is 0, we get:
             \[
                 E[x^2]= n + \sum_{j = 1}^n\sum_{k = j+1}^n E[X_j]E[X_k] = n
            \]

            The mean is now given by:
            \[
            \mu = \frac{1}{n}\sum_{i = -n}^n i = 0
            \]

            Finally, the standard deviation is:
            \[
                \sigma = \sqrt{E(X^2) - \mu^2} = \sqrt{n}
            \]
    \end{enumerate}
\end{proof}

\section*{3.9. Expected product, without replacement}
\addcontentsline{toc}{section}{3.9. Expected product, without replacement}
Consider a set of $N$ given numbers, $a_1, a_2, \ldots a_N$. Let the mean of these $N$ numbers
be $\mu$, and let the standard deviation be $\sigma$. Draw two numbers $X_1$ and $X_2$
randomly $\emph{without replacement}$. Show that the expectation value of their
product is
\begin{equation*}\tag{3.90}
    E[X_1X_2] = \mu^2 - \frac{\sigma^2}{N - 1}
\end{equation*}

$\emph{Hint}$: All of the $a_ia_j$ possibilities (with $i \neq j$) are equally likely.

\vspace{1em}

\begin{proof}
    There are $\binom{N}{2} = \frac{N(N - 1)}{2}$ ways of choosing $X_1$ and $X_2$, all of
    them being equally likely, so:
    \[
        E[X_1X_2] = \frac{2}{N(N - 1)} \sum_{i = 1}^n \sum_{j = i + 1}^n a_ia_j
    \]

    Seeing that
    \[
        \bigg(\sum_{i = 1}^n a_i\bigg)^2 = \sum_{i = 1}^n {a_i}^2 + 2\sum_{i = 1}^n \sum_{j = i + 1}^n a_ia_j
    \]

    , the expression of the expectation becomes:
    \[
        E[X_1X_2] = \frac{1}{N(N - 1)} \bigg[\bigg(\sum_{i = 1}^n a_i\bigg)^2 - \sum_{i = 1}^n {a_i}^2\bigg]
    \]

    From the formula of the mean we notice that
    \begin{equation*}\tag{3.9.1}
        N\mu = \sum_{i = 1}^n a_i
    \end{equation*}

    , so then
    \begin{align*}
        E[X_1X_2] = \frac{1}{N(N - 1)} \bigg(N^2\mu^2 - \sum_{i = 1}^n {a_i}^2\bigg)
        =& \frac{N\mu^2}{N - 1} - \frac{1}{N(N - 1)}\sum_{i = 1}^n {a_i}^2 \\
        =& \mu^2 - \frac{1}{N(N - 1)}\bigg(\sum_{i = 1}^n {a_i}^2 - N\mu^2\bigg) \\
    \end{align*}

    By rewriting the expression of the variance and using (3.9.1), we obtain:
    \begin{align*}
        \frac{\sigma^2}{N - 1} = \frac{1}{N(N - 1)}\sum_{i = 1}^n (a_i - \mu)^2
        =& \frac{1}{N(N - 1)} \bigg(\sum_{i = 1}^n {a_i}^2 - 2\mu \sum_{i = 1}^n a_i + N\mu^2\bigg) \\
        =& \frac{1}{N(N - 1)} \bigg(\sum_{i = 1}^n {a_i}^2 - 2N\mu^2 + N\mu^2\bigg) \\
        =& \frac{1}{N(N - 1)} \bigg(\sum_{i = 1}^n {a_i}^2 - N\mu^2\bigg) \\
    \end{align*}

    In conclusion, by substituting the last expression in the expectation's form, we see
    that
    \begin{equation*}\tag{3.90}
        E[X_1X_2] = \mu^2 - \frac{\sigma^2}{N - 1}
    \end{equation*}
\end{proof}

\section*{3.10. Standard deviation of the mean, without replacement}
\addcontentsline{toc}{section}{3.10. Standard deviation of the mean, without replacement}
Consider a set of $N$ given numbers, $a_1, a_2, \ldots, a_N$. Let the mean of these
$N$ numbers be $\mu$ and let the standard deviation be $\sigma$. Draw a sample
of $n$ numbers $X_i$, randomly $\emph{without replacement}$, and calculate
their sample mean. Show that the variance of the sample mean is given by
\begin{equation*}\tag{3.91}
    E\bigg[\bigg(\frac{1}{n}\sum_{i = 1}^n X_i - \mu\bigg)^2\bigg]
    = \frac{\sigma^2}{n}\bigg(1 - \frac{n-1}{N-1}\bigg)
\end{equation*}

\vspace{1em}

\begin{proof}
    We start by expanding the square in the expression:

    \begin{align*}
        E\bigg[\bigg(\frac{1}{n}\sum_{i = 1}^n X_i - \mu\bigg)^2\bigg]
        =& E\bigg[\frac{1}{n^2}\bigg(\sum_{i = 1}^n X_i\bigg)^2
                              - \frac{2\mu}{n}\sum_{i = 1}^n X_i + \mu^2\bigg] \\
        =& E\bigg[\frac{1}{n^2}\sum_{i = 1}^{n} {X_i}^2 + \frac{2}{n^2}\sum_{i = 1}^n\sum_{j = i + 1}^n X_iX_j
                              - \frac{2\mu}{n}\sum_{i = 1}^n X_i + \mu^2\bigg] \\
    \end{align*}

    By using the linearity of expectation, the expression becomes:
    \[
        E\bigg[\bigg(\frac{1}{n}\sum_{i = 1}^n X_i - \mu\bigg)^2\bigg] =
        \frac{1}{n^2}\sum_{i = 1}^nE[{X_i}^2] + \frac{2}{n^2}\sum_{i = 1}^n\sum_{j = i + 1}^n E[X_iX_j]
        - \frac{2\mu}{n} \sum_{i = 1}^n E[X_i] + \mu^2
    \]

    We assume that the $n$ numbers $X_i$ are equally likely to be extracted, so
    we denote $X$ such that for all $1 \leq i \leq n$,
    \begin{align*}
        E[X_i] &= E[X] = \mu \\
        E[{X_i}^2] &= E[X^2] = \mu^2 + \sigma^2
    \end{align*}
    $X_iX_j$ are also distributed the same. Using (3.90), we denote $X_aX_b$ so that
    for all $1 \leq i \leq j \leq n$,
    \[
        E[X_iX_j] = E[X_aX_b] = \mu^2 - \frac{\sigma^2}{N - 1}
    \]

    By using the proposed substitutions, our expression becomes:
    \begin{align*}
        E\bigg[\bigg(\frac{1}{n}\sum_{i = 1}^n X_i - \mu\bigg)^2\bigg]
        =& \frac{1}{n^2}\sum_{i = 1}^nE[{X}^2] + \frac{2}{n^2}\sum_{i = 1}^n\sum_{j = i + 1}^n E[X_aX_b]
            - \frac{2\mu}{n} \sum_{i = 1}^n E[X] + \mu^2 \\
        =& \frac{1}{n}\bigg[\mu^2 + \sigma^2 + (n - 1)
            \bigg(\mu^2 - \frac{\sigma^2}{N - 1}\bigg)\bigg] - \mu^2 \\
        =& \mu^2 \bigg(\frac{1}{n} + \frac{n-1}{n} - 1\bigg) +
            \sigma^2\bigg(\frac{1}{n} - \frac{n-1}{n(N - 1)}\bigg)
    \end{align*}

    The coefficient of $\mu^2$ is 0, so we obtain the desired result:
    \begin{equation*}\tag{3.91}
        E\bigg[\bigg(\frac{1}{n}\sum_{i = 1}^n X_i - \mu\bigg)^2\bigg]
        = \frac{\sigma^2}{n}\bigg(1 - \frac{n-1}{N-1}\bigg)
    \end{equation*}
\end{proof}

\section*{3.11. Biased sample standard deviation}
\addcontentsline{toc}{section}{3.11. Biased sample standard deviation}
We mentioned on page 163 that the sample standard deviation $s$ is a $\emph{biased}$
estimator of the distribution standard deviation $\sigma$. The basic reason for this
is that  the square root operation is nonlinear, which means that the square
root of the average of a set of numbers isn't equal to the average of their
square roots. For example, the average of 1.1 and 0.9 is 1, but the average of
$\sqrt{1.1}$ and  $\sqrt{0.9}$ isn't 1. It is smaller than 1. Let's give a general
proof that  $E[s] \leq \sigma$ (unlike  $E[s^2] = \sigma$).

If we calculate the sample variances for a large number $N$ of sets of $n$ numbers,
then the $E[s^2] = \sigma^2$ equality in Eq. (3.74) tells us that in the $N \to \infty$
limit, we have
\begin{equation*}\tag{3.92}
    \frac{s_1^2 + s_2^2 + \ldots + s_N^2}{N} = \sigma^2
\end{equation*}

Our goal is to show that
\begin{equation*}\tag{3.93}
    \frac{s_1 + s_2 + \ldots + s_N}{N} \leq \sigma
\end{equation*}
in the $N \to \infty$ limit. To demonstrate this, square both sides of Eq. (3.93)
and make copious use of the arithmetic-geometric-mean inequality, $\sqrt{ab} \leq (a+b)/2$.

\vspace{1em}

\begin{proof}
    Let us define the series $(a_N)_{N \geq 1}, (b_N)_{N \geq 1} \subset \mathbb{N}$, with

    \vspace{1em}
    \begin{minipage}{0.5\textwidth}
        \[
            a_N = \frac{s_1 + s_2 + \ldots + s_N}{N}
        \]
    \end{minipage}
    \begin{minipage}{0.5\textwidth}
        \[
            b_N = \sqrt{\frac{s_1^2 + s_2^2 + \ldots + s_N^2}{N}}
        \]
    \end{minipage}
    \vspace{1em}

    We'll prove that $a_N \leq b_N$, for all $N \in \mathbb{N}$. The starting point is
    the inequality
    \[
        \sum_{i = 1}^N \sum_{j = i + 1}^N (s_i - s_j)^2 \geq 0
    \]
    which is true, since a sum of squares is always nonnegative. By expanding
    the sum, we get that
    \[
        (N - 1)\sum_{i = 1}^{N}s_i^2 - \sum_{i = 1}^N \sum_{j = i + 1}^N 2s_is_j \geq 0
    \]
    After moving the second term in the right-hand side of the inequality and then
    adding $\displaystyle \sum_{i = 1}^{N} s_i^2$ to both sides, the expression becomes:
    \[
        N\sum_{i = 1}^N s_i^2 \geq \sum_{i = 1}^N s_i^2 + \sum_{i = 1}^N \sum_{j = i + 1}^N 2s_is_j
    \]

    The member on the right is the expansion of the squared sum of $s_i$'s, so
    \[
        N\sum_{i = 1}^N s_i^2 \geq \bigg(\sum_{i = 1}^{N} s_i\bigg)^2
    \]

    We divide both sides by $N^2 > 0$ and. Then,
    \[
        \frac{1}{N}\sum_{i = 1}^N s_i^2 \geq \bigg(\frac{1}{N}\sum_{i = 1}^{N} s_i\bigg)^2
    \]

    The next step is applying the squared root operation on both sides of the expression.
    Since the squared root function is increasing, the inequality is preserved:
    \[
        \bigg(\frac{1}{N}\sum_{i = 1}^N s_i^2\bigg)^{\frac{1}{2}} \geq \frac{1}{N}\sum_{i = 1}^{N} s_i
    \]

    We expand the sum and see that we find $a_N$ and $b_N$ :
    \[
        \sqrt{\frac{s_1^2 + s_2^2 + \ldots s_N^2}{N}} = b_N \geq a_N = \frac{s_1 + s_2 + \ldots + s_N}{N}
    \]

    Therefore, we proved that $a_N \leq b_N$, for all $N \in \mathbb{N}$.

    \vspace{1em}

    Now, we know that:
    \[
        a_N \leq b_N, \forall N \in \mathbb{N} \implies \lim_{N \to \infty} a_N \leq \lim_{N \to \infty} b_N
    \]

    By substituting the actual values of $a_N$ and $b_N$, we get:
    \[
        \lim_{N \to \infty} \frac{s_1 + s_2 + \ldots + s_N}{N}
        \leq \lim_{N \to \infty} \sqrt{\frac{s_1^2 + s_2^2 + \ldots + s_N^2}{N}}
    \]

    By using the continuity of the square root and then substituting with (3.92) in the
    right-hand side member, we obtain the desired result:
    \begin{equation*}\tag{3.93}
        \lim_{N \to \infty} \frac{s_1 + s_2 + \ldots + s_N}{N} \leq \sigma
    \end{equation*}
\end{proof}

\section*{3.13. Sample variance for two dice rolls}
\addcontentsline{toc}{section}{3.13. Sample variance for two dice rolls}
\begin{enumerate}[(a)]
    \item We know from the first example in Section 3.2 that the variance of a
        single die roll is $\sigma^2 = 2.92$. If you use Eq. (3.73) to
        calculate the sample variance  $s^2$ for $n = 2$ dice rolls,
        the expected value of $s^2$ should be $\sigma^2 = 2.92$,
        according to Eq. (3.74). By considering the 36 equally
        likely pairs of dice in Table 1.5, verify that this is indeed the case.

    \item Using the information you generated from Table 1.5, calculate Var($s^2$).
        Then show that the result agrees with the expression of Var($s^2$) in
        Eq. (3.94), with $n = 2$.
\end{enumerate}

\vspace{1em}

\begin{proof}
    \hfill

    \begin{enumerate}[(a)]
        \item By using (3.74) for $n = 2$, our sample variance is:
             \[
                 s^2 = \frac{1}{n - 1}\sum_{i = 1}^n (x_i - \overline{x})^2
                 = (x_1 - \overline{x})^2 + (x_2 - \overline{x})^2
            \]

        Using the fact that $\overline{x} = \frac{1}{2}(x_1 + x_2)$, we
        can easily prove that
        \[
            s^2 = \frac{(x_1 - x_2)^2}{2}
        \]

        Now, we analyze each one of the 36 possible dice roll pairs and analyze their sample variances.
        We have 6 cases:
        \begin{enumerate}[(1)]
            \item $x_1$ and $x_2$ are equal. There are 6 such cases, with $s^2 = 0$.

            \item The difference between $x_1$ and $x_2$ is 1. There are $5 \cdot 2 = 10$
                such roll pairs, with $s^2 = \frac{1}{2}$

            \item The difference between $x_1$ and $x_2$ is 2. There are $4 \cdot 2 = 8$
                such roll pairs, with $s^2 = 2$

            \item The difference between $x_1$ and $x_2$ is 3. There are $3 \cdot 2 = 6$
                such roll pairs, with $s^2 = \frac{9}{2}$.

            \item The difference between $x_1$ and $x_2$ is 4. There are $2 \cdot 2 = 4$
                such roll pairs, with $s^2 = 8$.

            \item The difference between $x_1$ and $x_2$ is 5. There are $1 \cdot 2 = 2$
                such roll pairs, with $s^2 = \frac{25}{2}$.
        \end{enumerate}

        Therefore, the expected value of the sample variance $s^2$ for $n = 2$ is:
        \[
            E[s^2] = \bigg(\frac{6}{36} \cdot 0\bigg) + \bigg(\frac{10}{36} \cdot \frac{1}{2}\bigg)
                  + \bigg(\frac{8}{36} \cdot 2\bigg) + \bigg(\frac{6}{36} \cdot \frac{9}{2}\bigg)
                  + \bigg(\frac{4}{36} \cdot 8\bigg) + \bigg(\frac{2}{36} \cdot \frac{25}{2}\bigg)
                  = \frac{88}{36} \approx 2.92
        \]
        which matches the expected result.

    \vspace{1em}

    \item The variance of the sample variance is given by:
        \begin{align*}
            \text{Var}(s^2) = E[(s^2 - 2.92)^2]
               &= \bigg[\frac{6}{36} \cdot (0 - 2.92)^2\bigg]
                + \bigg[\frac{10}{36} \cdot (0.5 - 2.92)^2\bigg]
                + \bigg[\frac{8}{36} \cdot (2 - 2.92)^2\bigg] \\
               &+ \bigg[\frac{6}{36} \cdot (4.5 - 2.92)^2\bigg]
                + \bigg[\frac{4}{36} \cdot (8 - 2.92)^2\bigg]
                + \bigg[\frac{2}{36} \cdot (12.5 - 2.92)^2\bigg] \\
               &\approx 11.62
        \end{align*}

        The fourth-order mean $\mu_4$ is
        \begin{align*}
            \mu_4 = E[(X - \mu)^4]
            &= \frac{1}{6}\big[(1 - 3.5)^4 + (2 - 3.5)^4 + (3 - 3.5)^4
                + (4 - 3.5)^4 + (5 - 3.5)^4 + (6 - 3.5)^4] \\
            &\approx 14.73
        \end{align*}

        Eq. (3.94) is given by
        \begin{equation*}\tag{3.94}
            \text{Var}(s^2) = \frac{1}{n}\bigg[\mu_4 - \sigma^2\bigg(\frac{n-3}{n-1}\bigg)\bigg]
        \end{equation*}
        so by plugging the numbers, we see that:
        \[
            \text{Var}(s^2) = \frac{1}{2}(14.73 + {2.92}^2) = 11.62
        \]
        which matches the expected result.
    \end{enumerate}
\end{proof}t


\section{Edge homomorphisms, transgression}
Recall the Serre spectral sequence for a fibration $F\to E\to B$ has $E^2$-page
given by
$$
E^2_{s,t} = H_s(B;H_t(F)) \Rightarrow H_{s+t}(E).
$$
If $B$ is path-connected, $\widetilde{H}_t(F) = 0$ for $t<q$,
$\widetilde{H}_s(B) = 0$ for $s<p$, and $\pi_1(B)$ acts trivially on
$H_\ast(F)$, we showed that there is a long exact sequence (the Serre exact
sequence)
\begin{equation}\label{serre-exact}
    H_{p+q-1}(F)\xrightarrow{\bullet} H_{p+q-1}(E)\to H_{p+q-1}(B)\to
    H_{p+q-2}(F)\to\cdots
\end{equation}
Let us attempt to describe the arrow marked by $\bullet$. %If $t>q$, we know

Let $(E^r_{p,q},d^r)$ be any spectral sequence such that $E^r_{p,q} = 0$ if
$p<0$ or $q<0$; such a spectral sequence is called a \emph{first quadrant}
spectral sequence. The Serre spectral sequence is a first quadrant spectral
sequence. In a first quadrant spectral sequence, the $d^2$-differential
$d^2:E^2_{0,t}\to E^2_{-2,t+1}$ is zero, since $E^2_{s,t}$ vanishes for $s<0$.
This means that $H_t(F) = H_0(B;H_t(F)) = E^2_{0,t}$ surjects onto $E^3_{0,t}$.
Arguing similarly, this surjects onto $E^4_{0,t}$. Eventually, we find that
$E^{r}_{0,t} \simeq E^{t+2}_{0,t}$ for $r\geq t+2$.
In particular,
$$E^{t+2}_{0,t} \simeq E^\infty_{0,t} \simeq \gr_0 H_t(E) \simeq F_0 H_t(E),$$
which sits inside $H_t(E)$. The composite
$$E^2_{0,t} = H_t(F) \to E^3_{0,t}\to \cdots\to E^{t+2}_{0,t} \subseteq F_0
H_t(E)\to H_t(E)$$
is precisely the map $\bullet$! Such a map is known as an \emph{edge
homomorphism}.

The map $F\to E$ is the inclusion of the fiber; it induces a map $H_t(F)\to
H_t(E)$ on homology. We claim that this agrees with $\bullet$.
%We almost saw this in the construction of a sseq for a filtered complex.
Recall that $F_0H_t(E)$ is defined to be $\img(H_t(F_0 E) \to H_t(E))$. In the
construction of the Serre spectral sequence, we declared that $F_0 E$ is
exactly the preimage of the zero skeleton. Since $B$ is simply connected, we
find that $F_0 E$ is exactly the fiber $F$.

To conclude the proof of the claim, consider the following diagram:
\begin{equation*}
    \xymatrix{
	F\ar[r]\ar[d] & F\ar[d]\\
	F\ar[r]\ar[d] & E\ar[d]\\
	\ast\ar@{^(->}[r] & B
    }
\end{equation*}
The naturality of the Serre spectral sequence implies that there is an induced
map of spectral sequences. Tracing through the symbols, we find that this
observation proves our claim.

The long exact sequence \eqref{serre-exact} also contains a map $H_s(E)\to
H_s(B)$. The group $F_s H_s(E) = H_s(E)$ maps onto $\gr_s H_s(E) \simeq
E^\infty_{s,0}$. If $F$ is connected, then $H_s(B) = H_s(B;H_0(F)) =
E^2_{s,0}$. Again, the $d^2$-differential $d^2:E^2_{s+2,-1}\to E^2_{s,0}$ is
trivial (since the source is zero). Since $E^3 = \ker d^2$, we have an
injection $E^3_{s,0} \to E^2_{s,0}$. Repeating the same argument, we get
injections
$$E^\infty_{s,0} = E^{s+1}_{s,0}\to \cdots\to E^2_{s,0}\to E^2_{s,0} =
H_s(B).$$
Composing with the map $H_s(E)\to E^\infty_{s,0}$ gives the desired map $H_s(E)
\to H_s(B)$ in the Serre exact sequence. This composite is also known as an
edge homomorphism.

As above, this edge homomorphism is the map induced by $E\to B$. This can be
proved by looking at the induced map of spectral sequences coming from the
following map of fiber sequences:
\begin{equation*}
    \xymatrix{
	F\ar[r]\ar[d] & \ast\ar[d]\\
	E\ar[r]\ar[d] & B\ar[d]\\
	B\ar[r] & B
    }
\end{equation*}

The topologically mysterious map is the boundary map $\partial:H_{p+q-1}(B)\to
H_{p+q-2}(F)$. Such a map is called a \emph{transgression}. Again, let
$(E^r_{s,t},d^r)$ be a first quadrant spectral sequence. In our case,
$E^2_{n,0} = H_n(B)$, at least $F$ is connected. As above, we have injections
$$i:E^n_{n,0} \to \cdots\to E^3_{n,0} \to E^2_{n,0} = H_n(B).$$
Similarly, we have surjections
$$s:E^2_{0,n-1}\to E^3_{0,n-1}\to \cdots\to E^n_{0,n-1}.$$
There is a differential $d^n:E^n_{n,0}\to E^n_{0,n-1}$. The transgression is
defined as the \emph{linear relation} (not a function!) $E^2_{n,0}\to
E^2_{0,n-1}$ given by
$$x\mapsto i^{-1} d^n s^{-1}(x).$$
However, the reader should check that in our case, the transgression is indeed
a well-defined function.

Topologically, what is the origin of the transgression? There is a map
$H_n(E,F)\xrightarrow{\pi_\ast} H_n(B,\ast)$, as well as a boundary map
$\partial : H_n(E,F) \to H_{n-1}(F)$. We claim that:
$$\img \pi_\ast = \img(E^n_{n,0}\to H_n(B) = E^2_{n,0}),\quad
\partial\ker\pi_\ast = \ker(H_{n-1}(F) = E^2_{0,n-1} \to E^n_{0,n-1}).$$
\begin{proof}[Proof sketch]
    Let $x\in H_n(B)$. Represent it by a cycle $c\in Z_n(B)$. Lift it to a
    chain in the total space $E$. In general, this chain will not be a cycle
    (consider the Hopf fibration). The differentials record this boundary; let
    us recall the geometric construction of the differential. Saying that the
    class $x$ survives to the $E^n$-page is the same as saying that we can find
    a lift to a chain $\sigma$ in $E$, with $d\sigma\in S_{n-1}(F)$. Then
    $d^n(x)$ is represented by the class $[dc]\in H_{n-1}(F)$. This is
    precisely the trangression.

    Informally, we lift something from $H_n(B)$ to $S_n(E)$; this is
    well-defined up to something in $F$. In particular, we get an element in
    $H_n(E,F)$. We send it, via $\partial$, to an element of $H_{n-1}(F)$ ---
    and this is precisely the transgression.
\end{proof}
\subsection{An example}
We would like to compare the Serre exact sequence \eqref{serre-exact} with the
homotopy exact sequence:
$$\ast\to \pi_{p+q-1}(F)\to \pi_{p+q-1}(E)\to \pi_{p+q-1}(B)\xar{\partial}
\pi_{p+q-2}(F)\to \cdots$$
There are Hurewicz maps $\pi_{p+q-1}(X)\to H_{p+q-1}(X)$. We claim that there
is a map of exact sequences between these two long exact sequences.
\begin{equation*}
    \xymatrix{
	H_{p+q-1}(E) \ar[r]^{\pi_\ast} & H_{p+q-1}(B)\ar[r]_\partial &
	H_{p+q-2}(F)\ar[r] & \cdots\\
	\pi_{p+q-1}(E)\ar[r]_{\pi_\ast}\ar[u]_{h} &
	\pi_{p+q-1}(B)\ar[u]^h\ar[r] & \pi_{p+q-2}(F)\ar[r]\ar[u]^h &
	\cdots\\
    }
\end{equation*}
The leftmost square commutes by naturality of Hurewicz. The commutativity of
the righmost square is not immediately obvious. For this, let us draw in the
explicit maps in the above diagram:
\begin{equation*}
    \xymatrix{
	& & H_{p+q-1}(E,F)\ar[dl]\ar[dr] & &\\
	H_{p+q-1}(E) \ar[r]^{\pi_\ast} & H_{p+q-1}(B)\ar[rr]_\partial & &
	H_{p+q-2}(F)\ar[r] & \cdots\\
	\pi_{p+q-1}(E)\ar[r]_{\pi_\ast}\ar[dr]\ar[u]_{h} &
	\pi_{p+q-1}(B)\ar[u]^h\ar[rr] & & \pi_{p+q-2}(F)\ar[r]\ar[u]^h &
	\cdots\\
	& \pi_{p+q-1}(E,F)\ar[uuur]\ar[urr]\ar[u]^\cong_{s} & &
    }
\end{equation*}
The map marked $s$ is an isomorphism (and provides the long arrow in the above
diagram, which makes the square commute), since
$$
\pi_n(E,F) = \pi_{n-1}(\mathrm{hofib}(F\to E)) = \pi_{n-1}(\Omega B) =
\pi_n(B).
$$
Let us now specialize to the case of the fibration
$$\Omega X\to PX\to X.$$
Assume that $X$ is connected, and $\ast \in X$ is a chosen basepoint. Let
$p\geq 2$, and suppose that $\widetilde{H}_s(X) = 0$ for $s<p$. Arguing as in
\S \ref{loops-sn}, we learn that the Serre spectral sequence we know that the
homology of $\Omega X$ begins in dimension $p-1$ since $PX\simeq \ast$, so $q =
p-1$. Likewise, if we knew $\widetilde{H}_n(\Omega X) = 0$ for $n<p-1$, then
the same argument shows that $\widetilde{H}_n(X) = 0$ for $n<p$.
\subsection*{A surprise gust: the Hurewicz theorem}
The discussion above gives a proof of the Hurewicz theorem; this argument is
due to Serre.
\begin{theorem}[Hurewicz, Serre's proof]
    Let $p\geq 1$. Suppose $X$ is a pointed space with $\pi_i(X) = 0$ for
    $i<p$. Then $\widetilde{H}_i(X) = 0$ for $i<p$ and $\pi_p(X)^{ab}\to
    H_p(X)$ is an isomorphism.
\end{theorem}
\begin{proof}
    Let us assume the case $p=1$. This is classical: it is Poincar\'{e}'s
    theorem. We will only use this result when $X$ is a loop space, in which
    case the fundamental group is already abelian.

    Let us prove this by induction, using the loop space fibration. By
    assumption, $\pi_i(\Omega X) = 0$ for $i<p-1$. By our inductive hypothesis,
    $\widetilde{H}_i(\Omega X) = 0$ for $i<p-1$, and $\pi_{p-1}(\Omega X)
    \xrightarrow{\simeq} H_{p-1}(\Omega X)$. By our discussion above, we learn
    that $\widetilde{H}_i(X) = 0$ for $i<p$. The Hurewicz map
    $\pi_p(X)\xrightarrow{h}H_p(X)$ fits into a commutative diagram:
    \begin{equation*}
	\xymatrix{
	    \pi_{p-1}(\Omega X)\ar[r]^\simeq & H_{p-1}(\Omega X)\\
	    \pi_p(X)\ar[u]^\simeq \ar[r]_h &
	    H_p(X)\ar[u]^{\simeq}_{\text{transgression}}
	    }
    \end{equation*}
    It follows from the Serre exact sequence that the transgression is an
    isomorphism.
\end{proof}
%This proof has an enormous advantage, since you can make modifications that modify all primes except for a single prime, or get rational information.
%In other words, it's amenable to localizations.
%On Monday we'll talk about Serre classes and get information about homotopy groups way beyond conductivity of the space, if you do it right.


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

\section*{Question 2}

Let $f : \mathbb{R}^2 \rightarrow \mathbb{R}$ be the function given in Equation \ref{eq21}.

\begin{equation}
f(x) = x_1^2 + 4x_2^2
\label{eq21}
\end{equation}

Let $S$ be the set of feasible solutions defined by the restrictions

\begin{equation}
\begin{aligned}
5x_1 + 8x_2 - 41 &\leqslant 0,\\
x_1 &\geqslant 0,\\
x_2 &\geqslant 0.\\
\end{aligned}
\label{eq22}
\end{equation}

Using Fritz John theorem determine the point of minimum for $f$.

\subsection*{Solution}

Figure \ref{fig21} depicts the feasible region $S$ for function $f: \mathbb{R}^2 \rightarrow \mathbb{R}$.

\begin{figure}[H]\centering
\begin{tikzpicture}
    \draw [thick] (1.708,0) coordinate (a_1) -- (0,2.733) coordinate (a_2);
    \path [fill=gray!20] (0,0) -- (a_2) to
        (a_1) -- (0,0);
    \draw [<->,thick] (0,3) node (yaxis) [above] {$y$} |- (3,0) node (xaxis) [right] {$x$};
    \fill[black] (a_1) circle (2pt) node[above right] {$(0, 5.125)$};
    \fill[black] (a_2) circle (2pt) node[right] {$(8.2, 0)$};
    \node at (0.5,1) {$S$};
\end{tikzpicture}
\caption{Feasible region $S$ defined by restrictions given in Eq. \ref{eq22}}\label{fig21}
\end{figure}

We define $g_i$ for $i \in \{1,2,3\}$ as follows.

\begin{equation}
\begin{aligned}
g_1(\mathbf{x}) &= 5x_1 + 8x_2 - 41\\
g_2(\mathbf{x}) &= x_1\\
g_3(\mathbf{x}) &= x_2\\
\end{aligned}
\label{eq23}
\end{equation}

Using Eq. \ref{eq21} and \ref{eq23}, $(\nabla f)_x$ and $(\nabla g_i)$ can be obtained as follows.

\begin{equation}
\begin{aligned}
(\nabla f)'_\mathbf{x} &= \begin{pmatrix} 2x_1 & 8x_2\end{pmatrix} \\
(\nabla g_1)'_\mathbf{x} &= \begin{pmatrix} 5 & 8\end{pmatrix} \\
(\nabla g_2)'_\mathbf{x} &= \begin{pmatrix} 1 & 0\end{pmatrix} \\
(\nabla g_3)'_\mathbf{x} &= \begin{pmatrix} 0 & 1\end{pmatrix} \\
\end{aligned}
\label{eq24}
\end{equation}

We claim $\mathbf{x} = (0,0)$ is an optimal point for function $f$.
To prove this claim, we first need to show Fritz John necessary condition is satisfied; i.e. $F(f,\mathbf{x})$ and $FD(S,\mathbf{x_0})$ are disjoint.

For $\mathbf{x} = (0,0)$, constraints $g_2$ and $g_3$ are binding.
We have $\mathbf{d} \in F(f,\mathbf{x}) \cap G(\mathbf{g}, \mathbf{x})$ if following conditions are satisfied, simultaneously.

\begin{equation}
\begin{aligned}
2x_1 d_1 + 8x_2 d_2 &< 0\\
x_1 d_1 &< 0\\
x_2 d_2 &< 0\\
\end{aligned}
\label{eq25}
\end{equation}

However, since $x_1 = x_2 = 0$, none of the conditions in Eq. \ref{eq25} are satisfied and therefore, $F(f,\mathbf{x}) \cap G(\mathbf{g}, \mathbf{x}) = \emptyset$ for $x = (0,0)$.
Therefore we can continue with finding scalars $u_0$ and $u_i$ such that $u_0(\nabla f)_\mathbf{x} + \Sigma_{i=1}^{3} u_i (\nabla g_i)_\mathbf{x} = \mathbf{0}$.
Using Eq. \ref{eq24}, this condition can be rewritten as follows.

\begin{equation}
u_0 \begin{pmatrix} 2x_1\\ 8x_2\\ \end{pmatrix} + u_2 \begin{pmatrix} 1\\ 0\end{pmatrix} + u_3 \begin{pmatrix} 0\\ 1\end{pmatrix} = \begin{pmatrix}
0 \\ 0 \end{pmatrix}
\label{eq26}
\end{equation}

For Eq. \ref{eq26} to hold for $x = (0, 0)$, we should have $u_i = 0$ for all $i\in \{1,2,3\}$ and $u_0$ can be chosen as any positive number.
Since there is at least one positive scalar, all necessary conditions are satisfied for $\mathbf{x} = (0, 0)$ and $\mathbf{x}$ is proven to be the point of minimum for $f$.

\subsection*{Side Note}

In case the restrictions given in \ref{eq22} are changed to the following, the point of minimum of $f$ would be $\mathbf{x} = (5,2)$.

\begin{equation}
\begin{aligned}
5x_1 + 8x_2 - 41 &\geqslant 0,\\
x_1 &\geqslant 0,\\
x_2 &\geqslant 0.\\
\end{aligned}
\label{eq27}
\end{equation}

Choosing $\mathbf{x} = (5,2)$ will change the set of binding constraints to $\{g_1\}$.
Therefore, \ref{eq25} will change to Eq. \ref{eq28}.
In this case, $F(f,\mathbf{x}) \cap G(\mathbf{g}, \mathbf{x}) = \emptyset$ again, since no $d_1$ and $d_2$ exist to satisfy the following conditions simultaneously.

\begin{equation}
\begin{aligned}
10d_1 + 16d_2 &< 0,\\
5d_1 + 8d_2 &> 0\\
\end{aligned}
\label{eq28}
\end{equation}

Therefore, by showing there are $u_0$ and $u_i$ such that $u_0(\nabla f)_\mathbf{x} + \Sigma_{i=1}^{3} u_i (\nabla g_i)_\mathbf{x} = \mathbf{0}$, we can prove $\mathbf{x}(5,2)$ is really an optimal point.
The former is easy to show, using Eq. \ref{eq29}.

\begin{equation}
u_0 \begin{pmatrix} 2x_1\\ 8x_2\\ \end{pmatrix} + u_1 \begin{pmatrix} 5\\ 8\end{pmatrix} = \begin{pmatrix}
0 \\ 0 \end{pmatrix}
\label{eq29}
\end{equation}

And replacing $x_1 = 5$, $x_2 = 2$, Eq. \ref{eq29} holds by having $u_0 = u_1 = 0$ and choosing $u_2$ and $u_3$ as any arbitrary positive number.


\section{Conditional Expectation}
\begin{definition}
    Let $B$ be an event such that $\mathbb P(B)>0$ and let $X$ be a random variable, then we define
    $$\mathbb E[X|B]=\frac{\mathbb E[X\cdot 1_B]}{\mathbb P(B)}$$
\end{definition}
\begin{proposition}[Law of Total Expectation]
    Let $\Omega_n$ be a sequence of events that partitions $\Omega$, then
    $$\mathbb E[X]=\sum_n\mathbb E[X|\Omega_n]\mathbb P(\Omega_n)$$
\end{proposition}
\begin{proof}
    Write $X=\sum_nX\cdot1_{\Omega_n}$.
\end{proof}
\begin{definition}
    Let $X_1,\ldots,X_n$ be random variables, then the joint distribution is $\mathbb P(X_1=x_1,\ldots,X_n=x_n),x_i\in\omega_{X_i}$.
    Given such a joint distribution, the marginal distribution of $X_i$ is defined as
    $$\mathbb P(X_i=x_i)=\sum_{j\neq i,x_j\in\Omega_{X_j}}\mathbb P(X_1=x_1,\ldots,X_n=x_n)$$
\end{definition}
\begin{definition}
    Let $X,Y$ be random variables, then the conditional probability given $Y=y,y\in\Omega_Y$ is defined by
    $$\mathbb P(X=x|Y=y)=\mathbb P(X=x,Y=y)/\mathbb P(Y=y)$$
\end{definition}
Immediately
$$\mathbb P(X=x)=\sum_{y\in\Omega_Y}\mathbb P(X=x,Y=y)=\sum_{y\in\Omega_Y}\mathbb P(X=x|Y=y)\mathbb P(Y=y)$$
Note that if $X,Y$ are independent, then
\begin{align*}
    \mathbb P(X+Y=z)&=\sum_{y\in\Omega_Y}\mathbb P(X+Y=z|Y=y)\mathbb P(Y=y)\\
    &=\sum_{y\in\Omega_Y}\mathbb P(X=z-y|Y=y)\mathbb P(Y=y)\\
    &=\sum_{y\in\Omega_Y}\mathbb P(X=z-y)\mathbb P(Y=y)
\end{align*}
Similarly
$$\mathbb P(X+Y=z)=\sum_{x\in\Omega_X}\mathbb P(X=x)\mathbb P(Y=z-x)$$
\begin{example}
    Let $X\sim\operatorname{Pois}(\lambda),Y\sim\operatorname{Pois}(\mu)$, and $X,Y$ are independent, then
    $$\mathbb P(X+Y=n)=\sum_{r=0}^\infty \mathbb P(X=r)\mathbb P(X=n-r)=e^{-(\lambda+\mu)}\frac{(\lambda+\mu)^n}{n!}$$
\end{example}
\begin{definition}
    Let $X,Y$ be random variables,
    The conditional expectation of $X$ given $Y=y$ is defined to be the expectation of the conditional distribution of $X$ given $Y=y$, so
    $$\mathbb E[X|Y=y]=\frac{\mathbb E[X\cdot 1_{Y=y}]}{\mathbb P(Y=y)}=\sum_{x\in\Omega_X}x\mathbb P(X=x|Y=y)$$
\end{definition}
Note that $g(y)=\mathbb E[X|Y=y]$ is a function in $\Omega_Y\to\mathbb R$.
\begin{definition}
    The conditional expectation of $X$ given $Y$ is defined to be $\mathbb E[X|Y]=g(Y)$ which is a random variable as a function of $Y$.
\end{definition}
So obviously,
\begin{align*}
    \mathbb E[X|Y]&=g(Y)\\
    &=\sum_{y\in\Omega_Y}g(Y)\mathbb P(1_{Y=y})\\
    &=\sum_{y\in\Omega_Y}1_{Y=y}g(y)\\
    &=\sum_{y\in\Omega_Y}1_{Y=y}\mathbb E[X|Y=y]
\end{align*}
\begin{example}
    Toss a $p$-coin several times independently, so they produce a sequence of random variables $(X_i)_{i\in\mathbb N}\sim\operatorname{Bern}(p)$ and the number of heads are distributed in $Y_m=X_1+X_2+\cdots+X_m\sim\operatorname{Bin}(m,p)$
    We want to calculate $\mathbb E[X_i|Y_m]$.
    Note that $\mathbb E[X_i|Y_m=r]=\mathbb P(X_i=1|Y_m=r)=r/m$, which means
    $$\mathbb E[X_i|Y_m]=\frac{Y_m}{m}$$
\end{example}
\begin{proposition}
    1. $\forall c\in\mathbb R,\mathbb E[cX|Y]=c\mathbb E[X|Y],\mathbb E[c|Y]=c$.\\
    2.
    $$\mathbb E\left[\sum_{i=1}^nX_i\middle|Y\right]=\sum_{i=1}^n\mathbb E[X_i|Y]$$
    3. $\mathbb E[\mathbb E[X|Y]]=\mathbb E[X]$.
\end{proposition}
\begin{proof}
    1 and 2 are obvious.
    For 3,
    \begin{align*}
        \mathbb E[\mathbb E[X|Y]]&=\sum_{y\in\Omega_Y}\mathbb P(Y=y)\mathbb E[X|Y=y]\\
        &=\sum_{y\in\Omega_Y}\sum_{x\in\Omega_X}x\mathbb P(X=x|Y=y)\mathbb P(Y=y)\\
        &=\sum_{x\in\Omega_X}x\mathbb P(X=x)\\
        &=\mathbb E[X]
    \end{align*}
    As desired.
\end{proof}
Intuitively we also have the following:
\begin{proposition}\label{conditional_exp}
    1. If $X,Y$ are independent, then $\mathbb E[X|Y]=\mathbb E[X]$ which is constant.\\
    2. Suppose $Y,Z$ are independent, then $\mathbb E[\mathbb E[X|Y]|Z]=\mathbb E[X]$.\\
    3. Let $h:\mathbb R\to\mathbb R$, then $\mathbb E[h(Y)X|Y]=h(Y)\mathbb E[X|Y]$.
\end{proposition}
\begin{proof}
    1 is trivial.\\
    For 2, we have $\mathbb E[X|Y]$ is independent of $Z$, so $\mathbb E[\mathbb E[X|Y]|Z]=\mathbb E[\mathbb E[X|Y]]=\mathbb E[X]$ by 1.\\
    As for 3, we have $\mathbb E[h(Y)X|Y=y]=h(y)\mathbb E[X|Y=y]$, the identity follows.
\end{proof}
\begin{corollary}
    $\mathbb E[\mathbb E[X|Y]|Y]=\mathbb E[X|Y]$
\end{corollary}
\begin{proof}
    Take $h(y)=\mathbb E[X|Y=y]$ in Proposition \ref{conditional_exp}.3.
\end{proof}


\section*{Integration schemes}
Euler first order explicit
\begin{align}\label{eqn:euler}
    \begin{split}
        &v(t+\Delta{t}) = v(t) + a(t) * \Delta{t}\\
        &x(t+\Delta{t}) = x(t) + v(t) * \Delta{t}
    \end{split}
\end{align}
\par
The central difference scheme, also known as velocity Verlet is a very widely
used integration method of second order. This was initially proposed by Cundall
and Strack (1979) and adopted by several other authors. Velocities in the
upcoming time step $t + \Delta{t}/2 $ and positions at $t + \Delta{t}$ are
calculated as
\begin{align}\label{eqn:verlet}
    \begin{split}
        &v(t + \Delta{t}/2) = v(t + \Delta{t}/2) +a(t - \Delta{t}/2) * \Delta{t}\\
        &x(t + \Delta{t}) = x(t) +v(t + \Delta{t}/2) * \Delta{t}\\
    \end{split}
\end{align}
\par
predictor–corrector schemes very commonly used for molecular dynamics and
discrete element applications are Gear’s schemes. They are based on three
stages, whereas in addition to the predictor and corrector step known from the
Adams-method an evaluation step is added. Schemes considered here are the third
order Gear’s method (GPC3) and the fourth order Gear’s method (GPC4). In the
prediction step positions and their higher derivatives are calculated based on
Taylor series expansions as
\begin{equation}\label{eqn:gearP}
    \begin{split}
        c(t+\Delta{t}, p) =& c(t)\\
        b(t+\Delta{t}, p) =& b(t) + c(t) *\Delta{t}\\
        a(t+\Delta{t}, p) =& a(t) + b(t) *\Delta{t}^2 + \frac{1}{6} * c(t) *\Delta{t}^2\\
        v(t+\Delta{t}, p) =& v(t) + a(t) *\Delta{t} + \frac{1}{2} * b(t) *\Delta{t}^2 +\\
        &\frac{1}{6} * c(t) *\Delta{t}^3\\
        x(t+\Delta{t}, p) =& x(t) + v(t) * \Delta{t} + \frac{1}{2} * a(t) *\Delta{t}^2 +\\
        &\frac{1}{6} * b(t) *\Delta{t}^3 + \frac{1}{24} * c(t) *\Delta{t}^4\\
    \end{split}
\end{equation}
with the first and second derivative of the accelerations calculated as\par
for GPC3:
\begin{align}\label{eqn:gearJerk3}
    \begin{split}
        &b(t) = \frac{\Delta{a(t)}}{\Delta{t}}\\
        \\
        &c(t) = 0\\
    \end{split}
\end{align}\par
for GPC4:
\begin{align}\label{eqn:gearJerk4}
    \begin{split}
        &b(t) = \frac{\Delta{a(t)}}{\Delta{t}}\\
        \\
        &c(t) = \frac{\Delta{b(t)}}{\Delta{t}}\\
    \end{split}
\end{align}
\par
In the evaluation step the difference in the accelerations calculated based on
the acceleration $a(t+ \Delta{t}, p)$ and the acceleration $a(t+\Delta{t})$
calculated from positions $x(t+\Delta{t}, p)$ and velocities $v(t+\Delta{t}, p)$
is obtained by
\begin{align}\label{eqn:gearDa}
    \Delta{a} = a(t + \Delta{t}) - a(t + \Delta{t}, p)
\end{align}
\par
In the following, correction step positions and their higher derivatives are
calculated based on their values from the previous time step and the obtained
difference in acceleration as
\begin{align}\label{eqn:gearCorrector}
    \begin{split}
        &x(t+\Delta{t}) = x(t+\Delta{t}, p) + k1 * \Delta{a} * \Delta{t}^2\\
        &v(t+\Delta{t}) = v(t+\Delta{t}, p) + k2 * \Delta{a} * \Delta{t}\\
        &a(t+\Delta{t}) = a(t+\Delta{t}, p) + k3 * \Delta{a}\\
        &b(t+\Delta{t}) = b(t+\Delta{t}, p) + k4 * \frac{\Delta{a}}{\Delta{t}}\\
        &c(t+\Delta{t}) = c(t+\Delta{t}, p) + k5 * \frac{\Delta{a}}{\Delta{t}^2}\\
    \end{split}
\end{align}
\par
Gear’s scheme parameters k1-k5\par
for GPC3:
$k_1 = 1/12, k_2 = 5/12, k_3 = 1, k_4 = 1, k_5 = 0$\par
for GPC4:
$k_1 = 19/240, k_2 = 3/8, k_3 = 1, k_4 = 3/2, k_5 = 1$
\par
\newpage


\section{Merge Classification}

After generating the list of potential merge locations we extract a cubic region of interest around each.
We experimented with three different cube side lengths: $800 \textrm{nm}$, $1200 \textrm{nm}$, and $1600 \textrm{nm}$.
Side lengths of $1200 \textrm{nm}$ performed better on the training and validation data on our network.

\vspace{1cm}

\noindent
\textbf{Network Architecture}
We tested various architectures before deciding on the one we present in the paper.
Each architecture uses VGG-style blocks (i.e. two convolutions of size $3\times3\times3$ followed by a max-pooling layer)~\cite{chatfield2014return}.
For the first two layers the max-pooling is anisotropic with reduction only in the $x$ and $y$ dimensions.
The number of filters starts at 16 for the first block and doubles in each subsequent block.
Our architectures vary in the input size and number of layers.
In all instances we extract a \SI{1200}{\nano\meter^3} region of interest and map the extracted voxels to the given input size.
These input sizes correspond to specific output sizes from the last VGG block.
For training we use the mean squared error loss function and optimize with SGD with Nesterov momentum.

\begin{table}
	\scriptsize
	\centering
	\begin{tabular}{c c c c c c c}
		\hline
		\textbf{Depth} & \textbf{Input Size} & \textbf{No. Parameters} & \textbf{Output Size} & \textbf{Accuracy} & \textbf{Precision} & \textbf{Recall} \\ \hline
		      3        & (3, 18, 52, 52)     & 1,101,553               & (64, 3, 3, 3)        & 91.30             & 58.06              & 92.81 \\
		      3        & (3, 20, 60, 60)     & 2,313,969               & (64, 4, 4, 4)        & 92.41             & 61.70              & 92.41 \\
		      3        & (3, 22, 68, 68)     & 4,312,817               & (64, 5, 5, 5)        & 92.33             & 61.49              & 92.34 \\
		      3        & (3, 24, 76, 76)     & 7,294,705               & (64, 6, 6, 6)        & 93.51             & 65.78              & 93.13 \\
		      \textbf{3}        & \textbf{(3, 26, 84, 84)}     & \textbf{11,456,241}              & \textbf{(64, 7, 7, 7)}        & \textbf{95.38}             & \textbf{74.43}              & \textbf{92.34} \\
		      3        & (3, 28, 92, 92)     & 16,994,033              & (64, 8, 8, 8)        & 91.87             & 59.70              & 94.22 \\
		      3        & (3, 30, 100, 100)   & 24,104,689              & (64, 9, 9, 9)        & 92.01             & 60.24              & 93.75 \\
		      4        & (3, 28, 92, 92)     & 1,404,913               & (128, 2, 2, 2)       & 91.70             & 60.24             & 85.94 \\
		      4        & (3, 32, 108, 108)   & 2,650,097               & (128, 3, 3, 3)       & 92.80             & 64.28              & 86.88 \\\hline
	\end{tabular}
	\caption{The results of various network architectures trained on the Kasthuri data.}
	\label{table:input-size}
\end{table}

\noindent
\textbf{Inference Augmentation}
Augmenting the data for inference increases the precision by 3.79\% and the accuracy by 0.64\%.
Figure~\ref{fig:test-augmentation} shows the changes in the number of true positives, false positives, and false negatives as a function of the number of augmentations on the Kasthuri dataset.
The number of false positives decreases at first, falling below 200 after a second augmentation.
The gains become more gradual with the number of augmentations, leveling out around 175 false positives after 5 augmentations.

\begin{figure}[t]
	\centering
	\includegraphics[width=0.45\linewidth]{./figures/Kasthuri-test-augmentation.png}
	\caption{The number of false positives decreases with our data augmentation scheme. The benefit of augmentation gradually decreases with the number of random examples.}
	\label{fig:test-augmentation}
\end{figure}


We include some examples to demonstrate some of the uses of this package.

\section{Distinguishing groups}~

\begin{example}[Payne\_Grps]

We can use these functions to build groups from bilinear maps and distinguish
seemingly indistinguishable groups. In 2004, S. E. Payne asked if two elation
groups were isomorphic but suspected they were not \cite{Payne:elation-grps}.

The first group, $G_f$, is the elation group of the generalized quadrangle
$H(3,q^2)$, the Hermitian geometry. This group is defined as a Heisenberg group
whose bilinear map is the usual dot product.

\begin{code}
> p := 3;
> e := 4;
> q := p^e; // q = 3^e >= 27
> F := [KSpace(GF(q),2), KSpace(GF(q),2), KSpace(GF(q),1)];
>
> DotProd := function(x)
function>   return KSpace(GF(q),1)!(x[1]*Matrix(2,1,x[2]));
function> end function;
>
> DoubleForm := function(T)
function>   F := SystemOfForms(T)[1];
function>   K := BaseRing(F);
function>   n := Nrows(F);
function>   m := Ncols(F);
function>   MS := KMatrixSpace(K,n,m);
function>   Z := MS!0;
function>   M1 := HorizontalJoin(Z,-Transpose(F));
function>   M2 := HorizontalJoin(F,Z);
function>   D := VerticalJoin( M1, M2 );
function>   return Tensor( D, 2, 1 );
function> end function;
>
> f := DoubleForm( Tensor( F, DotProd ) );
> f;
Tensor of valence 2, U2 x U1 >-> U0
U2 : Full Vector space of degree 4 over GF(3^4)
U1 : Full Vector space of degree 4 over GF(3^4)
U0 : Full Vector space of degree 1 over GF(3^4)
>
> IsAlternating(f);
true
> Gf := HeisenbergGroup(f);
\end{code}

Now we define Payne's second group, $G_{\bar{f}}$, which is the elation group of
the Roman quadrangle with parameters $(q^2,q)$. In this example, $\bar{f}$ is a
biadditive map, but is bilinear over the prime field $\mathbb{F}_3$. Therefore,
we construct a vector space isomorphism from $\mathbb{F}_3^e$ to
$\mathbb{F}_{3^e}$ and the bilinear commutator map, induced by $\bar{f}$. Hence,
$G_{\bar{f}}$ is the Heisenberg group of this bilinear commutator map.

\begin{code}
> n := PrimitiveElement(GF(q)); // non-square
> MS := KMatrixSpace(GF(q),2,2);
> A := MS![-1,0,0,n];
> B := MS![0,1,1,0];
> C := MS![0,0,0,n^-1];
> F1 := Frame(f);
> F2 := [KSpace(GF(p),4*e), KSpace(GF(p),4*e),\
>   KSpace(GF(p),e)];
>
> // take 1/3^r root
> Root := function(v,r)
function>   k := Eltseq(v)[1];
function>   K := Parent(k);
function>   if k eq K!0 then return k; end if;
function>   R<x> := PolynomialRing(K);
function>   f := Factorization(x^(3^r)-k)[1][1];
function>   return K!(x-f);
function> end function;
>
> // biadditive map defining elation grp
> RomanGQ := function(x)
function>   u := Matrix(1,2,x[1]);
function>   v := Matrix(2,1,x[2]);
function>   M := [A,B,C];
function>   f := &+[Root(u*M[i]*v,i-1) : i in [1..3]];
function>   return KSpace(GF(q),1)![f];
function> end function;
>
> // vector space isomorphisms
> phi := map< F2[1] -> F1[1] | \
>   x :-> F1[1]![ GF(q)![ s : s in Eltseq(x)[i+1..e+i] ] : \
>     i in [0,e,2*e,3*e] ] >;
> gamma := map< F1[3] -> F2[3] | \
>   x :-> F2[3]!&cat[ Eltseq(s) : s in Eltseq(x) ] >;
>
> // bilinear commutator from RomanGQ
> RomanGQComm := function(x)
function>   x1 := Eltseq(x[1]@phi)[1..2];
function>   x2 := Eltseq(x[1]@phi)[3..4];
function>   y1 := Eltseq(x[2]@phi)[1..2];
function>   y2 := Eltseq(x[2]@phi)[3..4];
function>   comm := RomanGQ( <x2,y1> ) - RomanGQ( <y2,x1> );
function>   return comm @ gamma;
function> end function;
>
> f_bar := Tensor( F2, RomanGQComm );
> f_bar;
Tensor of valence 2, U2 x U1 >-> U0
U2 : Full Vector space of degree 16 over GF(3)
U1 : Full Vector space of degree 16 over GF(3)
U0 : Full Vector space of degree 4 over GF(3)
>
> IsAlternating(f_bar);
true
> Gfb := HeisenbergGroup(f_bar);
\end{code}

The groups $G_f$ and $G_{\bar{f}}$ have order $3^{20}$ and are class 2, exponent
3, and minimally generated by 16 elements. In other words, the groups $G_f$ and
$G_{\bar{f}}$ are central extensions of $\mathbb{Z}_3^{16}$ by
$\mathbb{Z}_3^{4}$ and have exponent $3$. Using standard heuristics, these
groups are indistinguishable. However, the invariants associated to their
exponent-$p$ central tensor are vastly different, and thus, they determine that
these groups are non-isomorphic. We show that the centroids of the tensors are
not isomorphic.

\begin{code}
> Tf := pCentralTensor(Gf,1,1);
> Tf;
Tensor of valence 2, U2 x U1 >-> U0
U2 : Full Vector space of degree 16 over GF(3)
U1 : Full Vector space of degree 16 over GF(3)
U0 : Full Vector space of degree 4 over GF(3)
>
> Tfb := pCentralTensor(Gfb,1,1);
> Tfb;
Tensor of valence 2, U2 x U1 >-> U0
U2 : Full Vector space of degree 16 over GF(3)
U1 : Full Vector space of degree 16 over GF(3)
U0 : Full Vector space of degree 4 over GF(3)
>
> Cf := Centroid(Tf);
> Cfb := Centroid(Tfb);
> Dimension(Cf) eq Dimension(Cfb);
false
\end{code}
\end{example}

\section{Simplifying automorphism group computations}~

\begin{example}[Ext\_Over\_Adj] We demonstrate how to to simplify the
automorphism group computation as discussed in \cite{BW:grps-tensor}. We
construct a class 2, exponent $p$, $p$-group $G$ which is a quotient of a
maximal unipotent subgroup of $\text{GL}(3,317^4)$.

\begin{code}
> p := 317;
> e := 4;
> H := ClassicalSylow( GL(3,p^e), p );
> U := UnipotentMatrixGroup(H);
> P := PCPresentation(U);
> Z := Center(P);
>
> N := sub< P | >;
> while #N lt p^2 do
while>   N := sub< P | Random(Z), N >;
while> end while;
>
> G := P/N;
> G;
GrpPC : G of order 10246902931634286779441449 = 317^10
PC-Relations:
    G.5^G.1 = G.5 * G.9^62 * G.10^133,
    G.5^G.2 = G.5 * G.9^312 * G.10^295,
    G.5^G.3 = G.5 * G.9^316,
    G.5^G.4 = G.5 * G.10^316,
    G.6^G.1 = G.6 * G.9^312 * G.10^295,
    G.6^G.2 = G.6 * G.9^316,
    G.6^G.3 = G.6 * G.10^316,
    G.6^G.4 = G.6 * G.9^138 * G.10^163,
    G.7^G.1 = G.7 * G.9^316,
    G.7^G.2 = G.7 * G.10^316,
    G.7^G.3 = G.7 * G.9^138 * G.10^163,
    G.7^G.4 = G.7 * G.9^188 * G.10^50,
    G.8^G.1 = G.8 * G.10^316,
    G.8^G.2 = G.8 * G.9^138 * G.10^163,
    G.8^G.3 = G.8 * G.9^188 * G.10^50,
    G.8^G.4 = G.8 * G.9^125 * G.10^151
\end{code}

We construct the exponent-$p$ central tensor of $G$ and compute its adjoint $*$-algebra $A$.

\begin{code}
> T := pCentralTensor(G,1,1);
> T;
Tensor of valence 2, U2 x U1 >-> U0
U2 : Full Vector space of degree 8 over GF(317)
U1 : Full Vector space of degree 8 over GF(317)
U0 : Full Vector space of degree 2 over GF(317)
>
> A := AdjointAlgebra(T);
> Dimension(A);
16
> star := Star(A);
\end{code}

If $V=G/\Phi(G)$ is the Frattini quotient of $G$, then our goal is to get the
cotensor space $V\wedge_A V$. Note that $\dim V\wedge V=28$, so standard methods
will compute a stabilizer of $\text{GL}(8,317)$ inside $V\wedge V$. We will
decrease the size of the ambient space resulting in an easier stabilizer
computation.

\begin{code}
> V := Domain(T)[1];
> E := ExteriorCotensorSpace(V,2);
> E;
Cotensor space of dimension 28 over GF(317) with valence 1
U2 : Full Vector space of degree 8 over GF(317)
U1 : Full Vector space of degree 8 over GF(317)
\end{code}

Now we create a sub cotensor space $S$ generated by all $(e_iX)\wedge e_j - e_i\wedge (e_jX)$ for $X\in A$,
and then quotient $V\wedge V$ by $S$. The result is a 4 dimensional space.

\begin{code}
> L := [];
> for E_gen in Generators(E) do
for>   F := SystemOfForms(E_gen)[1];
for>   for X in Basis(A) do
for|for>     L cat:= [E!Eltseq(X*F - F*Transpose(X@star))];
for|for>   end for;
for> end for;
>
> S := SubTensorSpace(E,L);
> S;
Cotensor space of dimension 24 over GF(317) with valence 1
U2 : Full Vector space of degree 8 over GF(317)
U1 : Full Vector space of degree 8 over GF(317)
>
> Q := E/S;
> Q;
Cotensor space of dimension 4 over GF(317) with valence 1
U2 : Full Vector space of degree 8 over GF(317)
U1 : Full Vector space of degree 8 over GF(317)
\end{code}
\end{example}