\section{Beading schemes}\label{sec_generalization}
A critical component in toolpath generation is how to distribute the beads over the feature radius.
While the framework presented in the previous section takes evenly distributed beads as an example, it allows to apply different beading schemes to configure the bead distribution to cater for specific requirements from the application, 3D printer or material.

\revise{
\begin{definition}\label{beading_scheme_definition}
The beading scheme is configured by the following set of functions and constants:
\begin{align*}
\{
&q(d),
B(n, r),
t(n),
t_0(n),
\\
&\alpha_\text{max},
t_\text{beading},
d_\text{max}^\text{transition},
d_\text{max}^\text{unmarked},
d_\text{max}^\text{intersection},
d^\text{discretization}
 \}
\end{align*}
where, as introduced in previous section,
$q(d)$ the quantization operator,
$B(n,r)$ is the beading function consisting of $\left( W(n,r), L(n,r) \right)$,
$t(n)$ the transition length,
$t_0(n)$ the transition anchor position,
$\alpha_{\text{max}}$ is the limit bisector angle,
$t_\text{beading}$ is the beading conflict transition length,
$d_\text{max}^\text{unmarked}$ is the filter distance for unmarked regions,
$d_\text{max}^\text{transition}$ is the filter distance for transition anchors,
$d_\text{max}^\text{intersection}$ is the reduction ratio at 3-way intersections
and
$d^\text{discretization}$ is the discretization length in vertex-vertex VD edges and vertex-line VD edges.
%$W(n, r)$ and $L(n, r)$ give the sequence of bead widths $\left\{ w_i \right\}$ and radial locations $\left\{ l_i \right\}$, respectively.
\end{definition}
}{}

\revise{
The following restrictions hold:
\begin{enumerate}
	\begin{minipage}{\columnwidth}
	\setlength\intextsep{0pt}
	\begin{wrapfigure}[4]{o}{.4\columnwidth} %\centering
	\includegraphics[width=.3\columnwidth,frame]{sources-method-transition-length-limit.pdf}
	%\caption{
	%%Transition ends (magenta and pink) shouldn't cross each other.
	%Placement of transition ends (magenta and pink) with respect to the anchor positions of the transitions (purple) on a ST edge (blue) with bisector angle $\alpha \approx \SI{135}{\degree}$.
	%The distance between the anchor position and the upper end ($t_1$) and the distance between the anchor position and the lower end of the transition ($t_0$) should add up to less than the total distance between the anchor positions, which is limited by $\alpha_\text{max}$.
	%}
	\label{transition_placement}
	\end{wrapfigure}
	\item Transitions shouldn't overlap each other: \\ $t_1(n) + t_0(n+1) < \frac{ q^{-1}(n + 1) - q^{-1}(n) }{ \cos \nicefrac12 \alpha_\text{max}}$ \\ for each $n \in \mathbb{N}$ \\ (see \cref{transition_placement})
\end{minipage}
\item An odd beading should produce an odd single toolpath exactly in the center: $B(n, r)_{\lfloor n/2 \rfloor} = r$ in case $n$ is odd
\item For the smoothness and continuity of toolpaths we require that $W_n$ is monotonic and continuous at each bead index $n$ for constant bead count $c$: $0 \leq \frac{\partial W(c, r)_n}{\partial r} \leq 1$
\end{enumerate}
}{}

\revise{}{
\begin{definition}\label{beading_scheme_definition}
A beading scheme is defined by the quantization operator $q$ and the beading operator $B$: $\{q(d), B(n ,r)\}$.
The beading function $B(n,r)$ consists of $\left( W(n,r), L(n,r) \right)$,
which provides sequences of $n$ bead widths and of $n$ distances from the outline to fill up a radial distance $r$.
\end{definition}
}

% the condition that transitions don't overlap is already caught by the way in which we defined $t_0$ in section 3.4 Smooth transitions

\revise{}{
For the smoothness and continuity of toolpaths we require that $W_n$ is monotonic and continuous at each bead index $n$ for constant bead count $c$: $0 \leq \frac{\partial W(c, r)_n}{\partial r} \leq 1$.
We further ensure
that beads don't overlap,
that beads are extruded from the center of where they end up
and that odd bead counts produce a single polyline toolpath exactly in the center
by determining the bead locations from the widths:
}
\begin{align*}
L(n,r)_i =
\begin{cases}
-\frac12 W(n,r)_i + \sum_{j=0}^i W(n,r)_j & \text{ if } i < \frac12 (n -1) \\
r & \text{ if } i =  \frac12 (n -1) \\
%d - W(n,r)_{n-1-i} & \text{ otherwise }\\
\end{cases}
\end{align*}

% \subsection{Beading schemes}
We introduce several beading schemes which determine the bead count and their widths in various ways.
We can emulate a variety of toolpath generation methods from related literature by defining new beading schemes.
We also introduce new beading schemes which produce toolpaths with less extreme widths compared to techniques from existing literature.

\revise{
A beading scheme is defined as a set of some particular variables and functions (\cref{beading_scheme_definition}).
Our beading schemes are based on a preferred width $w^* = \SI{0.4}{\milli\meter}$, which is equal to the diameter of the printing nozzle.
Most of the beading schemes we introduce share a common ground:
\begin{align*}
L(n,r)_i =
\begin{cases}
-\frac12 W(n,r)_i + \sum_{j=0}^i W(n,r)_j & \text{ if } i < \frac12 (n -1) \\
r & \text{ if } i =  \frac12 (n -1) \\
%d - W(n,r)_{n-1-i} & \text{ otherwise }\\
\end{cases}
\\
\begin{array}{rlrl}
t_\text{beading} &= w^*
&
d_\text{max}^\text{transition} &= \SI{1}{\milli\meter}
\\
t(n) &= w^*
&
d_\text{max}^\text{unmarked} &= w^*
\\
t_0(n) &=  t(n) \left( q^{-1}(n) / w^*  - n \right)
&
d^\text{discretization} &= \SI{0.2}{\milli\meter}
\\
\alpha_\text{max} &= \SI{135}{\degree}
&
d_\text{max}^\text{intersection} &= \SI{75}{\percent}
\end{array}
%\end{align*}
%\begin{align*}
\end{align*}
}{}

\revise{
The toolpath locations $L$ ensure
that the beads don't overlap,
that beads are extruded from the center of where they end up
and that the symmetry restrictions are met.
The transition anchor position $t_0$ ensures that the transitions never overlap with the locations $v$ where $R(v) = \nicefrac12 n w^*$ for $n \in \mathbb{N}$.
The transition length $t$ ensures that the center beads don't overlap with the innermost transitioning beads, while keeping the amount of underfill low and keeping the toolpath smooth.
The limit bisector angle $\alpha_\text{max}$ ensures that we don't employ transitioning in shallow wedge regions, which would result in a lot of short odd single bead polylines, which would break up the semi-continuous nature of polygonal extrusion paths.
}{}

\begin{table}%\centering
\caption{
\revise{Made figure into table}{
Beading schemes.
}
} % revise
\label{beading_schemes}
%
\setlength{\figwidth}{.45\linewidth}
\captionsetup[subtable]{justification=justified,singlelinecheck=false}
%
%
%
\begin{tabular}{|l|l|}
\hline
%
%
\begin{subtable}[t]{\figwidth}
\smallskip
\caption{Uniform scheme}\label{formula_uniform}
$\begin{aligned}
q^-(d) &= 2 \left\lfloor \frac{d}{ 2w^*} + \frac12 \right\rfloor \\
W(n,r)_i &= w^* \text{ for all } i
%\\
%L(n,r)_i &= w^* \left(i + \frac12 \right) \text{ for all } i < \frac12 n
\end{aligned}$
\end{subtable}
%
%
&
%
%
\begin{subtable}[t]{\figwidth}
\smallskip
\caption{Outer bead}\label{formula_outer_bead}
$\begin{aligned}
q(d) &=
\begin{cases}
1 & \text{ if } d < w^* \\
2 & \text{ otherwise } \\
\end{cases}
 \\
W(n,r)_i &=
\begin{cases}
2r & \text{ if } n = 1 \\
w^* & \text{ otherwise } \\
\end{cases}
%\\
%L(n,r)_i &=
%\begin{cases}
%2r / 2 & \text{ if } n = 1 \\
%w^* / 2 & \text{ otherwise } \\
%\end{cases}
\end{aligned}$
\end{subtable}
%
%
 \\ \hline
%
%
\begin{subtable}[t]{\figwidth}
\smallskip
\caption{Constant bead count}\label{formula_constant_bead_count}
$\begin{aligned}
q(d) &= C \\
W(n,r)_i &= 2 r / n \text{ for all } i
%\\
%L(n,r)_i &= 2r / n \left(i + \frac12 \right) \text{ for all } i < \frac12 n
\end{aligned}$
\end{subtable}
%
%
&
%
%
\begin{subtable}[t]{\figwidth}
\smallskip
\caption{Evenly distributed}\label{formula_evenly_distributed}
$\begin{aligned}
q(d) &= \left\lfloor \frac{d}{ w^*} + \frac12 \right\rfloor \\
W(n,r)_i &= 2 r / n \text{ for all } i
%\\
%L(n,r)_i &= 2r / n (i + \frac12) \text{ for all } i < \frac12 n
\end{aligned}$
\end{subtable}
%
%
 \\ \hline
%
%
\multicolumn{2}{|l|}{
\begin{subtable}[t]{\linewidth}
\smallskip
\caption{Centered}\label{formula_centered}
$\begin{aligned}
%q^-(d) &= 2 \left\lfloor \frac{d}{ 2w^*} + \frac12 \right\rfloor \\
q(d) &= q^-(d) +
\begin{cases}
-1 & \text{ if } q^-(d) w^* - d > w^* - \revise{r}{d}_\text{max} \\
1  & \text{ if }  q^-(d) w^* - d < w^* - \revise{r}{d}_\text{min} \\
0 & \text{ otherwise}
\end{cases}
\\
W(n,r)_i &=
\begin{cases}
2 r - (n-1) w^* &\text{ if } i = \frac12 (n-1) \\
w^* &\text{ otherwise }
\end{cases}
%\\
%L(n,r)_i &=
%\begin{cases}
%2r / 2 & \text{ if } i = \frac12 (n-1) \\
%w^* \left(i + \frac12 \right) & \text{ otherwise }
%\end{cases}
\end{aligned}$
\end{subtable}
}
%
%
 \\ \hline
%
%
\multicolumn{2}{|l|}{
\begin{subtable}[t]{\linewidth}
\smallskip
\caption{Inward distributed}\label{formula_inward_distributed}
$\begin{aligned}
q(d) &= \left\lfloor \frac{d}{ w^*} + \frac12 \right\rfloor \\
W(n,r)_i &= w^* + E(n,r) \frac{\omega(n,r)_i}{\sum_{j=0}^{n-1} \omega(n,r)_j} \text{ for all } i \\
E(n,r) &= 2r - n w^* \\
%L(n,r)_i &= 2r / n (i + \frac12) \text{ for all } i < \frac12 n
\omega(n,r)_i &= \max(0, 1 - N^{-2} (i - (n-1)/2)^2 )
\end{aligned}$
\end{subtable}
}
 \\ \hline
%
%
\end{tabular}
\end{table}

\paragraph{Uniform beading scheme}
We can define a beading scheme which emulates the uniform width offsetting technique by disabling the marking of edges, so that we never employ transitioning.
\revise{See}{We can simply set $\alpha_\text{max} = \SI{180}{\degree}$ and supply a simple beading scheme given by} \cref{formula_uniform}.

