\chapter{Learning the parameters of a state-space model}
\label{chap:inference}

This chapter describes the state-space model (SSM) formulation we are working with. In \autoref{sec:ssm-definition}, we formally define the SSM and state our assumptions about the individual probability distributions.

In \autoref{sec:parameter-inference}, we calculate the posterior distribution of the parameters of interest, and show that straightforward inference is not possible. Further on, we derive a sampler to approximate this distribution. This sampler is unusable, as it requires the evaluation of the intractable likelihood. Nevertheless, it is illustrative to compare it with the variant derived later.

To circumvent the likelihood evaluation, we introduce the particle filter in \autoref{sec:particle-filter}. This section gives the definition and some of the properties of the filter.

Finally, in \autoref{sec:particle-filter-estimate} we show how to use the particle filter to estimate the likelihood, and argue that it does not affect the asymptotic properties of the sampler.

Most of this chapter is based on \cite{andrieu} and \cite{schoen}.

\section{State-Space Model definition} \label{sec:ssm-definition}
The state-space model, often also called the hidden Markov model (HMM) assumes a sequence of latent states $\left\{\bx_t\right\}_{t=0}^\infty \subseteq \R^{d_x}$ following a Markov chain, and a sequence of observed variables $\left\{\by_t\right\}_{t=1}^\infty \subseteq \R^{d_y}$. All involved distributions are parameterized by an unknown static parameter $\btheta \in \Theta \subset \R^d$.

For a fixed time $T \geq 1$, we use the shorthands $\bx_{0:T} = \left\{\bx_t\right\}_{t=0}^T$ and $\by_{1:T} = \left\{\by_t\right\}_{t=1}^T$ throughout the thesis.

The HMM formulation means that the joint distribution of $\bx_{0:T}$ and $\by_{1:T}$ factorizes, for any $T \geq 1$, into
\begin{equation}\label{eq:factorization}
p(\bx_{0:T}, \by_{1:T} \mid \btheta) = \sprior(\bx_0 \mid \btheta) \prod_{t = 1}^{T} \trans_t(\bx_t \mid \bx_{t-1}, \btheta) \obs_t(\by_t \mid \bx_t, \btheta),
\end{equation}
where $\sprior(\bx_0 \mid \btheta)$ is the prior distribution over the initial state, $\trans_t(\bx_t \mid \bx_{t-1}, \btheta)$ is the transition distribution at time $t$ and $\obs_t(\by_t \mid \bx_t, \btheta)$ is the observation model at time $t$.

The factorization \eqref{eq:factorization} can be written more clearly as
\begin{alignat*}{2}
\bx_0 \mid \btheta & \sim \sprior(\bx_0 \mid \btheta), & \\
\bx_t \mid \bx_{t-1}, \btheta & \sim \trans_t(\bx_t \mid \bx_{t-1}, \btheta), \quad & t = 1, \ldots, T, \\
\by_t \mid \bx_t, \btheta & \sim \obs_t(\by_t \mid \bx_t, \btheta), \quad & t = 1, \ldots, T.
\end{alignat*}

Finally, in accordance with the Bayesian approach \citep{bayes}, we introduce a prior distribution $\pprior$ over the unknown parameter $\btheta$ quantifying our knowledge about $\btheta$ before having observed any data. This allows us to state the full joint distribution
\begin{equation}\label{eq:full-joint}
p(\bx_{0:T}, \by_{1:T}, \btheta) = p(\bx_{0:T}, \by_{1:T} \mid \btheta) \pprior(\btheta).
\end{equation}
The corresponding graphical model is depicted in \autoref{fig:graphical-model}.
\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
    % Style
    \tikzstyle{main}=[circle, minimum size = 10mm, thick, draw =black!80, node distance = 16mm]
    \tikzstyle{connect}=[-latex, thick]

    % Nodes X
    \node[main,shape=circle,draw=black](X0) at (1,4) {$\bx_0$};
    \node[main,shape=circle,draw=black](X1) at (3,4) {$\bx_1$};
    \node[main,shape=circle,draw=black](X2) at (5,4) {$\bx_2$};
    \node[](Xdots) at (7,4) {$\ldots$};
    \node[main,shape=circle,draw=black](XT) at (9,4) {$\bx_T$};

    % Node theta
    \node[](theta) at (7,2) {$\btheta$};

    % Nodes Y
    \node[main,shape=circle,draw=black,fill=black!20](Y1) at (3,0) {$\by_1$};
    \node[main,shape=circle,draw=black,fill=black!20](Y2) at (5,0) {$\by_2$};
    \node[](Ydots) at (7,0) {$\ldots$};
    \node[main,shape=circle,draw=black,fill=black!20](YT) at (9,0) {$\by_T$};

    % Edges XX
    \path [->] (X0) edge[connect] node[left] [above] {$\trans_1$} (X1);
    \path [->] (X1) edge[connect] node[left] [above] {$\trans_2$} (X2);
    \path [->] (X2) edge[connect] node[left] [above] {$\trans_3$} (Xdots);
    \path [->] (Xdots) edge[connect] node[left] [above] {$\trans_T$} (XT);

    % Edges XY
    \path [->] (X1) edge[connect] node[left] [left] {$\obs_1$} (Y1);
    \path [->] (X2) edge[connect] node[left] [left] {$\obs_2$} (Y2);
    \path [->] (XT) edge[connect] node[left] [left] {$\obs_T$} (YT);

    % Edges theta X
    \path [->] (theta) edge[connect] node[left] {} (X0);
    \path [->] (theta) edge[connect] node[left] {} (X1);
    \path [->] (theta) edge[connect] node[left] {} (X2);
    \path [->] (theta) edge[connect] node[left] {} (XT);

    % Edges theta Y
    \path [->] (theta) edge[connect] node[left] {} (Y1);
    \path [->] (theta) edge[connect] node[left] {} (Y2);
    \path [->] (theta) edge[connect] node[left] {} (YT);
    \end{tikzpicture}
    \caption{Graphical model describing the full joint distribution \eqref{eq:full-joint}. The shaded nodes denote the observed variables, white nodes represent the latent variables.}
    \label{fig:graphical-model}
\end{figure}

\section{Parameter inference} \label{sec:parameter-inference}
Given an observed sequence $\by_{1:T}$, Bayesian inference relies on the joint posterior density
\begin{equation}\label{eq:joint-posterior}
p(\btheta, \bx_{0:T} \mid \by_{1:T}) = \underbrace{p(\bx_{0:T} \mid \btheta, \by_{1:T})}_{\text{State inference}} \underbrace{p(\btheta \mid \by_{1:T})}_{\text{Parameter inference}}.
\end{equation}
Our primary goal is to infer the static parameter $\btheta$. From \eqref{eq:joint-posterior}, it is clear that for state inference, one needs knowledge about $\btheta$, so even if the latent states $\bx_{0:T}$ are of interest, knowledge about $\btheta$ is necessary.

\paragraph{Bayesian inference}

To perform Bayesian inference of $\btheta$, we express the posterior of $\btheta$ by applying the Bayes theorem:
\begin{equation} \label{eq:posterior}
p(\btheta \mid \by_{1:T}) = \frac{p(\by_{1:T} \mid \btheta) \pprior(\btheta)}{\int p(\by_{1:T} \mid \btheta) \pprior(\btheta) \; \dx{\btheta}}.
\end{equation}
Evaluating the likelihood $p(\by_{1:T} \mid \btheta)$ requires marginalising over $\bx_{0:T}$:
\begin{equation} \label{eq:likelihood}
p(\by_{1:T} \mid \btheta) = \int p(\bx_{0:T}, \by_{1:T} \mid \btheta) \; \dx{\bx_{0:T}},
\end{equation}
where $p(\bx_{0:T}, \by_{1:T} \mid \btheta)$ is given in \eqref{eq:factorization}. Unless the SSM is linear and Gaussian, such $d_x(T+1)$-dimensional integral is intractable \citep{andrieu}.

\paragraph{Inference under tractable likelihood assumption}

Let us first proceed as if the likelihood was tractable. We derive a sampler for $\btheta$ and note which component cannot be evaluated because of dependence on the intractable likelihood \eqref{eq:likelihood}. \autoref{sec:particle-filter-estimate} then describes the necessary modifications to allow circumventing the likelihood evaluation.

Often, the interest is not directly in the posterior $p(\btheta \mid \by_{1:T})$ itself, but in the expectation of some function $\phi$ w.r.t. this distribution, i.e., in
\begin{equation} \label{eq:posterior-integral}
\E_{p(\cdot \mid \by_{1:T})}[\phi(\btheta)] = \int \phi(\btheta) p(\btheta \mid \by_{1:T}) \; \dx{\btheta}.
\end{equation}
We construct a Metropolis-Hastings sampler \citep{metropolis, hastings} with target distribution $p(\btheta \mid \by_{1:T})$. This gives us $M$ samples approximately distributed according to this target, denoted $\btheta^{(m)},\ m = 1, \ldots, M$. The expectation \eqref{eq:posterior-integral} is then approximated by the arithmetic mean
\begin{equation*}
\frac{1}{M} \sum_{m=1}^M \phi(\btheta^{(m)}).
\end{equation*}
An appealing property of the Metropolis-Hastings algorithm is that such arithmetic mean almost surely converges to \eqref{eq:posterior-integral} as the number of samples grows \citep{robert-casella}, i.e.,
\begin{equation*}
\frac{1}{M} \sum_{m=1}^M \phi(\btheta^{(m)}) \xrightarrow[M \to \infty]{a.s} \int \phi(\btheta) p(\btheta \mid \by_{1:T}) \; \dx{\btheta}.
\end{equation*}

Finally, we note that if one is interested in the distribution $p(\btheta \mid \by_{1:T})$ itself, it can be recovered by the empirical distribution
\begin{equation*}
\widehat{p}(\btheta \mid \by_{1:T}) = \frac{1}{M} \sum_{m=1}^M \delta_{\btheta^{(m)}}(\btheta),
\end{equation*}
where $\delta$ denotes the Dirac distribution. This estimate can be additionally smoothed using kernel methods \citep{kernel-smoothing}.

\paragraph{Metropolis-Hastings algorithm}
The Metropolis-Hastings algorithm is described in \autoref{alg:metropolis-hastings}. Although well-known, it is included for comparison with the variant utilizing the particle filter introduced in \autoref{alg:marginal-metropolis-hastings}.

The algorithm constructs a Markov chain on the variable $\btheta$, whose transition distribution $q$ is called the proposal distribution in this context. Starting from an initial state $\btheta_0$, candidate states $\btheta^\prime$ are iteratively sampled according to $q(\cdot \mid \btheta)$, where $\btheta$ is the current state of the chain.

In the next step, the acceptance probability $\alpha$ is calculated in \eqref{eq:acceptance-probability}. This probability considers which of the two states $\btheta$ and $\btheta^\prime$ is more probable under the target distribution ${p(\cdot \mid \by_{1:T}) \propto p(\by_{1:T} \mid \btheta) \pprior(\btheta)}$. Additionally, it allows the chain to ``step back'' and not move to the new state $\btheta^\prime$ by comparing the probability of the two states under $q$, but in reverse direction. With probability $\alpha$, the Markov chain then evolves into $\btheta^\prime$; otherwise, it remains in the current state.

It can be shown \citep{robert-casella} that the distribution $p(\btheta \mid \by_{1:T})$ is the limiting distribution of such Markov chain. This means that with the number of transitions going to infinity, the sampled $\btheta$ are distributed according to our target distribution $p(\btheta \mid \by_{1:T})$. To approximately reach this limiting distribution, a number of initial samples (called the burn-in period) is often discarded. In addition, one usually wants independent samples from the target distribution, which the samples from a Markov chain are \emph{not}. In practice, only samples with a given spacing are kept to ensure their approximate independence; this is called thinning.

Similarly to the prior $\pprior$, setting the proposal $q$ is problem-dependent, and both distributions must be selected carefully. Diagnosing converge of the sampler is a notably difficult task, and one usually resorts to graphical tools to determine whether the sampled values have stabilized \citep{mcmc}. Some of such plots are given in \autoref{chap:applications}.

\begin{algorithm}[ht]
    \caption{Metropolis-Hastings}
    \label{alg:metropolis-hastings}
    \begin{algorithmic}[1]
        \Input $\text{Number of samples } M,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Initialize } \btheta^{(0)}.$

        \For{$m = 1\ \mathbf{to}\ M$}
            \State $\text{Sample } \btheta^\prime \sim \prop(\cdot \mid \btheta^{(m-1)}).$
            \State $\text{Calculate the aceptance probability } $ \begin{equation} \label{eq:acceptance-probability}
            \alpha = \min \left\{1, \frac{p(\by_{1:T} \mid \btheta^\prime) \pprior(\btheta^\prime)}{p(\by_{1:T} \mid \btheta^{(m-1)}) \pprior(\btheta^{(m-1)})} \frac{\prop(\btheta^{(m-1)} \mid \btheta^\prime)}{\prop(\btheta^\prime \mid \btheta^{(m-1)})} \right\}.
            \end{equation}
            \State $\text{Sample } u \sim \mathcal{U}(0,1).$
            \If {$u \leq \alpha$}
                \State $\btheta^{(m)} \gets \btheta^\prime$ \Comment{With probability $\alpha$, accept the proposed sample.}
            \Else
                \State $\btheta^{(m)} \gets \btheta^{(m-1)}$ \Comment{With probability $1 - \alpha$, reject the proposed sample.}
            \EndIf
        \EndFor

        \Output $\left\{ \btheta^{(1)}, \ldots, \btheta^{(M)} \right\}$
    \end{algorithmic}
