% Data Mining HS 2017; Ondrej Skopek, Lukas Jendele
% This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
%
% Adapted from:
%
% 0. Lecutre slides; https://las.inf.ethz.ch/teaching/dm-f17
% 1. Taivo Pungas; https://github.com/taivop/eth-dm
% 2. Vincent Ulitzsch, et al.; https://github.com/viniul/Data-Mining-Cheat-Sheet-ETHZ
%    Copyright for template: \copyright\ 2014 Winston Chang
%    \href{http://www.stdout.org/~winston/latex/}{http://www.stdout.org/$\sim$winston/latex/}
%    Cheat Sheet By Vincent Ulitzsch and Mario Gersbach

\documentclass[11pt,landscape]{article}
%\usepackage{times}
\usepackage[scaled]{helvet}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{calc}
\usepackage{ifthen}
\usepackage[landscape]{geometry}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumitem}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}
\setlist{nolistsep}
\usepackage{titlesec}
\ifthenelse{\lengthtest { \paperwidth = 11in}}
    { \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
    {\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
        {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
        {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
    }

\algnewcommand\algorithmicforeach{\textbf{for each}}
\algdef{S}[FOR]{ForEach}[1]{\algorithmicforeach\ #1\ \algorithmicdo}

% Turn off header and footer
\pagestyle{empty}
\titlespacing{\section}{0pt}{\parskip}{-\parskip}

\definecolor{sectionColor}{HTML}{EE0000}
\definecolor{subsectionColor}{HTML}{FF7F00}
\definecolor{subsubsectionColor}{HTML}{EE6600}

% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
                                {-1ex plus -.5ex minus -.2ex}%
                                {0.5ex plus .2ex}%x
                                {\color{sectionColor}\normalfont\normalsize\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
                                {-1ex plus -.5ex minus -.2ex}%
                                {0.5ex plus .2ex}%
                                {\color{subsectionColor}\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
                                {-1ex plus -.5ex minus -.2ex}%
                                {1ex plus .2ex}%
                                {\color{subsubsectionColor}\normalfont\small\bfseries}}
\makeatother

% Define BibTeX command
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

% Don't print section numbers
\setcounter{secnumdepth}{0}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}

\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{0pt}

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

\begin{document}

\raggedright
\footnotesize
\begin{multicols}{3}

% multicol parameters
% These lengths are set only within the two main columns
%\setlength{\columnseprule}{0.25pt}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{0pt}
\setlength{\columnsep}{0.5pt}

\section{General Remarks}
\begin{tabular}{ l c  }
$\mathrm{d}(x, y) = ||x - y||$, & $\mathrm{dist}(x, y) = 1 - \mathrm{sim}(x, y)$\\
$l_2$-euclidean distance & $\sqrt{\sum_{i=1}^d (x_i-y_i)^2}$ \\
$l_1$-manhatten distance & $\sum_{i=1}^d \left|x_i-y_i\right|$\\
$l_p$-distance & $(\sum_{i=1}^d |x_i-y_i|^p)^{1/p}$ \\
$l_\infty$-distance & $\max_i | x_i - y_i| $ \\
Mahalanobis norm & $||w||^2_G = ||Gw||^2_2$ \\
Cosine-Similarity & $\cos \frac{x^T y}{||x||_2 ||y||_2}$ \\
Jaccard-Distance & $1-\text{sim}(A,B) = 1 - \frac{|A \cap B|}{|A \cup B|}$
\end{tabular}

\subsection{Function Properties}
\begin{description}
    \item[Concave function] $f(a + s) - f(a) \geq f(b + s) - f(b)~\forall a \leq b, s > 0$
    \item[Convex functions] A function $f: S \rightarrow \mathbb{R}$, $S \subseteq \mathbb{R}^d$,
        is called convex if $\forall x,x' \in S, \lambda \in [0,1]$ it holds that
    $\lambda f(x) + (1 - \lambda) f(x') \geq f(\lambda x + (1 - \lambda) x')$
    \item[H-strongly convex]
    $f(x') \geq f(x) + \nabla f(x)^T (x'-x) + \frac{H}{2} ||x' -x||^2, H > 0$\\
    1-D: $f$ is H-sc $\Leftarrow f''(x) \geq H, \forall x$.\\
    $d$-D: $f$ is H-sc $\Leftarrow$ $\lambda_{\min}(\nabla^2 f(x)) \geq H, \forall x$.
\item[Subgradients] Given a convex not necessarily differentiable function $f$, a subgradient $\mathbf{g_x} \in \nabla f(x)$ is the slope of a linear lower bound of $f$, tight at $x$, that is
    $\forall x' \in S \colon f(x') \geq f(x) + \mathbf{g_x}^T(x' - x)$
\end{description}

\section{Locality Sensitive Hashing}
    \begin{description}
        \item[Near-duplicate detection] $ \{ (x,y) \in X \times X : x \neq y, \text{d}(x,y) \leq \epsilon \}$
        \item[$(r,\epsilon)$-neighbour search]
        Find all points with distance $\leq r$ and no points with distance $> (1+ \epsilon)r$ from query q.
        Pick $(r,(1+\epsilon) \cdot r,p,q)$-sensitive family and boost.
        \item[Min-hashing] $h(C) = h_{\pi}(C) = \min_{i : C(i) = 1} \pi(i)$
            \subitem $\pi(i) = h_{a, b}(i) = ((a \cdot i + b) \mod p) \mod N)$,
        $p \text{ prime (fixed) } > N$, $N$ number of documents
        $(d_1, d_2, 1-d_1, 1-d_2)$-sensitive with Jaccard sim.
            \hrule
            \begin{algorithmic}[1]
\For {each column $c$}
    \For {each row $r$}
        \If {$c$ has 1 in row $r$}
            \For {each hash fn $h_i$}
                \State $M_{i,c} \gets \min\{h_i(r), M_{i,c}\}$
            \EndFor
        \EndIf
    \EndFor
\EndFor
\end{algorithmic}
        \item[Band-hashing] Signature matrix into $b$ bands of $r$ hash fns,
            per-column into $b$ hash tables. If any h-table has a collision,
            report candidate pair. $s^r$ prob of col on band $j$: $P(\text{col in } \geq 1 \text{ band}) = 1-(1-s^r)^b$
        \item[$(d1,d2,p1,p2)$-sensitivity] Assume $d_1<d_2$, $p_1>p_2$.
                $\forall x,y \in S: d(x,y) \leq d_1 \Rightarrow Pr[h(x)=h(y)] \geq p_1$\\
                $\forall x,y \in S: d(x,y) \geq d_2 \Rightarrow Pr[h(x)=h(y)] \leq p_2$
        \item[r-way AND] $h(x)=h(y) \iff \forall i : h_i(x)=h_i(y)$
            $(d_1,d_2,p_1^r,p_2^r)$ -- big r, more FN
        \item[b-way OR] $h(x)=h(y) \iff \exists i : h_i(x)=h_i(y)$
            $(d_1,d_2,1-(1-p_1)^b,1-(1-p_2)^b)$ -- big b, more FP
        \item[AND-OR cascade] $(d_1,d_2,1-(1-p_1^r)^b,1-(1-p_2^r)^b)$
        \item[OR-AND cascade] $(d_1,d_2,(1-(1-p_1)^b)^r,(1-(1-p_2)^b)^r)$
    \end{description}
\subsection{Hash Functions}
\begin{description}
    \item[Euclidean distance] $h_{w,b}(x) =  \lfloor (\frac{w^Tx-b}{a}) \rfloor$
where $w \leftarrow \frac{w}{||w||_2}$, $w \sim \mathcal{N}(0,I)$, $w_i \sim \mathcal{N}(0,1)$, $b \sim Unif([0,a])$, yields $(a/2,2a,1/2,1/3)$-sensitive
    \item[Cosine distance] $\mathcal{H} = \{ h(v) = \text{sgn}(w^Tv) \}$ where
        $w \sim \text{Unif} \{ x \in \mathbb{R}^n : ||x||_2 = 1 \}$
        $Pr(h_u(x) = h_v(y)) = 1 - \Theta_{x, y}/\pi$
        $(\Theta_1, \Theta_2, 1-\Theta_1 / \pi, 1-\Theta_2 / \pi)$-sensitive
\end{description}

\section{Support Vector Machines}
\begin{description}
    %\item[Linear Classifier] $y = \text{sign} \left ( w^T x +b \right )$. Train classifier $\sim$ find $w$.
    %Want: $y_i w^T x_i > 0~\forall i$ for linearly separable data.
    \item[SVM] SVM = Max margin linear classifier
    $\min_{w, \xi \geq 0} \frac{1}{2} w^T w + C \sum_{i=1}^{n} \xi_i \text{ s. t. } y_i w^T x_i \geq 1 - \xi_i~\forall i$
    Support vectors (SV) are all data points on the margin and data points with non-zero slack
        \item[Regularized hinge loss formulation] $C = 1/\lambda$
            $\min_w \lambda w^T w + C \sum_{i} \max(0,1-y_i w^T x_i)$
        \item[Norm-constrained hinge loss minimization]
            $ \min_w \sum_{i} \max(0,1-y_iw^Tx_i)$ s.t. $||w||_2 \leq \frac{1}{\sqrt{\lambda}}$
        \item[Strongly convex formulation]
            $ \min_w \frac{1}{T} \sum_{t=1}^{T} \left ( \frac{\lambda}{2} ||w||_2^2 + \max(0,1-y_t w^T x_t) \right )$\\ s.t. $ ||w||_2 \leq \frac{1}{\sqrt{\lambda}}$\\
            $\text{Proj}_S^{(2)}(w) = w \cdot \min\left(1, \frac{1/\sqrt{\lambda}}{||w||}\right)$\\
            $\text{Proj}_S^{(\infty)}(w) = \min\left((1/\sqrt{\lambda}, \ldots, 1/\sqrt{\lambda})^T, w\right) \cdot \text{sgn}(w)$
    \end{description}
    \textbf{Small $C$, Big $\lambda$:} Greater margin, more misclassification

\subsection{Kernels}
\begin{description}
    \item[Dual SVM Formulation]
    $\max_{\alpha} \sum_{i=1}^{n} \alpha_i - \frac{1}{2} \sum_{i,j} \alpha_i \alpha_j y_i y_j x_i^T x_j$\\ s.t. $ 0 \leq \alpha_i \leq C$\\
    $\Rightarrow$ optimal w: $ w^{\ast} = \sum_{i} \alpha_i^{\ast} y_i x_i = \sum_{i \in \text{SV}} \alpha_i^{\ast} y_i x_i $
\item[Kernel trick] Substitute inner product $x_{i}^T x_{j}$ in dual formulation and in classification function with $k(x_{i}, x_{j}) = \phi(x_{i})^T \phi(x_{j})$, where $\phi(\cdot): \mathbb{R}^d \to \mathbb{R}^{>d}$
    \item[Kernel functions] A kernel is function $k: X \times X \rightarrow \mathbb{R}$:
        \begin{enumerate}
            \item Symmetry: $\forall x,x' \in X : k(x,x') = k(x',x)$
            \item PSD: $\forall n \in \mathbb{N}$, any set $S = \{x_1,..,x_n\} \subseteq X$, the Gram matrix is PSD.
        \end{enumerate}
    \end{description}

\subsubsection{Random Features (Inverse Kernel Trick)}
% $$x \in \mathbb{R}^d \overset{\text{kernel trick}}{\rightarrow} \Phi(x) \in \mathbb{R}^D \overset{\text{Inverse Kernel Trick}}{\rightarrow} z(x) \in \mathbb{R}^m$$ where $d << D$,$m<<D$,$m>d$.
\begin{description}
    \item[Shift-invariant kernel] $k(x,y) = k'(x-y)$. Then the kernel has Fourier transform, such that:
    $$ k(x-y) = \int_{\mathbb{R}^d} p(w) \cdot e^{i w^T (x-y)} dw $$
    where $p(w)$ is the Fourier transformation, i.e. we map $k(s)$ to another function $p(w)$.
    \item[Random fourier features (prerequisites)] Interpret kernel as expectation
    $ k(x-y) = $
    $$\int_{\mathbb{R}^d} p(w) \cdot \underbrace{e^{i w^T (x-y)}}_{g(w)} dw = \mathbb{E}_{w,b} \left [ z_{w,b} (x) z_{w,b} (y) \right ]$$
    where $z_{w,b}(x) = \sqrt{2} \cos \left (w^T x +b \right )$,\\
    $b \sim U([0,2 \pi])$, $w \sim p(w)$

\item[Random fourier features] \textbf{(kernel approximation)}\\
    \begin{enumerate}
        \item $w_i \sim p, b_i \sim U([0, 2\pi])$ for $i = 1, \ldots, m;$ iid
        \item $z(x) = [z_{w_1,b_1}(x),...,z_{w_m,b_m}(x)]/\sqrt{m}$
        \item $z(x)^T z(y) = \frac{1}{m} \sum_{i=1}^{m} z_{w_i,b_i} (x) \cdot     z_{w_i,b_i} (y)$
        \item If $m \rightarrow \infty$, then (almost surely) $z(x)^T z(y)     \rightarrow  \mathbb{E}_{w,b} ( z_{w,b}(x) \cdot z_{w,b} (y) ) = k(x-y)$
    \end{enumerate}
\end{description}

\section{Online Convex Programming}
\begin{description}[leftmargin=*]
    \item[Regret] $R_T = (\sum_{t=1}^{T} f_t(w_t)) - \min_{w \in S} \sum_{t=1}^{T} f_t(w)$
    \item[No-regret] $\lim_{T \rightarrow \infty}\frac{R_T}{T} \rightarrow 0$
    \item[Online convex programming (OCP)]
        If $y_t w_t^Tx_t < 1:$
        $w_{t+1} = \text{Proj}_S( w_t - \eta_t \nabla f_t(w_t))$\\
        $\text{Proj}_S(w) = \text{arg}\min_{w' \in S} ||w' - w ||_2 = \min \left(w, \frac{w}{\lambda||w||_2}\right)$
    \item[Regret for OCP] $$ \frac{R_T}{T} \leq \frac{1}{\sqrt{T}} [||w_0 - w^*||_2^2 + ||\nabla f||_2^2] $$ where $||\nabla f||_2^2 = \sup_{w \in S, t \in \{1,\dots,T\}} ||\nabla f(w)||_2^2 $
    \item[Parallel stochastic gradient descent]
    \begin{enumerate}
        \item Split data into $k$ subsets, $k =$ number of machines, want $k = O(1/\lambda)$
        \item Each machine produces $w_i$ on its subset
        \item After T iterations, compute $w = \frac{1}{k} \sum_{i = 1}^k w_i$
    \end{enumerate}
    \item[PEGASOS] Online SVM: H-sc. lossfn. Minibatch + reg
\end{description}

\section{Active Learning}
\begin{description}
    \item[Uncertainty sampling] Repeat until all labels inferred:
    \begin{enumerate}
        \item Assign uncertainty score $U_t(x)$ to each unlabeled data point:
        $U_t(x) = U \left ( x | x_{1:t-1},y_{1:t-1} \right )$
        \item Greedily pick the most uncertain point and request label
        $x_t = \text{arg } \max_x U_t(x)$ and retrain classifier
    \end{enumerate}
    \item For SVM: $U_t(x) = \frac{1}{|w_{t-1}^T x|}$
    \item Cost to pick $m$ labels: $m \cdot n \cdot d + m \cdot C(m)$\\
        $n =$ number of data points, $d =$ dimensions,\\
        $C(m) =$ cost to train classifier

    \item[Hashing a hyperplane query] Draw $u,v \sim \mathcal{N}(0,I)$. Then resulting two-bit hash is:
    $$ h_{u,v}(a,b) = \left [ \text{ sign }(u^T a),\text{ sign }(v^T b) \right ]$$
    Define the hash family:
    $$
    h_{\mathcal{H}}(z) = \begin{cases}
        h_{u,v} (z,z) & \text{if $z$ is a database point vector} \\
        h_{u,v} (z,-z) & \text{if $z$ is a query hyperplane vector}
    \end{cases}$$
    \item[Version space] Set of all classifiers consistent with the data:
        $V(D) = \{w : \forall (x,y) \in D : \text{ sign}(w^T x) = y \}$
    \item[Relevant version space] $\hat{V}(D;U)$ describes all possible labelings $h$ of all unlabeled data $U$ that are still possible under some model $w$, or,
    \begin{align*}
        \hat{V}(D;U) &= \{h: U \rightarrow  \{+1,-1\}  : \exists w  \in V(D)\\
        &\forall x \in U : \text{sign}(w^T x) = h(x) \}
    \end{align*}
\item[Generalized Binary Search] (GBS)\\
    \begin{algorithmic}[1]
        \State {Start with $D = \emptyset$}
        \While {$|\hat{V}(D;U)| > 1$}
            \ForEach {unlabeled example x in $U$}
                \State {$ v^+(x) = |\hat{V}(D \cup \{(x,+)\}; U)|$}
                \State {$ v^-(x) = |\hat{V}(D \cup \{(x,-)\}; U)|$}
                \State \Comment number of labelings left if $x$ is $-$/$+$
            \EndFor
            \State {Pick $x^{\ast} = \text{arg}\min_{x} \max(v^-(x),v^+(x))$}
            \State $D = D \cup \{x^\ast\}$
        \EndWhile
    \end{algorithmic}
\item[Decision rules] for GBS SVM, $m \sim$ margin $\sim \frac{1}{||w||}$\\
    \begin{description}
        \item[Max-min margin] $\max_x \min \left( m^+(x),m^-(x) \right)$
        \item[Ratio margin] $\max_x \min \left (\frac{m^+(x)}{m^-(x)},\frac{m^-(x)}{m^+(x)} \right)$
    \end{description}
\end{description}

\section{Clustering}
\subsection{K-Means}
\begin{description}
    \item[Cost Function] $L(\mu) = L(\mu_1,....,\mu_k) =$\vspace{-0.5em}
        $$\vspace{-0.5em}\sum_{i=1}^N \underbrace{\min_{j \in \{1,\dots,k\}} ||x_i- \mu_j ||_2^2}_{d(\mu, x_{i})}$$
    \item[Objective] $\mu^*  = \text{arg } \min_{\mu} L(\mu)$
    \item[Algorithm] Until convergence:\\
        \begin{algorithmic}[1]
        \State {Assign each point $x_{i}$ to closest center
            $$z_i \leftarrow \text{arg } \min_{j \in \{1,\dots,l\}} ||x_{i} - \mu_{j}^{(t-1)}||_2^2$$}
        \State {Update center as mean of assigned data points
            $$\mu_j^{(t)} \leftarrow \frac{1}{n_j} \sum_{i: z_i = j} x_i$$}
    \end{algorithmic}

    \item[Online k-means algorithm]
        $$\frac{\textbf{d} \text{d}(\mu, x_t)}{\textbf{d} \mu_j} =
        \begin{cases}
            0 & \text{if }  j \notin \text{arg} \min_i ||\mu_i-x_t||^2 \\
            2(u_j-x_t) & \text{else}
        \end{cases}$$
    \begin{enumerate}[leftmargin=*]
         \item Initialize centers randomly
         \item For $t=1:N$
            \begin{itemize}
                \item Find $c = \text{arg } \min ||\mu_j - x_t ||_2$
                \item $\mu_c = \mu_c + \eta_t (x_t - \mu_c)$
            \end{itemize}
        \item For convergence:
        $ \sum_t \eta_t = \infty$ and $\sum_t \eta_t^2 < \infty$, e.g. $\eta_t = \frac{c}{t}$.
     \end{enumerate}
\end{description}

\subsection{Coresets}
\begin{description}
    \item[Idea] Replace many points by one weighted point
    $ L_k(u;C) = \sum_{(w,x) \in C} w \cdot \min_j || u_j - x ||_2^2$
    \item[$(k,\epsilon)$-coreset] $C$ is called a $(k,\epsilon)$-coreset for $D$, if for all $\mu$:
    $(1-\epsilon) L_k(\mu;D) \leq L_k(\mu;C) \leq (1+\epsilon) L_k(\mu;D)$
\item[Operations]\textbf{Merge}: union of two $(k,\epsilon)$-csets is a $(k, \epsilon)$-cset\\
    \textbf{Compress}: $(k,\delta)$-cset of a $(k, \epsilon)$-cset of $D$ is a $(k, \epsilon + \delta + \epsilon\delta)$-cset of $D$
\item[Construction]
    \textbf{$D^2$-sampling}: iteratively build $B = \emptyset$ by sampling $\propto \frac{\text{d}(x, B)^2}{\sum_{x' \in X} \text{d}(x', B)^2}$\\
    \textbf{Importance sampling}: $\propto \frac{\alpha \text{d}(x, B)^2}{c_\Phi} + \frac{2\alpha\sum_{x' \in B_x} \text{d}(x', B)^2}{|B_x|c_\Phi} + \frac{4|X|}{|B_x|}$
        where $c_\Phi = \frac{1}{|X|}\sum_{x \in X}\text{d}(x, B)^2$\\
        $B_x = $ pts in $X$ that belong to same cluster as $x$
\end{description}

\section{Bandits}
\begin{description}
    \item[Regret] $R_T = \sum_{t=1}^T (\mu^* - \mu_{i_t})$, $i_t$ chosen arm at time $t$
    \item[$\epsilon$-greedy] The algorithm goes as follows:
    \begin{enumerate}
        \item Set $\epsilon_t = \mathcal{O}(\frac{1}{t})$
        \item With probability $\epsilon_t$: explore by picking uniformly at random
        \item With probability $1 - \epsilon_t$: exploit by picking arm with highest empirical mean
    \end{enumerate}
    Regret: $R_T = \mathcal{O}(k \log(T))$
\end{description}
\subsection{UCB1 \& LinUCB}
\begin{description}
    \item[Hoeffding's inequality] $\Pr(|\mu - \frac{1}{m} \sum_{t=1}^m X_t | \geq b) \leq 2 \cdot \exp(-2b^2m)$
    \item[Confidence bound] Want: Hoeffding $\leq \delta$
        $\Rightarrow b = \sqrt{\frac{1}{2m}\ln\frac{2}{\delta}}$
    \item[UCB/Mean update]
    $UCB(i) = \hat{\mu_i}+ \sqrt{\frac{2 \ln t}{\eta_i}}$
        $j = \text{arg}\max_i UCB(i)$\\
    $\hat{\mu_j} = \hat{\mu_j} + \frac{1}{\eta_j} (y_t - \hat{u}_j)$
    \item[Contextual bandits] Reward is now $y_t = f(x_t,z_t) + \epsilon_t$ with $z_t$ user features. For us: $f(x_i,z_t) = w^T_{x_i}z_t$
    \item[LinUCB (disjoint)] Ridge regression on $z_t$:
        $\hat{w}_i = (D_i^TD_i + I)^{-1}D_i^Ty_i$.\\
        $|\hat{w}_i^Tz_t - w_i^Tz_t| \leq \alpha \sqrt{z_t^T(D_i^TD_i)^{-1}z_t}$ with
        probability $1-\delta$ if $\alpha = 1 + \sqrt{\log(2/\delta)/2}$\\
        $R_T/T = O(d \cdot d' \cdot \text{poly}\log T / \sqrt{T})$
    \item[Hybrid model] Reward is now $y_t = w^T_{x_t} z_t + \beta^T \phi(x_t,z_t) + \epsilon_t$
    \item[Rejection sampling] First obtain data log through pure exploration, and then reiterate:
    \begin{enumerate}
        \item Get event $(x_t^{(1)},..,x_t^{(k)},z_t,a_t,y_t)$ from log
        \item Use algorithm that is testing to pick $a_t'$:
        \begin{itemize}
            \item If $a_t'  = a_t$
            $\Rightarrow$ Feed back reward $y_t$
            \item Else ignore log line
        \end{itemize}
        \item Stop when T rewards have been fed back
    \end{enumerate}
\end{description}

\subsection{Submodular Functions}
\begin{description}
    \item[Submodularity] A function $F: 2^V \mapsto \mathbb{R}$ is called submodular iff for all $A \subseteq B, s \notin B$:
        $$F(A \cup \{s\}) - F(A) \geq F(B \cup \{s\}) - F(B)$$
    \item[Monotonic] if $S \subseteq T \Rightarrow F(S) \leq F(T)$
    \item[Union-intersection def.] Same preconditions as above: $$F(A) - F(A\cap B) \geq F(A\cup B) - F(B)$$
    \item[Closure properties] $F, F_i$ submodular on $V$; $S, W \subseteq V$
    \begin{description}
        \item[Linear] \textbf{Combinations}\\ $F'(S) = \sum_{i} \lambda_i F_i(S)$, $\lambda_i \geq 0$
        \item[Restriction] $F'(S) = F(S \cap W)$
        \item[Conditiong] $F'(S) = F(S \cup W)$
        \item[Reflection] $F'(S) = F(V \setminus S)$
        \item[Monotonic truncation] $F$ also monotonic, $F'(S) = \min(c, F(S)), c \in \mathbb{R}$
    \end{description}
    \item[Min/Max] For $F_{1,2}(A)$, $\max \{F_1(A),F_2(A) \}$ or $\min \{F_1(A),F_2(A) \}$ \textbf{not} submodular in general.
    \item[Concavity]
    $F(A) = g(|A|)$ where $g: \mathbb{N} \mapsto \mathbb{R}$, then $F$ submodular iff $g$ concave.
\item[Lazy Greedy] Optimizing submodular \textbf{monotonic} functions: $\Delta(s|A_i) \geq \Delta(s|A_{i+1})$.
    \begin{algorithmic}[1]
    \State {$A_0 = \emptyset$.
        Keep ordered list of marginal benefits $\Delta_s$ from previous iterations $j < i$, for all $s \in \mathcal{S}$, set of articles}
        \For {$i = 1 \ldots k:$}
            \State $s$ is the article for which $\Delta_s$ is at the top
            \State {Update: $\Delta_s = F(A_{i-1} \cup \{s\}) - F(A_{i-1})$}
            \If {$\Delta_s$ is not top element anymore}
            \State{re-sort the list, goto 3}
            \EndIf
            \State {$A_i = A_{i-1} \cup \{s\}$}
        \EndFor
\end{algorithmic}
\end{description}

\end{multicols}
\end{document}


% ~~~ [ Control Flow Analysis ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

\subsubsection{Control Flow Analysis}
\label{sec:lit_review_control_flow_analysis}

The control flow analysis stage is responsible for analysing the control flow (i.e. flow of execution) of source programs to recover their high-level control flow structures. The control flow of a given function is determined by its branching instructions and may be expressed as a control flow graph (CFG), which is a connected graph with a single entry node (the function entry point) and zero or more exit nodes (the function return statements). A key insight provided by C. Cifuentes and S. Moll is that high-level control flow primitives (such as 1-way conditionals and pre-test loops) may be expressed using graph representations~\cite{reverse_comp, decomp_of_llvm}, as illustrated in figure~\ref{fig:graph_representations}. The problem of recovering high-level control flow primitives from CFGs may therefore be reformulated as the problem of identifying subgraphs (i.e. the graph representation of a high-level control flow primitive) in graphs (i.e. the CFG of a function) without considering node names. This problem is commonly referred to as \textit{subgraph isomorphism search}, the general problem of which is NP-hard~\cite{subgraph_isomorphism_algorithms}. However, the problem which is required to be solved by the control flow analysis stage may be simplified by exploiting known properties of CFGs (e.g. connected graph with a single entry node).

\begin{figure}[htbp]
	\centering
	% if
	\begin{subfigure}[ht]{0.23\textwidth}
		\centering
		\begin{subfigure}[ht]{0.45\textwidth}
			\lstinputlisting[language=go, style=go, breaklines=false, numbers=none]{inc/primitives/if.c}
		\end{subfigure}
		\begin{subfigure}[ht]{0.42\textwidth}
			\includegraphics[width=\textwidth]{inc/primitives/if.png}
		\end{subfigure}
		\caption{1-way conditional; entry: \texttt{A}, exit: \texttt{C}.}
		\label{fig:if_graph_representation}
	\end{subfigure}
	\qquad
	% if_else
	\begin{subfigure}[ht]{0.28\textwidth}
		\centering
		\begin{subfigure}[ht]{0.45\textwidth}
			\lstinputlisting[language=go, style=go, breaklines=false, numbers=none]{inc/primitives/if_else.c}
		\end{subfigure}
		\begin{subfigure}[ht]{0.50\textwidth}
			\includegraphics[width=\textwidth]{inc/primitives/if_else.png}
		\end{subfigure}
		\caption{2-way conditional; entry: \texttt{A}, exit: \texttt{D}.}
		\label{fig:if_else_graph_representation}
	\end{subfigure}
	\qquad
	% if_return
	\begin{subfigure}[ht]{0.30\textwidth}
		\centering
		\begin{subfigure}[ht]{0.45\textwidth}
			\lstinputlisting[language=go, style=go, breaklines=false, numbers=none]{inc/primitives/if_return.c}
		\end{subfigure}
		\begin{subfigure}[ht]{0.50\textwidth}
			\includegraphics[width=\textwidth]{inc/primitives/if_return.png}
		\end{subfigure}
		\caption{1-way condition with return statement in body; entry: \texttt{A}, exit: \texttt{C}.}
		\label{fig:if_return_graph_representation}
	\end{subfigure}
	\qquad
	% pre_loop
	\begin{subfigure}[ht]{0.32\textwidth}
		\centering
		\begin{subfigure}[ht]{0.45\textwidth}
			\lstinputlisting[language=C, style=go, breaklines=false, numbers=none]{inc/primitives/pre_loop.c}
		\end{subfigure}
		\begin{subfigure}[ht]{0.50\textwidth}
			\includegraphics[width=\textwidth]{inc/primitives/pre_loop.png}
		\end{subfigure}
		\caption{pre-test loop; entry: \texttt{A}, exit: \texttt{C}.}
		\label{fig:pre_loop_graph_representation}
	\end{subfigure}
	\qquad
	% post_loop
	\begin{subfigure}[ht]{0.30\textwidth}
		\centering
		\begin{subfigure}[ht]{0.50\textwidth}
			\lstinputlisting[language=C, style=go, breaklines=false, numbers=none]{inc/primitives/post_loop.c}
		\end{subfigure}
		\begin{subfigure}[ht]{0.35\textwidth}
			\includegraphics[width=\textwidth]{inc/primitives/post_loop.png}
		\end{subfigure}
		\caption{post-test loop; entry: \texttt{A}, exit: \texttt{B}.}
		\label{fig:post_loop_graph_representation}
	\end{subfigure}
	\qquad
	% seq
	\begin{subfigure}[ht]{0.24\textwidth}
		\centering
		\begin{subfigure}[ht]{0.20\textwidth}
			\lstinputlisting[language=C, style=go, breaklines=false, numbers=none]{inc/primitives/seq.c}
		\end{subfigure}
		\begin{subfigure}[ht]{0.35\textwidth}
			\includegraphics[width=\textwidth]{inc/primitives/seq.png}
		\end{subfigure}
		\caption{consecutive statements; entry: \texttt{A}, exit: \texttt{B}.}
		\label{fig:seq_graph_representation}
	\end{subfigure}
	\caption{The pseudo-code and graph representation of various high-level control flow primitives with denoted entry and exit nodes.}
	\label{fig:graph_representations}
\end{figure}

When the subgraph isomorphism of a high-level control flow primitive has been identified in the CFG of a function, it may be replaced by a single node that inherits the predecessors of the subgraph entry node and the successors of the subgraph exit node; as illustrated in figure~\ref{fig:subgraph_merge}. By recording the node names of the identified subgraphs and the name of their corresponding high-level control flow primitives, the high-level control flow structure of a CFG may be recovered by successively identifying subgraph isomorphisms and replacing them with single nodes until the entire CFG has been reduced into a single node; as demonstrated by the step-by-step simplification of a CFG in appendix~\ref{app:control_flow_analysis_example}. Should the control flow analysis fail to reduce a CFG into a single node, the CFG is considered irreducible with regards to the supported high-level control flow primitives (see figure~\ref{fig:graph_representations}). To structure arbitrary irreducible graphs, S. Moll applied node splitting (which translates irreducible graphs into reducible graphs by duplicating nodes) to produce functionally equivalent target programs~\cite{decomp_of_llvm}. In contrast, C. Cifuentes focused on preserving the structural semantics of the source program (which may be required in forensics investigations), and therefore used \texttt{goto}-statements in these cases to produce unstructured target programs.

\begin{figure}[htbp]
	\centering
	\begin{subfigure}[ht]{0.15\textwidth}
		\includegraphics[width=\textwidth]{inc/2_lit_review/cfg_pre_merge.png}
	\end{subfigure}
	\qquad
	\begin{subfigure}[ht]{0.15\textwidth}
		\includegraphics[width=\textwidth]{inc/2_lit_review/cfg_post_merge.png}
	\end{subfigure}
	\caption{The left side illustrates the CFG of a function in which the graph representation of a 1-way conditional (see figure~\ref{fig:if_graph_representation}) has been identified, and the right side illustrates the same CFG after the subgraph has been replaced with a single node (i.e. \texttt{if0}) that inherits the predecessors of the subgraph entry node (i.e. \texttt{3}) and the successors of the subgraph exit node (i.e. \texttt{list0}).}
	\label{fig:subgraph_merge}
\end{figure}


%!TEX root = ../notes.tex
\section{April 18, 2022}
\subsection{Midterm 2 Review}
\begin{itemize}
    \item Apologies for poor communication.
    \item Feedback is welcome---will send out form.
\end{itemize}
\emph{Average:} 28/40.

``A-level work'' would equate to doing around 3 problems, \\
``B-level work'' would equate to doing around 2 problems, and \\
``C-level work'' would equate to doing around 1 problem.

\begin{problem}
\emph{About 3/4 solved.}

\ul{Idea}: exploit something special about $n$ being a Carmichael number. That is that $a^{N-1}\equiv 1\pmod{N}$. We can do something inspired by the Miller-Rabin test. We then have
\[\left( a^{\frac{N-1}{2}}\equiv 1\pmod{N} \right)\]
If $a^{\frac{N-1}{2}}\equiv 1$, then we decrement the exponent by a factor of two. We then check
\[\left( a^{\frac{N-1}{4}}\equiv 1\pmod{N} \right)\]
and so on. Eventually, we'll find a nontrivial square root of $1$. This produces a factorization, since
\begin{align*}
    x^2        & \equiv 1\pmod{n} \\
    (x-1)(x+1) & \equiv 0\pmod{n}
\end{align*}
so $\gcd(x\pm 1, n)$ likely allows us to recover a factor of $N$. We run through this multiple times with different values of $a$.
\end{problem}

\begin{problem}
\emph{Hardest problem, 1/2 solved.}

\ul{Idea}: Implement some reasonably general-purpose factorization method:
\begin{enumerate}
    \item Lenstra's Elliptic Curve Factorization.
    \item Quadratic Sieve.
    \item Pollard $\rho$ method.
\end{enumerate}

Things that would not work:
\begin{enumerate}
    \item Trial division.
    \item Pollard $p-1$.
\end{enumerate}

The factorization was $15\times 35$ digits, which gives a relatively equal runtime for Elliptic Curve and Quadratic Sieve.
\end{problem}

\begin{problem}
\emph{About 2/3 solved (generally speaking, lost 3 points on it).}

\ul{Idea}: solve DLP. Babystep-Giantstep, as in class, yields 7/10 due to excessive memory usage. 4 people solved this problem in a way that didn't use a shit-ton of RAM, with 3 distinct solutions.

\emph{How to solve DLP without using tons of RAM:}
\begin{enumerate}
    \item Babystep-Giantstep with fewer babysteps ($B$ babysteps). We need $B + \frac{N}{B}$ time to solve this, which is minimized if $B\equiv N^{0.5}$. We take a smaller $B\equiv N^{0.3}$ or something, and our time would be $N^{0.7}$, still within the bounds.
    \item Index calculus. Time/Memory are asymptotically small compared to Babystep-Giantstep.
    \item Pollard $\rho$ method \emph{(discussion of which is in textbook)}. Gives runtime of $N^{0.5}$ with minimal memory usage.
\end{enumerate}

\end{problem}

\begin{problem}
\emph{This was a fairly easy problem.}
\end{problem}


\subsection{Symplectic Tensors}

\exercisehead{22.1}

{\scriptsize{\url{http://math.stackexchange.com/questions/342267/non-degenerate-bilinear-forms-and-invertible-matrices}}} (shout out to Branimir Cacic for the answer)  gave me a hint at how to approach this exercise, even though the original question was for symmetric bilinear forms.

Let $\lbrace e_1 \dots e_n \rbrace$ be (some) basis of $V$ \\
Let $\lbrace f^1 \dots f^n \rbrace$ be dual basis of $V$ s.t. $f^i(e_j)=\delta^i_{ \,\, j}$

Now
\[
\begin{aligned}
  & \widehat{\omega}(e_i) = (\widehat{\omega}(e_i))_jf^j \\
  & \widehat{\omega}(e_i)e_j = (\widehat{\omega}(e_i))_j = i_{e_i}\omega(e_j) = \omega(e_i,e_j) \equiv \omega_{ij}
\end{aligned}
\]
so $\omega_{ij} = (\widehat{\omega}(e_i))_j$ (i.e. matrix $\omega_{ij}$ is precisely $(\widehat{\omega}(e_i))_j$.

If $\omega_{ij}$ nonsingular, i.e. $\exists \, \omega^{-1}_{ki}$ s.t. $\omega^{-1}_{ki} \omega_{ij} = \omega_{ki}\omega^{-1}_{kj} = \delta_{kj}$ (by def.)

If $\widehat{\omega}$ invertible, $\widehat{\omega}^{-1} \widehat{\omega}(v) = v$
\[
\begin{gathered}
  \widehat{\omega}^{-1}\widehat{\omega}(e_i) = \widehat{\omega}^{-1}(\widehat{\omega}(e_i))_j f^j = (\widehat{\omega}(e_i))_j \widehat{\omega}^{-1}(f^j) = (\widehat{\omega}(e_i))_j(\widehat{\omega}^{-1}(f^j))^k e_k = e_i \\
\Longrightarrow (\widehat{\omega}(e_i))_j(\widehat{\omega}^{-1}(f^j))^k = \delta_i^{ \, \, k}
\end{gathered}
\]
So if $\omega_{ij}$ nonsingular, $(\widehat{\omega}^{-1}(f^j))^k$ exists and $(\widehat{\omega}(f^j))^k = \omega^{-1}_{jk}$ \\
\phantom{So } if $\widehat{\omega}$ invertible, $\omega^{-1}_{jk}$ exists and is given by $\omega^{-1}_{jk} = (\widehat{\omega}(f^j))^k$

So the (a) $\Longleftrightarrow$(c) part of the exercise is done.

Show (a) $\Longleftrightarrow$(b) and we're done.

If $\widehat{\omega}:V\to V^*$ linear isomorphism,
\[
\text{ker}\widehat{\omega}=0
\]

Suppose $\nexists \, w \in V$ s.t. $\omega(v,w)\neq 0$ (proof by contradiction strategy) \\
Then $\forall \, w \in V$, $\omega(v,w) =0$  \\
$\omega(v,w) = 0 = \widehat{\omega}(v)(w) \quad \, \forall \, w \in V$.  \\
Then $v=0$.  Contradiction.

(b) $\Longrightarrow $ (a): if $\forall \, v \neq 0$, $\exists \, w \in V$ s.t. $\omega(v,w) \neq 0$ \\
then if $\forall \, w \in V$, $\omega(v,w) =0$, then $v=0$ \\
$\omega(v,w) = \widehat{\omega}(v)(w) = 0$ implies $v=0$, $\forall \, w \in V$.  \\
Then $\text{ker}\widehat{\omega}=0$.  So $\widehat{\omega}$ linear isomorphism.  So $\omega$ nondegenerate.

\exercisehead{22.4} There was a proof of this in Konstantin Athanassopoulos, \textbf{Notes on Symplectic Geometry}, Iraklion, 2013
\url{http://www.math.uoc.gr/~athanako/symplectic.pdf}

Recall that \\
$S^{\perp} = \lbrace v \in V | \omega(v,w) =0 \quad \, \forall \, w \in S \rbrace$ \\
$(S^{\perp})^{\perp} = \lbrace u \in V | \omega(u,v)=0 \quad \, \forall \, v \in S^{\perp}\rbrace$

Let $s\in S$.  $\omega(s,v)=0 \quad \, \forall \, v \in S^{\perp}$, by def. of $S^{\perp}$ \\
$\begin{aligned} & \quad \\
  & s\in (S^{\perp})^{\perp} \\
  \Longrightarrow & S \subseteq (S^{\perp})^{\perp} \end{aligned}$

Then $\text{dim}S \leq \text{dim}(S^{\perp})^{\perp}$ with equality iff $S= (S^{\perp})^{\perp}$

Now by Lemma 22.3, \\
$\text{dim}S + \text{dim}S^{\perp} = \text{dim}V$, $\forall \, $ linear subspace $S\subseteq V$ \\
$\text{dim}S^{\perp} + \text{dim}(S^{\perp})^{\perp} = \text{dim}V = \text{dim}S + \text{dim}S^{\perp} \Longrightarrow \text{dim}S = \text{dim}(S^{\perp})^{\perp}$ \\

$\text{dim}S \leq \text{dim} (S^{\perp})^{\perp}$ with equality iff $S= (S^{\perp})^{\perp}$

\subsection{Symplectic Structures on Manifolds}

\exercisehead{22.10} $F:N\to M$ smooth immersion.  Recall definition: $F_*$ injective.  Recall Appendix B, Exercise B.22 (EY: 20150512 This exercise is \textbf{very useful}; I can't emphasize that enough).  $F_*$ injective so $\text{rank}F_* = \text{dim}N$. (implying $\text{ker}F_*=0$).

Recall that $F$ isotropic if \\

$(F_*)_p(T_pN) \subseteq T_{F(p)}M$ isotropic, i.e.

\[
(F_*)_p(T_pN) \subseteq ((F_*)_p(T_pN))^{\perp}
\]

Consider $X,Y \in T_pN$, with $X,Y$ nonzero.  Then, as $F_*$ injective, $Z,W \in ((F_*)_p(T_pN))$ nonzero, for $\begin{aligned}
& \quad \\
  & Z = (F_*)_pX \\
  & W = (F_*)_pY \end{aligned}$

Suppose $\omega(Z,W)=0$, $\forall \, W \in (F_*)_p(T_pN)$.  $Z \in ((F_*)_p(T_pN))^{\perp}$.

\[
\omega(Z,W) = \omega((F_*)_pX, (F_*)_pY) = (F^*)_p\omega(X,Y)=0
\]

If $F$ isotropic, then this is the case $\forall \, Z \in ((F_*)_p(T_pN)) \subseteq ((F_*)_p(T_pN))^{\perp}$.

Then since $(F^*)_p\omega(X,Y)=0$ \, $\forall \, p \in N$, $\forall \, X,Y \in T_pN$, then $F^*\omega=0$.

If $F$ symplectic, $(F_*)_p(T_pN) \cap ((F_*)_p(T_pN))^{\perp}=0$

Likewise, for the reverse.

If $F$ symplectic,

\[
(F_*)_p(T_pN) \cap ((F_*)_p(T_pN))^{\perp}=0
\]
For $ X,Y \in T_pN$, suppose

\[
F_p^*\omega(X,Y) = \omega((F_*)_pX, (F_*)_pY) =0
\]

This implies $(F_*)_pX \in (F_*)_p(T_pN) \cap ((F_*)_p(T_pN))^{\perp}$

Then

since $F_*$ immersion, $X,Y=0$.  So $F^*\omega$ is nondegenerate and so is a symplectic form.

\subsubsection{the Canonical Symplectic Form on the Cotangent Bundle}

\begin{quote}
The most important symplectic manifolds are total spaces of cotangent bundles, which carry canonical symplectic structures that we now define.
\end{quote}

\subsection{The Darboux Theorem}

\subsection{Hamiltonian Vector Fields}

\textbf{Hamiltonian vector field} of $f$
\[
X_f = \widehat{\omega}^{-1}(df)
\]

Hamiltonian vector field of $f$ in Darboux coordinates:
\begin{equation}
  X_f = \sum_{i=1}^n \left( \frac{ \partial f}{ \partial y^i} \frac{ \partial }{ \partial x^i } - \frac{ \partial f}{ \partial x^i} \frac{\partial}{ \partial y^i} \right)
\end{equation}
(22.9)

smooth $X \in \mathfrak{X}(M)$ \textbf{symplectic} if $\omega$ invariant under flow of $X$, i.e. $\mathcal{L}_X \omega =0$

\subsubsection{Poisson Brackets}

$f \in C^{\infty}(M)$ \textbf{conserved quantity} if $f$ constant on every integral curve of $X_H$.

smooth $V \in \mathfrak{X}(M)$ \textbf{infinitesimal symmetry} of $(M,\omega,H)$ if $\omega,H$ invariant under flow of $V$, i.e. EY (20150521)
\[
\begin{aligned}
  & \mathcal{L}_V \omega = 0
  & \mathcal{L}_V H = 0
\end{aligned}
\]

\begin{proposition}[22.21]
Let $(M,\omega,H)$ Hamiltonian system
\begin{enumerate}
\item[(a)] $f \in C^{\infty}(M)$ conserved quantity iff $\lbrace f,H\brace =0$
\item[(b)] infinitesimal symmetries of $(M, \omega,H)$ are precisely symplectic fields $V$ s.t. $VH=0$
\item[(c)] if $\theta$ flow of infinitesimal symmetry and $\gamma$ trajectory of system
\end{enumerate}
\end{proposition}

\begin{proof}
This is the solution to Problem 22-18.

\begin{enumerate}
\item[(a)] if $f\in C^{\infty}(M)$ conserved quantity, by def. $f$ constant on every integral curve of $X_H$
\[
\lbrace f,H\rbrace = \frac{ \partial f}{ \partial x^i} \frac{ \partial H}{ \partial y^i} - \frac{ \partial f}{ \partial y^i} \frac{ \partial H}{ \partial x^i} = X_H f = 0
\]
for
\[
X_H = \frac{ \partial H}{ \partial y^i} \frac{ \partial }{ \partial x^i} - \frac{ \partial H}{ \partial x^i} \frac{ \partial }{ \partial y^i}
\]
likewise, if $\lbrace f,H \rbrace =0$, then $X_Hf =0$, $X_Hf = \mathcal{L}_{X_H} f= 0 $, so $f$ constant on flow of $X_H$
\item[(b)] Recall smooth $V \in \mathfrak{X}(M)$ infinitesimal symmetry of $(M,\omega,H)$ if $\omega, H$ invariant under flow of $V$, i.e.
\[
\begin{aligned}
  & \mathcal{L}_V \omega =0
  &  \mathcal{L}_VH = 0
\end{aligned}
\]
smooth $V\in \mathfrak{X}(M)$ symplectic if $\omega$ invariant under flow of $V$, i.e. $\mathcal{L}_V\omega =0$
\[
\mathcal{L}_VH = VH = 0
\]
\item[(c)] EY : 20150521 I'm not sure how to go about this because what is a trajectory?

$\gamma: I \to M$

$\theta$ flow of an infinitesimal symmetry, so (collecting facts)

\[
\begin{aligned}
  & \mathcal{L}_{\dot{\theta}}\omega = di_{\dot{\theta}}\omega + i_{\dot{\theta}}d\omega = di_{\dot{\theta}}\omega \quad \, (\omega \text{ closed so } i_{\dot{\theta}}d\omega) \\
  & \dot{\theta}H = 0
\end{aligned}
\]

Now $\theta_s \circ \gamma : I \to M$

\[
\frac{d}{dt}(\theta_s \circ \gamma)(t) = (D\theta_s)(\gamma(t)) \dot{\gamma}(t) = V_{s,\gamma(t)} \dot{\gamma}(t)
\]
\end{enumerate}
\end{proof}

\problemhead{22.1}

\begin{proof}
\begin{enumerate}
  \item[(a)] If $S$ symplectic, $S \cap S^{\perp} = 0$.  $S=(S^{\perp})^{\perp}$ so $(S^{\perp})^{\perp} \cap S^{\perp} =0$.  $S^{\perp}$ symplectic.  \\
If $S^{\perp}$ symplectic, $S^{\perp} \cap (S^{\perp})^{\perp} = 0$.  $S=(S^{\perp})^{\perp}$ so $(S^{\perp})^{\perp} =S \cap S^{\perp} =0$.  $S$ symplectic.  \\
  \item[(b)] Suppose for $s\in S\cap S^{\perp}$, $s\neq 0$.  Then as $s\in S^{\perp}$, $\omega(s,w)=0 \, \forall \, \, w \in S^{\perp}$.

Then $\omega(s,s)=0$.  But $\omega$ nondegenerate so $s=0$.  Contradiction.

Suppose $S$ symplectic.  For $\left. \omega \right|_S(s,t) = \omega(s,t)=0$, for some $s \in S$, $\forall \, t \in S$, then $S\cap S^{\perp}=0$ implies that $s,t=0$.  Then $\left. \omega \right|_S$ nondegenerate.

  \item[(c)] If $S$ isotropic, $S\subseteq S^{\perp}$ so that $\omega(s,t)=0$ \, $\forall \, t \in S$ (def. of $S^{\perp}$).  $\left. \omega \right|_S =0$ as $\omega(s,t)=0$, \, $\forall \, s,t \in S$.

If $\left. \omega \right|_S =0$, then $\forall \, s,t \in S$, $\omega(s,t)=0 \, \, \forall \, t\in S$.  By def. of $S^{\perp}$, $S\subseteq S^{\perp}$.
  \item[(d)] if $S$ coisotropic, $S\supseteq S^{\perp}$.  $S^{\perp} \subseteq S=(S^{\perp})^{\perp}$.  Then $S^{\perp}$ isotropic.

If $S^{\perp}$ isotropic, $S^{\perp} \subseteq (S^{\perp})^{\perp} = S$, so $S$ coisotropic.
  \item[(e)] If $S$ Lagrangian, $\forall \, s \in S$, $s\in S^{\perp}$, so that $\omega(s,t) = 0$ \, $\forall \, t \in S$.  Then $\left. \omega \right|_S =0$, (i.e. identically $0$).  \\

$\text{dim}S + \text{dim}S^{\perp} = 2\text{dim}S = \text{dim}V $ by Lemma 22.3, so $\text{dim}S = \frac{1}{2} \text{dim}V$.

If $\text{dim}S = \frac{1}{2} \text{dim}V$, $\text{dim}S^{\perp} = \frac{1}{2} \text{dim}V = \text{dim}S$.  $\left. \omega \right|_S =0$, so $S$ isotropic, i.e. $S\subseteq S^{\perp}$.  $\text{dim}S \leq \text{dim}S^{\perp}$, with equality iff $S=S^{\perp}$.
\end{enumerate}
\end{proof}

\problemhead{22.-17} Given Hamiltonian system $(T^*Q, \omega,E)$.

Recall that
\[
\begin{aligned}
  q(t) & = (q_1^1(t),q_1^2(t), q_1^3(t) \dots q^1_n(t),q_n^2(t),q_n^3(t))=0 \\
  & = (q^1(t) \dots q^{3n}(t))
\end{aligned}
\]

Now $p(t) = (p_1^1,p_1^2,p_1^3\dots p_n^1,p_n^2,p_n^3)$ and \\
$p_i(t)=M_{ij}\dot{q}^j(t)$ with

$M_{ij}$ $3n \times 3n$ diagonal matrix $(m_1,m_1,m_1,m_2,m_2,m_2 \dots m_n, m_n, m_n)$

Now $E \in C^{\infty}(T^*Q)$ where
\[
E(q,p) = V(q) + K(p) = V(q) + \frac{1}{2} M^{ij}p_i p_j
\]

\begin{enumerate}
\item[(a)] Let $\mathbf{u} = (u^1,u^2,u^3)$

\[
\begin{aligned}
  & P : T^*Q \to \mathbb{R} \\
  & \begin{aligned} P(q,p) & = \mathbf{u}\cdot \mathbf{p}_1 + \mathbf{u}\cdot \mathbf{p}_2 \\
      & = u^1p_1^1 + u^2p_1^2 + u^3 p_1^3 +u^1p_2^1 + u^2 p_2^2 + u^3 p_2^3 \end{aligned}
\end{aligned}
\]

 Recall Prop. 22.21, Let $(M,\omega, H)$ Hamiltonian system
\begin{enumerate}
\item[(a)] $f \in C^{\infty}(M)$ conserved quantity iff $\lbrace f, H\rbrace =0$
\end{enumerate}

Now in Darboux coordinates,
\[
\lbrace f, g \rbrace = \sum_{i=1}^n \frac{ \partial f}{ \partial x^i} \frac{ \partial g}{ \partial y^i } - \frac{ \partial f}{ \partial y^i } \frac{ \partial g}{ \partial x^i}
\]
(22.16)

For
\[
E = V(|\mathbf{q}_2 - \mathbf{q}_1|) + \frac{1}{2} M^{ij}p_ip_j
\]

note that

\[
\begin{aligned}
  & V(| \mathbf{q}_2 - \mathbf{q}_1|) = V(r) \text{ with } \\
  & r = \sqrt{ (q_2^1 - q_1^1)^2 + \dots + (q_2^3 - q_1^3)^2 } \\
  & \frac{ \partial V}{ \partial q_i^j} = \frac{ \partial V}{ \partial r} \frac{1}{2} \frac{1}{r}(2)(q_2^j - q_1^j)(-1)^i = \frac{ \partial V}{ \partial r} \frac{1}{r} (q_2^j-q_1^j)(-1)^i
\end{aligned}
\]

then
\[
\begin{aligned}
  & \frac{ \partial E}{ \partial p_i^j} = \frac{p_i^j}{m_i} \\
  & \frac{ \partial E}{ \partial q_i^j} = \frac{ \partial V}{ \partial r} \frac{1}{r}(-1)^i (q_2^j-q_1^j)
\end{aligned}
\]
with $r = |\mathbf{q}_2 - \mathbf{q}_1| = \sqrt{ (q_2^1- q_1^1)^2 + \dots + (q_2^3- q_1^3)^2 }$

\[
P = \mathbf{u}\cdot (\mathbf{p}_1 + \mathbf{p}_2) = u^i p^i_1 + u^i p_2^i
\]
\[
\begin{aligned}
\frac{ \partial P}{ \partial p_i^j} = u^j  \\
 \frac{ \partial P}{ \partial q }=  0
\end{aligned}
\]

\[
\lbrace P,E \rbrace =0 - u^j \frac{ \partial V}{ \partial r} \frac{1}{r} (-1)^i(q_2^j-q_1^j) = -\mathbf{u}\cdot(\mathbf{q}_2-\mathbf{q}_1) \frac{ \partial V}{ \partial r} \frac{1}{r}(-1) + -\mathbf{u}\cdot (\mathbf{q}_2-\mathbf{q}_1) \frac{ \partial V}{ \partial r} \frac{1}{r} = 0
\]

\item[(b)]
\[
L(q,p) = q_1^1 p_1^2 - q_1^2 p_1^1 + q_2^1p_2^2 - q_2^2 p_2^1
\]
\[
\begin{aligned}
  & \frac{ \partial L}{ \partial q_i^j } = p_i^k \epsilon^{jk} \\
  & \frac{ \partial L}{ \partial p_i^k } = q_i^j \epsilon^{jk}
\end{aligned}
\]

\[
\begin{gathered}
  \lbrace L, E \rbrace = p_i^k \epsilon^{jk} \frac{p_i^j}{m_i} - q_i^k \epsilon^{kj} \frac{ \partial V}{ \partial r} \frac{1}{r} (-1)^i (q_2^j - q_1^j) = \frac{ p_i^2 p_i^1}{m_i} - \frac{ p_i^1 p_i^2}{m_i} -  q_i^k \epsilon^{kj} \frac{ \partial V}{ \partial r} \frac{1}{r} (-1)^i (q^j_2 - q^j_1) = \\
  = 0 - \frac{ \partial V}{ \partial r} \frac{1}{r} ( -q_1^2(q_2^1- q_1^1 )(-1) + q_1^1 (q_2^2 - q_1^2)(-1) - q_2^2 (q_2^1 - q_1^1) + q_2^1 (q_2^2 - q_1^2) ) = 0
\end{gathered}
\]

\end{enumerate}

\problemhead{22-18}

\begin{enumerate}
\item[(a)] if $f\in C^{\infty}(M)$ conserved quantity, by def. $f$ constant on every integral curve of $X_H$
\[
\lbrace f,H\rbrace = \frac{ \partial f}{ \partial x^i} \frac{ \partial H}{ \partial y^i} - \frac{ \partial f}{ \partial y^i} \frac{ \partial H}{ \partial x^i} = X_H f = 0
\]
for
\[
X_H = \frac{ \partial H}{ \partial y^i} \frac{ \partial }{ \partial x^i} - \frac{ \partial H}{ \partial x^i} \frac{ \partial }{ \partial y^i}
\]
likewise, if $\lbrace f,H \rbrace =0$, then $X_Hf =0$, $X_Hf = \mathcal{L}_{X_H} f= 0 $, so $f$ constant on flow of $X_H$
\item[(b)] Recall smooth $V \in \mathfrak{X}(M)$ infinitesimal symmetry of $(M,\omega,H)$ if $\omega, H$ invariant under flow of $V$, i.e.
\[
\begin{aligned}
  & \mathcal{L}_V \omega =0
  &  \mathcal{L}_VH = 0
\end{aligned}
\]
smooth $V\in \mathfrak{X}(M)$ symplectic if $\omega$ invariant under flow of $V$, i.e. $\mathcal{L}_V\omega =0$
\[
\mathcal{L}_VH = VH = 0
\]
\item[(c)] EY : 20150521 I'm not sure how to go about this because what is a trajectory?

$\gamma: I \to M$

$\theta$ flow of an infinitesimal symmetry, so (collecting facts)

\[
\begin{aligned}
  & \mathcal{L}_{\dot{\theta}}\omega = di_{\dot{\theta}}\omega + i_{\dot{\theta}}d\omega = di_{\dot{\theta}}\omega \quad \, (\omega \text{ closed so } i_{\dot{\theta}}d\omega) \\
  & \dot{\theta}H = 0
\end{aligned}
\]

Now $\theta_s \circ \gamma : I \to M$

\[
\frac{d}{dt}(\theta_s \circ \gamma)(t) = (D\theta_s)(\gamma(t)) \dot{\gamma}(t) = V_{s,\gamma(t)} \dot{\gamma}(t)
\]
\end{enumerate}


\documentclass[a4paper,12pt]{extarticle}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}

\renewcommand\familydefault{\sfdefault}
\usepackage{tgheros}
\usepackage[defaultmono]{droidmono}

\usepackage{amsmath,amssymb,amsthm,textcomp}
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{tikz}

\usepackage{geometry}
\geometry{left=25mm,right=25mm,%
bindingoffset=0mm, top=20mm,bottom=20mm}

\linespread{1.3}

\newcommand{\linia}{\rule{\linewidth}{0.5pt}}

% custom theorems if needed
\newtheoremstyle{mytheor}
    {1ex}{1ex}{\normalfont}{0pt}{\scshape}{.}{1ex}
    {{\thmname{#1 }}{\thmnumber{#2}}{\thmnote{ (#3)}}}

\theoremstyle{mytheor}
\newtheorem{defi}{Definition}

% my own titles
\makeatletter
\renewcommand{\maketitle}{
\begin{center}
\vspace{2ex}
{\huge \textsc{\@title}}
\vspace{1ex}
\\
\linia\\
\@author \hfill \@date
\vspace{4ex}
\end{center}
}
\makeatother
%%%

% custom footers and headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{}
\lfoot{Intro to Kalman Filter}
\cfoot{}
\rfoot{Page \thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%

% code listing settings
\usepackage{listings}
\lstset{
    language=Python,
    basicstyle=\ttfamily\small,
    aboveskip={1.0\baselineskip},
    belowskip={1.0\baselineskip},
    columns=fixed,
    extendedchars=true,
    breaklines=true,
    tabsize=4,
    prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
    frame=lines,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    keywordstyle=\color[rgb]{0.627,0.126,0.941},
    commentstyle=\color[rgb]{0.133,0.545,0.133},
    stringstyle=\color[rgb]{01,0,0},
    numbers=left,
    numberstyle=\small,
    stepnumber=1,
    numbersep=10pt,
    captionpos=t,
    mathescape=true,
    escapeinside={\%*}{*)}
}

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

\begin{document}

\title{ Extended Kalman Filter }

\author{ Dhruv Patel }

\maketitle

\section*{ Why Extended Filter is required ? }

If we already have Kalman Filter, first question in mind would be why do we require another alternative to that ... ?

Well, in real life, the assumptions of \emph{linear state transitions} and linear measurements with added \emph{Gaussian noise} are only found on the rarest of occasions. Let's see what I mean by that using an example.
" A robot that moves with constant translational and rotational velocity typically moves on a circular trajectory, which cannot be described by linear next state transitions. This fact, along with the assumption of unimodal beliefs, renders the simple Kalman Filter adequate enough for most trivial robotics problems. This Extended Kalman Filter (EKF) overcomes one of these assumptions : the linearity assumption. Here, the assumption is that the next state probability and the measurement probabilities are governed by nonlinear functions $g$ and $h$, respectively :

\begin{equation}
x_{t} = g(u_{t}, \: x_{t-1}) + \varepsilon_{t}
\end{equation}
\begin{equation}
z_{t} = h(x_{t}) + \delta_{t}
\end{equation}

This model strictly generalizes the linear Gaussian model underlying Kalman filters, postulated in Equations $1$ and $2$. The function $g$ replaces the matrices $A_{t}$ and $B_{t}$ in previous filter type, while $h$ replaces the matrix $C_{t}$. \textbf{Unfortunately, with arbitrary functions $g$ and $h$, the belief is no longer a Gaussian}. In fact, performing the belief update exactly is usually impossible for non-linear functions $g$ and $h$, in the sense that the Bayes filter does not possess a closed-form solution.
\newline
\newline
\textbf{So how come this EKF performs better than Kalman Filter?}

The extended Kalman filter (EKF) calculates an approximation to the true belief. Particularly, the belief $bel(x_{t})$ at time $t$ is represented by a mean $\mu_{t}$ and a covariance $\Sigma_{t}$. Thus, the EKF inherits from the Kalman filter the basic belief representation, but it differs in that this belief is only approximate, not exact as was the case in Kalman filters.

\vfill
% Pseudo Code - Extended Kalman Filter
\section*{The EKF Algorithm}

The algorithm, in many ways, is similar to the KF.

\begin{lstlisting}
Algorithm Kalman Filter($\mu_{t-1}, \Sigma_{t-1}, u_t, z_t$):
	# Prediction Step : ref. as Motion Model
	$\overline{\mu}_t =  g(u_{t}, \mu_{t-1})$
	$\overline{\Sigma}_t = G_t * \Sigma_{t-1} * G_t^T  +  R_t$
	# Update Step : ref. as Measurement Model
	$K_t = \overline{\Sigma}_t * H_t^T * (H_t * \overline{\Sigma}_t * H_t^T  +  Q_t)^{-1}$
	$\mu_t = \overline{\mu}_t + K_t * (z_t - h({\mu}_t))$
	$\Sigma_t = ( I - K_t * H_t ) * \overline{\Sigma}_t$
	return $\mu_t , \Sigma_t$

\end{lstlisting}

\begin{table}[h!]
   \begin{center}
    \caption{Comparison - Kalman Filter vs EKF}
    \label{tab:table1}

    \begin{tabular}{l|c|r} % <-- Alignments: 1st column left, 2nd middle and 3rd right, with vertical lines in between

      \textbf{} & \textbf{Kalman Filter} & \textbf{EKF}\\
      \hline
      state prediction (line 2) & $A_t * \mu_{t-1} + B_t * u_t$ & $g(u_t, \mu_{t-1})$\\
      measurement prediction (line 5) & $C_t * \overline{\mu}_t$ & $h(\overline{\mu}_t)$\\

    \end{tabular}
    \end{center}
\end{table}

In summary, the linear predictions in Kalman filters are replaced by their nonlinear generalizations in EKF. Also, EKF uses Jacobians $G_t$ and $H_t$ instead of the corresponding linear system matrices $A_t$, $B_t$ and $C_t$ in Kalman counterpart. The Jacobian $G_t$ corresponds to the matrices $A_t$ and $B_t$, while the Jacobian $H_t$ corresponds to $C_t$.

The Kalman Filter represents beliefs by the moments parameterization : At time $t$, the belief $bel(x_t)$is represented by the mean $\mu_t$ and the covariance $\Sigma_t$. The input of the Kalman filter is the belief at time $t-1$, represented by $\mu_{t-1}$ and $\Sigma_{t-1}$. To update these parameters, Kalman filters require the control $u_t$ and the measurement $z_t$. The output is the belief at time $t$, represented by $\mu_t$ and $\Sigma_t$. This predicted belief $\mu_t$ and $\Sigma_t$ is calculated representing the belief $\overline{bel}(x_t)$ one time step later, but before incorporating the measurement $z_t$. This belief is obtained by incorporating the control $u_t$. The update of the covariance considers the fact that states depend on previous states through the linear matrix $A_t$. This matrix is multiplied twice into the covariance, since the covariance is a quadratic matrix.

\vfill
\section*{Considerations}

The EKF has become just about the most popular tool for state estimation in robotics. Its strength is its simplicity and in its efficiency.

The reason for the computational efficiency is the fact that it (EKF) represents the belief by a multivariate Gaussian distribution. A Gaussian is a unimodal distribution, which can be thought of as a single guess, illustrated as an uncertainty ellipse. In many practical situations, Gaussians are robust estimators. EKF has been applied with great success to a number of state estimation problems that violate the underlying assumptions.

\textbf{An important limitation} : It approximates state transitions and measurements using Linear Taylor expansions. In most of the robotics problems, these functions are nonlinear. The goodness of this approximation depends on two main factors. First, it depends on the degree of nonlinearity of the functions that are being approximated. If these functions are approximately linear, the EKF approximation may generally be a good one, and EKF may approximates the posterior belief with sufficient accuracy. However, sometimes, the functions are not only nonlinear, but are also multi-modal, in which case the linearization may be a poor approximation. The goodness of the linearization also depends on the degree of uncertainty. The less certain the robot, the wider its Gaussian belief, and the more it is affected by nonlinearities in the state transition and measurement functions. In practice, when applying EKF, it is therefore important to keep the uncertainty of the state estimate small.

\textbf{Note} : Taylor series expansion is only one way to linearize. Two other approaches have often been found to yield superior results. One is the \emph{unscented Kalman filter}, which probes the function to be linearized at selected points and calculates a linearized approximation based on the outcomes of these probes. Another is known as moments matching, in which the linearization is calculated in a way that preserves the true mean and the true covariance of the posterior distribution (which is not the case for EKF). Both techniques are relatively recent but appear to be superior to the EKF linearization.

\section*{ A brief explanation about the variables }

The state transition probability $ p( x_t | u_t, x_{t-1} ) $ must be a linear function in its arguments with added Gaussian noise. This is expressed by the following equation :
\begin{equation} \label{eq1}
x_t =  A_t * x_{t-1}  +  B_t * u_t  +  \varepsilon_t
\end{equation}
Here, $x_t$ and $x_{t-1}$ are state vectors, and $u_t$ is the control vector at time $t$. These vectors are column vectors. They are of the form
\begin{equation}
x_t = \left( \begin{array}{c} x_{1,t} \\ x_{2,t} \\ . \\ . \\  x_{n,t} \end{array} \right)
\mbox{~and~}
u_t = \left( \begin{array}{c} u_{1,t} \\ u_{t,2} \\ . \\ . \\ u_{m,t} \end{array} \right)
\end{equation}
$A_t$ and $B_t$ are matrices. $A_t$ is a square matrix of size $ n \times n $, where $n$ is the dimension of the state vector $x_t$. $B_t$ is of size $ n \times m$, with $m$ being the dimension of the control vector $u_t$. By multiplying the state and control vector with the matrices $A_t$ and $B_t$, respectively, the state transition function becomes \emph{linear} in its arguments. Thus, Kalman Filters assume linear system dynamics.

The random variable $\varepsilon_t$ in (1) is a Gaussian random vector that models the uncertainty introduced by the state transition. It is of the same dimension as the state vector. It has zero mean, and its covariance will be denoted by $R_t$. A state transition probability of the form (1) is called a \emph{linear Gaussian}, to reflect the fact that it is linear in its arguments with additive Gaussian noise.

The measurement probability $ p(z_t | x_t ) $ must also be \emph{linear} in its arguments, with added Gaussian noise :
\begin{equation}
z_t = C_t * x_t + \delta_t
\end{equation}
Here, $C_t$ is a matrix of size $k \times n$, where $k$ is the dimension of the measurement vector $z_t$. The vector $\delta_t$ describes the measurement noise. The distribution of $\delta_t$ is a multivariate Gaussian with zero mean and covariance $Q_t$.

\vfill

\section*{ Given Problem }

Here, we have been given a simple target tracking problem in one-dimensional space. The state contains three components : position (one-dimensional), velocity and acceleration and it can be expressed as below.
\begin{equation}
x = \left( \begin{array}{c} x_k \\ \dot{x}_k \\ \ddot{x}_k \end{array} \right)
\end{equation}
where, $x_k$ is the position, $\dot{x}_k$ is the velocity and $\ddot{x}_k$ is the acceleration at time '$k$'. $T$ is the size of sample time step.

The process equation for target motion is given by the following kinematic equation:
\begin{equation}
\left[ \begin{array}{c} x_k \\ \dot{x}_k \\ \ddot{x}_k \end{array} \right] =
\left[ \begin{array}{ccc} 1 & T & \frac1{2}T^2 \\ 0 & 1 & T \\ 0 & 0 & 1  \end{array} \right] * \left[ \begin{array}{c} x_{k-1} \\ \dot{x}{k-1} \\ \ddot{x}{k-1} \end{array} \right] + \upsilon_{k-1}
\end{equation}
and
\begin{equation}
\upsilon_{k-1} \sim N(0, Q)
\end{equation}
where,
$ Q = \sigma^2 * \left[ \begin{array}{ccc} \frac{T^4}{4} & \frac{T^3}{2} & \frac{T^2}{2} \\ \frac{T^3}{2} & 2T^3 & T^2 \\ \frac{T^2}{2} & T^2 & T^2  \end{array} \right] $, $\sigma$ represents intensity of Gaussian noise.

Targets are usually tracked with the help of sensors such as radars or lidars which provide the position of the target. Hence, the measurement equation can be written as:
\begin{equation}
y_k =
\left[ \begin{array}{ccc} 1 & 0 & 0 \end{array} \right] * \left[ \begin{array}{c} x_k \\ \dot{x}_k \\ \ddot{x}_k \end{array} \right] + \omega
\end{equation}
where, $y_k$ is the measurement and $\omega \sim N(0, R) $ is the measurement noise.

\hfill

For the given problem, obtain the estimate of its state over a period of $ 20 ~sec $. Assume time step $ T = 0.1 ~sec $.

\end{document}


\RequirePackage[l2tabu, orthodox]{nag} % This gives you warnings if you use obsolete LaTeX commands.
\documentclass[12pt,reqno]{amsart}
\usepackage{fullpage,amssymb,amsbsy,url,enumerate,color,comment,colonequals,graphicx,ifthen,mathrsfs,stmaryrd,bm}
\usepackage[all]{xy}
\usepackage[ruled, linesnumbered]{algorithm2e}

% Color!
\usepackage[dvipsnames,xcdraw,hyperref]{xcolor}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\newcommand{\green}[1]{{\color{darkgreen} #1}}
\newcommand{\blue}[1]{{\color{blue} #1}}
\newcommand{\red}[1]{{\color{red} #1}}
\newcommand{\magenta}[1]{{\color{magenta} #1}}
\newcommand{\violet}[1]{{\color{violet} #1}}

\newcommand{\defi}[1]{\textsf{\color{blue} #1}} % for defined terms

% Characters
\newcommand{\Aff}{\mathbb{A}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\G}{\mathbb{G}}
\newcommand{\bbH}{\mathbb{H}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\PP}{\mathbb{P}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Sphere}{\mathbb{S}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Qbar}{{\overline{\Q}}}
\newcommand{\Zhat}{{\widehat{\Z}}}
\newcommand{\Zbar}{{\overline{\Z}}}
\newcommand{\kbar}{{\overline{k}}}
\newcommand{\Kbar}{{\overline{K}}}
\newcommand{\Fbar}{{\overline{\F}}}

\newcommand{\boldmu}{\bm{\mu}}
\newcommand{\boldalpha}{\bm{\alpha}}

\newcommand{\ii}{\mathbf{i}}
\newcommand{\jj}{\mathbf{j}}
\newcommand{\kk}{\mathbf{k}}

\newcommand{\pp}{\mathfrak{p}}
\newcommand{\mm}{\mathfrak{m}}

% mathcal characters
\newcommand{\calA}{\mathcal{A}}
\newcommand{\calB}{\mathcal{B}}
\newcommand{\calC}{\mathcal{C}}
\newcommand{\calD}{\mathcal{D}}
\newcommand{\calE}{\mathcal{E}}
\newcommand{\calF}{\mathcal{F}}
\newcommand{\calG}{\mathcal{G}}
\newcommand{\calH}{\mathcal{H}}
\newcommand{\calI}{\mathcal{I}}
\newcommand{\calJ}{\mathcal{J}}
\newcommand{\calK}{\mathcal{K}}
\newcommand{\calL}{\mathcal{L}}
\newcommand{\calM}{\mathcal{M}}
\newcommand{\calN}{\mathcal{N}}
\newcommand{\calO}{\mathcal{O}}
\newcommand{\calP}{\mathcal{P}}
\newcommand{\calQ}{\mathcal{Q}}
\newcommand{\calR}{\mathcal{R}}
\newcommand{\calS}{\mathcal{S}}
\newcommand{\calT}{\mathcal{T}}
\newcommand{\calU}{\mathcal{U}}
\newcommand{\calV}{\mathcal{V}}
\newcommand{\calW}{\mathcal{W}}
\newcommand{\calX}{\mathcal{X}}
\newcommand{\calY}{\mathcal{Y}}
\newcommand{\calZ}{\mathcal{Z}}

\newcommand{\CC}{\mathscr{C}}
\newcommand{\FF}{\mathscr{F}}
\newcommand{\GG}{\mathscr{G}}
\newcommand{\scriptH}{\mathscr{H}}
\newcommand{\II}{\mathscr{I}}
\newcommand{\JJ}{\mathscr{J}}
\newcommand{\KK}{\mathscr{K}}
\newcommand{\LL}{\mathscr{L}}
\newcommand{\OO}{\mathscr{O}}
\newcommand{\XX}{\mathscr{X}}
\newcommand{\ZZ}{\mathscr{Z}}

% various math expressions that shouldn't be italicized
\DeclareMathOperator{\cis}{cis}
\DeclareMathOperator{\var}{var}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator*{\lcm}{lcm}
\DeclareMathOperator{\ord}{ord}
\DeclareMathOperator{\Ker}{Ker}
\DeclareMathOperator{\Bin}{Bin}
\DeclareMathOperator{\res}{res}
\DeclareMathOperator{\rad}{rad}
\DeclareMathOperator{\Spec}{Spec}

\newcommand{\Cech}{\v{C}ech}
\newcommand{\del}{\partial}
\newcommand{\directsum}{\oplus} % binary direct sum
\newcommand{\Directsum}{\bigoplus} % direct sum of a collection
\newcommand{\injects}{\hookrightarrow}
\newcommand{\intersect}{\cap} % binary intersection
\newcommand{\Intersection}{\bigcap} % intersection of a collection
\newcommand{\isom}{\simeq}
\newcommand{\HH}{{\operatorname{H}}}
\newcommand{\HHcech}{{\check{\HH}}}
\newcommand{\HHat}{{\hat{\HH}}}
\newcommand{\notdiv}{\nmid}
\newcommand{\surjects}{\twoheadrightarrow}
\newcommand{\tensor}{\otimes} % binary tensor product
\newcommand{\Tensor}{\bigotimes} % tensor product of a collection
\newcommand{\To}{\longrightarrow}
\newcommand{\union}{\cup} % binary union
\newcommand{\Union}{\bigcup} % union of a collection

%Mau's
\newcommand{\ra}[1][]{\xrightarrow{#1}}
\newcommand{\f}[2]{\frac{#1}{#2}}
\DeclareMathOperator{\id}{id}
\newcommand{\be}{\mathbf{e}}
\newcommand{\bd}{\mathbf{d}}
\newcommand{\eps}{\varepsilon}
\DeclareMathOperator{\Stab}{Stab}
\DeclareMathOperator{\Span}{Span}
\DeclareMathOperator{\im}{im}
\DeclareMathOperator{\sgn}{sgn}

%%% \numberwithin{equation}{section}
%%% \newtheorem{theorem}[equation]{Theorem}
%%% etc.

% We use one numbering for all theorems, lemmas, etc.
% Use \newtheorem{theorem}{Theorem}[section] to number by section, so that the first theorem in Section 3 would be Theorem 3.1.
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{question}[theorem]{Question}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{remark}
\newtheorem{example}[theorem]{Example}
\newtheorem{examples}[theorem]{Examples}
\newtheorem{remark}[theorem]{Remark}
\newtheorem{remarks}[theorem]{Remarks}

\newenvironment{problem}
	{
		\bigskip
		\noindent
		{\large\textbf{Problem.}}
		\newline
	}
	{
		\bigskip
	}

\newenvironment{solution}[1]
	{
		\bigskip
		\noindent
		{\large\textbf{Solution to #1.}}
		\newline
	}
	{
		\ensuremath{\blacksquare} \bigskip
	}

\usepackage{microtype}  % This adjusts spacing between words so as to improve the probability of having line breaks in good places.

\usepackage[
%	draft,
%	colorlinks,
%	pagebackref,
%	pdfauthor={Authors go here}, % not necessary to enter this
%	pdftitle={Paper title}, % not necessary to enter this
]{hyperref} % This allows you to put hyperlinks in your document.

% \begin{center}
%    \includegraphics[width=0.5\textwidth]{Filename}
% \end{center}
\usepackage[utf8]{inputenc}

\title{Tetrahedra UROP Project 2}
\author{Mauricio Barba da Costa}
\date{September 2020}

\begin{document}

\maketitle
\section{Introduction}
A tetrahedron is a polyhedron with 4 faces. Suppose $T$ is a tetrahedron with angles
$\alpha_{12},\alpha_{13},\alpha_{14},\alpha_{23},\alpha_{24},\alpha_{34}$.
My team and I were curious about tetrahedra whose angles, when viewed as elements of
$\R/2\pi\Q$, span a 5-dimensional $\Q$ vector space. In particular, we wanted to know
if one such tetrahedra could have Dehn invariant zero. Showing that no such tetrahedron
with this property have Dehn invariant zero would have serious ramifications. For instance, Debrunner showed
that if a polyhedron $P$ tiles 3D space then it must have Dehn invariant zero.
\section{The Problem}
Given a tetrahedron $T$, enumerate the vertices 1,2,3,4. Denote $e_{ij}$
as the edge between vertices $i$ and $j$ and $\theta_{ij}$ as the dihedral
angle of $e_{ij}$. One convention that I follow throughout this paper
is listing edges and angles using the ordering $12,13,14,23,24,34$.
Suppose we're given dihedral angles $\theta_{12},...,\theta_{34}\in \R/\Q\pi$
that span a 5-dimensional $\Q$-vector space.
For a tetrahedron with such angle to have Dehn invariant 0, the edge
lengths $e_{12},...,e_{34}$ must be proportional to a tuple of positive
integers such that $\sum_{i<j} e_{ij}\theta_{ij}=0 \mod{\Q\pi}$. Let $z_{ij}=e^{i\theta_{ij}}$.
Let $w_{ij}=e^{i(2\theta_{ij})}$. Then $2\cos\theta_{ij}=z_{ij}+z_{ij}^{-1}$
and $2\cos2\theta_{ij}=w_{ij}+w_{ij}^{-1}$. By Theorem 1 of Wirth-Dreiding,
$\cos\theta_{ij}=\frac{D_{ij}}{\sqrt{D_{ijk}D_{ijl}}}\in \sqrt{\Q}$
and consequently $\cos2\theta_{ij}=2\cos^2\theta_{ij}-1\in \Q$. Then $w_{ij}$
satisfies the polynomial relation $w_{ij}^2-c_{ij} w_{ij}+1=0$ where
$c_{ij}=2\cos2\theta_{ij}$.
\section{Numerical Approach}
The code I developed as part of this project is distributed across 3 branches of my forked
repository of Abdelatiff Chentouf Anas' original work. It includes my p-adic approach
to resolving the problem, my numerical approach, and some functions that were generally essential for
making computations with tetrahedra.

Suppose $\sum_{i<j} e_{ij}\theta_{ij}=0\mod{\Q\pi}$. Then $\sum e_{ij}\theta_{ij}=q\pi$
for some $q\in \Q$ so $\sum_{i<j} e_{ij}\theta_{ij} i=iq\pi$. Then $\prod z_{ij}^{e_{ij}}$ is a root
of unity in a field obtained by adjoining square roots to $\Q$. Now,
$Gal(\Q(\zeta_n/\Q)=(\Z/n\Z)^\times$ so $\zeta_n\in \Q(\sqrt{\Q})$
if and only if $(\Z/n\Z)^\times$
is an elementary 2-group. This forces $(\Z/n\Z)^\times=\prod_{p|n}(\Z/p^{e_p}\Z)^\times$
where $e_2\leq 3,e_3\leq 1$ and $e_p=0$ for all other primes $p$. Thus,
$n|24$. Thus, $(\prod_{j=1}^6 w_{ij}^{e_{ij}})^{24}=1$. For a tetrahedra $T$, we can
calculate $W=\prod_{j=1}^6 w_{ij}^{e_{ij}})^{24}$ with a computer and see if it falls
within some $\epsilon$ of 1. To identify a counterexample, I iterated over randomly generated
sextuples, checked if they determined a tetrahedron according to Lemma 4 of Wirth-Dreiding.
I had some setbacks when I undertook this approach. For a while, I was applying the conditions
for Lemma 4 of Wirth-Dreiding into my computer incorrectly. When I did realize that I was
doing it wrong, I had to end my AWS EC2 instance because my free plan was running out.
It might be worth further exploring this approach.
\section{p-adic Approach}
If we think about p-adics now, if $\sum_{i<j}e_{ij}\theta_{ij}=0\mod{\Q\pi}$ then $\sum e_{ij}v_p(w_{ij})=0$
for every prime $p$. Now, $c_{ij}\in \Q$ so
computing $v_p(c_{ij})$ is easy. Calculating, $v_p(w_{ij})$
is more difficult to calculate since it's in a field extension of $\Q$.
We can use the following tool:
$$
v_p(w_{ij})=\begin{cases}
  0&v(c_{ij})\geq 0\\
  \pm v(c_{ij})&v(c_{ij})<0
\end{cases}
$$
If $v_p(w_ij)<0$, whether $v_p(w_{ij})=+v(c_{ij})$ or $v_p(w_{ij})=-v(c_{ij})$ we can't know.
\section{My algorithm}
My algorithm exploits the p-adic approach to the problem to find tetrahedra
whose angles span a 5-dimensional $\Q$-vector space and have Dehn invariant zero.
It does this by randomly generating sextuples, checking if they determine a
tetrahedra (using Lemma 4 of Wirth-Dreiding). If it is a tetrahedron, then it calcualtes the denominator
of $c_{ij}$ when it is expressed in simplest terms. Recall that
$$
c_{ij}=2\cos2\theta_j=4\cos^2\theta_j-2=\frac{4D_{ij}^2-2D_{ijk}D_{ijl}}{D_{ijk}D_{ijl}}
$$
Now, the denominator can be expressed as
$$
L_{ij}=\frac{D_{ijk}D_{ijl}}{\gcd(D_{ijk}D_{ijl},4D_{ij}^2-2D_{ijk}D_{ijl})}=
\frac{D_{ijk}D_{ijl}}{\gcd(D_{ijk}D_{ijl},4D_{ij}^2)}
$$
Then, it gets the prime factors of all these. Then, it
finds the valuations of all of these with respect to all the primes. We
can arrange these $L$s into a list $[L_{12},L_{13},L_{14},L_{23},L_{24},L_{34}]$.
Taking the valuation with respect to $p$ of these yields
$$
v_p(L_{ij})=\begin{cases}
  0&v_p(c_{ij})\geq 0\\
  \pm v_p(c_{ij})&v_p(c_{ij})<0
\end{cases}
$$
If $\sum_{i<j}\pm e_{ij}v_p(L_{ij})=0$ for some combination of pluses and minuses,
this is a necessity for the condition to hold. It is not a sufficiency since we don't know
which combination of pluses and minuses is right. Here is how I checked if
$\sum_{i<j}\pm e_{ij}v_p(L_{ij})=0$ computationally:

For every prime $p$
\begin{enumerate}
  \item Identify all $L_{ij}$ such that $v_p(L_{ij})>0$. Suppose there are $m$ such
  $L_{ij}$s. Insert these valuatioins into an $1\times m$ matrix $VL$.
  \item Create an $2^m\times m$ matrix $M$ where the elements in the $i$th row
  are the digits of the $i$th integer (starting from 0) when expressed in base 2.
  \item Perform an elementwise multiplication $M\star VL$ then the matrix
  multiplication $(M\star VL)\times E$ where
  $$
  E=\begin{pmatrix}
    e_{12}\\e_{13}\\e_{14}\\e_{23}\\e_{24}\\e_{34}
  \end{pmatrix}
  $$
  \item Use np.any($(M\star VL)\times E==0$) to see if any combination
  of pluses and minuses yielded the correct result.
\end{enumerate}
Not unless np.any($(M\star VL)\times E==0$) for all primes is the tetrahedron a candidate
for having Dehn invariant zero. This seems to never happen.
\section{Some Observations and Analysis}
I have a \href{https://github.com/mauriciobarba/AC-18.Tetra/tree/main}{GitHub Repo}
where the code for this project is stored. In here is also a file that's the result
of running the p-adic analysis of my code. Here is an example output:\\

\begin{verbatim}
[39, 82, 56, 47, 56, 90]
[2314830, 4265533440, 36952226532, 384102810, 3407333959107, 796296384]
193 [0, 0, 0, 1, 1, 1]
2 [1, 13, 2, 1, 0, 6]
3 [1, 3, 2, 7, 6, 2]
5 [1, 1, 0, 1, 0, 0]
7 [1, 1, 0, 1, 0, 0]
73 [1, 0, 1, 0, 1, 0]
13 [0, 0, 2, 1, 3, 1]
19 [0, 1, 1, 0, 0, 1]
151 [1, 0, 1, 0, 1, 0]
29 [0, 1, 1, 0, 0, 1]
primes passed: {3}
\end{verbatim}
This is how you interpret the results:
\begin{enumerate}
  \item On the first line is randomly generated sextuple of integers that denote the edges of a tetrahedron
  in the order that we've been using $(e_{12},e_{13},e_{14},e_{23},e_{24},e_{34})$.
  \item In the second line is the list $[L_{12},L_{13},L_{14},L_{23},L_{24},e_{34}]$.
  \item In the lines below that, the number in the first column is a prime that divides
  some $L_{ij}$.
  \item The primes that ``passed" are those such that there exists a combination
  of pluses and minuses such that $\sum_{i<j}\pm e_{ij}v_p(L_{ij})=0$.
\end{enumerate}

The Erdos-Kac Theorem states that the probability distribution of
$$
\frac{\omega(n)-\log\log n}{\sqrt{\log\log n}}
$$
where $\omega(n)$ is the number of distinct
prime factors of $n$
is the standard normal distribution. With this, we can calculate the average
number of distinct prime factors of a number with 9 digits to be 3
with standard deviation approximately 1.6 (need to cite
and check if 1.6 is right). $L_{ij}$
is usually around 9 digits and has more than 3 distinct prime factors.
For instance, from looking at the table above,
$$
384102810=193\times 2\times 3^7\times 5\times 7\times 13
$$
However, the primes that comprise 384102810 tend to be smaller and have a
greater power.
By contrast 1,000,000,003=$23\times 307\times 141623$. This suggests that the
prime factorizations of the $L_{ij}$s is somewhat anomalous. Further exploring why
this is might bear some fruit.

The most common valuation list is [1,1,0,1,0,0]
or some tetrahedral rotation of it. Also, you never get [2,0,0,0,0,0,2] or any
tetrahedral rotation of this.
Which valuation lists are possible? I looked at valuation lists mod 2. I found that only a
few valuation lists are possible mod 2. They are
$$
  [1,1,0,0,1,1]
$$
$$
  [1,1,0,1,0,0]
$$
$$
  [0,0,0,0,0,0]
$$
Maybe we can prove the claim
by showing that for every tetrahedron, there exists a prime $p$ that
yields a valuation list [1,1,0,1,0,0] (or some tetrahedral rotation of it). Note that it's not possible that
$\pm e_{12}\pm e_{13}\pm e_{23}=0$ otherwise this would lead to a degenerate tetrahedron because
one of the faces violates the triangle inequality.

In general, my findings support the conjecture that there are no tetrahedra whose angles
span a 5-dimensional $\Q$ vector space. The output of my code showed that seldom does
a prime pass.
\end{document}


\documentclass[fontsize=9pt, parskip=half, notitlepage, fleqn]{scrartcl}

\usepackage[UKenglish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amssymb, amsmath, amsfonts, amsthm, empheq}
\usepackage[dvipsnames]{xcolor}
\usepackage{xspace}
\usepackage{siunitx}
\usepackage[margin=3cm]{geometry}
\usepackage{natbib}

\frenchspacing

% So equation numbers are not coloured
\makeatletter
\renewcommand\tagform@[1]{%
   \maketag@@@{\normalcolor\ignorespaces(#1)\unskip\@@italiccorr}}
\makeatother

\newcommand{\bkcol}[1]{\textcolor{MidnightBlue}{#1\xspace}}
\newcommand{\hun}{\textsc{Hun15}\xspace}
\newcommand{\book}{\bkcol{\textsc{Book}}\xspace}
\newcommand{\empymod}{\texttt{empymod}\xspace}
\newcommand{\tmtemod}{\texttt{tmtemod.py}\xspace}
\newcommand{\mr}[1]{\mathrm{#1}}

\begin{document}
\setlength{\jot}{10pt}  % Increase spacing between equations

\section*{Adjust Hunziker et al. (2015) for TM/TE-split}

{\ttfamily \small Version 1.0, \today

\hun refers to \cite{GEO.15.Hunziker}, \book refers to the derivation of
\cite{CUP.17.Ziolkowski}.}

The modeller \empymod returns the total field, hence not distinguishing between
TM and TE mode, and even less between up- and down-going fields. The reason
behind this is simple: The derivation of \hun, on which \empymod is based,
returns the total field. Internally it also calculates TM and TE modes, and
sums these up. However, the separation into TM and TE mode introduces a
singularity at $\kappa = 0$. It has no contribution in the space-frequency
domain to the total fields, but it introduces non-physical events in each mode
with opposite sign (so they cancel each other out in the total field). In
order to obtain the correct TM and TE contributions one has to remove these
non-physical parts.

To remove the non-physical part we use the file \tmtemod in this
directory. This routine is basically a heavily simplified version of \empymod
with the following limitations:
\begin{itemize}
  \item x-directed electric sources and electric receivers;
  \item only frequency domain;
  \item direct field is always calculated in the wavenumber-domain;
  \item the Fast Hankel transform is used with a 201\,pt filter;
  \item source and receivers have to be in the same layer;
  \item the model must have more than one layer (there is only direct field
    contribution anyway for a fullspace); and
  \item electric permittivity and magnetic permeability are isotropic.
\end{itemize}

So \tmtemod returns the signal separated into TM$^{++}$, TM$^{+-}$, TM$^{-+}$,
TM$^{--}$, TE$^{++}$, TE$^{+-}$, TE$^{-+}$, and TE$^{--}$ as well as the direct
field TM and TE contributions. The first superscript denotes the direction in
which the field diffuses towards the receiver and the second superscript
denotes the direction in which the field diffuses away from the source. For
both the plus-sign indicates the field diffuses in the downward direction and
the minus-sign indicates the field diffuses in the upward direction. The
routine uses \empymod wherever possible, see the corresponding functions in
\empymod for more explanation and documentation regarding input parameters.

We start with equation (105) in \hun:
%
\begin{align}
  \hat{G}^{ee}_{xx}(\boldsymbol{x}, \boldsymbol{x'}, \omega)& =
  \hat{G}^{ee;i}_{xx;s}(\boldsymbol{x}-\boldsymbol{x'}, \omega)
  + \frac{1}{8\pi}\int^\infty_{\kappa=0}
  \left(\frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s}-
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}\right)
  J_0(\kappa r)\kappa\,\mr{d}\kappa \nonumber\\
  %
  &\quad - \frac{\cos(2\phi)}{8\pi}\int^\infty_{\kappa=0}
  \left(\frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s} +
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}\right)
  J_2(\kappa r)\kappa\,\mr{d}\kappa \ .
\end{align}
%

Ignoring the incident field, and using $J_2 = \frac{2}{\kappa r}J_1 - J_0$ to
avoid $J_2$-integrals, we get
%
\begin{align}
  \hat{G}^{ee}_{xx}(\boldsymbol{x}, \boldsymbol{x'}, \omega)& =
  \frac{1}{8\pi}\int^\infty_{\kappa=0}
  \left(\frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s}-
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}\right)
  J_0(\kappa r)\kappa\,\mr{d}\kappa \nonumber\\
  %
  &\quad + \frac{\cos(2\phi)}{8\pi}\int^\infty_{\kappa=0}
  \left(\frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s} +
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}\right)
  J_0(\kappa r)\kappa\,\mr{d}\kappa \nonumber\\
  %
  &\quad - \frac{\cos(2\phi)}{4\pi r}\int^\infty_{\kappa=0}
  \left(\frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s} +
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}\right)
  J_1(\kappa r)\,\mr{d}\kappa \ .
\end{align}
%

From this the TM- and TE-parts follow as
%
\begin{align}
  \mr{TE}& = \frac{\cos(2\phi)-1}{8\pi}\int^\infty_{\kappa=0}
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}
  J_0(\kappa r)\kappa\,\mr{d}\kappa
   - \frac{\cos(2\phi)}{4\pi r}\int^\infty_{\kappa=0}
  \frac{\zeta_s \tilde{g}^{te}_{zz;s}}{\bar{\Gamma}_s}
  J_1(\kappa r)\,\mr{d}\kappa \ , \\
  %
  \mr{TM}& = \frac{\cos(2\phi)+1}{8\pi}\int^\infty_{\kappa=0}
  \frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s}
  J_0(\kappa r)\kappa\,\mr{d}\kappa
  - \frac{\cos(2\phi)}{4\pi r}\int^\infty_{\kappa=0}
  \frac{\Gamma_s \tilde{g}^{tm}_{hh;s}}{\eta_s}
  J_1(\kappa r)\,\mr{d}\kappa \ .
\end{align}
%

Equations (108) and (109) in \hun yield the required parameters
$\tilde{g}^{tm}_{hh;s}$ and $\tilde{g}^{te}_{zz;s}$,
%
\begin{align}
  \tilde{g}^{tm}_{hh;s}& = P^{u-}_s W^u_s + P^{d-}_s W^d_s \ , \\
  \tilde{g}^{te}_{zz;s}& = \bar{P}^{u+}_s \bar{W}^u_s +
                           \bar{P}^{d+}_s \bar{W}^d_s \ .
\end{align}
%

The parameters $P^{u\pm}_s$ and $P^{d\pm}_s$ are given in equations (81) and
(82), $\bar{P}^{u\pm}_s$ and $\bar{P}^{d\pm}_s$ in equations (A-8) and (A-9);
$W^u_s$ and $W^d_s$ in equation (74). This yields
%
\begin{align}
  \tilde{g}^{te}_{zz;s} &=
  %
  \frac{\bar{R}_s^+}{\bar{M}_s}\left\{\exp[-\bar{\Gamma}_s(z_s-z+d^+)] +
    \bar{R}_s^-\exp[-\bar{\Gamma}_s(z_s-z+d_s+d^-)]\right\} \nonumber \\
  %
  &\quad +
    \frac{\bar{R}_s^-}{\bar{M}_s}\left\{\exp[-\bar{\Gamma}_s(z-z_{s-1}+d^-)]+
    \bar{R}_s^+\exp[-\bar{\Gamma}_s(z-z_{s-1}+d_s+d^+)]\right\}\nonumber \ ,\\
  %
  &=\frac{\bar{R}_s^+}{\bar{M}_s}\left\{\exp[-\bar{\Gamma}_s(2z_s-z-z')] +
    \bar{R}_s^-\exp[-\bar{\Gamma}_s(z'-z+2d_s)]\right\} \nonumber \\
  %
  &\quad +
  \frac{\bar{R}_s^-}{\bar{M}_s}\left\{\exp[-\bar{\Gamma}_s(z+z'-2z_{s-1})]+
  \bar{R}_s^+\exp[-\bar{\Gamma}_s(z-z'+2d_s)]\right\}\ ,
  %
\end{align}
%
where $d^\pm$ is taken from the text below equation (67). There are four terms
in the right-hand side, two in the first line and two in the second line. The
first term in the first line is the integrand of TE$^{+-}$, the second term in
the first line corresponds to TE$^{++}$, the first term in the second line is
TE$^{-+}$, and the second term in the second line is TE$^{--}$.

If we look at TE$^{+-}$, we have
%
\begin{equation}
  \tilde{g}^{te+-}_{zz;s} =
  \frac{\bar{R}_s^+}{\bar{M}_s}\exp[-\bar{\Gamma}_s(2z_s-z-z')] \ ,
  %
\end{equation}
%
and therefore
%
\begin{align}
  \mr{TE}^{+-}& = \frac{\cos(2\phi)-1}{8\pi}\int^\infty_{\kappa=0}
  \frac{\zeta_s \bar{R}_s^+}{\bar{\Gamma}_s\bar{M}_s}
  \exp[-\bar{\Gamma}_s(2z_s-z-z')]
  J_0(\kappa r)\kappa\,\mr{d}\kappa \nonumber \\
  &\quad - \frac{\cos(2\phi)}{4\pi r}\int^\infty_{\kappa=0}
  \frac{\zeta_s \bar{R}_s^+}{\bar{\Gamma}_s\bar{M}_s}
  \exp[-\bar{\Gamma}_s(2z_s-z-z')]
  J_1(\kappa r)\,\mr{d}\kappa \ .
  \label{eq:hunte}
\end{align}
%

We can compare this to equation (4.165) in \book, with $\hat{I}^e_x=1$ and
slightly re-arranging it to look more alike, we get
%
\bkcol{
\begin{align}
  \hat{E}^{+-}_{xx;H} &= \frac{y^2}{4\pi r^2}
  \int^\infty_{\kappa=0} \frac{\zeta_1}{\Gamma_1}
  \frac{R^-_{H;1}}{M_{H;1}}
  \exp(-\Gamma_1 h^{+-})J_0(\kappa r)\kappa\rm{d}\kappa \nonumber \\
  %
  &\quad + \frac{x^2-y^2}{4\pi r^3}
  \int^\infty_{\kappa=0} \frac{\zeta_1}{\Gamma_1}
  \left(\frac{R^-_{H;1}}{M_{H;1}} -
  \frac{R^-_{H;1}(\kappa=0)}{M_{H;1}(\kappa=0)}\right)
  \exp(-\Gamma_1 h^{+-})J_1(\kappa r)\rm{d}\kappa \nonumber \\
  %
  &\quad - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^-_{H;1}(\kappa=0)}{M_{H;1}(\kappa=0)}
  \exp(-\gamma_1 R^{+-}) \ .
  \label{eq:bookte}
\end{align}
}

The equation is marked in \bkcol{blue} to make it clear that the symbols and
parameters in \hun and in \book are not exactly the same.

The difference between equations \ref{eq:hunte} and \ref{eq:bookte} is that the
first one contains non-physical contributions. These have opposite signs in
TM$^{+-}$ and TE$^{+-}$, and therefore cancel each other out. But if we want to
know the specific contributions from TM and TE we have to remove them. The
non-physical contributions only affect the $J_1$-integrals, and only for
$\kappa = 0$.

The following lists for all 8 cases the term that has to be removed, in the
notation of \book (for the notation as in \hun see the implementation in
\tmtemod):
%
\bkcol{
\begin{align}
  TE^{++} &= + \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{\exp(-\gamma_1 |h^-|) }{M_{H;1}(\kappa=0)} \ ,
  \label{eq:1}\\
  %
  TE^{-+} &= - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^+_{H;1}(\kappa=0)\exp(-\gamma_1 h^{-+}) }{M_{H;1}(\kappa=0)}\ ,
  \label{eq:2}\\
  %
  TE^{+-} &= - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^-_{H;1}(\kappa=0)\exp(-\gamma_1 h^{+-}) }{M_{H;1}(\kappa=0)}\ ,
  \label{eq:3}\\
  %
  TE^{--} &= + \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^+_{H;1}(\kappa=0)R^-_{H;1}(\kappa=0)\exp(-\gamma_1 h^{--}) }
  {M_{H;1}(\kappa=0)}\ ,
  \label{eq:4}\\
  %
  %
  TM^{++} &= - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{\exp(-\gamma_1 |h^-|) }{M_{V;1}(\kappa=0)}\ ,
  \label{eq:5}\\
  %
  TM^{-+} &= - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^+_{V;1}(\kappa=0)\exp(-\gamma_1 h^{-+}) }{M_{V;1}(\kappa=0)}\ ,
  \label{eq:6}\\
  %
  TM^{+-} &= - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^-_{V;1}(\kappa=0)\exp(-\gamma_1 h^{+-}) }{M_{V;1}(\kappa=0)}\ ,
  \label{eq:7}\\
  %
  TM^{--} &= - \frac{\zeta_1 (x^2-y^2)}{4\pi\gamma_1 r^4}
  \frac{R^+_{V;1}(\kappa=0)R^-_{V;1}(\kappa=0)\exp(-\gamma_1 h^{--}) }
  {M_{V;1}(\kappa=0)} \ .
  \label{eq:8}
\end{align}
}

Note that in equations \ref{eq:1} and \ref{eq:4} the correction terms have
opposite sign as those in equations \ref{eq:5} and \ref{eq:8} because at
$\kappa=0$ the TM and TE mode correction terms are equal. Also note that in
equations \ref{eq:2} and \ref{eq:3} the correction terms have the same sign as
those in equations \ref{eq:6} and \ref{eq:7} because at $\kappa=0$ the TM and
TE mode reflection responses in those terms are equal but with opposite sign:
\bkcol{$R^\pm_{V;1}(\kappa=0) = -R^\pm_{V;1}(\kappa=0)$}.

\hun uses $\phi$, whereas \book uses $x$, $y$, for which we can use
%
\begin{equation}
  \cos(2\phi) = -\frac{x^2-y^2}{r^2} \ .
  \label{eq:phixy}
\end{equation}

% REFERENCES
\bibliographystyle{seg}
\begin{thebibliography}{}
\itemsep0pt

\bibitem[Hunziker et~al., 2015]{GEO.15.Hunziker}
Hunziker, J., J. Thorbecke, and E. Slob, 2015, The electromagnetic response in
  a layered vertical transverse isotropic medium: A new look at an old problem:
  Geophysics, {\bf 80}, F1--F18, doi: 10.1190/geo2013-0411.1.

\bibitem[Ziolkowski and Slob, 2017]{CUP.17.Ziolkowski}
Ziolkowski, A. and E. Slob, 2017, CSEM book (TODO, add citation once the book
  is out): Cambridge University Press.  ISBN: ??????.

\end{thebibliography}

\end{document}