\paragraph{Outer bead}
We can emulate the method from \citeauthor{Moesen2011} by carefully choosing how the beading scheme functions deal with the outermost bead.
Also we turn off the reduction of toolpaths near 3-way intersections $d_\text{max}^\text{intersection} = \SI{0}{\percent}$, so that the polygonal toolpaths emulate the remaining area to be filled by another path planning technique similar to their technique.
We don't need transitioning, so we also set $t(n) = 0 $.
See \cref{formula_outer_bead}.

\paragraph{Constant bead count}
We can emulate the method from \citeauthor{Ding2016a} by dividing the feature radius over the widths of a constant number of beads.
Additionally in order to emulate their definition of ``branches'' we mark all ST edges \revise{}{($\alpha_\text{max} = \SI{0}{\degree}$) and }we unmark the outer edges connected to the outline shape in a separate algorithm.
Note that this deviation from the proposed framework violates the robustness against small perturbations in the outline polygon, since this marking depends on the topology of the graph of the ST.
See \cref{formula_constant_bead_count}.

\paragraph{Centered}
We can emulate the method from \citeauthor{Jin2017JMS} by transcribing how they deviate from the uniform width toolpaths.
We therefore base the beading scheme on the bead count $q^-(d)$ defined by the uniform beading scheme.
\citeauthor{Jin2017JMS} replace two beads from the uniform toolpaths by a single one when the \revise{radius}{distance} between the center \revise{and either }{}of those beads falls short of \revise{$r_\text{min} = 0.8 w^*$}{$d_\text{min} = 0.8 w^*$}\revise{}{, which gives us $w_\text{max}=d_\text{min} + 2 \cdot \frac12 w^* = 1.8 w^*$}.
Conversely, they place an extra bead when the \revise{radius between the center and either of the innermost beads}{distance} exceeds \revise{$r_\text{max} = 1.25 w^*$}{$d_\text{max} = 1.25 w^*$}~\cite{Jin2017JMS}\revise{}{, which gives us $w_\text{min}= d_\text{max} - 2 \cdot \frac12 w^* = 0.25 w^*$}~\cite[p.~72]{Jin2017JMS}.
We emulate the rounded polygonal path rerouting they define by supplying a transition length \revise{}{$t(n) = \frac12 w^*$} which results in a discretized version of their rounded polygon segment.
See \cref{formula_centered}.

\paragraph{Evenly distributed}
By taking the advantages of the above two schemes we can define a beading scheme which constitutes a novel toolpathing technique.
We can evenly divide the local feature diameter over the widths of all beads, but choose a local bead count better matching the local feature size.
We determine the local bead count by dividing the diameter by the preferred bead width and rounding to the nearest integer.
This reduces the demands on the system and deviation from mechanical properties caused by beads with extreme deviations from the preferred width.
See \cref{formula_evenly_distributed}.

\paragraph{Inward distributed scheme}
The evenly distributed scheme can be conceptualized as calculating the total discrepancy $E$ between the actual feature diameter $d$ and the total preferred width $n w^*$, dividing the total discrepancy by the number of beads and setting the width of each bead to
$w^* + E / n$.
However, depending on the application we might want a different distribution of widths.
We therefore supply a beading scheme which supports an arbitrary distribution of the discrepancy.
The distribution is determined by some weighing function $\omega(n,r)$, which defines the portion of the discrepancy to distribute to each bead.
%\paragraph{Inward distributed}
See \cref{formula_inward_distributed}.
For example, we can choose an $\omega$ which distributes the discrepancy over the innermost $N$ beads, and distribute most of it to the inner beads.
See \cref{distributed_comparison}.
That way we limit the region of impact of the distributed scheme to a central region and have the preferred bead width $w^*$ in regions farther away.
This limits the impact of transitioning regions so that transitions keep the toolpaths smooth farther away from the central regions. % and it forces most of the beads to have exactly the preferred width.
%Moreover, the bead widths equal the preferred bead width for large regions meaning that mechanical properties derived for prints using the uniform scheme will still hold approximately.

\begin{figure}
\centering
\setlength{\figwidth}{.8\columnwidth}
\setlength{\figheight}{.3\columnwidth}
\begin{minipage}[b]{0.8\linewidth}
% -p test_geometry/wedge.svg -o wedge -s nrdi -n 1.5 -a --scale 1.5
\begin{subfigure}[t]{\figwidth}\centering
\includegraphics[width=\figwidth]{sources-validation-wedge-Distributed-pretty-evenly.png}
\caption{Evenly distributed}
\end{subfigure}
\begin{subfigure}[t]{\figwidth}\centering
\includegraphics[width=\figwidth]{sources-validation-wedge-Distributed-pretty-inward.png}
\caption{Inward distributed}
\end{subfigure}
\end{minipage}
\begin{subfigure}[t]{.1\columnwidth}\centering
\includegraphics[height=\figheight]{sources-validation-widths-legend-small.pdf}
\end{subfigure}
\caption{
Closeup of toolpaths generated with the distributed \revise{}{and inward ($N=1.5$)} beading schemes for a large wedge shape.
Colors represent bead widths.
}
\label{distributed_comparison}
\end{figure}

\paragraph{Widening \revise{}{meta-scheme}}
Complementary to any of these schemes we can enforce a minimum feature size \revise{at no extra cost}{and minimum bead width} in our framework.
Regions where the model is narrower than \revise{the nozzle size}{some $r_\text{min}$} can be printed with a bead width \revise{}{$w_\text{min}$} larger than the model thickness.
See \cref{min_feature_size,widening}.
We can simply override
\begin{align*}
q'(d) &=
\begin{cases}
0 & \text{ if } 0 \leq d < 2 r_\text{min} \\
1 & \text{ if }  2 r_\text{min} \leq d < w^*  \\
q(d) & \text{ otherwise}
\end{cases}
\\
W'(n,r)_0 &=
\begin{cases}
\max \left( w_\text{min}  ,  2 r \right) & \text{ if } 2 r < w^* \\
W(n,r)_0 & \text{ otherwise}
\end{cases}
\end{align*}

\revise{}{
\paragraph{Shell meta-scheme}
The industry standard of FDM is to generate only a limited contour-parallel perimeters and to fill the remainder using a direction-parallel strategy.
We therefore provide a meta-scheme to generate adaptive bead width toolpaths only in narrow regions and generate the limited number of perimeters $M$ using the preferred width in regions which are wide enough.
We also take care not to leave gaps which are too small to be filled using the direction-parallel strategy:

\begin{align*}
q'(d) &= \min(M, q(d))
\\
W'(n,r)_i &=
\begin{cases}
W(n, M w^*)_i & \text{ if } 2 r > q^{-1}(M) \\
W(n,r)_i & \text{ otherwise}
\end{cases}
\end{align*}

These meta-schemes introduce non-linearities in the quantization function.
Because the beading is only evaluated at nodes in the skeleton, we need to make sure that there are nodes at the locations along the skeleton where the non-linearities happen.
We therefore insert extra nodes along with their ribs at locations $v$ with a radial distance $R(v) = r_\text{min}$ for widening and at $R(v) \in \left\{ M w^*, q^{-1}(M), q^{-1}(M) + \nicefrac12 w^* \right\} $ for the transition from narrow shell to unconstrained shell.
Combining all meta-schemes functionality we can generate results such as depicted in \cref{shell}.

\begin{figure}
\centering
\setlength{\figwidth}{.35\columnwidth}
\setlength{\figwidthTwo}{.6\columnwidth}
\begin{minipage}[b]{\figwidthTwo} \centering
\begin{subfigure}[b]{\figwidthTwo}\centering
\includegraphics[width=\columnwidth]{sources-validation-wedge-TEST-Distributed-feature-size.png}
\caption{Widening meta-scheme using $w_\text{min} = 2 r_\text{min} = 0.1$
\\ on top of the Distributed scheme}
\label{min_feature_size}
\end{subfigure}
\begin{subfigure}[b]{\figwidthTwo}\centering
\includegraphics[width=\columnwidth]{sources-validation-wedge-TEST-Center-widening.png}
\caption{Widening meta-scheme using $w_\text{min} = 0.5$, $2 r_\text{min} = 0.2$
\\ on top of the Centered scheme}
\label{widening}
\end{subfigure}
\end{minipage}
\begin{subfigure}[b]{\figwidth}\centering
\includegraphics[width=\figwidth]{sources-validation-gMat_example_all_combined.png}
\caption{Shell and widening}
\label{shell}
\end{subfigure}
\caption{
Toolpaths using the widening and shell meta-schemes.
\subref{min_feature_size} and~\subref{widening} show widening.
\subref{shell}
show toolpaths generated with the inward distributed strategy ($N=1.5$) in conjunction with the shell meta-scheme ($M=4$) and the same widening as in~\subref{widening}.
% -p test_geometry/gMAT_example.svg -b 4 -s i -n 1.5 -a --mins 0.2 --minw .5
Widening and shell require extra edges (green) at key locations in the skeleton.
The azure area is to be filled using some direction-parallel toolpaths.
}
\label{widening_shell}
\end{figure}

}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\title{Statistical Machine Learning \\ Reading Assignment 1 Report}

\linespread{1.25}

\newtheorem{prop}{property}

\author{Alireza Sadeghi - Mohsen Shojaee}

\begin{document}

\maketitle

\section{Introduction}

As we've seen earlier, formal definition of probability is a triple $P: (\Omega, \mathcal{F}, \mathbb{P})$, in which $\Omega$ denotes the sample space, $\mathcal{F}$ denotes a $\sigma$-field over $\Omega$ consisting of the desired events and finally, $\mathbb{P}$ denotes a measure over the $\sigma$-field  $\mathcal{F}$.
A random variable $X$ is also defined as follows: $$ X: \Omega \rightarrow \mathbb{R} $$
To be more implicit, a random variable $X$ is defined as a measurable function over the desired $\sigma-field$, mapping events to real numbers, such that the following holds: $$ \forall x: \{\omega|X(\omega) \leq x \} \in \mathcal{F}$$

\section{Random Process}

A random process can be implicitly defined as follows: $$ \Pi: \Omega \rightarrow S^\infty $$
$S$ is the state space, which in most cases, considered to be the same as $\mathbb{R}^n$, and $S^\infty$ denotes the collection of all countable subsets of $S$.
In most real world cases, we are not interested in all subsets of $S$, instead we're interested in a smaller collections which we call \textit{Test Sets}. Test sets usually is considered to be all open intervals in $\mathbb{R}^n$.

Since we are often interested in counting the occurrences of events in a specific interval, we move on to define a counting measure $N$ over test sets:
$$ N(A) = \# \{\Pi(\omega) \cap A\} $$
$N(A)$ counts the number of occurrences of the process points in a test set $A$. It's important to notice, that $N(A)$ is a random variable, mapping outcomes in $\Omega$ to non-negative integers. This drills down to $N(A)$ being a measurable function defined as follows:
$$ \forall n: \{ \omega | N(A)=n\} \in \mathcal{F}$$
If for a random process $\Pi$, the resulted N(A) over test sets, follows a Poisson distribution and for every non-overlapping test sets $A$, $B$ we have that random variables $N(A)$ and $N(B)$ are independent, $\Pi$ is called a \textit{Poisson process}.

It's important to notice that there are several other ways of defining a Poisson process, such as taking the distribution of inter arrival times to be of the Geometric distribution (Notice that in this case, we focus on inter event times instead of the points themselves).

\section{Poisson Process}