\end{algorithm}

We see from \autoref{alg:metropolis-hastings} that the acceptance probability \eqref{eq:acceptance-probability} cannot be calculated, as it depends on the intractable likelihood $p(\by_{1:T} \mid \btheta)$. In \autoref{sec:particle-filter-estimate}, we give a modified variant of the Metropolis-Hastings algorithm, where the likelihood is approximated using the particle filter. The derivation of this filter is the content of the next section.

\section{The particle filter} \label{sec:particle-filter}
The particle filter \citep{particle-filter} is a method for approximating the filtering distribution $p(\bx_t \mid \by_{1:t}, \btheta)$ using a finite number of samples called particles. The algorithm is also known as sequential Monte Carlo or sequential importance sampling. The latter name sheds some light on how the method works, and it is exactly through importance sampling that the particle filter is derived.

\paragraph{Importance sampling}
Here we briefly review the basic idea behind importance sampling. For a more thorough treatment, the reader is referred to \cite{information-theory} or \cite{robert-casella}.

Consider a situation where the expectation of some function $\phi$ w.r.t. the distribution with density $p(\bm{x})$,
\begin{equation} \label{eq:is-expectation}
\Phi \coloneqq \E_{p}[\phi(\bm{X})] = \int \phi(\bm{x}) p(\bm{x}) \; \dx{\bm{x}},
\end{equation}
is of interest. Assume that the integral is analytically intractable and that one cannot generate samples from $p(\bm{x})$ to approximate this expectation. Assume further that the density $p(\bm{x})$ can be evaluated, at least up to a multiplicative constant, i.e., that it takes the form
\begin{equation*}
p(\bm{x}) = \frac{p^*(\bm{x})}{Z},
\end{equation*}
where $Z$ is an unknown normalizing constant, and $p^*(\bm{x})$ can be evaluated. Such situation frequently arises in Bayesian statistics, where a posterior distribution of interest
\begin{equation*}
{p(\btheta \mid \bm{x}) = \frac{p(\bm{x} \mid \btheta) p(\btheta)}{\int p(\bm{x} \mid \btheta) p(\btheta) \; \dx{\btheta}}}
\end{equation*}
is given in terms of the Bayes theorem. The normalizing constant in the denominator is often unavailable in analytic form. However, the numerator can be evaluated.

Next, we introduce a (typically simpler) distribution with density $q(\bm{x}) = \frac{q^*(\bm{x})}{Z_Q}$ s.t.
\begin{enumerate}
    \item One can sample from $q$;
    \item One can evaluate $q^*$;
    \item $p(\bm{x}) > 0$ implies $q(\bm{x}) > 0$.
\end{enumerate}
The expectation \eqref{eq:is-expectation} can then be written as
\begin{equation*}
\Phi = \int \phi(\bm{x}) \frac{q(\bm{x})}{q(\bm{x})} p(\bm{x}) \; \dx{\bm{x}} = \int \phi(\bm{x}) \underbrace{\frac{p(\bm{x})}{q(\bm{x})}}_{w^*(\bm{x})} q(\bm{x}) \; \dx{\bm{x}} = \E_{q}[\phi(\bm{X}) w^*(\bm{X})],
\end{equation*}
where $w^*(\bm{x})$ are called the importance weights. By defining $w(\bm{x}) = \frac{p^*(\bm{x})}{q^*(\bm{x})}$, $\Phi$ can be approximated by
\begin{equation*}
\Phi \approx \widehat{\Phi} \coloneqq \frac{\sum_{i=1}^N \phi(\bm{x}^{(i)}) w(\bm{x}^{(i)})}{\sum_{i=1}^Nw(\bm{x}^{(i)})}, \quad \bm{x}^{(1)}, \ldots, \bm{x}^{(N)} \stackrel{iid}{\sim} q(\bm{x}).
\end{equation*}
We note that by using $w$ instead of $w^*$ and normalizing by the weights sum instead of the sample size $N$, we bypass the evaluation of $Z$ and $Z_Q$, since they cancel out. The importance weights here account for correcting the discrepancy between the distribution $q(\bm{x})$ and the true distribution $p(\bm{x})$.

The estimator $\widehat{\Phi}$ converges to the true expectation $\Phi$ as $N \to \infty$. However, it is not necessarily unbiased \citep{information-theory}.

\paragraph{Sequential importance sampling (SIS)}
The SIS algorithm uses a set of weighted particles $\left\{\left(\bm{x}_t^{(i)}, w_t^{(i)} \right) : i = 1, \ldots, N \right\}$ to represent the filtering distribution $p(\bm{x}_t \mid \by_{1:t}, \btheta)$. To simplify notation, we write $w_t^{(i)}$ instead of $w_t(\bm{x}^{(i)})$ from now on. The empirical approximation to ${p(\bm{x}_t \mid \by_{1:t}, \btheta)}$ is then
\begin{equation*}
\widehat{p}(\bm{x}_t \mid \by_{1:t}, \btheta) = \frac{\sum_{i=1}^N w_t^{(i)} \delta_{\bm{x}_t^{(i)}}(\bm{x}_t)}{\sum_{i=1}^N w_t^{(i)}}.
\end{equation*}

As the name suggests, the algorithm involves a sequential application of the importance sampling procedure with increasing time $t$.

Returning to the SSM \eqref{sec:ssm-definition}, we consider the posterior distribution of a sequence of states $\bx_{0:t}$ given a sequence of observations $\by_{1:t}$. By application of the Bayes theorem, we obtain the following recursive formula:
\begin{equation*}
\begin{split}
p(\bx_{0:t} \mid \by_{1:t}) & \propto p(\by_t \mid \bx_{0:t}, \by_{1:t-1}) p(\bx_{0:t} \mid \by_{1:t-1}) \\
&= \obs_t(\by_t \mid \bx_t) p(\bx_t \mid \bx_{0:t-1}, \by_{1:t-1}) p(\bx_{0:t-1} \mid \by_{1:t-1}) \\
&= \obs_t(\by_t \mid \bx_t) \trans_t(\bx_t \mid \bx_{t-1}) p(\bx_{0:t-1} \mid \by_{1:t-1}),
\end{split}
\end{equation*}
where the equalities follow from the hidden Markov model independence assumptions. For clarity, we suppress the static parameter $\btheta$ from the conditioning.

For the target $p(\bx_{0:t} \mid \by_{1:t})$, we introduce an importance sampling distribution ${q(\bx_{0:t} \mid \by_{1:t})}$ and sample $\bx_{0:t}^{(i)}$ from it. The importance weights are (up to normalization) given by
\begin{equation} \label{eq:weight-recursion1}
\begin{split}
w_t^{(i)} & \propto \frac{p(\bx_{0:t}{(i)} \mid \by_{1:t})}{q(\bx_{0:t}^{(i)} \mid \by_{1:t})} \\
& \propto \frac{\obs_t(\by_t \mid \bx_t^{(i)}) \trans_t(\bx_t^{(i)} \mid \bx_{t-1}^{(i)}) p(\bx_{0:t-1}^{(i)} \mid \by_{1:t-1})}{q(\bx_{0:t}^{(i)} \mid \by_{1:t})}.
\end{split}
\end{equation}
By definition of the conditional probability and the hidden Markov model assumptions, we can write the importance sampling distribution as
\begin{equation*}
q(\bx_{0:t} \mid \by_{1:t}) = q(\bx_t \mid \bx_{0:t-1}, \by_{1:t}) q(\bx_{0:t-1} \mid \by_{1:t-1}).
\end{equation*}
By substituting into \eqref{eq:weight-recursion1}, we obtain the following recursion:
\begin{equation} \label{eq:weight-recursion2}
\begin{split}
w_t^{(i)} & \propto \frac{\obs_t(\by_t \mid \bx_t^{(i)}) \trans_t(\bx_t^{(i)} \mid \bx_{t-1}^{(i)})}{q(\bx_t^{(i)} \mid \bx_{0:t-1}^{(i)}, \by_{1:t})} \frac{p(\bx_{0:t-1}^{(i)} \mid \by_{1:t-1})}{q(\bx_{0:t-1}^{(i)} \mid \by_{1:t-1})} \\
& \propto \frac{\obs_t(\by_t \mid \bx_t^{(i)}) \trans_t(\bx_t^{(i)} \mid \bx_{t-1}^{(i)})}{q(\bx_t^{(i)} \mid \bx_{0:t-1}^{(i)}, \by_{1:t})} w_{t-1}^{(i)}.
\end{split}
\end{equation}
Evidently, updating the $i$th weight when transitioning from time $t-1$ to $t$ is a relatively simple task involving only multiplication by the first fraction in \eqref{eq:weight-recursion2}.

The sequential importance sampling algorithm is summarized in \autoref{alg:sis}. This is almost the particle filter; there are still two issues to be addressed, though. First, the problem of weight degeneracy discussed in the next paragraph. Second, the choice of the importance sampling distribution $q(\bm{x})$ addressed later.
\begin{algorithm}[ht]
    \caption{Sequential Importance Sampling}
    \label{alg:sis}
    \begin{algorithmic}[1]
        \Input $\text{Number of particles } N,\ \text{current parameter value } \btheta,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Sample } \bx_0^{(i)} \sim \sprior(\cdot \mid \btheta), \quad i = 1, \ldots, N.$ \Comment{Initialize $N$ particles.}

        \State $w_0^{(i)} \gets \frac{1}{N}, \quad i = 1, \ldots, N.$ \Comment{Initialize uniform weights.}

        \For{$t = 1\ \mathbf{to}\ T$}
            \State $\text{Sample } \bx_t^{(i)} \sim q(\cdot \mid \bx_{0:t-1}^{(i)}, \by_{1:t}, \btheta), \quad i = 1, \ldots, N.$ \Comment{Sample $N$ new particles.}
            \State $\text{Set } w_t^{(i)} \propto \frac{\obs_t(\by_t \mid \bx_t^{(i)}, \btheta) \trans_t(\bx_t^{(i)} \mid \bx_{t-1}^{(i)}, \btheta)}{q(\bx_t^{(i)} \mid \bx_{0:t-1}^{(i)}, \by_{1:t}, \btheta)} w_{t-1}^{(i)}, \quad i = 1, \ldots, N.$ \Comment{Update the weights as per \eqref{eq:weight-recursion2}.}
        \EndFor
    \end{algorithmic}
\end{algorithm}

\paragraph{Resampling}
A serious problem preventing the use of the SIS algorithm is that the weights degenerate over time. At each time step, the variance of the weights reduces \citep{particle-filter}. This means that the (normalized) weights always converge to a situation where a single weight is 1 and the others are 0.

To alleviate this, the following resampling step is introduced.
\begin{algorithm}[ht]
    \caption{Multinomial resampling}
    \label{alg:resampling}
    \begin{algorithmic}[1]
        \Input $\text{Importance weights } w_t^{(1)}, \ldots, w_t^{(N)},\ \text{particles } \bx_t^{(1)}, \ldots, \bx_t^{(N)}.$

        \State $\widetilde{w}_t^{(i)} \gets \frac{w_t^{(i)}}{\sum_{j=1}^N w_t^{(j)}}, \quad i = 1, \ldots, N.$ \Comment{Normalize weights.}

        \State $\text{Sample } a_i \text{ s.t. } \mathbb{P}(a_i = j) = \widetilde{w}_t^{(j)}, \quad i,j = 1, \ldots, N.$ \Comment{Sample indices with replacement.}

        \State $w_t^{(a_i)} \gets \frac{1}{N}, \quad i = 1, \ldots, N.$ \Comment{Reset weights.}

        \Output $\text{Resampled particles } \bx_t^{(a_1)}, \ldots, \bx_t^{(a_N)} \text{ and weights } w_t^{(a_1)}, \ldots, w_t^{(a_N)}.$
    \end{algorithmic}
\end{algorithm}

The normalized importance weights are interpreted as a probability vector of a categorical distribution. The particles are then resampled (sampled with replacement) according to this distribution. This effectively selects a population of ``strong individuals'' for the next time step.

\autoref{alg:resampling} is known as multinomial resampling. There are other, more sophisticated, approaches, such as stratified resampling \citep{resampling}, which come at the cost of increased complexity.

\paragraph{The particle filter}
The remaining step is the choice of the importance sampling distribution $q(\bx_t \mid \bx_{0:t-1}, \by_{1:t}, \btheta)$. Obviously, the more similar this distribution is to the target $p(\bx_{0:t} \mid \by_{1:t}, \btheta)$, the closer approximation we obtain.

The particle filter arises when the transition distribution $\trans_t(\bx_t \mid \bx_{t-1}, \btheta)$ is chosen as the importance distribution, that is, when
\begin{equation*}
q(\bx_t \mid \bx_{0:t-1}, \by_{1:t}, \btheta) = \trans_t(\bx_t \mid \bx_{t-1}, \btheta).
\end{equation*}
The importance weights \eqref{eq:weight-recursion2} then simplify into
\begin{equation} \label{eq:weight-recursion3}
w_t^{(i)} \propto \obs_t(\by_t \mid \bx_t^{(i)}) w_{t-1}^{(i)}.
\end{equation}
The particle filter is summarized in \autoref{alg:particle-filter}. The algorithm is called \emph{bootstrap} particle filter, due to resemblance of the resampling step to the non-parametric bootstrap \citep{bootstrap}. By being defined in terms of importance sampling, the algorithm inherits the appealing asymptotic properties.
\begin{algorithm}[ht]
    \caption{Bootstrap particle filter}
    \label{alg:particle-filter}
    \begin{algorithmic}[1]
        \Input $\text{Number of particles } N,\ \text{current parameter value } \btheta,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Sample } \bx_0^{(i)} \sim \sprior(\cdot \mid \btheta), \quad i = 1, \ldots, N.$ \Comment{Initialize $N$ particles.}

        \State $w_0^{(i)} \gets \frac{1}{N}, \quad i = 1, \ldots, N.$ \Comment{Initialize uniform weights.}

        \For{$t = 1\ \mathbf{to}\ T$}
        \State $\text{Sample } \bx_t^{(i)} \sim \trans_t(\bx_t \mid \bx_{t-1}^{(i)}, \btheta), \quad i = 1, \ldots, N.$ \Comment{Sample $N$ new particles.}

        \State $\text{Set } w_t^{(i)} \propto \obs_t(\by_t \mid \bx_t^{(i)}, \btheta) w_{t-1}^{(i)}, \quad i = 1, \ldots, N.$ \Comment{Update the weights as per \eqref{eq:weight-recursion3}.}

        \State $\text{Resample } \bx_t^{(i)} \text{ and reset } w_t^{(i)} \text{ using \autoref{alg:resampling}}, \quad i = 1, \ldots, N.$
        \EndFor
    \end{algorithmic}
\end{algorithm}

\section{Using the particle filter to estimate the likelihood} \label{sec:particle-filter-estimate}

As mentioned in \autoref{sec:particle-filter}, the particle filter is typically used to approximate the filtering distribution $p(\bx_t \mid \by_{1:t}, \btheta)$. This will be utilized to provide a tractable approximation to the likelihood $p(\by_{1:T} \mid \btheta)$ such that the limiting distribution of the Metropolis-Hastings Markov chain remains unaffected. This section describes how it is done and gives the resulting variant of the sampler

\paragraph{Likelihood estimate in general}
Suppose that we are in possession of an estimator $\widehat{\aux}$ of the likelihood $p(\by_{1:T} \mid \btheta)$. As such, it necessarily depends on $\by_{1:T}$ and $\btheta$. Since we aim to use the particle filter to calculate $\widehat{\aux}$, the estimator also depends on the importance weights calculated using random samples $\bx_t^{(i)}$. This makes the estimator a random variable with some distribution denoted $\psi(\aux \mid \btheta, \by_{1:T})$. It is not necessary to have this distribution available, as it is later shown to cancel out in the Metropolis-Hastings acceptance ratio.

We now return to our model \eqref{eq:posterior} and introduce $\widehat{\aux}$ as an auxiliary variable, along with our variable of interest $\btheta$. This changes the target distribution from $p(\btheta \mid \by_{1:T})$ to
\begin{equation} \label{eq:psi-joint}
\psi(\btheta, \aux \mid \by_{1:T}) = p(\btheta \mid \by_{1:T}) \psi(\aux \mid \btheta, \by_{1:T}) = \frac{p(\by_{1:T} \mid \btheta) \pprior(\btheta)}{p(\by_{1:T})} \psi(\aux \mid \btheta, \by_{1:T}).
\end{equation}
In theory, we could now construct a Metropolis-Hastings algorithm with $\psi(\btheta, \aux \mid \by_{1:T})$ as the target, instead of $p(\btheta \mid \by_{1:T})$ as was the case in \autoref{alg:metropolis-hastings}. However, this would not solve our problem, since calculating the acceptance ratio still requires the calculation of the likelihood $p(\by_{1:T} \mid \btheta)$, as \eqref{eq:psi-joint} makes clear.

Instead, we define a new target distribution over $(\btheta, \widehat{\aux})$ by replacing the likelihood in \eqref{eq:psi-joint} by its estimate $\widehat{\aux}$:
\begin{equation} \label{eq:aux-joint}
\auxjoint(\btheta, \aux \mid \by_{1:T}) \coloneqq \frac{\aux \pprior(\btheta)}{p(\by_{1:T})} \psi(\aux \mid \btheta, \by_{1:T}).
\end{equation}
There are of course some conditions imposed on $\auxjoint(\btheta, \aux \mid \by_{1:T})$ for it to be useful:
\begin{enumerate}
    \item $\auxjoint(\btheta, \aux \mid \by_{1:T})$ must be non-negative for all $(\btheta, \aux)$;
    \item $\auxjoint(\btheta, \aux \mid \by_{1:T})$ must integrate to 1;
    \item the marginal distribution of $\auxjoint(\btheta, \aux \mid \by_{1:T})$ for $\btheta$ must be the original target $p(\btheta \mid \by_{1:T})$.
\end{enumerate}
The first two conditions simply state that $\auxjoint$ is a valid probability distribution. The third condition ensures that by constructing a Metropolis-Hastings algorithm with $\auxjoint$ as the target, the original target distribution is preserved once the auxiliary variables are marginalised out. All three conditions are satisfied if $\widehat{\aux}$ is a non-negative unbiased estimator of the likelihood $p(\by_{1:T} \mid \btheta)$. This is shown as follows.

\begin{enumerate}[align=left]
    \item Non-negativity of $\auxjoint$ follows from the assumed non-negativity of the estimator $\widehat{\aux}$ and validity of the distributions in \eqref{eq:aux-joint}.
    \item[2, 3.] Assume that $\widehat{\aux}$ is an unbiased estimate of $p(\by_{1:T} \mid \btheta)$, i.e., that $\E_{\psi}[\widehat{\aux}] = p(\by_{1:T} \mid \btheta)$. Consider now the marginal of $\auxjoint$ for $\btheta$:
    \begin{equation} \label{eq:marginal}
    \begin{split}
    \int \auxjoint(\btheta, \aux \mid \by_{1:T})\; \dx{\aux} & = \frac{\pprior(\btheta)}{p(\by_{1:T})} \int \aux \psi(\aux \mid \btheta, \by_{1:T}) \; \dx{\aux} \\
    & = \frac{\pprior(\btheta)}{p(\by_{1:T})} \E_{\psi}[\widehat{\aux}] \\
    & = \frac{\pprior(\btheta)}{p(\by_{1:T})} p(\by_{1:T} \mid \btheta) \\
    & = p(\btheta \mid \by_{1:T}),
    \end{split}
    \end{equation}
    the original target distribution. This satisfies condition 3. For condition 2, we simply integrate \eqref{eq:marginal} w.r.t. $\btheta$, which results in unity due to $p(\btheta \mid \by_{1:T})$ being a valid probability distribution.
\end{enumerate}

\paragraph{Acceptance ratio computation}
Given the new target distribution $\auxjoint$, we can now construct a Metropolis-Hastings algorithm on the joint space of $(\btheta, \aux)$.

This means that the proposed samples are now given as $(\btheta^\prime, \aux^\prime) \sim \psi(\cdot, \cdot \mid \by_{1:T})$. In practice, this is done by first sampling $\btheta^\prime \sim q(\cdot \mid \btheta^{(m-1)})$, and then $\widehat{\aux}^\prime \sim \psi(\cdot \mid \btheta^\prime, \by_{1:T})$. The acceptance ratio can now be computed as
\begin{equation*}
\begin{split}
\alpha & = \min \left\{1, \frac{\auxjoint(\btheta^\prime, \aux^\prime \mid \by_{1:T})}{\auxjoint(\btheta^{(m-1)}, \aux^{(m-1)} \mid \by_{1:T})} \frac{\prop(\btheta^{(m-1)} \mid \btheta^\prime) \psi(\aux^{(m-1)} \mid \btheta^{(m-1)}, \by_{1:T})}{\prop(\btheta^\prime \mid \btheta^{(m-1)}) \psi(\aux^\prime \mid \btheta^\prime, \by_{1:T})} \right\} \\
& = \min \left\{1, \frac{\aux^\prime \pprior(\btheta^\prime) \psi(\aux^\prime \mid \btheta^\prime, \by_{1:T})}{\aux^{(m-1)} \pprior(\btheta^{(m-1)}) \psi(\aux^{(m-1)} \mid \btheta^{(m-1)}, \by_{1:T})} \frac{\prop(\btheta^{(m-1)} \mid \btheta^\prime) \psi(\aux^{(m-1)} \mid \btheta^{(m-1)}, \by_{1:T})}{\prop(\btheta^\prime \mid \btheta^{(m-1)}) \psi(\aux^\prime \mid \btheta^\prime, \by_{1:T})} \right\} \\
& = \min \left\{1, \frac{\aux^\prime \pprior(\btheta^\prime)}{\aux^{(m-1)} \pprior(\btheta^{(m-1)})} \frac{q(\btheta^{(m-1)} \mid \btheta^\prime)}{q(\btheta^\prime \mid \btheta^{(m-1)})} \right\}.
\end{split}
\end{equation*}

Since \eqref{eq:marginal} shows that the marginal of $\auxjoint$ for $\btheta$ is the original target $p(\btheta \mid \by_{1:T})$, all we need to do is to discard the sampled $\widehat{\aux}^{(m)}$ and keep only $\btheta^{(m)}$ when running Metropolis-Hastings on the joint space of $(\btheta, \aux)$.

\paragraph{Calculating the estimate using the particle filter}
Finally, we describe how exactly is the particle filter used as an estimator of $p(\by_{1:T} \mid \btheta)$.

First, we decompose the likelihood into a product of simpler distributions, which are then marginalised over the corresponding hidden state:
\begin{equation} \label{eq:likelihood-factorization}
\begin{split}
p(\by_{1:T} \mid \btheta) &= \prod_{t=1}^T p(\by_t \mid \by_{1:t-1}, \btheta) \\
&= \prod_{t=1}^T \int p(\by_t, \bx_t \mid \by_{1:t-1}, \btheta) \; \dx{\bx_t} \\
&= \prod_{t=1}^T \int p(\by_t \mid \bx_t, \btheta) p(\bx_t \mid \by_{1:t-1}, \btheta) \; \dx{\bx_t}.
\end{split}
\end{equation}

Using the particles $\left\{\bx_t^{(i)}\right\}_{i=1}^N$, we plug in the empirical approximation to $p(\bx_t \mid \by_{1:t-1}, \btheta)$, $\widehat{p}(\bx_t \mid \by_{1:t-1}, \btheta) = \frac{1}{N} \sum_{i=1}^N \delta_{\bm{x}_t^{(i)}}(\bx_t)$, into \eqref{eq:likelihood-factorization}, obtaining
\begin{equation*}
\begin{split}
p(\by_{1:T} \mid \btheta) & \approx \prod_{t=1}^T \int p(\by_t \mid \bx_t, \btheta) \left[ \frac{1}{N} \sum_{i=1}^N \delta_{\bm{x}_t^{(i)}}(\bx_t) \right] \; \dx{\bx_t} \\
& = \prod_{t=1}^T \frac{1}{N} \sum_{i=1}^N \int p(\by_t \mid \bx_t, \btheta) \delta_{\bm{x}_t^{(i)}}(\bx_t) \; \dx{\bx_t} \\
& = \prod_{t=1}^T \frac{1}{N} \sum_{i=1}^N p(\by_t \mid \bx_t^{(i)}, \btheta)
\end{split}
\end{equation*}
due to linearity of the integral and properties of the Dirac distribution.

In $p(\by_t \mid \bx_t^{(i)}, \btheta)$, we recognize the particle filter weights $w_t^{(i)}$ defined in \eqref{eq:weight-recursion3}. This allows us to finally define the likelihood estimate as
\begin{equation} \label{eq:likelihood-estimate}
\widehat{\aux} \coloneqq \prod_{t=1}^T \frac{1}{N} \sum_{i=1}^N w_t^{(i)}.
\end{equation}
This estimator is obviously non-negative due to construction of the weights. The proof that it is also unbiased (and therefore also integrates to unity) is more involved and the reader is referred to \cite{del-moral} for the original proof.

Finally, we describe the resulting variant of the Metropolis-Hastings algorithm employing the likelihood estimate \eqref{eq:likelihood-estimate}. This algorithm, called marginal Metropolis-Hastings, was introduced by \cite{andrieu}. Compared to \autoref{alg:metropolis-hastings}, all components of this algorithm can be evaluated. Due to construction of the estimator $\widehat{\aux}$, the marginal of the limiting distribution of \autoref{alg:marginal-metropolis-hastings} is the original target $p(\btheta \mid \by_{1:T})$.

\begin{algorithm}[ht]
    \caption{Marginal Metropolis-Hastings}
    \label{alg:marginal-metropolis-hastings}
    \begin{algorithmic}[1]
        \Input $\text{Number of samples } M,\ \left\{\by_1, \ldots, \by_T\right\}.$

        \State $\text{Initialize } \btheta^{(0)}.$
        \State $\text{Run \autoref{alg:particle-filter} with } \btheta^{(0)} \text{ to obtain the weights } w_{0,t}^{(i)}, \quad t = 1, \ldots, T,\ i = 1, \ldots, N.$
        \State $\text{Calculate } \widehat{\aux}^{(0)} \text{ according to \eqref{eq:likelihood-estimate} using } w_{0,t}^{(i)}.$

        \For{$m = 1\ \mathbf{to}\ M$}
        \State $\text{Sample } \btheta^\prime \sim \prop(\cdot \mid \btheta^{(m-1)}).$
        \State $\text{Run \autoref{alg:particle-filter} with } \btheta^\prime \text{ to obtain the weights } w_{m,t}^{(i)}, \quad t = 1, \ldots, T, \ i = 1, \ldots, N.$
        \State $\text{Calculate } \widehat{\aux}^\prime \text{ according to \eqref{eq:likelihood-estimate} using } w_{m,t}^{(i)}.$
        \State $\text{Calculate the aceptance probability } $ \begin{equation*} \label{eq:acceptance-probability-tractable}
        \alpha = \min \left\{1, \frac{\widehat{\aux}^\prime \pprior(\btheta^\prime)}{\widehat{\aux}^{(m-1)} \pprior(\btheta^{(m-1)})} \frac{\prop(\btheta^{(m-1)} \mid \btheta^\prime)}{\prop(\btheta^\prime \mid \btheta^{(m-1)})} \right\}.
        \end{equation*}
        \State $\text{Sample } u \sim \mathcal{U}(0,1).$
        \If {$u \leq \alpha$}
        \State $\left( \btheta^{(m)}, \widehat{\aux}^{(m)} \right) \gets \left( \btheta^\prime, \widehat{\aux}^\prime \right)$ \Comment{With probability $\alpha$, accept the proposed sample.}
        \Else
        \State $\left( \btheta^{(m)}, \widehat{\aux}^{(m)} \right) \gets \left( \btheta^{(m-1)}, \widehat{\aux}^{(m-1)} \right)$ \Comment{With probability $1 - \alpha$, reject the proposed sample.}
        \EndIf
        \EndFor

        \Output $\left\{ \btheta^{(1)}, \ldots, \btheta^{(M)} \right\}$
    \end{algorithmic}