A Poisson process is a random process, parametrized by a deterministic measure on state space $S$. Notice that a random process can itself be defined as a random measure on $S$, this can be further explained using an example. Take for example the number of points in an area $A$. The number of points is a random measure, since there can be countably many points inside the the area, but the number itself follows a Poisson distribution whose parameter is the size of $A$, which is a deterministic measure (e.g. Area of $A$).

Implicitly put, let $\mu(A)$ be a measure on S, which we shall call the \textit{mean measure}. $\mu$ must be a non atomic measure in a sense that:
$$ \forall x \in S, \mu(\{x\}) = 0$$
meaning, there can be no single point with a non zero mass.

The counting measure $N(A)$ of a Poisson process, can be parametrized by a Poisson distribution with mean of $\mu(A)$. It's important to notice that the only piece of information, required to uniquely parametrize a Poisson process, is its mean measure $\mu(A)$.

To give a more thorough intuition on Poisson processes properties, it's good to notice that the parametrization above, is consistent with the initial definition in the previous section. Consider a sequence of disjoint test sets $A_1, A_2, \ldots$, and let
\begin{equation}
A = \bigcup_{i} A_i
\end{equation}
 We plan to compute the distribution $N(A)$ using two different approaches.

\begin{itemize}
    \item Direct:
        \begin{equation}
            \label{asghar}
            N(A) = Poisson (\mu(A))
        \end{equation}
    \item Indirect: Since we have $N(A) = \sum_i N(A_i)$ and according to the above parametrization $ N(A_i) = Poisson (\mu(A_i))$, we can conclude that $N(A) = Poisson(\sum_n{\mu(A_n)})$. Considering $\mu$ is a measure, we have that $\mu(A) = \sum_n{\mu(A_n)} $ which is basically the same as \ref{asghar}.
\end{itemize}

\subsection{Rate/Intensity}
When $S = \mathbb{R}^n$, the mean measure is in most interesting cases given in terms of a rate or intensity. This is a positive measurable function $\lambda$ with respect to d-dimensional measure:

\begin{equation}
    \label{ahmad}
    \mu(A) = \int_A \lambda(x)dx
\end{equation}

The term \textit{rate} is used when we're dealing with 1D state space, which is usually the time axis.

In the special case when $\lambda$ is a constant, so that $$ \mu(A) = \lambda|A|)$$ the Poisson process is called \textit{Uniform} or \textit{Homogeneous}. In this case, stochastic properties of a test set $A$, are invariant to rotation and translation. The reverse argument also holds true, in which Poisson processes invariant to rotation and translation, are essentially \textit{Homogeneous}.

\subsection{Properties}
In this section, we skim over three interesting properties of a Poisson process.

\textbf{Superposition:}
    Let $\Pi_1, \Pi_2, \ldots$ be a countable collection of independent Poisson processes on $S$ and let $\Pi_n$ have mean measure $\mu_n$ for each n. then their superposition
    $$ \Pi = \bigcup_{n=1}^\infty \Pi_n$$
    is a Poisson process with mean measure:
    $$ \mu = \sum_{n=1}^\infty \mu_i $$

    \smallskip

\textbf{Restriction:}
    Let $\Pi$ be a Poisson process with mean measure $\mu$ on $S$, and let $S_1$ be a measurable subset of $S$. Then the random countable
    $$ \Pi_1 = \Pi \cap S_1$$
    can be regarded either as a Poisson process on S with mean measure
    $$ \mu_1(A) = \mu(A \cap S_1)$$
    or as a Poisson process on $S_1$ whose mean measure is the restriction of $\mu$ to $S_1$.

    \smallskip

\textbf{Mapping:}
    Let $\Pi$ be a Poisson process on state space $S$ whose measure $\mu$ conforms to a specific condition ($\sigma$-finiteness), and let $f: S \rightarrow T$ be a measurable function such that the induced measure is non atomic. Then $f(\Pi)$ is a Poisson process on T having the induced measure $\mu^*$ as its mean measure.

\section{Marked Process}

\subsection{Coloring}

    Sometimes we want to model a situation in which the points can differ in someway. For example, consider a case in which we want to model a router and we want to take into account, from which devices the packets are being received. Assuming there's only three different types of devices connecting to our router, points on the time axis can be of those three types.

    We might be interested in analyzing this event types individually, or analyze them all together (keeping track of arrival times and types of the sender devices). If we define a multinomial distribution over different types, Coloring theorem provides us with a framework to analyze the incoming events.

    \textbf{Coloring Theorem}
        Let $\Pi$ be a Poisson process on S with mean measure $\mu$. Let the points of $\Pi$ be colored randomly with k colors, the probability that a point receives the $i^th$ color being $p_i$ and the colors of different points being independent (of one another and of the positions of the points). Let $\Pi_i$ be the set of points with it $i^th$ color. Then the $\Pi_i$ are independent Poisson processes with means measures
            $$ \mu_i = p_i \mu$$

    Note that this is consistent with the \textit{Superposition} Theorem, since $\Pi$ is the superposition of the independent Poisson processes $\Pi_i$.

    This theorem seems to be too restrictive, in such a manner that it assumes lots of independencies and expects parameters to be fixed and non changeable. We might want our event to be dependent on its location or time. For example, take the case of modeling the traffic in a highway. To overcome these shortcomings, we define the notion of \textit{Marked Poisson Process}.

\subsection{Marked Poisson Process}

    We want to associate a new random variable $M(x)$ with each point $x$ in the $S$, describing the additional information we're aiming to model. The distribution of $M(x)$ may depend on $x$ but not on the other points of $\Pi$ and $M(x)$ for different $x$s are independent.

    The pair $(X, M(x))$ can then be regarded as a random point $X^*$ in the product space $S * M$. In this space, a new random process $\Pi^*$ can be defined as follows:
    $$ \Pi^* = \{(X, M(x)) | X \in \Pi \} $$

    Under general conditions, $\Pi^*$ turns out to be a poisson process.

    \textbf{Marking Theorem} The random subset $\Pi^*$ is a Poisson process on $S * M$ with mean measure $\mu^*$ given by
    $$ \mu^*(C) = \iint\limits_{(x,m) \in C}{\mu(dx)p(x, dm)}$$

    This model can be specifically beneficial for use cases in which we want to measure the intensity of an event. Measuring the intensity, alongside the frequency of earthquakes, thunderbolts is of the likes of this model's usecases. Another field of usecases is in trading and asset return estimation.

\end{document}


% Created 2020-03-26 jue 09:43
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{Process automation laboratory - Root locus, PI control}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Process automation laboratory - Root locus, PI control},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Intro}
\label{sec:org71aeb1b}

\begin{frame}[label={sec:orga305772}]{Repetition: The RC-circuit - a first-order system}
\begin{center}
\begin{tikzpicture}
\node (circuit) {\includegraphics[width=0.4\linewidth]{../../figures/RC-input-output.pdf}};
\begin{scope}[xshift=6cm, yshift=-0.8cm]
  \draw[->] (-2,0) to (1,0);
  \draw[->] (0,-1) to (0,1);
  \node at (1,1) {$s$-plane};
  \node[color=red!80!black] (pole) at (-1.2, 0) {\large $\times$};
  \node[below of=pole, node distance=5mm, ] {$-\frac{1}{\tau}$};