\end{algorithm}


\section*{Linear programming and optimization - Peter Christensen}

\begin{enumerate}
	\item Define LP probem with example.
	\item Set example to standard form
	\item Convert the LP problem to one including slack variables
	\item Use simple method on example
	\item Define duality and prove Thm 29.10.
\end{enumerate}


\subsection{Wave structure for two-dimensional problems}

\begin{frame}
  \begin{block}{Spectral analysis of $\Jbsf$}
    \begin{itemize}
      % \item acoustic tensor's spectrum $\rightarrow$ 2 positive eigenvalues $\omega_p$ and eigenvectors $\vect{l}^p$
    \item 4 non-zero eigenvalues $c_K$ and left eigenvectors $\Lcb^K$
      \begin{itemize}
      \item[] left and right-going slow waves $\pm c_s$
      \item[] left and right-going fast waves $\pm c_f$
      \end{itemize}
    \item 1 contact wave $c=0$
    \item non-linear waves: $c_K(\tens{\sigma})$ %(expressions depending on $\Cbb^{ep}$)
    \item \textbf{assumption: $c_1 \geq c_f \geq c_2 \geq c_s$ $\Rightarrow$ simple waves}
    \end{itemize}
  \end{block}
\end{frame}

\subsection{Loading paths through simple waves}
\begin{frame}
  \begin{overprint}
    \begin{block}{Method of characteristics \cite{Courant}}
      \begin{equation*}
        \Lcb^K \cdot d\Qcb = 0 \quad \forall \: K
      \end{equation*}
      $ \rightarrow$ set of ODEs through the simple waves
    \end{block}
    \begin{block}{Particular cases $\vect{n}=\vect{e}_i$}
      \begin{flalign*}
        & d\sigma_{11}=\psi_i (\tens{\sigma})\: d\sigma_{12}\\
        & d\sigma_{22}=\phi_i (\tens{\sigma})\: d\sigma_{12}
      \end{flalign*}
    \end{block}
    \onslide<2>
    \begin{block}{Mathematical study}
      \begin{itemize}
      \item Orthogonality of "slow" and "fast" loading paths in stresses space
      \item Singular points
      %\item Complex functions $\rightarrow$ numerical investigations
      \end{itemize}
    \end{block}
  \end{overprint}

  \footnoteCite{Courant}
\end{frame}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../presentation"
%%% End:


\chapter{Units Conversion System}\label{ch:units}

\section{Introduction}

The units conversion system as implemented in \aprepro{} defines
several variables that are abbreviations for unit quantities. For
example, if the output format for the current unit system was inches,
the variable \cmd{foot} would have the value \cmd{12}. Therefore, an
expression such as \cmd{8*foot} would be equal to \cmd{96} which is the
number of inches in 8 feet\footnote{This can also be written as 8\~{}foot since \~{} has been defined to be the same as $*$ (multiplication).}.

Seven consistent units systems have been defined including four metric
based systems: \cmd{si}, \cmd{cgs}, \cmd{cgs-ev}, and \cmd{shock}; and
three english-based systems: \cmd{in-lbf-s}, \cmd{ft-lbf-s}, and
\cmd{ft-lbm-s}. The output units for these unit systems are shown in
Table 8 (metric) and Table 9 (english). A list of the defined units
abbreviations is given in Table 10.

In addition to the definition of the conversion factors, several
string variables are also defined which describe the output format of
the current units system.  For example, the string variable \cmd{dout}
defines the output format for density units. For the \cmd{in-lbf-sec}
units system, \cmd{dout} =
\cmd{\texttt{"}lbf-sec\^{}2/in\^{}4}\texttt{"}
which is the output format for densities in this system. The string
variables can be used to document the \aprepro{} output. The
string variable names are listed in the last column of Table 8 and Table 9.

\begin{longtable}{l|lllll}
\caption{Units Systems and Corresponding Output Format--Metric} \\
\hline
Quantity      &si&  cgs       & cgs-ev     & shock      & string \\
\hline
Length        & metre    & centimetre & centimetre & centimetre & lout \\
Mass          & kilogram & gram       & gram       & gram       & mout \\
Time          & second   & second     & second     & micro-sec  & tout \\
Temperature   & kelvin   & kelvin     & eV         & kelvin     & Tout \\
Velocity      & metre/sec& cm/sec     & cm/sec     & cm/usec    & vout \\
Acceleration  & metre/sec$^2$ & cm/sec$^2$ & cm/sec$^2$ & cm/usec$^2$ & aout \\
Force         & newton   & dyne       & dyne       & g-cm/usec$^2$ & fout \\
Volume        & metre$^3$ &  cm$^3$ & cm$^3$ & cm$^3$ & Vout \\
Density       & kg/m$^3$ &  g/cc & g/cc & g/cc & dout \\
Energy        & joule    & erg & erg & g-cm$^2$/usec$^3$ & eout \\
Power         & watt     & erg/sec & erg/sec & g-cm$^2$/usec$^4$ & Pout \\
Pressure      & pascal   & dyne/cm$^2$ & dyne/cm$^2$ & Mbar & pout \\
\hline
\end{longtable}

\begin{longtable}{l|llll}
\caption{Units Systems and Corresponding Output Format--English} \\
\hline
Quantity & in-lbf-s & ft-lbf-s & ft-lbm-s & string \\
\hline
Length       & inch         & foot & foot & lout \\
Mass         & lbf-sec$^2$/in & slug & pound-mass & mout \\
Time         & second & second & second & tout \\
Temperature  & rankine & rankine & rankine & Tout \\
Velocity     & inch/sec & foot/sec & foot/sec & vout \\
Acceleration & inch/sec$^2$ & foot/sec$^2$ & foot/sec$^2$ & aout \\
Force        & pound-force & pound-force & poundal & fout \\
Volume       & inch$^3$ & foot$^3$ & foot$^3$ & Vout \\
Density      & lbf-sec$^2$/in$^4$ & slug/ft$^3$ & lbm/ft$^3$ & dout \\
Energy       & inch-lbf & foot-lbf & ft-poundal & eout \\
Power        & inch-lbf/sec & foot-lbf/sec & ft-poundal/sec & Pout\\
Pressure     & lbf/in$^2$ & lbf/ft$^2$ & poundal/ft$^2$ & pout \\
\hline
\end{longtable}

The units definitions are accessed through the \afunc{Units} function in \aprepro{}:
\begin{apinp}
\{Units("\var{unit\_system}")\}
\end{apinp}
where \var{unit\_system} is one of the strings listed in the first row
of the previous two tables.

\section{Defined Units Variables}

In the following table, the first column lists the variables
that are defined in the \aprepro{} unit system and the second column
is a short description of the unit. All units variables are defined in
terms of the five SI Base Units metre (length), second (time),
kilogram (mass), temperature (kelvin), and radian (angle)\footnote{The
radian is actually a SI Supplementary Unit since it has not been
decided whether it is a Base Unit or a Derived Unit. There are three
other SI Base Units, the candela, ampere, and mole, but they are not
yet used in the Aprepro units system.}. The bolded rows
delineate the type of unit variable and the base quantities used to
define it where $L$ is length, $T$ is time, $M$ is mass, and $t$ is
temperature. For example density is defined in terms of $M/L^3$ which
is mass/ length$^3$.

\begin{longtable}{l|l}
\caption{Defined Units Variables} \\
\hline\hline
\multicolumn{2}{c}{\bf Length \boldmath{[L]}} \\
\hline
m, meter, metre & \cmd{Metre (base unit)} \\
cm, centimeter, centimetre & {Metre / 100} \\
mm, millimeter, millimetre & {Metre / 1,000} \\
um, micrometer, micrometre & {Metre / 1,000,000} \\
km, kilometer, kilometre & {Metre * 1,000} \\
in, inch & Inch   \\
ft, foot & Foot  \\
yd, yard & Yard   \\
mi, mile & Mile   \\
mil & Mil (inch/1000) \\
\multicolumn{2}{c}{} \\
\hline \multicolumn{2}{c}{\bf Time \boldmath{[T]}} \\
\hline
second, sec & \cmd{Second (base unit)} \\
usec, microsecond & Second / 1,000,000 \\
msec, millisecond & Second / 1,000 \\
minute & Minute \\
hr, hour & Hour   \\
day & Day   \\
yr, year & Year = 365.25 days \\
decade & 10 Years \\
century & 100 Years \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Velocity \boldmath{[L/T]}} \\
\hline
mph & Miles per hour \\
kph & Kilometres per hour \\
mps & Metre per second \\
kps & Kilometre per second \\
fps & Foot per second \\
ips & Inch per second \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Acceleration \boldmath{[$L/T^2$]}} \\
\hline
ga & Gravitational acceleration \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Mass \boldmath{[$M$]}} \\
\hline
kg & \cmd{Kilogram (base unit)} \\
g, gram & Gram \\
lbm & Pound (mass) \\
slug & Slug \\
lbfs2pin & Lbf-sec$^2$/in \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Density \boldmath{[$M/L^3]$}} \\
\hline
gpcc & Gram / cm$^3$ \\
kgpm3 & Kilogram / m$^3$ \\
lbfs2pin4 & Lbf-sec$^2$ / in$^4$ \\
lbmpin3 & Lbm / in \\
lbmpft3 & Lbm / ft$^3$ \\
slugpft3 & Slug / ft$^3$ \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Force \boldmath{[$ML/T^2$]}} \\
\hline
N, newton & Newton = 1 kg-m/sec$^2$ \\
dyne & Dyne = newton/10,000 \\
gf & Gram (force) \\
kgf & Kilogram (force) \\
lbf & Pound (force) \\
kip & Kilopound (force) \\
pdl, poundal & Poundal \\
ounce & Ounce = lbf / 16 \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Energy \cmd{[$ML^2/T^2$]}} \\
\hline
J,   joule & Joule = 1 newton-metre \\
ftlbf & Foot-lbf \\
erg & Erg = 1e-7 joule \\
calorie & International Table Calorie \\
Btu & International Table Btu \\
therm & EEC therm \\
tonTNT & Energy in 1 ton TNT \\
kwh & Kilowatt hour \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Power \boldmath{[$ML^2/T^3$]}} \\
\hline
W, watt & Watt = 1 joule / second \\
Hp & Elec. Horsepower (746 W) \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Temperature \boldmath{[t]}} \\
\hline
degK, kelvin & \cmd{Kelvin (Base Unit)} \\
degC & Degree Celsius \\
degF & Degree Fahrenheit \\
degR, rankine & Degree Rankine \\
eV & Electron Volt \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Pressure \boldmath{[$M/L/T^2$]}} \\
\hline
Pa, pascal & Pascal = 1 newton / metre$^2$ \\
MPa & Megapascal \\
GPa & Gigapascal \\
bar & Bar \\
kbar & Kilobar \\
Mbar & Megabar \\
atm & Standard atmosphere \\
torr & Torr = 1 mmHg \\
mHg & Metre of mercury \\
mmHg & Millimetre of mercury \\
inHg & Inch of mercury \\
inH2O & Inch of water \\
ftH2O & Foot of water \\
psi & Pound per square inch \\
ksi & Kilo-pound per square inch \\
psf & Pound per square foot \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Volume \boldmath{[L$^3$]}} \\
\hline
liter & Metre$^3$ / 1000 \\
gal, gallon & Gallon (U.S.) \\
\multicolumn{2}{c}{} \\
\hline
\multicolumn{2}{c}{\bf Angular }  \\
\hline
rad & \cmd{Radian (base unit)} \\
rev & Full circle = 360 degree \\
deg, degree & Degree \\
arcmin & Arc minute = 1/60 degree \\
arcsec & Arc second = 1/360 degree \\
grade & Grade = 0.9 degree \\
\end{longtable}
The conversion expressions were obtained from
References~\cite{bib:isotopes}, \cite{bib:jaeger}, \cite{bib:lambe}, and~\cite{bib:simpson}.

\section{Usage}

The following example illustrates the basic usage of the \aprepro{} units conversion utility.

\begin{apinp}
\$ Aprepro Units Utility Example
\$ \{ECHO(OFF)\} \textit{\ldots{}Turn off echoing of the conversion factors}
\$ \{Units(``shock'')\} \textit{\ldots{}Select the shock units system}
\$  NOTE:  Dimensions  -  \{lout\},  \{mout\},  \{dout\},  \{pout\}
\textit{\ldots{}This will document what quantities are used in the file after it is run through Aprepro}
\{len1  = 10.0 * inch\}  \textit{\ldots{}Define a length in an english unit (inches)}
$ \{len2  = 12.0\textasciitilde{}inch\} \textit{\ldots{}\textasciitilde{} is a synonym for * (multiplication)}
Material  1,  Elastic Plastic,  \{1890\textasciitilde{}kgpm3\}  \$  \{dout\}
   Youngs Modulus = \{28.3e6\textasciitilde{}psi\} \$ {pout}
   Yield  Stress  = \{30\textasciitilde{}ksi\}
   Initial Veclocity = \{10\textasciitilde{}mph\} \$ {vout}
   \textit{\ldots{}Define the density and material parameters in whatever units they are available}
End
Point 100 \{0.0\}  \{0.0\}
Point 110 \{len1\} \{0.0\}
Point 120 \{len1\} \{len2\}
Point 130 \{0.0\}  \{len1\}
\end{apinp}

The output from this example input file is:

\begin{apout}
\$  Aprepro  Units  Utility  Example
\$  NOTE:  Dimensions  -  cm,  gram,  g/cc,  Mbar  \textit{\ldots{}The documentation of what quantities this file uses}
\$ 25.4
\$ 30.48

Material 1, Elastic Plastic, 1.89 \$ g/cc
   Youngs Modulus = 1.951216314 \$ Mbar
   Yield  Stress  =  0.002068427188  \textit{\ldots{}All material parameters are now in consistent units}
   Initial Velocity = 0.00044704 \$ cm/usec
End

Point  100  0  0
Point  110  25.4 0
Point  120  25.4 30.48
Point  130  0   25.4   \textit{\ldots{}Lengths have all been converted to centimetres}
\end{apout}

The same input file can be used to output in SI units simply by changing Units
command from \var{shock} to \var{si}. The output in SI units is:

\begin{apout}
\$ Aprepro Units Utility Example
\$  NOTE:  Dimensions  -  meter,  kilogram,  kg/m^3,  Pa
\textit{\ldots{}Quantities are now output in standard SI units}
\$ 0.254
\$ 0.3048

Material  1,  Elastic  Plastic,  1890  \$  kg/m^3
   Youngs  Modulus =  1.951216314e+11 \$ Pa
   Yield  Stress   =  206842718.8
   Initial Velocity = 4.4704 \$ meter/sec
End

Point  100 0  0
Point  110 0.254  0
Point  120 0.254  0.3048
Point  130 0   0.254  \textit{\ldots{}Lengths have all been converted to metres}
\end{apout}

\section{Additional Comments}

A few additional comments and warnings on the use of the units system are detailed
below.

Omitting the \{ECHO(OFF)\} line prior to the \{Units(``unit\_system'')\}
function will print out the contents of the units header and conversion files.
Each line in the output will be preceded by the current comment character which
is \$ by default.