\end{scope}
\node (laplace) at(6,1.5)  {$U_C(s) = \frac{1}{s\underbrace{RC}_{\tau} + 1} U(s)$};
\begin{axis}[
yshift=-5cm,
clip=false,
width = 12cm,
height = 3.5cm,
xlabel = {$t$},
ylabel = {V},
title = {$u_C(t) = 10 (1 - \mathrm{e}^{\frac{t}{\tau}})$, for $u(t)$ step of size 10},
]
\addplot[blue!80!black, thick, no marks, const plot] coordinates {
(-0.2,0)
(0,10)
(2,0)
(4,10)
(6,0)
} node[coordinate, pos=0.75, pin=180:{$u(t)$}] {};
\addplot[orange!80!black, thick, no marks, domain=-0.2:0, samples=2] {0};
\addplot[orange!90!black, thick, no marks, domain=0:2, samples=40] {10*(1-exp(-x/0.2)};
\addplot[orange!90!black, thick, no marks, domain=4:6, samples=40] {10*(1-exp(-(x-4)/0.2)} node[coordinate, pos=0.84, pin=-90:{$u_C(t)$}] {};
\addplot[orange!90!black, thick, no marks, domain=2:4, samples=40] {10*exp(-(x-2)/0.2)};
\end{axis}

\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[label={sec:org6050dc7}]{A concept to keep in mind}
In a system with an \alert{integrator} steady-state can only exist if the signal to the integrator is zero

\begin{center}
\includegraphics[width=0.4\linewidth]{../../figures/RC-input-output.pdf}
\end{center}

\begin{center}
  \begin{tikzpicture}[scale = 0.8, node distance=25mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

  \node[coordinate] (input) {};
  \node[block, right of=input] (cap) {$C$};
  \node[coordinate, right of=cap] (output) {};

  \draw[->] (finput) -- node[above, pos=0.3] {$i(t)$} (cap);
  \draw[->] (cap) -- node[above, pos=0.7] {$u_C(t)$} (output);

  \node[right of=output, node distance=40mm] {$u_C(t) = u_C(0) + \frac{1}{C}\int i(s) ds$};
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org7077543}]{Feedback control}
\begin{center}
  \begin{tikzpicture}[scale = 0.8, node distance=25mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

  \node[coordinate] (refinput) {};
  \node[sumnode, right of=refinput, node distance=20mm] (sumerr) {\tiny $\sum$};
  \node[block, right of=sumerr] (controller) {$F(s)$};
  \node[above of=controller, node distance=6mm] {controller};
  \node[sumnode, right of=controller, node distance=20mm] (sumdist) {\tiny $\sum$};
  \node[block, right of=sumdist, node distance=20mm] (plant) {$G(s)$};
  \node[above of=plant, node distance=6mm] {plant};
  \node[coordinate, right of=plant, node distance=20mm] (output) {};
  \node[coordinate, above of=sumdist, node distance=20mm] (dist) {};

  \draw[->] (refinput) -- node[above, pos=0.3] {$y_{ref}(t)$} (sumerr);
  \draw[->] (sumerr) -- node[above] {$e(t)$} (controller);
  \draw[->] (controller) -- node[above] {$u(t)$} (sumdist);
  \draw[->] (sumdist) -- node[above] {$$} (plant);
  \draw[->] (plant) -- node[coordinate] (measure) {} node[above, pos=0.8] {$y(t)$} (output);
  \draw[->] (measure) -- ++(0,-14mm) -| node[right, pos=0.95] {$-$} (sumerr);
  \draw[->] (dist) -- node[right, pos=0.3] {$v(t)$} (sumdist);

  \end{tikzpicture}
\end{center}

For the closed-loop system we get
\[ Y(s) = \frac{G_o(s)}{1+G_o(s)}Y_{ref}(s) + \frac{G(s)}{1 + G_o(s)} V(s), \]
where \(G_o(s)=G(s)F(s)\) is called the \emph{loop gain}.
\end{frame}

\begin{frame}[label={sec:orgc4b7770}]{Feedback control}
\begin{center}
  \begin{tikzpicture}[scale = 0.8, node distance=25mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

  \node[coordinate] (refinput) {};
  \node[sumnode, right of=refinput, node distance=20mm] (sumerr) {\tiny $\sum$};
  \node[block, right of=sumerr] (controller) {$F(s)$};
  \node[above of=controller, node distance=6mm] {controller};
  \node[sumnode, right of=controller, node distance=20mm] (sumdist) {\tiny $\sum$};
  \node[block, right of=sumdist, node distance=20mm] (plant) {$G(s)$};
  \node[above of=plant, node distance=6mm] {plant};
  \node[coordinate, right of=plant, node distance=20mm] (output) {};
  \node[coordinate, above of=sumdist, node distance=12mm] (dist) {};

  \draw[->] (refinput) -- node[above, pos=0.3] {$y_{ref}(t)$} (sumerr);
  \draw[->] (sumerr) -- node[above] {$e(t)$} (controller);
  \draw[->] (controller) -- node[above] {$u(t)$} (sumdist);
  \draw[->] (sumdist) -- node[above] {$$} (plant);
  \draw[->] (plant) -- node[coordinate] (measure) {} node[above, pos=0.8] {$y(t)$} (output);
  \draw[->] (measure) -- ++(0,-14mm) -| node[right, pos=0.95] {$-$} (sumerr);
  \draw[->] (dist) -- node[right, pos=0.3] {$v(t)$} (sumdist);

  \end{tikzpicture}
\end{center}

\[ Y(s) = \frac{G_o(s)}{1+G_o(s)}Y_{ref}(s) + \frac{G(s)}{1+G_o(s)}V(s), \quad G_o(s)=G(s)F(s). \]

Let \(G(s) = \frac{1}{s}\) and \(F(s)=K\).
\begin{enumerate}
\item Will there be a steady-state control error (\(\lim_{t\to\infty} e(t)\)  \(\neq\) 0) if \(y_{ref}(t)=0\) and \(v(t)\) is a unit step? Why? \alert{Answer on Socrative}
\item What is the \alert{characteristic equation} for the closed-loop system?
\item Sketch the location of the poles in the imaginary plane as the gain \(K\) varies from 0 to \(\infty\). \alert{Group exercise in breakout room}
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:orgad4a3db}]{How to get rid of the steady-state error}
Use a proportional-integral controller (PI controller)
\[ F(s) = K_p + \frac{K_i}{s}. \]
This gives closed-loop system

\begin{center}
   \begin{tikzpicture}[scale = 0.8, node distance=20mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

   \node[coordinate] (refinput) {};
   \node[sumnode, right of=refinput, node distance=20mm] (sumerr) {\tiny $\sum$};
   \node[block, right of=sumerr, node distance=24mm] (Ipart) {$\frac{K_i}{s}$};
   \node[block, above of=Ipart, node distance=11mm] (Ppart) {$K_p$};
   \node[sumnode, right of=Ipart, node distance=14mm] (sumcontrol) {\tiny $\sum$};
   \node[sumnode, right of=sumcontrol, node distance=20mm] (sumdist) {\tiny $\sum$};
   \node[block, right of=sumdist, node distance=20mm] (plant) {$G(s)$};
   \node[coordinate, right of=plant, node distance=24mm] (output) {};
   \node[coordinate, above of=sumdist, node distance=12mm] (dist) {};

   \draw[->] (refinput) -- node[above, pos=0.3] {$y_{ref}(t)$} (sumerr);
   \draw[->] (sumerr) -- node[above, near start] {$e(t)$} node[coordinate] (split) {} (Ipart);
   \draw[->] (split) |- (Ppart);
   \draw[->] (Ppart) -| (sumcontrol);
   \draw[->] (Ipart) -- (sumcontrol);

   \draw[->] (sumcontrol) -- node[above] {$u(t)$} (sumdist);
   \draw[->] (sumdist) -- node[above] {$$} (plant);
   \draw[->] (plant) -- node[coordinate] (measure) {} node[above, pos=0.8] {$y(t)$} (output);
   \draw[->] (measure) -- ++(0,-14mm) -| node[right, pos=0.95] {$-$} (sumerr);
   \draw[->] (dist) -- node[right, pos=0.3] {$v(t)$} (sumdist);

   \end{tikzpicture}
 \end{center}
\end{frame}

\begin{frame}[label={sec:org305d314}]{How to get rid of the steady-state error}
Use a proportional-integral controller (PI controller)
\[ F(s) = K_p + \frac{K_i}{s}. \]
This gives closed-loop system

\begin{center}
   \begin{tikzpicture}[scale = 0.8, node distance=20mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

   \node[coordinate] (refinput) {};
   \node[sumnode, right of=refinput, node distance=20mm] (sumerr) {\tiny $\sum$};
   \node[block, right of=sumerr, node distance=24mm] (Ipart) {$\frac{K_i}{s}$};
   \node[block, above of=Ipart, node distance=11mm] (Ppart) {$K_p$};
   \node[sumnode, right of=Ipart, node distance=14mm] (sumcontrol) {\tiny $\sum$};
   \node[sumnode, right of=sumcontrol, node distance=20mm] (sumdist) {\tiny $\sum$};
   \node[block, right of=sumdist, node distance=20mm] (plant) {$G(s)$};
   \node[coordinate, right of=plant, node distance=24mm] (output) {};
   \node[coordinate, above of=sumdist, node distance=12mm] (dist) {};

   \draw[->] (refinput) -- node[above, pos=0.3] {$y_{ref}(t)$} (sumerr);
   \draw[->] (sumerr) -- node[above, near start] {$e(t)$} node[coordinate] (split) {} (Ipart);
   \draw[->] (split) |- (Ppart);
   \draw[->] (Ppart) -| (sumcontrol);
   \draw[->] (Ipart) -- (sumcontrol);

   \draw[->] (sumcontrol) -- node[above] {$u(t)$} (sumdist);
   \draw[->] (sumdist) -- node[above] {$$} (plant);
   \draw[->] (plant) -- node[coordinate] (measure) {} node[above, pos=0.8] {$y(t)$} (output);
   \draw[->] (measure) -- ++(0,-14mm) -| node[right, pos=0.95] {$-$} (sumerr);
   \draw[->] (dist) -- node[right, pos=0.3] {$v(t)$} (sumdist);

   \end{tikzpicture}
 \end{center}

\alert{The only way that steady-state can exist is if the input to the integrator of the controller is zero.}
\end{frame}

\section{The root locus diagram}
\label{sec:orgb6f8a9c}

\begin{frame}[label={sec:org8e0f9a4}]{Root locus}
Given loop gain \[G_o(s) = K \frac{Q(s)}{P(s)}\] how does the solutions to the characteristic equation
\[ 1 + G_o(s) = 0 \quad \Leftrightarrow\quad P(s) + KQ(s) = 0 \]
(i.e. the \alert{poles} of the closed-loop system) depend on \(K\)?
\end{frame}

\begin{frame}[label={sec:org713aeac}]{The root locus for PI-control of the integrator}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/rlocus-integrator-PI}
\end{center}
\end{frame}

\begin{frame}[label={sec:org199e90e}]{Root locus question}
\end{frame}

\begin{frame}[label={sec:orgb1887b7}]{Root locus definition}
Let
\[\begin{cases} P(s)&=s^n+a_1s^{n-1}+\dots+a_n = (s-p_1)(s-p_2)\cdots(s-p_n)\\
Q(s)&=s^m+b_1s^{m-1}+\dots+b_m=(s-q_1)(s-q_2)\cdots(s-q_m) \end{cases},\ \ \ n\ge m \]
The root locus shows how the roots to the equation
\begin{equation}
\label{eq:P(s)+KQ(s)=0}
P(s)+K\cdot Q(s)=0,\ \ \ 0\le K<\infty
\end{equation}
depend on the parameter \(K\). The root locus consists of the set of all points in the complex plane that are roots to \eqref{eq:P(s)+KQ(s)=0} for some non-negative value of \(K\).
\end{frame}

\begin{frame}[label={sec:org2f34635}]{Characteristics of the root locus}
The polynomial \(P(s)+KQ(s)=0\) above will always have \(n\) roots. Each gives a \emph{branch} in the root locus. Since the polynomials \(P(s)\) and \(Q(s)\) have real-valued coefficients, all roots are either real or complex-conjugated pairs. This means that the root locus is \emph{symmetric about the real axis.} Other characteristics
\begin{itemize}
\item Start points - marked by crosses
\item End points - marked  by circles
\item Asymptotes
\item Pieces of the real axis
\end{itemize}
\end{frame}

\begin{frame}[label={sec:orgf80e036}]{Start- and end points}
\begin{description}
\item[{Start points}] These are the \(n\) roots of \(P(s) + KQ(s)\) for \(K=0\), i.e. the roots of \(P(s)\). These are the open-loop poles, and are marked with crosses '\(\times\)'
\item[{End points}] These are the \(m\) (finite) roots of \(P(s)+KQ(s)\) when \(K\to\infty\), and are hence the roots of \(Q(s)\). The end points are marked with circles '\(\circ\)'
\end{description}
\end{frame}

\begin{frame}[label={sec:org398a233}]{The real axis}
Those parts of the real axis that have an \alert{odd number} of real-valued start- or end points to the right (including multiplicity) belong to the root locus.
\end{frame}

\begin{frame}[label={sec:org8aa0f2f}]{Asymptotes}
With \(n\) starting points and \(m\) end points, then \(m\) of the branches will go to end points. The rest will go out towards infinity along \(n-m\) asymptotes. The asymptotes go out symmetrically from a point on the real axis.
\end{frame}

\begin{frame}[label={sec:org1180ef0}]{Asymptotes, directions}
The directions of the asymptotes are given by the expression
\[ \theta_k = \arg s = \frac{(2k+1)\pi}{n-m}, \; k \in \mathbb{Z} \]
Example: 6 start points and 3 end points gives \(n-m = 6-3 = 3\) and the directions

\begin{columns}
\begin{column}{0.35\columnwidth}
\[ \theta = \begin{cases} \frac{\pi}{3}, & k=0\\ \pi, & k=1\\ -\frac{\pi}{3}, & k=-1 \end{cases}. \]
\end{column}

\begin{column}{0.65\columnwidth}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../MR2004/figures/root-locus-ex-3asymptotes-crop}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org1d8f892}]{Asymptotes, intersection with the real axis}
\[ i.p. = \frac{ \sum_{i=0}^n p_i - \sum_{i=0}^m q_i}{n-m}, \]
where \(\{p_i\}\) are the starting points (open-loop poles) and \(\{q_i\}\) are the end points (open-loop zeros).
\end{frame}

\section{Examples}
\label{sec:org06f35c8}
\begin{frame}[label={sec:org7a92958}]{PI-Control of the integrator}
Write the controller
\[ F(s) = K_p + \frac{K_i}{s} = K\big(( 1 + \frac{1}{sT_i}\Big) = K/T_i \frac{sT_i + 1}{s}, \]
and let \(T_i = 2\).
The characteristic equation can be written
\[ s^2 + \frac{K}{2}(2s + 1) = 0\]

\begin{itemize}
\item Start points: \(n=2\), in  \(s=0\)
\item End points: \(m=1\), \(s=-\frac{1}{2}\)
\item Asymptotes: \(m-n=1\), with directions \(\theta=\pi\)
\item The real line: The real-line left of the end-point is part of the root locus.
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org88a3882}]{PI-Control of the integrator}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/rlocus-integrator-PI}
\end{center}
\end{frame}

\begin{frame}[label={sec:org0bbe7ce}]{Do on your own: First-order system}
Instead of the plant being an integrator
\[ F(s) = \frac{1}{s}\]
consider a stable first order system
\[ F(s) = \frac{1}{s+a}\]
How does the root locus change?
\end{frame}
\end{document}


\subsubsection{Degradation Rate Radionuclide Mass Balance Model}\label{sec:deg_rate}
Barrier materials in a repository environment can degrade very slowly over long
time scales.  The Degradation Rate mass balance model is the simplest implemented model and is
most appropriate for rate based modeling of a degrading barrier volume.  The
Degradation Rate mass balance model does not attempt to model the physical
mechanisms responsible for this degradation. Rather, it generically captures
this behavior as a simple fractional degradation rate.  The fundamental concept
is depicted in Figure \ref{fig:deg_volumes}.

\input{./nuclide_models/mass_balance/deg_rate/deg_volumes}

For a situation as in \Cyder and \Cyclus, with discrete time steps, the time
steps are assumed to be small enough to assume a constant rate of degradation over
the course of the time step.  The degraded volume, then, is a simple fraction,
$d$, of the total volume, $V_T$, such that

\begin{align}
V_T &= V_i + V_d
\label{deg_volumes}
\intertext{where}
V_d(t) &= d(t)V_T\nonumber\\
V_i(t) &= (1-d(t))V_T\nonumber\\
V_T &= \mbox{ total volume }[m^3]\nonumber\\
V_i(t) &= \mbox{ intact volume at time t }[m^3]\nonumber\\
V_d(t) &= \mbox{ degraded volume at time t }[m^3]\nonumber
\intertext{and}
d(t) &= \mbox{ the fraction that has been degraded by time t }[-]\nonumber\\
     &= \sum_{n=0}^{N}f_n\Delta t\nonumber\\
\intertext{where}
f_n &= \mbox{ the constant rate over a time step }[1/s]\nonumber\\
\Delta t &= \mbox{ the length of a time step }[s].\nonumber
\end{align}

In this model, at each time step, contaminants in the degraded fraction of the control volume
are available for transfer to adjacent components such that,

\begin{align}
m_{jk}(t_n) &= m_{j,d}(t_n)
\intertext{where}
m_{jk}(t_n) &= \mbox{ incoming mass from the inner boundary }[kg]\nonumber\\
m_{j,d} &= \mbox{ mass in degraded volume of cell j }[kg] \nonumber\\
t_n &= \mbox{ time }[s].\nonumber
\end{align}

The total contaminants $m_{j,d}(t_n)$, in the degraded volume
at time $t_n$ are calculated based on mass flux from the
inner boundary, the updated mass in the degraded volume at the previous
time step, and the mass released by degradation during the
current time step. The contaminants in the degraded fluid ($V_{df}$) are
effectively released in that barrier layer and can be
transferred to the adjacent component in the mass transfer stage.  Specifically,

\begin{align}
m_{k,d}(t_n) &= m_{jk}(t_n) + m_{k,d}^*(t_{n-1}) + m_{k,i}^*(t_{n-1})f_n\Delta t
\label{deg_rate_source_cont}
\intertext{where}
m_{k,d}^*(t_{n-1}) &= \mbox{ mass in the degraded volume of k at the end of }t_{n-1}\mbox{ }[kg]\nonumber\\
m_{k,i}^*(t_{n-1}) &= \mbox{ mass in the intact volume of k at the end of }t_{n-1}\mbox{ }[kg]\nonumber\\
f_n&= \mbox{ degradation rate during the time step }t_n\mbox{ }[1/s]\nonumber\\
\Delta t &= t_n-t_{n-1}\mbox{ }[s].\nonumber
\end{align}

The concentration calculation results from the mass balance calculation in
\eqref{deg_rate_source_cont} to support parent components that utilize the
Dirichlet boundary condition.  For the degradation rate model, which
incorporates no diffusion or advection, the concentration, $C_j(r_j)$, the
boundary ($r_j$) between cells $j$ and $k$, is the average concentration
throughout the degraded volume,

\begin{align}
C_{d} &= \frac{m_{d}(t_n)}{V_{d}(t_n)} \label{deg_rate_conc}\\
&= \frac{\mbox{ solute mass in degraded fluid in cell j }}{\mbox{ degraded fluid volume in cell j}}.\nonumber
\end{align}


\section{Avalanche involving a shock wave}

We consider an avalanche involving a shock wave.
This problem is similar to dam break on wet areas, and so, it involves a shock. We consider a flat topography with positive slope. Shallow water approach is used to solve the problem. The analytical solution of this problem was derived by Mungkasi and Roberts~\cite{MR2012PAAG}. This shallow water approach was also implemented by a number of researchers, such as Mangeney et al.~\cite{MHR2000} and Naaim et al.~\cite{NVC1997}. The simulation should show a rarefaction and a shock.