The comment character can be changed by invoking \aprepro{} with the
\cmd{-c} option. For example \cmd{aprepro -c\# input\_file output\_file}
will change the comment character at the beginning of the lines to \#.

The temperature conversions are only valid for relative temperatures, for
example, 100\textasciitilde{}degC is equal to 180\textasciitilde{}degF, not 212\textasciitilde{}degF.

Since several variables are defined in the units system, it is
possible to redefine one of the variable names in your input file. If
the \aprepro{} warning messages are turned off, you will not be
notified of the variable redefinition and erroneous results may
occur. Therefore, you should not turn off \aprepro{} warning
messages while using the units system, and you should investigate all
redefined variable messages to ensure that you are getting the results
you expect.


\section{Transcritical flow with a shock over a bump}

This scenario simulates a transcritical flow over a bump with a shock. The topography and the initial conditions are the same as those used in the subcritical flow (See the description given in the report on the subcritical flow test). However, to get a transcritical flow, the boundary conditions are different from those used in the subcritical flow test. Here we refer to the parameters used by Goutal and Maurel~\cite{GM1997}.

Referring to our description for the subcritical flow test, the analytical height or depth $h$ of the transcritical flow at smooth regions is found by solving the Bernoulli equation. The analytical solution for the shock position is found by implementing three equations, namely, (a) the Bernoulli equation at upstream (on the left of the shock), (b) the Bernoulli equation at downstream (on the right of the shock), and (c) the Rankine-Hugoniot relation. The Rankine-Hugoniot relation for the steady flow can be expressed as
\begin{equation}
q^2 \left( \frac{1}{h_1} - \frac{1}{h_2} \right) + \frac{g}{2} \left(h_1^2 - h_2^2\right) = 0\,,
\end{equation}
where $q$ is the discharge or momentum, $h_1$ is the height upstream (on the left of the shock), and $h_2$ is the height downstream (on the right of the shock). When the height $h$ has been found, the velocity is computed as $u=q/h$\,.

\subsection{Results}
For our simulation, we consider Dirichlet boundary conditions
at $x=0^{-}$ given by
\begin{equation}
[w,hu,hv]=[0.41373588752426715,~~~0.18,~~~0]\,,
\end{equation}
and at $25^{+}$ given by
\begin{equation}
[w,hu,hv]=[0.33,~~~0.18,~~~0]\,.
\end{equation}
With these conditions, representatives of the simulation results are shown in the following three figures. They show the stage, $x$-momentum, and $x$-velocity respectively. We should see good 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


Successful attack in digital environment does not guarantee same success in the physical world. In contrast to the digital environment in real world we have to print eyeglass frames using some kind of 2d or 3d printer (in this case 2d) and then conduct an attack on a camera used by FRS. None printer is able to print every possible color. Every camera has a sampling error and is unable to capture colors 1:1. Because of that we have to apply slightly changes to the perturbation finding method.

\subsection{Robust Perturbations}
Because of changing condition such as image background, light conditions, attacker posture or his/her facial expression it's not fairly possible that two attacker's face images will be exactly the same. In initial assumption we assumed that \textit{Considered attacks should also be robust to changes in image conditions - light changes, attacker position changes, standing further/closer to camera shouldn't’t affect effectiveness of an attack}. That's why authors assumed that in order to successfully carry out attacks it is necessary to find perturbation $r$ such that it will be independent of exact image conditions. In other words we want to avoid some kind of \textit{overfitted} perturbation, but instead to find perturbation that will generalize beyond a single face image.

This was achieved by gathering a set of photos of the attacker $X$, taken in different conditions (changing background, lighting, facial expression, distance from the camera, head angle, etc.) and use that set $X$ to find a single perturbation $r$ that optimizes its objective for every input $x \in X$. We can formalize this as as optimization problem for both Impersonation (6) and Dodging (7):

\begin{equation}
\operatorname{argmin}_{r} \sum_{x \in X} \operatorname{softmaxloss}(f(x+r), c_t)
\end{equation}

\begin{equation}
\operatorname{argmin}_{r} \sum_{x \in X} \operatorname{- softmaxloss}(f(x+r), c_x)
\end{equation}

\subsection{Smooth transitions}

Natural images tend to contain smooth and regular patches, which are separated by a few edges. However generated perturbation eyeglass frames generated by minimizing \textit{softmaxloss} contain lot of edges and aren't very smooth. It might be a problem for cameras, because of sampling noise, extreme differences between neighboring pixels are unlikely to be accurately captured by camera. As a result non-smooth eyeglass frames may lead to not physically realizable attacks (which was paper initial assumption, see 6.1).

To achieve smooth perturbation we have to update the optimization function. Except minimizing \textit{softmaxloss} we have to also minimize \textit{total variation} ($TV$). For a perturbation $r$, $TV(r)$  is defined as:

\begin{equation}
T V(r)=\sum_{i, j}\left(\left(r_{i, j}-r_{i+1, j}\right)^{2}+\left(r_{i, j}-r_{i, j+1}\right)^{2}\right)^{\frac{1}{2}}
\end{equation}

Where $r_{i,j}$ is an eyeglass frames pixel value at coordinates $(i,j)$. $TV(r)$ is low when the neighboring pixels have similar values (perturbation patch is smooth and regular) and high otherwise. A result of minimizing $TV(r)$ is smooth perturbation.

\subsection{Printability}

For printing an eyeglass frames authors have used a simple ink-jet printer - (Epson XP-83). It prints 2d eyeglass frames, but as any printer in the world it has some physical limitations. It can reproduce only a subset of the $[0,1]^3$ RGB color space not all possible colors. That's why in order to conduct successful attacks we need to limit perturbation to only colors reproducible by the printer. To do so authors defined \textit{non-printability score} ($NPS$). $NPS$  have high value for pixel of unreproducible color and low  otherwise. Let $P \subset [0,1]^3 $ be the set of printable RGB triplets. We
define the $NPS$ of a pixel $\hat{p}$ as:

\begin{equation}
N P S(\hat{p})=\prod_{p \in P}|\hat{p}-p|
\end{equation}

To measure total $NPS$ for a perturbation $r$ we sum $NPS$ for each pixel $\hat{p}$ from a perturbation $r$. Instead of using all printable RGB triplets authors decided to pick the 30 triplets with a minimal variance in distances from the complete set of printable RGB triplets and use only them in the set $P$ used in definition of $NPS$. Authors claim that this optimization with using 30 \textit{centroids} worked great for them and lead to big improvements in physical realizability of an attack.

\subsection{Putting Everything Together}

Now we can to put everything together and model the new objective that will consist of \textit{misclassification} \textit{smoothness} and \textit{printability}. This new objective can be modeled as:

\begin{equation}
\underset{r}{\operatorname{argmin}}\left(\sum_{x \in X} \operatorname{softmaxloss}\left(f(x+r), c_{t}\right)\right)+\kappa_{1} \cdot \operatorname{TV}(r)+\kappa_{2} \cdot \operatorname{NPS}(r)
\end{equation}

Where $\sum_{x \in X} \operatorname{softmaxloss}\left(x+r, c_{t}\right)$ is a \textit{misclassification} part (it is version for impersonation, for dodging it would be $\operatorname{argmin}_{r}\left(-\operatorname{softmaxloss}\left(f(x+r), c_{x}\right)\right)$), $\kappa_1$ and $\kappa_2$ are hyperparameters to balance \textit{smoothness} ($TV$) and \textit{printability} ($NPS$), $r$ is the perturbation we want to find and $X$ is a set of images of an attacker's face,


\section{Developed Method}
\label{sec:HomographyDevelopedMethod}

Our work aimed to devise a systematic approach to select the ``best'' homography according to the proposed score function. The assumption was that there was no prior knowledge about the quality of individual markers.

Here is the description of the proposed method. Each homography is induced by a single independent marker. The input to our method is multiple sets (\ietext{}, groups) of point correspondences between the warped and the ground-truth (ideal) markers. Therefore, each marker is represented by a unique set of keypoints. The use case of our method is to rank multiple homographies and select the best performing one with respect to the tailor-made score function. Consequently, we require a homography matrix for each marker (a set of point correspondences) on the input. The great advantage comes from the fact that to compute these matrices, any state-of-the-art method can be utilized as a black box. The benefit is that it is capable of ranking the referred homographies without the knowledge of absolute or relative positions of markers in the world (\figtext{}~\ref{fig:GraphicalAbstract}). However, we have to emphasize that we did not propose any method to simultaneously estimate multiple homographies. We only build upon the existing homography matrices.

Due to our assumption of not knowing the arrangement of markers in the scene, there is no way to create one virtual, compound marker that contains all the keypoints. If we could, then we would employ RANSAC~\cite{fischler1981ransac} or any other sophisticated algorithm to select the best subset of keypoints to estimate the homography. In that scenario, our approach would be useless. We only have information about the relative position of the marker’s keypoints at our disposal, not the markers themselves. As a result, the point correspondence is globally indeterminate. We can only establish a local point correspondence between a single marker and its corresponding ground-truth shape. For the best performance, to obtain the isolated homographies, we suggest the user chooses the most robust method available.

The homography estimation between existing point correspondences is a standard problem we heavily rely on. As already highlighted, we did not contribute to this problem in terms of improving the homography estimation itself. We only provided a way to rank the resulting homographies. We developed a way to, under certain circumstances, choose the ``best'' homography from multiple existing ones. Therefore, our method could not even be compared to RANSAC, because we tackle a different problem. There are three following assumptions the proposed method is based upon:
\begin{enumerate}
    \item The markers are geometrically similar, which means that they are allowed to differ only in translation, rotation, and uniform scale in the real world.
    \item The shape of at least one of the used markers is known beforehand.
    \item These markers are positioned on the same planar surface visible in the scene.
\end{enumerate}
One important caveat is that our method handles only transformation from a distorted to the undistorted view of the target plane.

We exploited the properties of homography and similarity transformations and expressed them in a single score function, which stands at the core of our contribution. The score function value is exploited as a proxy for homography ranking according to their reprojection error over the entire image using only markers' keypoints. It is only an estimate. The usual use case would be to select the homography with the lowest score, \ietext{}, the highest-ranked matrix, to perform the image rectification with the expectation of obtaining the most accurate reprojection.

% ------------------------------------------------------------------------------
\begin{figure}[t]
    \centerline{\includegraphics[width=\linewidth]{figures/homography/graphical_abstract.pdf}}
    \caption[Graphical abstract for homography ranking]{The graphical abstract from our paper. The basic idea is that existing approaches may only estimate an isolated homography for each marker and cannot determine which homography achieves the best reprojection over the entire image. Therefore, we proposed a method to rank isolated homographies obtained from multiple distinct markers to select the best homography. This method extends existing approaches, provided that the point correspondences are available and the markers differ only by similarity transformation after rectification.}
    \label{fig:GraphicalAbstract}
\end{figure}
% ------------------------------------------------------------------------------

% ------------------------------------------------------------------------------
\begin{figure}[t]
    \centerline{\includegraphics[width=\linewidth]{figures/homography/system_diagram.pdf}}
    \caption[Homography ranking system diagram]{A system diagram of our method. \imgpartdesc{a} The input consists of a many-to-one point correspondence specified by multiple similar markers together with the information about the ground-truth shape (up to an arbitrary positive scale) of the target marker. \imgpartdesc{b} The assumption is that the isolated homographies related to each marker are ready on the input as well. \imgpartdesc{c} The algorithm processes each marker by applying its corresponding homography matrix to the image to produce a rectified image. Subsequently, it computes optimal similarity matrices using auxiliary markers. These transformations are required for the computation of the score function. The obtained score values then serve for comparison when ranking the homographies. The homography that ends up ranked first is considered (predicted) to the ``best'' candidate for achieving the minimal reprojection error over the whole image.}
    \label{fig:HomographySystemDiagram}
\end{figure}
% ------------------------------------------------------------------------------

Our method utilizes multiple similar markers (\figtext{}~\ref{fig:HomographySystemDiagram}). The input is point correspondences and homographies estimated for each marker. Each marker becomes the reference marker only once during the course of the algorithm. All the remaining markers serve as auxiliary markers. The reference marker's homography is used to perform the perspective transformation to rectify all the visible markers. To rank which reference markers' homography yields the best reprojection, we exploit auxiliary markers. Auxiliary markers are subsequently mapped onto the target marker using similarity transformations (\eqtext{}~\ref{eq:SimilarityMatrices}). The transformed keypoints are converted to homogeneous coordinates and the reprojection error is measured as the mean Euclidean distance between the rectified and the target keypoints~(\eqtext{}~\ref{eq:HomographyScoreFunction}). The objective is to minimize the computed quantity.

Let $r$ be the index of the reference marker. The $3 \times 3$ matrices describing similarity transformations are contained in a set $\mset{S} = \cbrackets{\suprbrackets{\mtx{S}}{i} \ |\ i = 1, \dots, m}$, such that
\begin{equation}
    \label{eq:SimilarityMatrices}
    \suprbrackets{\mtx{S}}{i} =
    \begin{cases}
        \begin{aligned}
             & \begin{bmatrix}
                1 & 0 & 0 \\
                0 & 1 & 0 \\
                0 & 0 & 1
            \end{bmatrix} & \text{if } i = r   \\
             & \begin{bmatrix}
                \subsuprbrackets{\mtx{R}}{2 \times 2}{i} & \subsuprbrackets{\mtx{T}}{2 \times 1}{i} \\
                \mathbf{0}_{1 \times 2}                  & 1
            \end{bmatrix} & \text{if }i \neq r \\
        \end{aligned}
    \end{cases},
\end{equation}
for $i = 1, \dots, m$, where
\begin{equation}
    \subsuprbrackets{\mtx{R}}{2 \times 2}{i} =
    \begin{bmatrix}
        \suprbrackets{s}{i} \cdot \func{\cos}{\suprbrackets{\theta}{i}} & -\suprbrackets{s}{i} \cdot \func{\sin}{\suprbrackets{\theta}{i}} \\
        \suprbrackets{s}{i} \cdot \func{\sin}{\suprbrackets{\theta}{i}} & \suprbrackets{s}{i} \cdot \func{\cos}{\suprbrackets{\theta}{i}}
    \end{bmatrix}, \quad
    \subsuprbrackets{\mtx{T}}{2 \times 1}{i} =
    \begin{bmatrix}
        \subsuprbrackets{t}{x}{i} \\
        \subsuprbrackets{t}{y}{i}
    \end{bmatrix}.
\end{equation}
This transformation (besides the identity) involves $4$ \gls{dof}: a single rotation angle $\suprbrackets{\theta}{i}$, two $x$ and $y$ translation coefficients $\subsuprbrackets{t}{x}{i}$, $\subsuprbrackets{t}{y}{i}$, and a scale coefficient $\suprbrackets{s}{i}$. A full affine transformation ($6$ \gls{dof}) would incorporate horizontal and vertical
scales, shear and rotation, and $x$, $y$ offsets~\cite{barath2016novel}. The application of homography that rectifies an image generates a frontal plane that is related to the ground-truth plane by a similarity transformation~\cite{hartley2003multiple, beck2016planar}. Thus, we do not include the shear and we only support uniform scaling. The mathematical justification can be found in the appendix section of our paper~\cite{ondrasovic2021homography}.

As all the markers share the same planar surface, a valid homography corresponding to any of them by definition to provide a valid perspective projection. However, all perspective projections are subjected to different noise. The endeavor then is to quantify which homography estimation could provide the best perspective projection for the whole plane in the image. To do so, we propose a score function based on the aforementioned constraints. The score function computes a score for individual homographies in along with the estimated similarity matrices corresponding to auxiliary markers as
\begin{equation}
    \label{eq:HomographyScoreFunction}
    \func{\scoref}{\H, \mset{S}} =
    \frac{1}{m}
    \sum_{i = 1}^{m}
    \frobnorm{
        \func{h}{
            \suprbrackets{\mtx{S}}{i}
            \H
            \suprbrackets{\mtx{W}}{i}
        }
        -
        \mtx{T}
    },
\end{equation}
where $\frobnorm{\cdot}$ denotes the Frobenius norm. The function $\func{h}{\cdot}$ converts points to homogeneous coordinates as
\begin{equation}
    \label{eq:HomoCoordsConversion}
    \func{h}{
        \begin{bmatrix}
            x_1 & x_2 & \dots & x_k \\
            y_1 & y_2 & \dots & y_k \\
            z_1 & z_2 & \dots & z_k
        \end{bmatrix}
    } =
    \begin{bmatrix}
        \nicefrac{x_1}{z_1} & \nicefrac{x_2}{z_2} & \dots & \nicefrac{x_k}{z_k} \\
        \nicefrac{y_1}{z_1} & \nicefrac{y_2}{z_2} & \dots & \nicefrac{y_k}{z_k} \\
        1                   & 1                   & \dots & 1
    \end{bmatrix}.
\end{equation}

In what follows, we describe the proposed Algorithm~\ref{alg:HomographyRanking} for homography ranking. Assume a set of warped markers described by the warped keypoints and a single target marker represented by the target keypoints. There is a many-to-one point correspondence linking these objects. Besides, assume that homographies have been estimated for each marker in isolation. Our algorithm ranks the input set of all pairs $\rbrackets{\suprbrackets{\mtx{W}}{i}, \mtx{T}}$, $i = 1, \dots, m$ in ascending order by how well each $i$-th marker preserves the target shape of all the markers in the image after removing the perspective distortion. The score function defined in \eqtext{}~\ref{eq:HomographyScoreFunction} is used to measure this objective. The algorithm evaluates all markers as candidates for the reference marker. In each iteration, it computes optimal similarity matrices belonging to the auxiliary markers in the rectified plane, \ietext{}, after applying the perspective projection induced by the current homography. The aim is to find a homography with a minimal score. The algorithmic complexity is quadratic in the number of markers, thus $\func{\Theta}{m \rbrackets{m - 1} + m \func{\text{log}_2}{m}} \simeq \func{\Theta}{m^2}$. It is important to remark that the two functions used to compute the homography and similarity matrices in the pseudocode may stand for arbitrary methods that produce the required transformations.

\def\hmatrices{\boldsymbol{\bar{H}}}
\def\scoref{\mathcal{F}}

\begin{algorithm}[t]
    \caption[Homography ranking algorithm]{Homography ranking algorithm.}
    \label{alg:HomographyRanking}
    \begin{algorithmic}[1]
        \State $\hmatrices \gets \arraydef \left[ m \right]$
        \Comment{empty array for the homography matrices}

        \State $\scores \gets \arraydef \left[ m \right]$
        \Comment{array of scores computed before the ranking (sorting)}

        \For{$i \gets 1, \dots , m$}
        \Comment{for each reference marker}

        \State $\hmatrices \left[ i \right] \gets$
        \Call{homography}{$\suprbrackets{\mtx{W}}{i}$, $\mtx{T}$}
        \Comment{retrieve or estimate perspective transform.}

        \State $\suprbrackets{\mtx{\bar{S}}}{i} \gets \mtx{I}_{3 \times 3}$
        \Comment{identity matrix to stand for a similarity transformation}

        \State $\mset{\bar{S}} \gets \cbrackets{\suprbrackets{\mtx{\bar{S}}}{i}}$
        \Comment{set of similarity matrices}

        \ForAll{$j$ : $\cbrackets{1, \dots, m} - \cbrackets{i}$}
        \Comment{for each auxiliary marker}

        \State $\suprbrackets{\mtx{\bar{S}}}{j} \gets$ \Call{similarity}{$\hmatrices \left[ i \right] \cdot \suprbrackets{\mtx{W}}{j}$, $\mtx{T }$}
        \Comment{estimate similarity transformation}

        \State$\mset{\bar{S}} \gets \mset{\bar{S}} \cup \suprbrackets{\mtx{\bar{S}}}{j}$
        \Comment{store the similarity matrix}

        \EndFor

        \State $\scores \left[ i \right] \gets \func{\scoref}{\hmatrices \left[ i \right], \mset{\bar{S}}}$
        \Comment{evaluate score function (\eqtext{}~\ref{eq:HomographyScoreFunction})}
        \EndFor
        \State $\sortres \gets \Call{argsort}{\scores}$
        \Comment{indirect sort, only obtain indices of ``would-be'' sorted elements}

        \State \Return $\hmatrices, \sortres$
        \Comment{return homographies and their respective ranking positions}
    \end{algorithmic}
\end{algorithm}


\section{Motivating Example}

We take as a motivating example, the gearing controller component for an autonomous vehicle system.
In a typical scenario, we might build a neural network to control the output of the gear box at every time step based on all relevant sensor data.
We then have a neural network, $\fullNN$, processing inputs (acceleration, braking, speed, rpm, gear) to produce a new (gear) signal.

We can imagine this as a mealy machine with a single state and a single transition, which is always active.

\begin{figure}[h!]
\centering
\begin{tikzpicture}[shorten >=1pt,node distance=2.8cm,on grid]
  \node[state]   (q_0)                {$q_0$};
  \path[->] (q_0)
		  edge [loop above]   node  [above,align=center]         {$\top,$\\$ \fullNN$} ();
\end{tikzpicture}
\caption{A mealy machine of a single monolithic neural network}
\label{fig:full}
\end{figure}

Now assume we have some adversarial actor that takes control of the speed sensor.
Since this network is processing the speed signal at every time step, we cannot have any guarantee of the effect a faulty speed sensor will have on our overall output.

In contrast, imagine we build an automata of neural networks as below.
Here, we have composed three networks; $\isAccel$ for a binary classifier that indicates whether the car is accelerating and the two multiclass classifiers, $\rpmGear$ and $\speedGear$, mapping their inputs to the target gear into which the car should shift (for example gears 1-5).
The mealy machine says that when the car is not accelerating, we should not shift the gear.
It also specifies that when we start to accelerate after a period of slowing down, we should set the gear based on the current speed.
As the car continues to accelerate, the gear should be set based on the rpm of the engine.

\begin{figure}[h!]
\centering
\begin{tikzpicture}[shorten >=1pt,node distance=2.8cm,on grid]
  \node[state]   (q_0)                {$q_0$};
  \node[state] (q_1) [right=of q_0] {$q_1$};
  \path[->] (q_0)
		  edge [loop left]   node  [above,align=center]         {$\neg \isAccel,$\\$ \varnothing$} ()
		  edge [bend left=45] node [above,align=center] {$\isAccel,$\\$ \rpmGear$} (q_1)
            (q_1)
		  edge [loop right]   node [above right,xshift=-0.5cm,align=center]        {$\isAccel,$\\$ \speedGear$} ()
		  edge [bend left=45] node [below,align=center] {$\neg \isAccel,$\\$ \varnothing$} (q_0);
\end{tikzpicture}
\caption{A mealy machine of multiple composed neural networks}
\label{fig:components}
\end{figure}

In Fig.~\ref{fig:components}, we clearly see that an attack on the speed sensor is guaranteed to only have an effect on the transition from state $q_0$ to state $q_1$.
Because of this compartmentalization of vulnerability, safety tactics such as trying to detect anomalous values may be employed to greater effect.
In the sequel, we will present a quantitative formalization of the vulnerability of the systems in both Fig.~\ref{fig:full} and Fig.~\ref{fig:components}.


\documentclass[a4paper,12pt]{article}
%\documentclass[a4paper,12pt]{scrartcl}

\usepackage{xltxtra}

\input{../preamble.tex}

% \usepackage[spanish]{babel}

% \setromanfont[Mapping=tex-text]{Linux Libertine O}
% \setsansfont[Mapping=tex-text]{DejaVu Sans}
% \setmonofont[Mapping=tex-text]{DejaVu Sans Mono}

\title{Homework \#12}
\author{Isaac Ayala Lozano}
\date{\today}

\begin{document}
\maketitle

\begin{enumerate}
 \item Propose a random process (Figure \ref{fig: samples}) and prove that it is ergodic.

 Let $y(\zeta, t) = \sum_{n=1}^N a_n \cos (\omega_n t + \Psi(\zeta))$ be the proposed random process, where

 \begin{itemize}
  \item $a_n$ is the amplitude of each cosine function.
  \item $\omega_n$ is the frequency corresponding to each $n$.
  \item $\Psi(\zeta) \in R $ is the random noise added to the process.
 \end{itemize}

 \begin{figure}[htb!]
\centering
\import{./img/}{hw12_samples.tex}
\caption{Random functions from the ensemble.}
\label{fig: samples}
\end{figure}

 We prove that the process is ergodic by verifying that for the ensemble of sample functions $y_k$, the mean value $\mu_y (k)$ and the autocorrelation function $R_{yy}(\tau, k)$ do not differ over different sample functions \cite{bendat2011random}.

 The mean value of the sample function is obtained as follows

 \begin{align*}
  \mu_y(k) &= \lim _{T \rightarrow \infty} \frac{1}{T} \int_0^T y_k (t) dt \\
  &= \lim _{T \rightarrow \infty} \frac{1}{T} \int_0^T \sum_{n=1}^N a_n \cos(\omega_n t + \Psi (\zeta)) dt\\
  &= \lim _{T \rightarrow \infty} \frac{1}{T}  \sum_{n=1}^N \int_0^T a_n \cos(\omega_n t + \Psi (\zeta)) dt\\
  &= \lim _{T \rightarrow \infty} \frac{1}{T}  \sum_{n=1}^N \left. \frac{a_n}{\omega_n} \sin(\omega_n t + \Psi (\zeta))  \right\rvert_{0}^{T} \\
  &= 0
 \end{align*}

 Given that $N < \infty$, the sum of all terms will also be number less than infinity.
 Thus, as $T$ tends towards infinity the mean value of the random process approaches zero.
 This holds true for all values of $\Psi(\zeta)$.

\begin{figure}[htb!]
\centering
\import{./img/}{hw12_ergodic.tex}
\caption{Mean vlaue for different amount of samples.}
\label{fig: ergodic}
\end{figure}

 For the autocorrelation function, a similar process is followed.

 \begin{align*}
  R_{yy} (\tau, k) & = \lim _{T \rightarrow \infty} \frac{1}{T} \int_0^T y_k (t) y_k (t + \tau) dt\\
  &= \lim _{T \rightarrow \infty} \frac{1}{T} \int_0^T \sum_{n=1}^N a_n \cos(\omega_n t + \Psi (\zeta))
  \sum_{n=1}^N a_n \cos(\omega_n (t+ \tau) + \Psi (\zeta))
  dt
 \end{align*}

We present a simplified version of the proof when $N$ is equal to one, though the proof holds for all values of $N$.
We begin by applying the trigonometric identity of $\cos(u\pm v) = \cos(u)\cos(v) \mp \sin(u) \sin(v)$ , such that $u = \omega_n t + \Psi(\zeta)$ and $v = \omega_n \tau$.

 \begin{align*}
  R_{yy} (\tau, k) &= \lim _{T \rightarrow \infty} \frac{1}{T} \int_0^T  (a \cos(u))(a\cos(u)\cos(v) - a\sin(u)\sin(v)) dt\\
  &= \lim _{T \rightarrow \infty} \frac{1}{T} \int_0^T  (a^2 (\cos (u))^2 \cos(v) - a^2 \cos(u)\sin(u)\sin(v)) dt\\
  &= \lim _{T \rightarrow \infty} \frac{a^2}{T} ( \cos(v)\int_0^T (\cos(u))^2 dt -  \sin(v) \int_0^T \cos(u) \sin(u) dt )
 \end{align*}

 Evaluating each integral yields the following results.

 \begin{align*}
\int_0^T (\cos(u))^2 dt &= \int_0^T (\cos(\omega t + \Psi(\zeta)))^2 dt \\
&= \left. \frac{2 (\omega t + \Psi(\zeta)) + \sin (2(\omega t + \Psi(\zeta)))}{4 \omega}  \right\rvert_{0}^{T}\\
&= \frac{T}{2} + \frac{\sin(2(\omega T + \Psi(\zeta)))}{4\omega} - \frac{\sin(2\Psi(\zeta))}{4\omega} \\
%
\int_0^T \cos(u) \sin(u) dt &= \int_0^T \cos(\omega t + \Psi(\zeta)) \sin(\omega t + \Psi(\zeta)) dt\\
&= \left .  - \frac{\cos (2 (\omega t + \Psi(\zeta)))}{4 \omega} \right\rvert_{0}^{T}\\
&= \frac{\cos(2\Psi (\zeta))}{4\omega} - \frac{\cos (2 (\omega T + \Psi(\zeta)))}{4 \omega}
 \end{align*}

Substituting these results into the original equation and evaluating the limit yields

\begin{equation*}
 R_{yy} (\tau, k) =  \frac{a^2}{2} \cos(\omega \tau)
\end{equation*}

This is due to the fact that every other term for the integrals lacks a $T$ in the numerator of their fractions.
Given the absence of it, as $T$ approaches infinity the value of all the other terms will approach zero.

This result can be generalized for any value of N.
From the original equation we notice that the equation was a sum of integrals, hence for values of different from one it is only necessary to add the results of those other integrals.

\begin{equation*}
 R_{yy} (\tau, k) =  \sum_{n=1}^N \frac{a_n^2}{2} \cos(\omega_n \tau)
\end{equation*}

Observe that the autocorrelation function is not dependent on time, but on the lag between measurements.
It does not vary accross sample functions because they all present the same behaviour described in the random process' equation.

Given that both $\mu_y (k)$ and $R_{yy}(k)$ do not vary between sample functions, as has been proven, the proposed random process is indeed ergodic.

\item Present plots of the probability density functions for a sine wave, a sine wave plus random noise, and a sample of white\footnote{Also called Gaussian} noise.

\item Present the autocorrelation plots for the three previous functions.

\begin{figure}[htb!]
\centering
\import{./img/}{hw12_sin.tex}
\caption{Sine function.}
\label{fig: sin}
\end{figure}

\newpage
\pagebreak

\begin{figure}[htb!]
\centering
\import{./img/}{hw12_noise.tex}
\caption{Sine function with noise.}
\label{fig: noise}
\end{figure}

\newpage
\pagebreak

\begin{figure}[htb!]
\centering
\import{./img/}{hw12_white.tex}
\caption{White noise.}
\label{fig: white}
\end{figure}

\end{enumerate}

\printbibliography

\newpage
\pagebreak
\appendix
\section{Octave Code}
\lstinputlisting[language=Matlab]{hw08_plots.m}

% https://ocw.mit.edu/courses/mechanical-engineering/2-22-design-principles-for-ocean-vehicles-13-42-spring-2005/readings/r6_spectrarandom.pdf
\end{document}


\documentclass[]{BasiliskReportMemo}
\usepackage{AVS}

\newcommand{\submiterInstitute}{Autonomous Vehicle Simulation (AVS) Laboratory,\\ University of Colorado}

\newcommand{\ModuleName}{test\textunderscore sunlineUKF}
\newcommand{\subject}{Sunline UKF Module and Test}
\newcommand{\status}{Initial document}
\newcommand{\preparer}{T. Teil}
\newcommand{\summary}{This module implements and tests a Unscented Kalman Filter in order to estimate the sunline direction.}

\begin{document}

\makeCover

%
%	enter the revision documentation here
%	to add more lines, copy the table entry and the \hline, and paste after the current entry.
%
\pagestyle{empty}
{\renewcommand{\arraystretch}{2}
\noindent
\begin{longtable}{|p{0.5in}|p{4.5in}|p{1.14in}|}
\hline
{\bfseries Rev}: & {\bfseries Change Description} & {\bfseries By} \\
\hline
Draft & Initial Revision & T. Teil \\
\hline

\end{longtable}
}

\newpage
\setcounter{page}{1}
\pagestyle{fancy}

\tableofcontents
~\\ \hrule ~\\

%\begin{figure}[htb]
%	\centerline{
%	\includegraphics[]{Figures/Fig1}
%	}
%	\caption{Sample Figure Inclusion.}
%	\label{fig:Fig1}
%\end{figure}

\section{Introduction}
The Unscented Kalman filter (UKF) in the AVS Basilisk simulation is a sequential
filter implemented to give the best estimate of the desired states.
In this method we estimate the sun heading as well as it's rate of change along the observable axes.
The UKF reads in the message written by the coarse sun sensor, and writes a message
containing the sun estimate.

This document summarizes the content of the module, how to use it, and the test that
was implemented for it. More information on the filter derivation can be found in Reference [\citenum{Teil:2018fe}], and more information on the square root unscented filter can be found in Reference [\citenum{Wan2001}] (attached alongside this document).

\subsection{Dynamics}

The states that are estimated in this filter are the sunline vector, and it's rate of change $\bm X^* = \begin{bmatrix} \bm d &  \dot{\bm d}\end{bmatrix}^T$. The star superscript represents that
this is the reference state.

The dynamics are given in equation \ref{eq:dyn}. Given the nature of the filter, there is an unobservable state component: the rotation about the $\bm d$ axis. In order to remedy this, we project the states along this axis and subtract them, in order to measure only observable state components.

\begin{equation}\label{eq:dyn}
\bm F(\bm X) = \begin{bmatrix} \bm F_1(\bm d) \\  \bm F_2(\dot{\bm d})\end{bmatrix} =  \begin{bmatrix} \dot{\bm d} - \left( (\bm d \cdot \dot{\bm d} )\frac{\bm d}{||\bm d||^2} \right) \\ - \frac{1}{\Delta t}\left( (\bm d \cdot \dot{\bm d}) \frac{\bm d}{||\bm d||^2} \right)\end{bmatrix}
\end{equation}

The measurement model is given in equation \ref{eq:meas}, and the $H$ matrix defined as $H = \left[\frac{\partial \bm G (\bm X, t_i)}{\partial \bm X}\right]^{*}$ is given in equation $\ref{eq:Hmat}$.

In this filter, the only measurements used are from the coarse sun sensor. For the $i^\mathrm{th}$ sensor, the measurement is simply given by the dot product of the sunline heading and the normal to the sensor. This yields easy partial derivatives for the H matrix, which is a matrix formed of the rows of transposed normal vectors (only for those which received a measurement). Hence the $H$ matrix has a changing size depending on the amount of measurements.

\begin{equation}\label{eq:meas}
\bm G_i(\bm X) = \bm n_i \cdot \bm d
\end{equation}

\begin{equation}\label{eq:Hmat}
\bm H(\bm X) = \begin{bmatrix} \bm n_1^T \\ \vdots \\ \bm n_i^T \end{bmatrix}
\end{equation}

\section{Filter Set-up, initialization, and I/O}

\subsection{User initialization}

In order for the filter to run, the user must set a few parameters:

\begin{itemize}
\item The unscented filter has 3 parameters that need to be set, and are best as: \\
      \texttt{ filterObject.alpha = 0.02} \\
      \texttt{ filterObject.beta = 2.0} \\
      \texttt{ filterObject.kappa = 0.0}
\item The angle threshold under which the coarse sun sensors do not read the measurement: \\
\texttt{FilterContainer.sensorUseThresh = 0.}
\item The process noise matrix: \\
   \texttt{qNoiseIn = numpy.identity(5)} \\
   \texttt{ qNoiseIn[0:3, 0:3] = qNoiseIn[0:3, 0:3]*0.01*0.01} \\
   \texttt{ qNoiseIn[3:6, 3:6] = qNoiseIn[3:6, 3:6]*0.001*0.001} \\
    \texttt{filterObject.qNoise = qNoiseIn.reshape(25).tolist()}
\item The measurement noise value, for instance: \\
 \texttt{FilterContainer.qObsVal = 0.001}
\item The initial covariance: \\
 \texttt{Filter.covar =} \\
  \texttt{ [0.4, 0.0, 0.0, 0.0, 0.0, 0.0, \\
                          0.0, 0.4, 0.0, 0.0, 0.0, 0.0, \\
                          0.0, 0.0, 0.4, 0.0, 0.0, 0.0, \\
                          0.0, 0.0, 0.0, 0.04, 0.0, 0.0, \\
                          0.0, 0.0, 0.0, 0.0, 0.04, 0.0, \\
                          0.0, 0.0, 0.0, 0.0, 0.0, 0.04]}
\item The initial state :\\
 \texttt{Filter.state =[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]}
\end{itemize}
The messages must also be set as such:

\begin{itemize}
\item    \texttt{ filterObject.navStateOutMsgName = "sunline$\_$state$\_$estimate"}
\item    \texttt{ filterObject.filtDataOutMsgName = "sunline$\_$filter$\_$data"}
\item   \texttt{ filterObject.cssDataInMsgName = "css$\_$sensors$\_$data"}
\item   \texttt{ filterObject.cssConfInMsgName = "css$\_$config$\_$data"}
\end{itemize}

\subsection{Inputs and Outputs}

The UKF reads in the measurements from the coarse sun sensors. These are under the form of a list of cosine values. Knowing the normals to each of the sensors, we can therefore use them to estimate sun heading.

\section{Test Design}
The unit test for the sunlineUKF module is located in:\\

\noindent
{\tt fswAlgorithms/attDetermination/sunlineUKF/$\_$UnitTest/test$\_$SunlineUKF.py} \\

As well as another python file containing plotting functions:

\noindent
{\tt fswAlgorithms/attDetermination/sunlineUKF/$\_$UnitTest/SunlineUKF$\_$test$\_$utilities.py} \\

The test is split up into 3 subtests. The first test creaks up all of the individual filter methods and tests them individually. These notably go over the square-root unscented filter specific functions. The second test verifies that in the case where the state is zeroed out from the start of the simulation, it remains at zero. The third test verifies the behavior of the time update with a measurement modification in the middle of the run.

\subsection{Individual tests}

In each of these individual tests, random inputs are fed to the methods and their values are computed in parallel in python. These two values are then compared to assure that the correct computations are taking place.
\begin{itemize}
\item \underline{QR Decomposition}: This tests the QR decomposition function which returns just the R matrix. Tolerance to absolute error $\epsilon = 10^{-15}$.

\textcolor{ForestGreen}{Passed}
\item \underline{LU Decomposition}: This tests the LU Decomposition accuracy. Tolerance to absolute error $\epsilon = 10^{-14}$.

\textcolor{ForestGreen}{Passed}
\item \underline{LU backsolve}: This tests the LU Back-Solve accuracy. Tolerance to absolute error $\epsilon = 10^{-14}$.

\textcolor{ForestGreen}{Passed}
\item \underline{LU matrix inverse}: This tests the LU Matrix Inverse accuracy. Tolerance to absolute error $\epsilon = 10^{-14}$.

\textcolor{ForestGreen}{Passed}
\item \underline{Cholesky decomposition}: This tests the Cholesky Matrix Decomposition accuracy. Tolerance to absolute error $\epsilon = 10^{-14}$.

\textcolor{ForestGreen}{Passed}
\item \underline{L matrix inverse}: This tests the L Matrix Inverse accuracy. Tolerance to absolute error $\epsilon = 10^{-14}$.

\textcolor{ForestGreen}{Passed}

\item \underline{U matrix inverse}: This tests the U Matrix Inverse accuracy. Tolerance to absolute error $\epsilon = 10^{-12}$.

\textcolor{ForestGreen}{Passed}
\end{itemize}

\subsection{Static Propagation}

\input{AutoTeX/StatesPlotprop.tex}

This test also takes no measurements in, and propagates with the expectation of no change. It then tests that the states and covariance are as expected throughout the time of simulation. Plotted results are seen in Figure \ref{fig:StatesPlotprop}. We indeed see that the state and covariance that evolve nominally and without bias .

Tolerance to absolute error: $\epsilon = 10^{-10}$

\subsection{Full Filter test}

This test the filter working from start to finish. No measurements are taken in for the first 20 time steps. Then a heading is given through the CSS message. Halfway through the simulation, measurements stop, and 20 time steps later a different heading is read. The filter must be robust and detect this change. This test is parametrized for different test lengths, different initial conditions, different measured headings, and with or without measurement noise. All these are successful.

\vspace{0.2cm}
Tolerance to absolute error without measurement noise: $\epsilon = 10^{-10}$

\textcolor{ForestGreen}{Passed}

Plotted results are seen in Figures \ref{fig:StatesPlotupdate}, and \ref{fig:PostFitupdate}. Figure \ref{fig:StatesPlotupdate} shows the state error and covariance over the run. We see the covariance initially grow, then come down quickly as measurements are used. It grows once again as the measurements stop before bringing the state error back to zero with a change in sun heading.

Figure \ref{fig:PostFitupdate} shows the post fit residuals for the filter, with no measurement noise. We see that the observations are read in well an that the residuals are brought back down to zero.

\input{AutoTeX/StatesPlotupdate.tex}
\input{AutoTeX/PostFitupdate.tex}

\bibliographystyle{AAS_publication}   % Number the references.
\bibliography{references}   % Use references.bib to resolve the labels.

\end{document}


%==============================================================================
\chapter{Correlator of Field Strength}
\label{chap:corr_field}
%==============================================================================

Having revealed the discrepancy in the kinematic description of Hawking
radiation, namely as either a pure or a thermal and mixed state, the author
will show the difference in a physically relevant form. Recall that the
particle-number expectations of the pure and mixed states are the same,
i.e.\ the diagonal elements of the density operators in the particle-number
basis are the same. Hence one seeks operators revealing the off-diagonal
elements of the density operator.

%------------------------------------------------------------------------------
\section{Correlator and Fluctuation of Fourier Modes}
\label{sec:corr_Fourier}
%------------------------------------------------------------------------------

A natural candidate to reveal the off-diagonal elements of the density
operators is the correlator. Bearing in mind the correlator of generalised
Gaussian wave functions (see \cref{eq:multi-har-cor}), the correlator of the
wave functional \cref{eq:squeezed-wave-functional} can be read off as
\begin{align}
\abr{\rfun{g^\dagger}{p_1}\rfun{g}{p_2}}_{\chi_b} &=
\frac{1}{2}\rbr{\abr{\rfun{g_\Re}{p_1}\rfun{g_\Re}{p_2}}_{\chi_b}+\abr{
\rfun{g_\Im}{p_1}\rfun{g_\Im}{p_2}}_{\chi_b}}
= \frac{1}{2}\frac{\tanh\frac{\pp p_1}{2\lambda}}{p_1} \rfun{\delta}{p_1 -
p_2} \nonumber \\
&\propto \frac{1}{8T_\text{HD}}\frac{\rfun{\tanh}{q/4}}{q/4} =
\frac{1}{8T_\text{HD}}\rbr{1 + \rfun{\Omicron}{q}}
\label{eq:correlator-pure}
\end{align}
by substituting
\begin{equation}
\rfun{g}{p} = 2^{-1/2} \rbr{\rfun{g_\Re}{p} + \ii \rfun{g_\Im}{p}},
\end{equation}
where $2^{-1/2}\rfun{g_\Re}{p}$ and $2^{-1/2}\rfun{g_\Im}{p}$ are the
real and imaginary part of $\rfun{g}{p}$, respectively. In the last line of
\cref{eq:correlator-pure}, the delta function is ignored, and the dimensionless
parameter
\begin{equation}
	q \coloneqq p_1/T_\text{HD} \equiv 2\pp p_1/\lambda
\end{equation}
has been used. By the same argument, one also solves the correlator
\begin{equation}
\abr{\rfun{g^\dagger}{p_1}\rfun{g}{p_2}}_\text{vac} =
\frac{1}{2\vbr{p_1}}\rfun{\delta}{p_1-p_2} \propto
\frac{1}{2 T_\text{HD}} q^{-1}
\label{eq:correlator-vacuum}
\end{equation}
for the vacuum wave functional $\cfun{\exp}{-\int_{-\infty}^{+\infty}\dd p \,
\vbr{p}\vbr{\rfun{g}{p}}^2}$.
%\begin{equation}
%\label{eq:vacuum-wave-functional-g}
%\end{equation}
For the thermal state, on the other hand, the correlator follows from
\cref{eq:correlator-multiple-thermal}, so that
\begin{align}
\abr{\rfun{g^\dagger}{p_1}\rfun{g}{p_2}}_\text{th} &=
\frac{\coth\frac{\pp p_1}{\lambda}}{2 p_1} \rfun{\delta}{p_1-p_2}
\nonumber \\
&\propto \frac{1}{4T_\text{HD}} \frac{\rfun{\coth}{q/2}}{q/2}
= \frac{1}{4T_\text{HD}}\rbr{\rbr{\frac{q}{2}}^{-2} + \frac{1}{3} +
\rfun{\Omicron}{q}}.
\label{eq:correlator-thermal}
\end{align}

One sees immediately that
\cref{eq:correlator-pure,eq:correlator-vacuum,eq:correlator-thermal} vanish
identically for $p_1 \neq p_2$, which is due to the absence of interaction in
the scalar field. The remaining diagonal terms have the meaning of
\emph{fluctuation} in field strength, because $\abr{g} \equiv 0$, so that
\begin{equation}
	\abr{\rbr{\Delta g}^2} = \abr{g^2} - \abr{g}^2 \equiv \abr{g^2}.
\end{equation}
Ignoring the $\rfun{\delta}{0}$ divergence, the fluctuations are plotted in
\cref{fig:fluc-Fourier}.

\begin{figure}
\begin{center}
\input{./graphics/graph_fluc_fourier}
\end{center}
\caption[Fluctuations of the Fourier modes]{Fluctuations of the Fourier modes
of the field, plotted in log--log scales. The critical energy scale is the
Hawking temperature, below which the various fluctuations depart. The
discrepancy between the pure and thermal descriptions is most significant for
low-energy modes, while for high energies the fluctuations of them and
the vacuum state are practically the same. Moreover, compared with vacuum, the
low-energy fluctuation of thermal case is enhanced, so it diverges faster than
that of vacuum; for the pure description, however, the fluctuation is
suppressed, such that it converges to a constant of order unity and does not
diverge any more. \label{fig:fluc-Fourier}}
\end{figure}

%------------------------------------------------------------------------------
\section{Discussion}
\label{sec:corr-discussion}
%------------------------------------------------------------------------------

It has been noticed that the fluctuation discussed above has been extensively
used in cosmology, see e.g.\ \cite{Glenz2009}. The fluctuation in the
radiation field, however, looks different from that in cosmology and thus
remains yet to be explained.

Moreover, the most natural candidate to reveal the off-diagonal elements is the
correlator in \emph{real space}, which is just the Fourier transform of the
correlator calculated above. Unfortunately, the transformation has yet to be
made for the thermal state due to an infra-red divergence.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../mythesis"
%%% End:


% Created 2021-07-08 Thu 10:15
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=1610]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{amssymb}
\DeclareMathOperator{\shift}{q}
\DeclareMathOperator{\diff}{p}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{2021-07-08}
\title{Discretizing continuous-time controllers}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Discretizing continuous-time controllers},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle

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

\section{Discretization}
\label{sec:org497335f}
\begin{frame}[label={sec:orga5cf6e7}]{Context}
\begin{itemize}
\item Controller \(F(s)\) obtained from a design in continuous time.
\item Need discrete approxmation in order to implement on a computer
\end{itemize}

\begin{center}
 \includegraphics[width=0.7\linewidth]{../../figures/fig8-1.png}\\
 \footnotesize Source: Åström \& Wittenmark
\end{center}
\end{frame}

\section{Warm-up: Differentiation}
\label{sec:org1fa17cb}

\begin{frame}[label={sec:org07d6f70}]{Warm-up exercise}
\begin{columns}
\begin{column}{0.4\columnwidth}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/block-simple-derivative}
\end{center}

\alert{Draw the Bode diagram for the transfer function}
\end{column}
\begin{column}{0.6\columnwidth}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/bode-derivative-empty}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:org2b5bba8}]{Discrete-time differentiation}
\begin{center}
\begin{tabular}{lll}
\includegraphics[width=0.3\linewidth]{../../figures/block-simple-shift-z} & \(\Leftrightarrow\) & \includegraphics[width=0.3\linewidth]{../../figures/block-simple-shift}\\
\end{tabular}
\end{center}

\begin{columns}
\begin{column}{0.4\columnwidth}
\vspace*{5mm}

\includegraphics[width=\linewidth]{../../figures/block-simple-discrete-derivative-fwd}

\textcolor{white}{Space}

\begin{center}
\includegraphics[width=\linewidth]{../../figures/block-simple-discrete-derivative}
\end{center}
\end{column}

\begin{column}{0.6\columnwidth}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:orgc048ecb}]{Discretization methods}
\begin{enumerate}
\item Forward difference. Substitute
\[ s = \frac{z-1}{h} \] in \(F(s)\) to get
\[ F_d(z) = F(s')|_{s'=\frac{z-1}{h}}. \]
\item Backward difference. Substitute
\[ s = \frac{z-1}{zh} \] in \(F(s)\) to get
\[ F_d(z) = F(s')|_{s'=\frac{z-1}{zh}}. \]
\end{enumerate}
\end{frame}
\begin{frame}[label={sec:org6c3e541}]{Discretization methods, contd.}
\begin{enumerate}
\setcounter{enumi}{2}
\item Tustin's method (also known as the bilinear transform). Substitute
\[ s = \frac{2}{h}\frac{z-1}{z+1} \] in \(F(s)\) to get
\[ F_d(z) = F(s')|_{s'=\frac{2}{h}\cdot \frac{z-1}{z+1}}. \]
\item Ramp invariance. This is similar to ZoH, which is step-invariant approximation.
Since a unit ramp has z-transform \(\frac{zh}{(z-1)^2}\) and Laplace-transform \(1/s^2\),  the discretization becomes
\[ F_d(z) = \frac{(z-1)^2}{zh} \ztrf{\laplaceinv{\frac{F(s)}{s^2}}}. \]
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:orgcde3040}]{Frequency warping using Tustin's}
\begin{center}
\includegraphics[width=0.6\linewidth]{../../figures/fig8_3.png}
\end{center}
The infinite positive imaginary axis in the s-plane is mapped to the finite-length upper half of the unit circle in the z-plane.
\end{frame}
\begin{frame}[label={sec:org92aad28}]{Exercise}
Find the discrete approximation of the lead-compensator \(F(s) = \frac{s+b}{s+a}\), and determine the pole for
\begin{enumerate}
\item Forward difference. Substitute
\[ F_d(z) = F(s')|_{s'=\frac{z-1}{h}}. \]
\item Backward difference. Substitute
\[ F_d(z) = F(s')|_{s'=\frac{z-1}{zh}}. \]
\item Tustin's approximation
\[ F_d(z) = F(s')|_{s'=\frac{2}{h}\cdot \frac{z-1}{z+1}}. \]
\end{enumerate}
\end{frame}
\begin{frame}[label={sec:org4007077}]{Forward difference exercise}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/forward-diff-exercise}
\end{center}
\end{frame}

\begin{frame}[label={sec:org912e773}]{Backward difference exercise}
\begin{center}
\includegraphics[width=\linewidth]{../../figures/backward-diff-exercise}
\end{center}
\end{frame}
\end{document}