The initial condition is
\begin{equation} \label{eq:dap_init_wet}
u(x,0)=0, ~~v(x,y)=0, ~~\textrm{and}~~
h(x,0) = \left\{ \begin{array}{ll}
h_1 & \textrm{if $x < 0$}\\
h_0 & \textrm{if $x > 0$}\\
\end{array} \right.
\end{equation}
where $h_0>h_1>0$. The topography is a flat bed with positive slope. Note that when $h_1=0$, the problem becomes avalanche involving a dry area~\cite{MR2011DA}.

The analytical solution~\cite{MR2012PAAG} at time $t>0$ is
\begin{equation}
h(x,t) = \left\{ \begin{array}{ll}
h_1 & \textrm{if $x < \sigma t + \frac12 mt^2$}\,,\\
h_2 & \textrm{if $ \sigma t + \frac12 mt^2 \leq x < (u_2+c_2)t + \frac12 mt^2$}\,,\\
\frac{1}{9g} \left( \frac{x}{t} + 2c_0 - \frac12 mt \right)^2 & \textrm{if $(u_2+c_2)t + \frac12 mt^2 \leq x < c_0 t + \frac12 mt^2$}\,,\\
h_0 & \textrm{if $x \geq c_0 t + \frac12 mt^2$}\,, \\
\end{array} \right.
\end{equation}
and
\begin{equation}
u(x,t) = \left\{ \begin{array}{ll}
mt & \textrm{if $x < \sigma t + \frac12 mt^2$}\,,\\
u_2+mt & \textrm{if $ \sigma t + \frac12 mt^2 \leq x < (u_2+c_2)t + \frac12 mt^2$}\,,\\
\frac23 \left( \frac{x}{t} - c_0 + mt \right) & \textrm{if $(u_2+c_2)t + \frac12 mt^2 \leq x < c_0 t + \frac12 mt^2$}\,,\\
mt & \textrm{if $x \geq c_0 t + \frac12 mt^2$}\,, \\ \end{array} \right.
\end{equation}
for time $t>0$\,. Here $u_2$\,, $c_2$\,, and $\sigma$ are the solutions of the three simultaneous equations
\begin{equation}
u_2  = \sigma - \frac{c_1^2}{4\sigma} \left(1+\sqrt{1+8\left( \frac{\sigma}{c_1} \right)^2}     \right),
\end{equation}
\begin{equation}
c_2 = c_1 \sqrt{\frac12 \left(\sqrt{1+  8 \left(\frac{\sigma}{c_1}\right)^2}-1  \right)},
\end{equation}
and
\begin{equation}
-2c_0 = u_2 -2c_2.
\end{equation}
The value of $h_2$ is calculated using relation $c_2=\sqrt{gh_2}$\,.
Here $m=-g\tan{\theta}+F$, where $\tan{\theta}$ is the slope of the topography. Variable $F$ is the Coulomb-type friction given by
\begin{equation}
F=g \cos^2{\theta} \tan{\delta},
\end{equation}
in which $\tan{\delta}$ is a given value of friction slope such that $\tan{\delta} \leq \tan{\theta}$.

\subsection{Results}

For our test, we consider $h_0=20$ and $h_1=10$ in (\ref{eq:dap_init_wet}).
The following figures show the stage, $x$-momentum, and $x$-velocity at several instants of time. We should see excellent agreement between the analytical and numerical solutions.

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{stage_plot.png}
\end{center}
\caption{Stage results}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{xmom_plot.png}
\end{center}
\caption{Xmomentum results}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{xvel_plot.png}
\end{center}
\caption{Xvelocity results}
\end{figure}

\endinput


\documentclass[nobib, nofonts, notoc, justified]{tufte-handout}
\usepackage{amro-common}
\usepackage{amro-tufte}
\usepackage[toc,page]{appendix}
\addbibresource{references.bib}

% Spacing between lines
\setstretch{1}

% \makeatletter
% % % Remove paragraph indentation
% % \renewcommand{\@tufte@reset@par}{%
% %   \setlength{\RaggedRightParindent}{0.0pc}%
% %   \setlength{\JustifyingParindent}{0.0pc}%
% %   \setlength{\parindent}{0pc}%
% %   \setlength{\parskip}{\baselineskip}%
% % }
% \@tufte@reset@par
% \makeatother

% Renew the PDF symbol
\renewcommand{\symPdf}{p}

\newcommand{\cpp}{C\texttt{++}\xspace}

% Might prevent marginnotes from jumping to another page
\def\mathnote#1{%
  \tag*{\rlap{\hspace\marginparsep\smash{\parbox[t]{\marginparwidth}{%
  \footnotesize#1}}}}
}

% % This command is needed for building in linux
% \captionsetup{compatibility=false}

\newcommand{\lplus}{\overset{{\scriptscriptstyle\mathrm{L}}}{\oplus}}
\newcommand{\liplus}{\overset{{\scriptscriptstyle\mathrm{LI}}}{\oplus}}
\newcommand{\rplus}{\overset{{\scriptscriptstyle\mathrm{R}}}{\oplus}}
\newcommand{\riplus}{\overset{{\scriptscriptstyle\mathrm{RI}}}{\oplus}}
% Ominus
\newcommand{\lminus}{\overset{{\scriptscriptstyle\mathrm{L}}}{\ominus}}
\newcommand{\liminus}{\overset{{\scriptscriptstyle\mathrm{LI}}}{\ominus}}
\newcommand{\rminus}{\overset{{\scriptscriptstyle\mathrm{R}}}{\ominus}}
\newcommand{\riminus}{\overset{{\scriptscriptstyle\mathrm{RI}}}{\ominus}}

\title{Invariant Filtering}
\author{Amro Al~Baali}

% Generates the index
\usepackage{makeidx}
\makeindex

\setcounter{tocdepth}{2}

\begin{document}
  % \frontmatter
  {
    \input{sections/titlepage.tex}
    % \forceheader{Contents}
    \tableofcontents
    \clearpage
    % \thispagestyle{empty}
    % \addtocontents{toc}{\protect\thispagestyle{empty}}
  }
  % \mainmatter

  \section{Why this document?}
  This document is provided to explain and clarify the code uploaded with it.
  The repository includes examples of implementing filters, usually Kalman fitlers, in \cpp.
  The filters will be mainly implemented on
  \begin{packed_enum}
    \item a linear system, and
    \item a non-Euclidean nonlinear system (usually defined on a Lie group).
  \end{packed_enum}

  \section{The Kalman filter}
  \subsection{The system}
  Consider the linear ordinary differential equation (ODE) describing a mass-spring-damper system
  \begin{align}
    \label{eq:mass spring damper ode}
    m\ddot{x}(t) + b\dot{x}(t) + kx(t) &= u(t),
  \end{align}
  where $m$ is the mass, $b$ is the damping, $k$ is the spring constant, and $u(t)$ is the forcing function. The system \eqref{eq:mass spring damper ode} can be written in state space form
  \begin{align}
    \mbfdot{x} &= \bbm 0 & 1\\ -\f{k}{m} & -\f{b}{m} \ebm \mbf{x} + \bbm 0\\\f{1}{m} \ebm u\\
    &= \boldsymbol{A}\mbf{x} + \boldsymbol{B}u_{t},
  \end{align}
  where
  \begin{align}
    \label{eq:DT linear kinematic model}
    \mbf{x} &= \bbm x & \dot{x} \ebm^{\trans},
  \end{align}
  and the time arguments $(t)$ are dropped for brevity.

  The disrete-time kinematic model is given by
  \begin{align}
    \mbf{x}_{k} &= \mbf{A}\mbf{x}_{k-1} + \mbf{B}u_{k-1},
  \end{align}
  where the discrete-time system matrices $\mbf{A}$ and $\mbf{B}$ are computed using some discretization scheme. For the linear example above, the $\mbf{A}$ matrix is given by
  \begin{align}
    \mbf{A} &= \exp\left( \boldsymbol{A} T_{k} \right),\\
    \mbf{B} &= \int_{0}^{T_{k}}\exp(\boldsymbol{A}\alpha)\dee\alpha \boldsymbol{B},
  \end{align}
  where $T_{k}$ is the sampling period \cite{Farrell_Aided_2008}.

  The matrix $\mbf{B}$ can be approximated using forward Euler to get
  \begin{align}
    \mbf{B} &\approx T_{k}\boldsymbol{B}.
  \end{align}

  \subsection{Process model}
  The discrete-time process model\sidenote{Also referred to as the kinematic model, motion model, progression model, etc.} is used in the \emph{prediction} step of the Kalman filter. It is given by
  \begin{align}
    \label{eq:DT linear process model}
    \mbf{x}_{k} &= \mbf{A}\mbf{x}_{k-1} + \mbf{B}\mbf{u}_{k-1} + \mbf{L}\mbf{w}_{k-1},
  \end{align}
  where $\mbf{x}_{k}\in\rnums^{n_{x}}$ is the state, $\mbf{u}_{k}\in\rnums^{n_{u}}$ is the control input, and $\mbf{w}_{k}\in\rnums^{n_{w}}$ where $\mbf{w}_{k}\sim\mc{N}\left( \mbf{0}, \mbf{Q}_{k} \right)$ is the process noise and $\mbf{Q}_{k}$ is the process noise covariance.

  \subsection{Measurement functions}
  The correction step requires a measurement model which is given by
  \begin{align}
    \mbf{y}_{k} &= \mbf{C}\mbf{x}_{k} + \mbf{M}\mbf{n}_{k},
  \end{align}
  where $\mbf{y}_{k}\in\rnums^{n_{y}}$ and $\mbf{n}_{k}\in\rnums^{n_{n}}$ where $\mbf{n}_{k}\sim\mc{N}\left( \mbf{0}, \mbf{R}_{k} \right)$ is the measurement noise and $\mbf{R}_{k}$ is the measurement noise covariance.

  For the example presented, the measurement is a position measurememnt, so the measurement function is given by
  \begin{align}
    y_{k} &= \bbm 1 & 0 \ebm\mbf{x}_{k} + n_{k}\\
    &= \mbf{C}\mbf{x}_{k} + n_{k}.
  \end{align}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % EKF
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \section{The extended Kalman filter}
  Without getting into the derivation of the equations, the (extended) Kalman filter equations are given by \cite[eq.~(4.32)]{Barfoot_State_2017}
  \begin{subequations}
    \label{eq:EKF eqns}
    \begin{align}
      \label{eq:EKF: prediction}
      \mbfcheck{x}_{k} &= \mbf{f}\left( \mbfhat{x}_{k-1}, \mbf{u}_{k-1}, \mbf{0} \right), \\
      \label{eq:EKF: prediction cov}
      \mbfcheck{P}_{k} &= \mbf{A}_{k-1}\mbfhat{P}_{k-1}\mbf{A}_{k-1}^{\trans} + \mbf{L}_{k-1}\mbf{Q}_{k}\mbf{L}_{k-1}^{\trans},\\
      \label{eq:EKF: KF gain}
      \mbf{K}_{k} &= \mbfcheck{P}_{k}\mbf{H}_{k}\left( \mbf{H}_{k}\mbfcheck{P}_{k}\mbf{H}_{k}^{\trans} + \mbf{M}_{k}\mbf{R}_{k}\mbf{M}_{k}^{\trans} \right)\inv,\\
      \label{eq:EKF: correction}
      \mbfhat{x}_{k} &= \mbfcheck{x}_{k} + \mbf{K}_{k}\left( \mbf{y}_{k} - \mbf{g}_{k}\left( \mbfcheck{x}_{k}, \mbf{0} \right) \right),\\
      \label{eq:EKF: correction cov}
      \mbfhat{P}_{k} &= \left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)\mbfcheck{P}_{k}\left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)^{\trans} \nonumber\\&\qquad+ \mbf{K}_{k}\mbf{M}_{k}\mbf{R}_{k}\mbf{M}_{k}^{\trans}\mbf{K}_{k}^{\trans},
    \end{align}
  \end{subequations}
  where
  \begin{align}
    \mbf{A}_{k-1} &= \left.\pd{\mbf{f}\left( \mbf{x}_{k-1}, \mbf{u}_{k-1}, \mbf{w}_{k-1} \right)}{\mbf{x}_{k-1}}\right|_{\overset{\mbf{x}_{k-1}=\mbfhat{x}_{k-1},}{\mbf{w}_{k-1}=\mbf{0}}},\\
    \mbf{L}_{k-1} &= \left.\pd{\mbf{f}\left( \mbf{x}_{k-1}, \mbf{u}_{k-1}, \mbf{w}_{k-1} \right)}{\mbf{w}_{k-1}}\right|_{\overset{\mbf{x}_{k-1}=\mbfhat{x}_{k-1},}{\mbf{w}_{k-1}=\mbf{0}}},\\
    \mbf{H}_{k-1} &= \left.\pd{\mbf{g}\left( \mbf{x}_{k}, \mbf{n}_{k} \right)}{\mbf{x}_{k}}\right|_{\overset{\mbf{x}_{k}=\mbfcheck{x}_{k},}{\mbf{n}_{k}=\mbf{0}}},\\
    \mbf{M}_{k-1} &= \left.\pd{\mbf{g}\left( \mbf{x}_{k}, \mbf{n}_{k} \right)}{\mbf{n}_{k}}\right|_{\overset{\mbf{x}_{k}=\mbfcheck{x}_{k},}{\mbf{n}_{k}=\mbf{0}}}.
  \end{align}

  The covariance equations \eqref{eq:EKF: prediction cov} and \eqref{eq:EKF: correction cov} are computed using first-order covariance propagation on \eqref{eq:EKF: prediction} and \eqref{eq:EKF: correction}, respectively. Let's clarify this point as it will be important when discussing the invariant extended Kalman filter.

  \begin{remark}
    \label{remark:splitting gaussian rv into sum}
    A Gaussian random variable
    \begin{align}
      \mbfrv{x}\sim\mc{N}\left( \mbs{\mu}, \mbs{\Sigma} \right)
    \end{align}
    can be written as
    \begin{align}
      \mbfrv{x} &= \mbs{\mu} + \delta\mbfrv{x},\\
      \delta\mbfrv{x} &\sim \mc{N}\left( \mbf{0}, \mbs{\Sigma} \right).
    \end{align}
  \end{remark}
  Using Remark~\ref{remark:splitting gaussian rv into sum}, define the (random) variables
  \begin{align}
    \label{eq:KF: prediction err. def.}
    \delta\mbfcheckrv{x}_{k} &\coloneqq \mbfcheckrv{x}_{k} -  \mbf{x}_{k},\\
    \label{eq:KF: correction err. def.}
    \delta\mbfhatrv{x}_{k} &\coloneqq \mbfhatrv{x}_{k} -  \mbf{x}_{k},
  \end{align}
  where
  \begin{align}
    \delta\mbfcheckrv{x}_{k} &\sim\mc{N}\left( \mbf{0}, \mbfcheck{P}_{k} \right),\\
    \delta\mbfhatrv{x}_{k} &\sim\mc{N}\left( \mbf{0}, \mbfhat{P}_{k} \right).
  \end{align}
  Using Taylor's expansion of \eqref{eq:EKF: prediction}, the error dynamics of the (extended) Kalman filter equations are then given by
  \begin{align}
    \delta\mbfcheckrv{x}_{k}
    &= \mbfcheckrv{x}_{k} - \mbf{x}_{k} \\
    &= \mbf{f}\left( \mbfhatrv{x}_{k-1}, \mbf{u}_{k-1}, \mbf{0} \right) - \mbf{f}\left( \mbf{x}_{k-1}, \mbf{u}_{k-1}, \mbfrv{w}_{k-1} \right)\\
    &\approx \mbf{f}\left( \mbfhat{x}_{k-1}, \mbf{u}_{k-1}, \mbf{0} \right) - \mbf{f}\left( \mbfhat{x}_{k-1},\mbf{u}_{k-1}, \mbf{0} \right)
      \nonumber\\&\qquad
      - \mbf{A}_{k-1}\underbrace{\left( \mbf{x}_{k-1} - \mbfhatrv{x}_{k-1} \right)}_{-\delta\mbfhatrv{x}_{k-1}} - \mbf{L}_{k-1}\left( \mbfrv{w}_{k-1} - \mbf{0} \right)\\
    &= \mbf{A}_{k-1}\delta\mbfhatrv{x}_{k-1} - \mbf{L}_{k-1}\mbfrv{w}_{k-1}.
  \end{align}
  The covariance on $\delta\mbfcheck{x}_{k}$ is then given by
  \begin{align}
    \cov{\delta\mbfcheckrv{x}_{k}} &= \mbf{A}_{k}\mbfhat{P}_{k-1}\mbf{A}_{k-1}^{\trans} + \mbf{L}_{k-1}\mbf{Q}_{k-1}\mbf{L}_{k-1}^{\trans}
  \end{align}
  which is equivalent to \eqref{eq:EKF: prediction cov}.

  Applying the same concept on the correction equation gives
  \begin{align}
    \delta\mbfhatrv{x}_{k}
    &= \mbfcheckrv{x}_{k} + \mbf{K}_{k}\left( \mbfrv{y}_{k} - \mbf{g}\left( \mbfcheckrv{x}_{k}, \mbf{0} \right) \right) - \mbf{x}_{k}\\
    &= \mbfcheckrv{x}_{k} + \mbf{K}_{k}\left( \mbf{g}\left( \mbf{x}_{k},\mbfrv{n}_{k} \right) - \mbf{g}\left( \mbfcheckrv{x}_{k}, \mbf{0} \right) \right) - \mbf{x}_{k}\\
    &= \mbf{x}_{k} + \delta\mbfcheckrv{x}_{k} + \mbf{K}_{k}\left( \mbf{g}\left( \mbf{x}_{k},\mbfrv{n}_{k} \right) - \mbf{g}\left( \mbfcheckrv{x}_{k}, \mbf{0} \right) \right)\\
    &\approx \delta\mbfcheckrv{x}_{k} +\mbf{K}_{k}\left( \mbf{g}\left( \mbfcheck{x}_{k}, \mbf{0} \right) + \mbf{H}_{k}\left( \mbf{x}_{k} - \mbfcheckrv{x}_{k} \right) \right.\nonumber\\&\qquad \left.+ \mbf{L}_{k}\left( \mbfrv{n}_{k}-\mbf{0} \right) - \mbf{g}\left( \mbfcheck{x}_{k},\mbf{0} \right) \right)\\
    &= \delta\mbfcheckrv{x}_{k} +\mbf{K}_{k}\left( \mbf{H}_{k}\left( \mbf{x}_{k} - \mbfcheckrv{x}_{k} \right) + \mbf{L}_{k}\left( \mbfrv{n}_{k}-\mbf{0} \right) \right)\\
    &= \delta\mbfcheckrv{x}_{k} +\mbf{K}_{k}\left( -\mbf{H}_{k}\delta\mbfcheckrv{x}_{k} + \mbf{L}_{k}\left( \mbfrv{n}_{k}-\mbf{0} \right) \right)\\
    &= \left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)\delta\mbfcheckrv{x}_{k} + \mbf{K}_{k}\mbf{M}_{k}\mbfrv{n}_{k}.
  \end{align}
  The covariance is then given by
  \begin{align}
    \cov{\delta\mbfhatrv{x}_{k}} &= \left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)\mbfcheck{P}_{k}\left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)^{\trans} + \mbf{K}_{k}\mbf{M}_{k}\mbf{R}_{k}\mbf{M}_{k}^{\trans}\mbf{K}_{k}^{\trans}
  \end{align}
  which is equivalent to \eqref{eq:EKF: correction cov}.

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % InEKF
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \section{The invariant extended Kalman filter}
  The invariant filter\cite{Barrau_Invariant_2018} is applicable to states that live in Lie groups. However, since the filter deals with random variables, it's important to know how to represent random variables living in Lie groups. That is, $\mbfrv{X}\in G$, where $G$ is some group.

  Let the $SE(n)$ process model be given by
  \begin{align}
    \label{eq:InEKF: process model}
    \mbfrv{X}_{k} &= \mbf{F}\left( \mbfrv{X}_{k-1}, \mbf{u}_{k-1}, \mbfrv{w}_{k-1} \right)\\
    &= \mbfrv{X}_{k-1}\Exp\left( T_{k-1}\mbf{u}_{k-1} \right)\Exp(\mbfrv{w}_{k-1})\\
    \label{eq:InEKF: SE3 process model}
    &= \mbfrv{X}_{k-1}\mbs{\Xi}_{k-1}\Exp(\mbfrv{w}_{k-1}),
  \end{align}
  \marginnote[-4em]{$T_{k-1} \coloneqq t_{k}-t_{k-1}$ is the sampling period.}
  the left-invariant measurement model is given by
  \begin{align}
    \label{eq:InEKF: LI meas. model}
    \mbfrv{y}_{k} &= \mbfrv{X}_{k}\mbf{b} + \mbfrv{n}_{k},
  \end{align}
  and the right-invariant measurement model is given by
  \begin{align}
    \label{eq:InEKF: RI meas. model}
    \mbfrv{y}_{k} &= \mbfrv{X}_{k}\inv\mbf{b} + \mbfrv{n}_{k},
  \end{align}
  where $\mbf{b}$ is some known constant column matrix.

  \subsection{Random variables on Lie groups}
  \label{sec:Random variables on Lie groups}
  In the Euclidean case, Remark~\ref{remark:splitting gaussian rv into sum} can be used to describe a random variable. However, how can we do that in a non-Euclidean case? Let's restrict ourselves with Lie groups.

  A random variable (on a Lie group) can be given by
  \begin{align}
    \mbfrv{X} &= \mbfbar{X}\delta\mbfrv{X}\\
    &= \mbfbar{X}\exp\left( \mbsrv{\xi}\expand \right)\\
    &= \mbfbar{X} \rplus \mbsrv{\xi},
  \end{align}
  where $\rplus$ is the `right perturbation' operator\footnote{There are multiple versions of the $\oplus$ operator such as left, left-invariant, right, and right-invariant.} from
   \cite{Sola_Micro_2020} and
  \begin{align}
    \mbsrv{\xi} &\sim \mc{N}\left( \mbf{0}, \mbs{\Sigma} \right).
  \end{align}
  This is the Lie-group version of Remark~\ref{remark:splitting gaussian rv into sum}. Note that $\mbfbar{X}\in G$ and $\exp\left( \mbsrv{\xi}\expand \right)\in G$, thus $\mbfrv{X}\in G$ since $G$ is a Lie group which is closed under multiplication.

  \subsection{Left-invariant perturbation}
  The left-invariant ``addition'' $\liplus : G \times \rnums^{n} \to G$ is defined by
  \begin{align}
    \mbf{X}\liplus\mbs{\xi}
    &\coloneqq \mbf{X}\exp\left( -\mbs{\xi}\expand \right)\\
    &= \mbf{X}\Exp\left( -\mbs{\xi} \right),
  \end{align}
  and the left-invariant ``subtraction'' $\liminus : G \times G \to \rnums^{n}$ is defined by
  \begin{align}
    \mbf{X}_{2}\ominus\mbf{X}_{1}
    &\coloneqq \log\left(\mbf{X}_{2}\inv\mbf{X}_{1}\right)\contract\\
    &= \Log\left(\mbf{X}_{2}\inv\mbf{X}_{1}\right).
  \end{align}

  \subsection{The invariant extended Kalman filter}
  Without deep derivation, let's take the extended Kalman filter equations \eqref{eq:EKF eqns} and expand them to states that live on smooth manifolds. Let's simply replace the Euclidean $+$ and $-$ operators with $\oplus$ and $\ominus$, respectively\footnote{There are different `flavors' as discussed above.}.

  The prediction equations will then be
  \begin{align}
    \label{eq:InEKF: state prediction}
    \mbfcheck{X}_{k} &= \mbf{F}\left( \mbfhat{X}_{k-1}, \mbf{u}_{k-1}, \mbf{0} \right),\\
    \label{eq:InEKF: state prediction cov.}
    \mbfcheck{P}_{k} &= \mbf{A}_{k-1}\mbfhat{P}_{k-1}\mbf{A}_{k-1} + \mbf{L}_{k-1}\mbf{Q}_{k-1}\mbf{L}_{k-1}^{\trans}.
  \end{align}
  But what are $\mbfcheck{P}_{k}$, $\mbf{A}_{k-1}$, and $\mbf{L}_{k-1}$ exactly? Remember, we had to define an error (left invariant, right invariant, etc.).

  Similar to the estimator prediction error \eqref{eq:KF: prediction err. def.} and correction error \eqref{eq:KF: correction err. def.} of the extended Kalman filter, define the estimator prediction and correction left-invariant errors as
  \sidenote{Note that $\mbf{X}_{k}$ is not a random variable in the error definition.}
  \sidenote{The definition $\delta\mbscheckrv{\xi}\coloneqq \mbfcheckrv{X} \liminus \mbf{X}$ can also be used. However, the KF equations would look slightly different but the results would still hold.}
  \begin{align}
    \label{eq:InEKF: prediction error}
    \delta\mbscheckrv{\xi}_{k} &\coloneqq \mbf{X}_{k} \liminus \mbfcheckrv{X}_{k},\\
    \label{eq:InEKF: correction error}
    \delta\mbshatrv{\xi}_{k}  &\coloneqq \mbf{X}_{k} \liminus \mbfhatrv{X}_{k},
  \end{align}
  respectively.

  Plugging \eqref{eq:InEKF: process model} and \eqref{eq:InEKF: state prediction} into \eqref{eq:InEKF: prediction error} results in
  \begin{align}
    \delta\mbscheckrv{\xi}_{k}
    &= \mbf{X}_{k} \liminus \mbfcheckrv{X}_{k}\\
    &= \mbf{F}\left( \mbfhatrv{X}_{k-1}, \mbf{u}_{k-1}, \mbfrv{w}_{k-1} \right) \liminus \mbf{F}\left( \mbfhat{X}_{k-1}, \mbf{u}_{k-1}, \mbf{0} \right)\\
    &= \Log\left( \left( \mbf{X}_{k-1}\mbs{\Xi}_{k-1}\Exp(\mbfrv{w}_{k-1}) \right)\inv\mbfhatrv{X}_{k-1}\mbs{\Xi}_{k-1} \right)\\
    &= \Log\left( \Exp(-\mbfrv{w}_{k-1})\mbs{\Xi}_{k-1}\inv\mbf{X}_{k-1}\inv\mbfhatrv{X}_{k-1}\mbs{\Xi}_{k-1} \right)\\
    &= \Log\left( \Exp(-\mbfrv{w}_{k-1})\mbs{\Xi}_{k-1}\inv\Exp(\mbshatrv{\xi}_{k-1})\mbs{\Xi}_{k-1} \right)\\
    &= \Log\left( \Exp(-\mbfrv{w}_{k-1})\Exp\left( \Adj{\mbs{\Xi}_{k-1}\inv}\mbshatrv{\xi}_{k-1} \right) \right)\\
    \label{eq:InEKF: prediction err. function of Jacs}
    &\approx \underbrace{\Adj{\mbs{\Xi}_{k-1}\inv}}_{\mbf{A}_{k-1}}\mbshatrv{\xi}_{k-1} - \mbfrv{w}_{k-1},
  \end{align}
  \marginnote[-11em]{Note the usage of $\Exp(\mbshatrv{\xi}_{k-1}) = \mbf{X}_{k-1}\inv\mbfhatrv{X}_{k-1}$.}
  \marginnote[-7em]{Note $\mbf{X}\Exp(\mbs{\xi})\mbf{X}\inv = \Exp(\Adj{\mbf{X}}\mbs{\xi})$.}
  where the last equation is an approximation from the BCH formula \cite{Barfoot_State_2017} and $\mbf{L}_{k-1} = -\eye$.

  The covariance on the prediction error is then given by
  \begin{align}
    \mbfcheck{P}_{k} &\coloneqq \cov{\mbscheckrv{\xi}_{k}} \\
    &\approx \mbf{A}_{k-1}\mbfhat{P}_{k-1}\mbf{A}_{k-1}^{\trans} + \mbf{L}_{k-1}\mbf{Q}_{k-1}\mbf{L}_{k-1}^{\trans}.
  \end{align}

  The correction equations can be generalized from \eqref{eq:EKF: correction} by using `$\liplus$' in place of `$+$'. Specifically,
  \begin{align}
    \mbfhatrv{X}_{k} &= \mbfcheckrv{X}_{k} \liplus \left( \mbf{K}_{k}(\mbf{y}_{k} - \mbf{g}(\mbfcheckrv{X}_{k}, \mbf{0})) \right).
  \end{align}
  What's the covariance on $\mbfhatrv{X}_{k}$?
  \sidenote{Actually, covariance is on $\mbshatrv{\xi}_{k}$.} To answer the question, lets' stick with the left-invariant measurement function \eqref{eq:InEKF: LI meas. model} and plug in the appropriate variables into \eqref{eq:InEKF: correction error}.
  \begin{align}
    \mbshatrv{\xi}_{k}
    &= \mbf{X}_{k}\liminus\mbfhatrv{X}_{k}\\
    &= \mbf{X}_{k} \liminus \left( \mbfcheckrv{X}_{k}\liplus (\mbf{K}_{k}\mbfrv{z}_{k}) \right)\\
    &= \Log\left( \mbf{X}_{k}\inv\mbfcheckrv{X}_{k}\Exp(-\mbf{K}_{k}\mbfrv{z}_{k}) \right)\\
    &= \Log(\Exp(\mbscheckrv{\xi}_{k})\Exp(-\mbf{K}_{k}\mbfrv{z}_{k}))\\
    \label{eq:InEKF: correction error as a function of innovation}
    &\approx \mbscheckrv{\xi}_{k} - \mbf{K}_{k}\mbfrv{z}_{k}.
  \end{align}
  What's the innovation $\mbfrv{z}_{k}$? That's where invariant filtering comes in. Define the left-invariant innovation\sidenote{The left-invariant innovation is used since we assumed we have a left-invariant measurement function.} by
  \begin{align}
    \label{eq:InEKF: innovation: begin}
    \mbfrv{z}_{k}
    &\coloneqq \mbfcheckrv{X}_{k}\inv\left( \mbfrv{y}_{k} - \mbf{g}(\mbfcheckrv{X}_{k},\mbf{0}) \right)\\
    &= \mbfcheckrv{X}_{k}\inv\left( \mbf{X}_{k}\mbf{b} + \mbfrv{n}_{k} - \mbfcheck{X}_{k}\mbf{b}\right)\\
    &= \mbfcheckrv{X}_{k}\inv\mbf{X}_{k}\mbf{b} + \mbfcheckrv{X}_{k}\inv\mbfrv{n}_{k} - \mbf{b}\\
    &= \Exp(-\mbscheckrv{\xi}_{k})\mbf{b} - \mbf{b} \mbfcheckrv{X}_{k}\inv\mbfrv{n}_{k}\\
    &\approx \left( \eye - \mbscheckrv{\xi}_{k}\expand \right)\mbf{b} - \mbf{b} + \mbfcheckrv{X}_{k}\inv\mbfrv{n}_{k}\\
    &= -\mbscheckrv{\xi}_{k}\expand\mbf{b} + \mbfcheckrv{X}_{k}\inv\mbfrv{n}_{k}\\
    \label{eq:InEKF: innovation: Jacobians}
    &= \underbrace{-\mbf{b}^{\odot}}_{\mbf{H}_{k}}\mbscheckrv{\xi}_{k} + \underbrace{\mbfcheckrv{X}_{k}\inv}_{\mbf{M}_{k}}\mbfrv{n}_{k},
  \end{align}
  \marginnote[-1.5cm]{Note that the Jacobian w.r.t. the state  $\mbf{H}_{k}$ is state-independent. That is, it does not depend on the state $\mbf{X}_{k}$.}
  where
  \begin{align}
    \mbs{\xi}\expand\mbf{b} \coloneqq \mbf{b}^{\odot}\mbs{\xi}\\
  \end{align}
  is defined
  \begin{align}
    \bbm
      \mbf{b}_{1:3}\\
      b_{4}
    \ebm^{\odot}
    &=
    \bbm
      -\mbf{b}_{1:3}\crossop & b_{4}\eye \\ \mbf{0} & \mbf{0}
    \ebm
  \end{align}
  for $SE(3)$ and
  \begin{align}
    \bbm
      \mbf{b}_{1:2}\\
      b_{3}
    \ebm^{\odot}
    &=
    \bbm
      (1)\crossop\mbf{b}_{1:2} & b_{3}\eye \\ \mbf{0} & \mbf{0}
    \ebm\\
    &=
    \bbm
      \bbm 0 & -1\\1 & 0 \ebm\mbf{b}_{1:2} & b_{3}\eye \\ \mbf{0} & \mbf{0}
    \ebm
  \end{align}
  for $SE(2)$.

  Plugging the above results into \eqref{eq:InEKF: correction error as a function of innovation} gives the approximation
  \begin{align}
    \mbshatrv{\xi}_{k} &=
    \mbscheckrv{\xi}_{k} - \mbf{K}_{k}\mbfrv{z}_{k}\\
    &\approx
    \mbscheckrv{\xi}_{k} - \mbf{K}_{k}\left( \mbf{H}_{k}\mbscheckrv{\xi}_{k} + \mbf{M}_{k}\mbfrv{n}_{k} \right)\\
    &= \left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)\mbscheckrv{\xi}_{k} - \mbf{K}_{k}\mbf{M}_{k}\mbfrv{n}_{k},
  \end{align}
  which looks very similar to the Kalman filter correction equation with a difference of sign on $\mbfrv{n}_{k}$. The covariance on the correction error is then given by
  \begin{align}
    \mbfhat{P}_{k} &\coloneqq \cov{\mbshatrv{\xi}_{k}}\\
    &\approx \left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)\mbfcheck{P}_{k}\left( \eye - \mbf{K}_{k}\mbf{H}_{k} \right)^{\trans} + \mbf{K}_{k}\mbf{M}_{k}\mbf{R}_{k}\mbf{M}_{k}^{\trans}\mbf{K}_{k}^{\trans},
  \end{align}
  which exactly matches the EKF correction covariance \eqref{eq:EKF: correction cov}.

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % L-InEKF example
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \section{Example: Left-invariant extended Kalman filter}
  In this section, an example of the left-invariant extended Kalman filter (L-InEKF) is presented. The example is applied on a robot in 3D space. That is, the states $\mbf{X}$ live in the 3D special Euclidean Lie group $SE(3)$. A \cpp implementation of this example is provided in the repository.

  The state is given by
  \begin{align}
    \mbf{X}_{k} &=
    \bbm \mbf{C}_{ab_{k}} &\disp[b_{k}a][a] \\ \mbf{0} & 1 \ebm\\
    &= \Exp(\mbs{\xi}_{k}),
  \end{align}
  where $\dcm[ab]\in SO(3)$ is the attitude\footnote{Specifically, it's the DCM of frame $\rframe_{a}$ relative to frame $\rframe_{b}$.} and $\disp[b_{k}a][a]$ is the displacement of point $b_{k}$ (robot body) relative to some arbitrary point $a$ resolved in the (world) frame $\rframe_{a}$.\footnote{I realize that it might be confusing to use the same letter to denote a point and a frame.}
  Furthermore, the Lie algebra coordinates are given by
  \begin{align}
    \label{eq:SE2 xi = [xiphi; xir]}
    \mbs{\xi}_{k} &=
    \bbm
      \mbs{\xi}^{\phi}\\
      \mbs{\xi}^{\mathrm{r}}
    \ebm,
  \end{align}
  where $\mbs{\xi}^{\phi}$ are coordinates associated with attitude, and $\mbs{\xi}^{\mathrm{r}}$ are the generalized position\footnote{Note that $\mbs{\xi}^{\mathrm{r}}\neq\disp[b_{k}a][a]$ in general.}. Note that some authors use different ordering for $\mbs{\xi}$. For example, in \cite{Barfoot_State_2017}, the ordering of $\mbs{\xi}^{\phi}$ and $\mbs{\xi}^{\mathrm{r}}$ is flipped. This has an effect on the Adjoint representation and the Jacobians, so care must be taken.

  \subsection{Process model}
  The process model is given by \eqref{eq:InEKF: SE3 process model} and the Jacobians are given by \eqref{eq:InEKF: prediction err. function of Jacs}.
  Specifically, the Jacobian of the process model w.r.t. $\mbs{\xi}$ is given by
  \begin{align}
    \mbf{A}_{k-1} &= \Adj{\mbs{\Xi}_{k-1}\inv},
  \end{align}
  where
  \begin{align}
    \Adj{\mbf{X}} &=
    \bbm
      \dcm[ab] & \mbf{0} \\
      {\disp[ba][a]}\crossop\dcm[ab] & \dcm[ab]
    \ebm,\\
    \Adj{\mbf{X}\inv} &=
    \bbm
    \dcmtrans[ab] & \mbf{0} \\
    -\dcmtrans[ab]{\disp[ba][a]}\crossop & \dcmtrans[ab]
    \ebm.
  \end{align}
  \marginnote[-2cm]{Note that the Adjoint representation depends on the ordering of $\mbs{\xi}$ defined in \eqref{eq:SE2 xi = [xiphi; xir]}.}

  \subsection{Measurement model: GPS}
  Let the measurement model be a position sensor given by
  \begin{align}
    \mbfrv{y}_{k}
    &= \disp[b_{k}a][a] + \mbfrv{n}_{k} \\
    &= \bbm \eye & \mbf{0} \ebm
    \underbrace{\bbm \dcm[ab_{k}] & \disp[b_{k}a][a] \\\mbf{0} & 1 \ebm}_{\mbf{X}_{k}}
    \underbrace{\bbm \mbf{0}\\ 1 \ebm}_{\mbf{b}} + \mbfrv{n}_{k}.
  \end{align}
  To make things easier, let
  \begin{align}
    \underbrace{
    \bbm
      \mbfrv{y}_{k} \\
      1
    \ebm}_{\mbftilderv{y}_{k}}
    &=
    \mbf{X}_{k}\mbf{b} +
    \underbrace{
    \bbm
      \mbfrv{n}_{k} \\ 0
    \ebm}_{\tilde{\mbfrv{n}}_{k}}.
  \end{align}

  The innovation \eqref{eq:InEKF: innovation: Jacobians} (using $\tilde{\mbfrv{y}}_{k}$) is then given by
  \begin{align}
    \mbftilderv{z}_{k}
    &\approx -\mbf{b}^{\odot}\mbscheckrv{\xi}_{k} + \mbfcheckrv{X}_{k}\inv \mbftilderv{n}_{k}\\
    &= -\bbm \mbf{0} & \eye \\ \mbf{0} & \mbf{0} \ebm \mbscheckrv{\xi}_{k} + \bbm \mbfcheckrv{C}_{ab_{k}}^{\trans} & -\mbfcheckrv{C}_{ab_{k}}^{\trans}\mbfcheckrv{r}^{b_{k}a}_{a} \\ \mbf{0} & 1 \ebm \bbm \mbfrv{n}_{k} \\ 0 \ebm\\
    &=
    \bbm
      - \bbm \mbf{0} & \eye \ebm\mbscheckrv{\xi}_{k} + \mbfcheckrv{C}_{ab_{k}}^{\trans}\mbfrv{n}_{k}\\
      0
    \ebm\\
    &= \bbm
      \mbfrv{z}_{k}\\
      0
     \ebm.
  \end{align}
  \marginnote[-4.5cm]{
    From \cite{Barfoot_State_2017}, the $(\cdot)^{\odot}$ operator for $SE(3)$ is given by
    \begin{align}
      \bbm
        \mbf{b}_{1:3}\\
        b_{4}
      \ebm^{\odot}
      &=
      \bbm
        -\mbf{b}_{1:3}\crossop & b_{4}\eye \\ \mbf{0} & \mbf{0}
      \ebm.
    \end{align}
  }
  Ignoring the last row of the equation above gives the (modified) innovation
  \begin{align}
    \mbfrv{z}_{k} &= \underbrace{-\bbm \mbf{0} & \eye \ebm}_{\mbf{H}_{k}}\mbscheckrv{\xi}_{k} + \underbrace{\mbfcheckrv{C}_{ab_{k}}^{\trans}}_{\mbf{M}_{k}}\mbfrv{n}_{k}.
  \end{align}
  Thus,
  \begin{align}
    \mbf{H}_{k} &= - \bbm \mbf{0} & \eye \ebm,\\
    \mbf{M}_{k} &= \mbfcheckrv{C}_{ab_{k}}^{\trans}.
  \end{align}
  \marginnote[-1.7cm]{Note that the Jacobian of the innovation w.r.t. to $\mbscheck{\xi}_{k}$ is state-independent.}

  \subsection{Results}
  The results of using the $SE(2)$ \cpp filter are presented in
  Figure~\ref{fig:se2_example_trajectories}-\ref{fig:se2_example_error_plots}.

  \begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figs/se2_example_trajectories.eps}
    \caption{Ground truth and estimated trajectories from the $SE(2)$ example.}
    \label{fig:se2_example_trajectories}
  \end{figure}

  \begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figs/se2_example_error_plots.eps}
    \caption{Error plots from the $SE(2)$ example.\\ Note that $\mbs{\xi}_{k} = \Log(\mbf{X}_{k})$, where $\mbf{X}_{k}\in SE(2)$ is the planar pose at time $t_{k}$.}
    \label{fig:se2_example_error_plots}
  \end{figure}

  \clearpage
  \begin{appendices}
    \input{sections/appendices.tex}
  \end{appendices}

  \clearpage
  \printbibliography
\end{document}