\chapter{Investigating results on the limiting distribution}
\section{Playing with the (original) sequence of maxima}
\paragraph{}
Here, we will generate finite size sequences (N = 10000) of independent identically distributed random variables following respectively :
\begin{itemize}
	\item a standard Normal Distribution $\mathcal{N}(0,1)$
	\item a Cauchy Distribution \textit{Cauchy(0,1)}
	\item an Exponential Distribution \textit{Exp(1)}
\end{itemize}
We will compute the sequence of maxima, neither centred nor normalised, and draw the scatter plot as well as the plot of the maxima  $M_n$ as a function of the time steps $n$.
We will also draw the $1 - \frac{1}{n}$-quantiles of the distributions (distributions of the sample, not of the maxima), which we will denote by $q_n$, as a function of the time steps $n$. This will lead us to make an interesting observation.
\section{Sample following a Normal distribution}
\paragraph{Computing the quantiles}
The Normal distribution is a particular case because, unlike in the cases of the Cauchy and the Exponential distribution, there is no explicit form to the cumulative distribution function. We will thus use a "well-known"\footnote{Many textbooks mention it, though it is not necessarily what springs to the mind when thinking about the properties of Gaussian RVs.} inequality, holding $\forall t > 0$ :
\begin{equation}
(\frac{1}{t} - \frac{1}{t^3} ) \frac{\exp(-\frac{t^2}{2})}{\sqrt{2 \pi}} < 1 - \Phi(t) < \frac{1}{t} \frac{\exp(-\frac{t^2}{2})}{\sqrt{2 \pi}}
\end{equation}
From there, it is easy to see that the following holds :
\begin{equation}
1 - \Phi(t) \sim_{t \rightarrow +\infty} \frac{1}{t} \frac{\exp(-\frac{t^2}{2})}{\sqrt{2 \pi}} \\
\end{equation}
When $n$ grows large, the $1 - \frac{1}{n}$-quantile grows very large so it is valid to replace $1 - \Phi(t) $ by its equivalent in the equation satisfied by the quantiles : \\
\begin{equation}
\begin{alignat*}{2}
&\textcolor{white}{\iff} & F_X(q_n) &= 1 - \frac{1}{n} \\
&\iff &  \frac{1}{q_n} \frac{\exp(-\frac{q_n^2}{2})}{\sqrt{2 \pi}}  &= \frac{1}{n} \\
&\iff & \log(q_n) + \log(\exp(-\frac{q_n^2}{2})) + \log(\sqrt{2 \pi}) &= \log(n)\\
\end{alignat*}
\end{equation}
This equation cannot be solved analytically, we will resolve it iteratively. The starting point is $\log(n) = \frac{t_0^2}{2}$, which gives us $t_0 = \sqrt{(2 \log(n))}$. If we then run the Newton-Raphson algorithm, we see that the corrections to $t_0$ from the next iterations are small enough that we can keep $t_0$ as solution.\footnote{See the fourth of the figures below}.
\begin{figure}[h!]
	\centering
	\caption{Below, a realisation of the sequence of maxima for i.i.d. standard unit Gaussian RVs}\label{fig:toyingLimitGaussian}
\end{figure}
\begin{figure}[h!]
	\centering
	\begin{minipage}[b]{0.4\textwidth}
        \centering
        \includegraphics[scale =0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/gaussianScatterMaxima.jpeg}
        \caption{Scatter Plot of the \\Maxima, n = 10000}
        \label{fig:toyingLimitGaussianScatter}
	\end{minipage}
	\begin{minipage}[b]{0.4\textwidth}
		        \centering
		        \includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/gaussianMaximaAgainstSteps.jpeg}
		        \caption{Maxima against the time steps}
		        \label{fig:toyingLimitGaussianAgainst}
	\end{minipage}
 \end{figure}
 \begin{figure}[h!]
       \centering
       \includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/gaussianFitting.jpeg}
       \caption{Maxima against the time steps and function $n \rightarrow \sqrt{2 \log(n)}$}
       \label{fig:toyingLimitGaussianFitting}
\end{figure}
 \begin{figure}[h!]
 	\centering
 	\includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_3/corrApproxQuantileGaussian.jpeg}
 	\caption{The correction becomes negligible compared to the starting term as n grows large}
 	\label{fig:toyingLimitGaussianCorr}
 \end{figure}
\section{Sample following a Cauchy distribution}
\paragraph{Computing the quantiles}
\newline
Let $X_1, \cdots, X_n $ be i.i.d. RVs $\sim$ \textit{Cauchy(0,1)}. The distribution function is $F_X(t) = \frac{1}{\pi} \arctan(x) - \frac{1}{2}$. The $1 - \frac{1}{n}$-quantiles satisfy the equation :
\newline
\begin{equation}
\begin{alignat*}{2}
&\textcolor{white}{\iff} & F_X(q_n) &= 1 - \frac{1}{n} \\
&\iff &  \frac{\arctan(q_n)}{\pi} + \frac{1}{2} &= 1 - \frac{1}{n} \\
&\iff & \frac{\arctan(q_n)}{\pi} &= \frac{2-n}{n} \\
&\iff & q_n &= \tan(\frac{\pi}{2} \frac{2-n}{n})
\end{alignat*}
\end{equation}
\begin{figure}[h!]
		\caption{Below, a realisation of the sequence of maxima for i.i.d. \textit{Cauchy(0,1)} RVs}\label{fig:toyingLimitCauchy}
\end{figure}
\begin{figure}[h!]
	\centering
	\begin{minipage}[b]{0.4\textwidth}
		\centering
		\includegraphics[scale =0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/cauchyScatterMaxima.jpeg}
		\caption{Scatter Plot of the \\Maxima, n = 10000}
		\label{fig:toyingLimitCauchyScatter}
	\end{minipage}
	\begin{minipage}[b]{0.4\textwidth}
		\centering
		\includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/cauchyMaximaAgainstSteps.jpeg}
		\caption{Maxima against the time steps}
		\label{fig:toyingLimitCauchyAgainst}
	\end{minipage}
\end{figure}
 \begin{figure}[h!]
 	\centering
 	\includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/cauchyFitting.jpeg}
    \caption{Maxima against the time steps and function $n \rightarrow \tan(\pi \frac{2-n}{2 n})}$}
    \label{fig:toyingLimitCauchyFitting}
 \end{figure}
\newpage
 \paragraph{}
$n \rightarrow \tan(\pi \frac{2-n}{2 n})}$ is roughly linear in $n$. We know that if $\lvert x \rvert < \frac{\pi}{2}$, $\tan(x) = x + \frac{x^3}{3} + \frac{2 x^5}{15} + \cdots$ so if we take the first order approximation, we get $\tan(x) \approx x$ which confirms what the plot seems to suggest.\footnote{Of course the expansion is valid as for a number of observations $n$ greater than $1$, $ -1 < \frac{2}{n} - 1 < 1$ and thus $\lvert \frac{\pi}{2} \frac{2 - n}{n} \rvert = \vert \frac{\pi}{2} (\frac{2}{n} - 1) \rvert < \frac{\pi}{2}$.}
\section{Sample following an Exponential Distribution}
\paragraph{Computing the quantiles}
\newline
Let $X_1, \cdots, X_n $ be i.i.d. RVs $\sim$ \textit{Exponential($\lambda$)}. The distribution function is $F_X(t) = 1 - \exp(-\lambda t)$. The $1 - \frac{1}{n}$-quantiles satisfy the equation :
\newline
\begin{equation}
\begin{alignat*}{2}
&\textcolor{white}{\iff} & F_X(q_n) &= 1 - \frac{1}{n} \\
&\iff & 1 - \exp(-\lambda q_n) &= 1 - \frac{1}{n} \\
&\iff & q_n &= \frac{1}{\lambda} \log(n)
\end{alignat*}
\end{equation}
\begin{figure}[h!]
		\caption{Below, a realisation of the sequence of maxima for i.i.d. \textit{Exp(1)} RVs}\label{fig:toyingLimitExponential}
\end{figure}
\begin{figure}[h!]
	\centering
	\begin{minipage}[b]{0.4\textwidth}
		\centering
		\includegraphics[scale =0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/exponentialScatterMaxima.jpeg}
		\caption{Scatter Plot of the Maxima, n = 10000}
		\label{fig:toyingLimitExponentialScatter}
	\end{minipage}
	\begin{minipage}[b]{0.4\textwidth}
		\centering
		\includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/exponentialMaximaAgainstSteps.jpeg}
		\caption{Maxima against the time steps}
		\label{fig:toyingLimitExponentialAgainst}
	\end{minipage}
\end{figure}
\begin{figure}[h!]
		\centering
		\includegraphics[scale = 0.4]{/Users/kimartin/Desktop/PDM_thesis_report/latex_template5_57/main/R_Files_1/exponentialFitting.jpeg}
		\caption{Maxima against the time steps and function $n \rightarrow \log(n)}$}
	\label{fig:toyingLimitExponentialFitting}
\end{figure}
\newpage
\section{Why does this work ?}
\paragraph{(The underlying idea)}
Why have we made a link between the $\frac{1}{n}$-quantiles of the common distribution of the $X_i$ and the sequence of the $M_n$ ? Actually, the link is that if the sample is made up of independent realizations, then $M_n$ is an estimate of the $1 - \frac{1}{n}$ quantile. \newline
The idea behind this is as follows, that the maxima will get closer to the upper end-point of the distribution of the $X_i$, $F_{X_i}$. That is also the case for the $1 - \frac{1}{n}$-quantiles of $F_{X_i}$. Of course, it is only an intuition !
\section{What are the limiting distributions in theses cases ?}
\paragraph{Preliminary remark}
In what follows we are using theoretical results exposed in the next chapter, chapter 3, concerning the three possible limiting distributions as well as Von Mises' theorem.
\paragraph{Fr\'{e}chet limiting distribution}
Let us assume that a random variable $X$ follows a $Cauchy(0,1)$ distribution, $x^+ = + \infty$. The limiting distribution can only be either a Gumbel or a Fréchet-type distribution.
\begin{equation}
\begin{alignat*}{2}
f_X(x) = \frac{1}{\pi} \frac{1}{1 + x^2}
\end{alignat*}
\end{equation}
\begin{equation}
\begin{alignat*}{2}
F_X(x) = \frac{1}{\pi} \arctan(x) + \frac{1}{2}
\end{alignat*}
\end{equation}
\begin{equation}
\begin{alignat*}{2}
r(x) &= \frac{f_X(x)}{1 - F_X(x)} \\
&= \frac{\frac{1}{1 + x^2}}{\frac{\pi}{2} - \arctan(x)} \\
&= \frac{\frac{1}{1 + x^2}}{\frac{\pi}{2} - (\frac{\pi}{2} - \arctan(\frac{1}{x}))}\\
&= \frac{1}{(1 + x^2) \arctan(\frac{1}{x})}\\
&= \frac{1}{(1 + x^2) (\frac{1}{x} + o(\frac{1}{x^2}))}\\
&= \frac{1}{\frac{1 + x^2}{x} + o(1)}\\
\implies x r(x) &= \frac{x^2}{x^2 + 1 + o(1)} \\
\implies x r(x) &\xrightarrow[x \rightarrow + \infty]{} 1
\end{alignat*}
\end{equation}
Finally, by Von Mises' theorem, we can conclude that the limiting distribution for the standardized maxima of a $Cauchy(0,1)$ sample is a Fréchet distribution.
%\paragraph{Fréchet distribution}
%Let $X_1, \cdots, X_n $ be i.i.d. RVs $\sim$ \textit{Exponential($\lambda$)}. Let us remember from the previous section the form of the $\frac{1}{n}$-quantile $q_n = \frac{1}{\lambda}\log(n)$. Let us evaluate $F_{M_n}$ in $q_\frac{1}{n}-t$ : \\
%\begin{equation}
%\begin{alignat*}{2}
%F_{M_n}(q_\frac{1}{n}-t) &= (1 - \exp(-\lambda(q_\frac{1}{n}-t)))^n\\
%&= (1 - \exp(\lambda t)\exp(-\lambda q_\frac{1}{n}))^n\\
%&= (1 - \frac{\exp(\lambda t)}{n})^n \\
%&\longrightarrow_{n \rightarrow +\infty} \exp(-\exp(\lambda t)) \\
%\end{alignat*}
%\end{equation}
\paragraph{Gumbel limiting distribution}
Let us assume that a random variable $X$ follows a $Exp(1)$ distribution, $x^+ = + \infty$. The limiting distribution here again can only be either a Gumbel or a Fréchet-type distribution.
\begin{equation}
\begin{alignat*}{2}
f_X(x) = \lambda \exp(- \lambda x)
\end{alignat*}
\end{equation}
\begin{equation}
\begin{alignat*}{2}
F_X(x) =  1 - \exp(- \lambda x)
\end{alignat*}
\end{equation}
\begin{equation}
\begin{alignat*}{2}
r(x) &= \frac{f_X(x)}{1 - F_X(x)} \\
&= \frac{\lambda \exp(- \lambda x)}{1 - (1 - \exp(- \lambda x))} \\
&= \frac{\lambda \exp(- \lambda x)}{\exp(- \lambda x)} \\
&= \lambda \\
\implies \frac{\mathrm{d}r}{\mathrm{d}x}(x) &= 0
\end{alignat*}
\end{equation}
Finally, by Von Mises' theorem, we can conclude that the limiting distribution for the standardized maxima of an $Exp(1)$ sample is a Gumbel distribution.
\paragraph{Gumbel limiting distribution - bis}
Let us assume that a random variable $X$ follows a $\mathcal{N}(0,1)$ distribution, $x^+ = + \infty$. The limiting distribution here again can only be either a Gumbel or a Fréchet-type distribution. It turns out that in that case, the limiting distribution is a Gumbel-type distribution. The computation is slightly more involved than previously, as shown below :
\end{equation}
\begin{equation}
\begin{alignat*}{2}
\frac{\mathrm{d}r}{\mathrm{d}x}(x) &= \frac{\frac{\mathrm{d}f}{\mathrm{d}x}(x) (1 - F_X(x)) - f(x) (-f(x))}{(1 - F_X(x))^2} \\
&= \frac{f(x)^2 + \frac{\mathrm{d}f}{\mathrm{d}x}(x) (1 - F_X(x)) }{(1 - F_X(x))^2}
\end{alignat*}
\end{equation}
It yields an indeterminate form. The key to solve the issue here is to use an expansion of the cumulative distribution function of a standard Gaussian random variable : \newline
$\Phi(x) \approx \frac{1}{2} + \frac{1}{\sqrt{2 \pi}} \exp(\frac{-x^2}{2}) (x + \frac{x^3}{3} + \frac{x^5}{15})$ \newline
\begin{equation}
\begin{alignat*}{2}
\frac{\mathrm{d}r}{\mathrm{d}x}(x) &= \frac{
	\frac{\exp(- x^2)}{2 \pi} - \frac{x}{2 \sqrt{2 \pi}} \exp(\frac{-x^2}{2}) + \frac{x \exp(- x^2)}{2 \pi} (x + \frac{x^3}{3} + \frac{x^5}{15})
	}{
	(\frac{1}{2} - \frac{1}{\sqrt{2 \pi}} \exp(\frac{-x^2}{2}) (x + \frac{x^3}{3} + \frac{x^5}{15}))^2
	} \\
	&=  \frac{
		\frac{\exp(- x^2)}{2 \pi} - \frac{x}{2 \sqrt{2 \pi}} \exp(\frac{-x^2}{2}) + \frac{x \exp(- x^2)}{2 \pi} (x + \frac{x^3}{3} + \frac{x^5}{15})
	}{
	\frac{1}{4} - \frac{\exp(\frac{-x^2}{2})}{\sqrt{2 \pi}} (x + \frac{x^3}{3} + \frac{x^5}{15}) + \frac{\exp(-x^2)}{2 \pi} (x + \frac{x^3}{3} + \frac{x^5}{15})^2
} \\
\end{alignat*}
\end{equation}
Now we can see that if we take the limit when $x$ goes to $+ \infty$, we get a $0$. By Von Mises' theorem, we can conclude that the limiting distribution for the standardized maxima of a $\mathcal{N}(0,1)$ sample is a Gumbel distribution.
\paragraph{Weibull limiting distribution}
Cauchy, Normal and Exponential distributions all have an infinite upper end-point, thus we cannot get a Weibull distribution as limiting distribution. Let us assume that a random variable $X$ follows a $Unif([0,1])$ distribution, $x^+ = 1 < + \infty$.
\begin{equation}
\begin{alignat*}{2}
f_X(x) = 1
\end{alignat*}
\end{equation}
\begin{equation}
\begin{alignat*}{2}
F_X(x) =  x
\end{alignat*}
\end{equation}
\begin{equation}
\begin{alignat*}{2}
r(x) &= \frac{f_X(x)}{1 - F_X(x)} \\
&= \frac{1}{1 - x} \\
\implies (x^+ - x) r(x) &= (x^+ - x) \frac{1}{1 - x} \\
\implies (1 - x) r(x) &= (1 - x) \frac{1}{1 - x} = 1 \\
\implies (x^+ - x) r(x) &\xrightarrow[x \rightarrow x^+]{} 1 > 0
\end{alignat*}
\end{equation}
Finally, by Von Mises' theorem, we can conclude that the limiting distribution for the standardized maxima of a $Unif([0,1])$ sample is a Weibull distribution.


%!TEX root = ../notes.tex
\section{February 28, 2022}
\subsection{Miller-Rabin Primality Test}
Recall \cref{prop:aq-1-mod-p} from last class.
\begin{proposition*}
    Let $p$ be an odd prime. Write
    \[p-1 = 2^k\cdot q\quad\text{with $q$-odd}\]
    Then either
    \[a^{q}\equiv 1\mod p\]
    or one of $a^q, a^{2q}, a^{4q},\dots, a^{2^{k-1}q}$ is $\equiv -1\mod p$.
\end{proposition*}

For $561$, we write $561 = 2^4\cdot 35$.
\begin{align*}
    2^{35}  & \equiv 263 \pmod{561} \\
    2^{70}  & \equiv 166 \pmod{561} \\
    2^{140} & \equiv 67 \pmod{561}  \\
    2^{280} & \equiv 1 \pmod{561}
\end{align*}
using the following code\dots
\begin{lstlisting}[language=Python]
from crypto import pow_mod

pow_mod(2, 35) # 263
263 ** 2 % 561 # 166
166 ** 2 % 561 # 67
67 ** 2 % 561 # 1
\end{lstlisting}

\begin{definition}[Miller-Rabin Witness]
    $a$ is a \ul{Miller-Rabin witness} if $a$ does not satisfy above proposition.
\end{definition}

\begin{theorem}
    If $n$ is composite, then at at least $75\%$ of $a\in(\ZZ/n\ZZ)$ are Miller-Rabin witnesses.
\end{theorem}
\begin{proof}
    Given on faith.
\end{proof}

\begin{algorithm}[Miller-Rabin Probabilistic Primality Test]
    ~\begin{lstlisting}[numbers=none,language=Python,escapeinside={(*}{*)}]
from random import randrange
from crypto import pow_mod

def miller_rabin(n, a):
    """
    Miller-Rabin primality test on number n and base a
    """
    q, k = n - 1, 0
    # Write (*$n-1 = 2^k\cdot q$*)
    while q % 2 == 0:
        k = k + 1
        q = q // 2
    a = pow_mod(a, q, n)
    if a == 1 or a == n - 1:
        return False
    for _ in range(k - 1):
        a = a ** 2 % n
        if a == n - 1:
            return False
    return True

def is_prime(n):
    for _ in range(50):
        if miller_rabin(n, randrange(1, n)):
            return False
    return True
\end{lstlisting}
\end{algorithm}

Using this, we can find large prime numbers using
\begin{lstlisting}[language=Python]
from crypto import miller_rabin, is_prime
def next_prime(n):
    while not is_prime(n):
        n = n + 1
    return n
\end{lstlisting}

\begin{theorem}[Prime Number Theorem]
    Probability that $n$ is prime is about
    \[\frac{1}{\log(n)}\]
    More formally,
    \[\lim_{x\to \infty}\frac{\text{\# of primes $\leq x$}}{x/\log x} = 1\]
\end{theorem}

So the Miller-Rabin test lets us efficiently find (large) prime numbers.


\subsection{The Hahn-Banach theorem}\label{subsec:hahn_banach}

The Hahn-Banach theorem is an important result that can be stated differently and in different levels of generality.

\begin{theorem}[Geometric Hahn-Banach theorem/Mazur's theorem]\label{thm:geometric_hahn_banach}\mcite[24]{ИоффеТихомиров1974}
  Fix a \hyperref[def:topological_vector_space]{topological vector space} \( X \). Let \( A \subseteq X \) be an open \hyperref[def:convex_set]{convex} set and \( L \subseteq X \) be a subspace that is disjoint from \( A \). Then there exists a continuous linear functional \( x^* \in X^* \) such that
  \begin{equation*}
    \begin{array}{l}
      \real \inprod{x^*} x > 0, x \in A \\
      \real \inprod{x^*} x = 0, x \in L
    \end{array}
  \end{equation*}

  See \fullref{rem:linear_functionals_over_c} for a justification of only considering the real part of \( x^* \).
\end{theorem}

\begin{corollary}\label{thm:hahn_banach_implies_functionals_vanish_nowhere}\mcite[24]{ИоффеТихомиров1974}
  The \hyperref[def:dual_vector_space]{dual} of a Hausdorff \hyperref[def:locally_convex_space]{locally convex space} \( X \) does not \hyperref[def:functions_vanish_nowhere]{vanish} at the nonzero vectors of \( X \).
\end{corollary}
\begin{proof}
  Fix a nonzero point \( x \in X \). The result follows from \fullref{thm:geometric_hahn_banach} with \( L \coloneqq \{ 0 \} \) and \( A \) --- any convex set containing \( x \) and not containing zero. Such a set \( A \) exists because the topology is Hausdorff and \( x \) has a neighborhood disjoint from any point in \( L \).
\end{proof}

\begin{corollary}\label{thm:hahn_banach_implies_annihilator_nontrivial}\mcite[25]{ИоффеТихомиров1974}
  The \hyperref[def:vector_space_annihilator]{annihilator} of any proper subspace of a Hausdorff \hyperref[def:locally_convex_space]{locally convex space} contains nonzero elements.
\end{corollary}
\begin{proof}
  Denote the proper subspace by \( L \subsetneq X \). Fix \( x \in X \setminus L \) and let \( A \) be a convex neighborhood of \( x \) that is disjoint from \( L \). The result follows from \fullref{thm:geometric_hahn_banach}.
\end{proof}

\begin{corollary}\label{thm:hahn_banach_implies_duality_mapping_nonempty}\mcite[25]{ИоффеТихомиров1974}
  In a \hyperref[def:norm]{normed} space \( X \), for any nonzero vector \( x \in X \) there exists a continuous functional \( x^* \in S_{X^*} \) such that \( \inprod {x^*} x = \norm x \). In other words, the duality \hyperref[def:duality_mapping]{mapping} is nonempty for any point.
\end{corollary}
\begin{proof}
  This follows from \fullref{thm:hahn_banach_implies_annihilator_nontrivial} by taking \( A \coloneqq B(x, \abs{x}) \) and \( L \coloneqq \{ 0 \} \) and then scaling the obtained functional.
\end{proof}

\begin{theorem}[Hahn-Banach hyperplane separation theorem]\label{thm:hahn_banach_hyperplane_separation}\mcite[25]{ИоффеТихомиров1974}
  Fix a \hyperref[def:topological_vector_space]{topological vector space} \( X \). Let \( A, B \subseteq X \) be disjoint \hyperref[def:convex_set]{convex} sets. If \( \int{A} \neq \varnothing \), there exists a continuous linear functional \hyperref[def:hyperplane_separation]{separating} \( A \) and \( B \).
\end{theorem}


\lab{Algorithms}{Building Matrices, Sparse Matrices and Algorithmic Complexity}{Matrices and Complexity, cont.}

\objective{This section explains how to create specific types of large matrices. It also introduces the concept of temporal complexity. Finally, it explores SciPy's special methods for working with sparse matrices.}

\section*{Temporal Complexity}

%{\bf The next two paragraphs are an alternate description of Big O, designed as a more intuitive approach, to help with Vol I (a more technical explanation would be in Vol II). Let me know what you think...}

%I am not sure that this is clear or precise enough.  I think that the previously written stuff with some easier problems may be better...

One of the most important questions in scientific computing is: How long will this operation take?  The concept of temporal complexity attempts to answer this question by determining how much time a function needs to operate on a given size of input.  For example suppose calculating the inverse of a matrix of size $n$ requires the following number of calculations.
%For this reason we often discuss algorithms in terms of their temporal complexity, which describes how long an operation takes in terms of the size of the input. For example, suppose calculating the inverse of a matrix of size $n$ requires the following number of calculations:
\[
f(n) = \frac{3n^3}{2} + 75n^2 + 250n + 30
\]
What is the most important part of this expression? When our input gets very large the only relevant term in this equation is $n^3$. For this reason we say that $f(n) \in O(n^3)$, or more commonly that $f(n)$ is $O(n^3)$ (spoken ``Big O of n cubed'' or ``Order of n cubed"). This notation is borrowed from analysis. This notation captures the salient behavior of our temporal complexity, or more precisely the growth rate we can expect of the execution time of our algorithm. We will discuss this concept later, but this is a simple introduction to the notion of complexity and Big O. Spatial complexity is the amount of memory an algorithm uses, and is defined similarly.

\begin{comment}

Now that we have begun to study some more complex MATLAB expressions, it is prudent to have a basic notion of the complexity of the operations we are interested in executing.  Complexity theory is the study of the difficulty of computational problems.  What makes computing the sum of two integers easy, and what makes computing the inverse of a large matrix so difficult?  There is an entire field dedicated to answering such questions; in this work we only do a cursory examination to communicate the basic ideas and vocabulary.

	To begin, we use an example to illustrate what we are trying to accomplish.  There are occasions when one is willing to sacrifice precision in order to focus on more important features of the object of study.  For example, $f(x) = x^3 + \frac{sin(20x)}{10}$ produces a cubic function with a slight wobble.  In some applications that wobble may be a crucial detail, but in many cases the wobble will be irrelevant and only serves to distract from the salient features of the function.  We extend this example to algorithms.

	An algorithm is an ordered set of instructions.  Something simple like a recipe to bake a cake is an algorithm, but the only algorithms you will be dealing with here are MATLAB operations and programs.  To describe the complexity of an algorithm we use asymptotic notation called ``Big O.''  Big O makes us focus on salient features of the complexity of an algorithm while at the same time suppressing unnecessary details, much like we ignored the sine wobble in the previous example.  It has been said that using Big O is ``the art of knowing where to be sloppy and where to be precise.''

	With that introduction, we define Big O:

\begin{theorem}
	$f(x) = O(g(x))$ if and only if $\exists$ M $\in \mathbb{N}, x_0 \in dom(f)$ such that $|f(x)| \le M|g(x)|$ $\forall x > x_0$
 \end{theorem}

 This is an abuse of notation, we really should be saying that $f(x) \in O(g(x))$ since $O(g(x))$ is not a function but an equivalence class of functions.  Lamentably, using $=$ has become embedded into the culture, so there is not much to be done except make a note of the correct notation and move along.

 \begin{example}
 $x^2 = O(x^3)$ since $x^2 < x^3$ when $x > 1$
 \end{example}

 \begin{example}
 $x^3 + 3x^2 + x + 4 = O(x^3)$ since $x^3 + 3x^2 + x + 4 < 10x^3$ when $x > 1$
 \end{example}

  \begin{example}
 $x^2 + 10000x = O(x^2)$ since $x^2 + 10000x < 100000x^2$ for all x.
 \end{example}

You can see that when we talk about Big O of polynomials, we are simply dropping off lower order terms and ignoring coefficients.
%
% (These problems may be too challenging)
% \begin{problem}
% Show that $O(\log{x}) \subset O(x) \subset O(x\log{x})$
% \end{problem}
%
% \begin{problem}
% Show that $O(x^k) \subset O(x^{k+1})$
% \end{problem}
%
% \begin{problem}
% Show that $O(x^p) \subset O(2^x)$ for all $p \in \mathbb{N}$
% \end{problem}
%
	What does this have to do with computer programs?  Every algorithm executed on a computer corresponds to a function that returns the number of steps taken (and therefore the time) given an input of size n.  Certain problems can be solved with fewer steps, whilst others require many more.  For example, given two matrices of size n, it only takes about n steps to add the entries together and thus matrix addition is $O(n)$.  On the other hand, given a matrix of size $n$, it takes about $n^3$ steps to calculate its inverse, and thus inverting a matrix is $O(n^3)$.  There is something inherently more difficult about inverting a matrix; there is complexity that simply isn't there with the straightforward operation of addition.
%
%	We will return to complexity analysis after we know a little bit more about writing our own scripts and functions.
\end{comment}

\section*{Advanced Matrix Tools}

We now introduce a few different ways to build matrices. Two important methods available for building matrices are \li{zeros()} and \li{ones()}. These commands allow us to build matrices populated entirely with zeros or ones, respectively. For example, to build a 3-vector filled with zeros we enter the following command:

\begin{lstlisting}[style=python]
: import scipy as sp
: sp.zeros((3,1))
array([[ 0.],
       [ 0.],
       [ 0.]])
\end{lstlisting}

To find additional options for these methods, you can use the help system.

%Better explanation and an example or two should go here.

One important use of the \li{zeros()} method is to allow us to pre-allocate memory. Pre-allocation is simply the practice of reserving a chunk of memory for later use. We can always add more space to a matrix using the methods we learned in lab 1, but this requires many extra internal operations because of way arrays are stored in memory. Thus, it is generally faster to allocate a matrix with its final size and modify its values rather than building an array as you go.

%Problem here comparing pre-allocation vs. no pre-allocation.  There should be several orders of magnitude difference.

% Perhaps more of a segue here?

Table 1.3 gives a few commands that allow us to build types of useful matrices.

\begin{table}[h!]

\begin{center}

    \begin{tabular}{|l|p{4cm}|l|}

    \hline

    Function & Description & Usage \\

    \hline

    \li{eye()} & Identity matrix & sp.eye(m, n)\\

    \li{zeros()} & Zero matrix & sp.zeros((m, n))\\

    \li{ones()} & One matrix & sp.ones((m, n))\\

    \li{diag()} & Building (or retrieving) along a diagonal&\\

    \li{linalg.toeplitz()} & Matrix with constant diagonals & la.toeplitz()\\

    \li{linalg.triu()} & Upper triangular&\\

    \li{linalg.tril()} & Lower triangular&\\

    \li{rand} & Psuedo-random matrix, uniformily distributed&\\

   \li{randn} & Psuedo-random matrix, normally distributed&\\

   \li{random.randint()} & Psuedo-random matrix, uniformily distributed integers & sp.random.randint()\\

    \li{tile()} & Copy across a given dimension & sp.tile(A, reps)\\

    \hline

    \end{tabular}
	\caption{Special matrix creation commands}

\end{center}
\end{table}

For example, suppose that we want to create a matrix with $-2$ on the diagonal, and ones on the super and sub diagonal. We can do this by using the following command:

\begin{lstlisting}[style=python]
: from scipy import linalg as la
: la.toeplitz([-2,1,0])
array([[-2,  1,  0],
       [ 1, -2,  1],
       [ 0,  1, -2]])
\end{lstlisting}

This matrix is useful because it numerically approximates the second derivative of a function. We investigate some properties of this matrix in Problem 6 of this lab, and explain more about this matrix later.

\begin{problem}
Use the \li{diagflat()} method to create the following matrices. All of these matrices should be easily scaleable (ie only minor modification would be required to change the size).
\[
\begin{pmatrix}
1&2&3&4&5\\
0&1&2&3&4\\
0&0&1&2&3 \\
0&0&0&1&2 \\
0&0&0&0&1 \\
\end{pmatrix}
\hspace{8mm}
\begin{pmatrix}
1&1/2&1/3 & 1/4 &1/5\\
1/2&1&1/2&1/3&1/4\\
1/3&1/2&1&1/2&1/3 \\
1/4&1/3&1/2&1&1/2 \\
1/5&1/4&1/3&1/2&1 \\
\end{pmatrix}
\]
\end{problem}

\begin{problem}
Create the matrices from Problem 1 using the methods \li{linalg.toeplitz()} or \li{linalg.triu()}. Which method is easier? Now use whichever command is easiest to create the matrix:
\[
\begin{pmatrix}
1&0&0&0&0\\
0&2&0&0&0\\
0&0&3&0&0 \\
0&0&0&4&0 \\
0&0&0&0&5 \\
\end{pmatrix}
\]
\end{problem}

\begin{problem}
Write a function that will create a matrix of size $n$ that has ones on the diagonal and has normally-distributed random entries in the last two columns and the last two rows.
%Do this in one line using the commands from this section and matrix building techniques from Lab 1.1.
\end{problem}

\section*{Sparse Matrices}
In this section we discuss how sparse matrices are used and constructed. A sparse matrix is a matrix that has few non-zero entries (where few is generally relative to the number of entries in the matrix).  SciPy has several different ways of storing sparse matrices.  Each way has it pros and cons (the reader is encouraged to read the help for way).

\begin{table}[h!]

\begin{center}

    \begin{tabular}{|c|c|}

    \hline

    Function & Description \\

    \hline

    \li{sparse.bsr()} & Compressed Block Sparse Row\\

    \li{sparse.coo()} & Coordinate\\

    \li{sparse.csc()} & Compressed Sparse Column\\

    \li{sparse.csr()} & Compress Sparse Row\\

    \li{sparse.dia()} & Sparse Diagonal\\

    \li{sparse.dok()} & Dictionary of Keys\\

    \li{sparse.lil()} & Linked List\\

    \hline

    \end{tabular}
        \caption{Sparse matrix representations in SciPy}
\end{center}
\end{table}

Type the following into IPython.
\begin{lstlisting}[style=python]
: from scipy import sparse as spar
: A = sp.diagflat([2,3,4])
: B = spar.csc_matrix(A)
: C = B.todense()
\end{lstlisting}

Notice that the matrix $A$ has only three non-zero entries, and so we can consider it sparse. In memory, an array stores a bit of data (be it an integer, float, or complex number) each entry, meaning that a $3 \times 3$ matrix requires a total 9 blocks of memory. However, if we leverage the sparsity of $A$ we realize that we only need to store 3 numbers. The \li{sparse} methods do exactly this: they store only the non-zero entries and their locations in the matrix. No longer are we working with array.  SciPy has many methods for performing operations on sparse arrays.  To convert back to a dense matrix, we use the \li{.todense()} property of the sparse matrix.  We can also convert between the different types sparse arrays.

We remark that if you want to make a sparse diagonal matrix, the
best way to do it isn't to use \li{diagflat()} followed by \li{sparse},
it's actually better to use the \li{sparse.spdiags()} method:
\begin{lstlisting}[style=python]
: spar.spdiags([2,3,4],0,3,3)
\end{lstlisting}

This is because oftentimes when we are using sparse matrices we are dealing with matrices that are too large to be handled efficiently by python when represented in full form.

\section*{Banded Matrices}
A banded matrix is one whose only non-zero entries are diagonal
strips.  For example, the matrix
\[
A = \begin{pmatrix} 1&2&0&0\\3&4&5&0\\0&6&7&8\\0&0&9&10
\end{pmatrix}
\]
is banded because there are three nonzero diagonals.  This
particular type of banded matrix is called a tri-diagonal matrix.

You can easily create banded matrices using the \li{diagflat()} method.  For example, the matrix $A$ above can be created by
entering
\begin{lstlisting}[style=python]
: sp.diagflat([3,6,9],-1) + sp.diagflat([1,4,7,10],0) + sp.diagflat([2,5,8],1)
\end{lstlisting}

Often a better way to create a tri-diagonal is it use the \li{spar.spdiags()} method. This is because many diagonal matrices are sparse. For example, we create the same matrix in Python (while designating that it is sparse) using the command:
\begin{lstlisting}[style=python]
: Z = sp.array([[3, 1, 0],[6, 4, 2],[9, 7, 5],[0,10,8]]).T
: spar.spdiags(Z,[-1,0,1],4,4)
\end{lstlisting}

For more information, check the documentation by typing \li{spar.spdiags?}. For example we create a tri-diagonal array with uniformily distributed random entries.  This example also demonstartes the efficiency of using sparse arrays.
\begin{lstlisting}[style=python]
: B = sp.rand(3,10000)
: A = spar.spdiags(B,range(-1,2),10000,10000)
: denseA = A.todense()  #only do this step if you have _lots_ of memory!
: A.data.nbytes
240000          #about 0.24MB of memory
: denseA.nbytes
800000000       #about 762.9MB of memory!
\end{lstlisting}

We can't use the \li{full} command in this case because the computer will almost certainly run out of memory (the matrix is $10,\!000 \times 10,\!000$). However, we can still visualize this matrix using the \li{plt.spy()} command from matplotlib, which essentially shows the location of non-zero entries in a matrix. The output of \li{plt.spy(A)} in this case is shown in Figure 1.2:

%change to plt figure
\begin{figure}[h!]
\begin{center}
\includegraphics[scale = .5]{./Figures/spy}
\end{center}
\caption{The output of the \li{spy} command.}
\end{figure}

\section*{Using Sparse Matrices}

Consider the linear system $A x = b$, where $A$ is a
 $100,\!000\times 100,\!000$ tri-diagonal matrix.  To store a full
matrix of that size in your computer, it would normally require 10
billion double-precision floating-point numbers.  Since it takes 8
bytes to store a double, it would take roughly 80GB to store the
full matrix.  For most desktop computers, that fact alone makes the
system numerically prohibitive to solve. The temporal complexity of the problem is even more problematic. Methods for directly solving an arbitrary linear system are usually $O(n^3)$.  As
a result, even if the computer could store an 80GB matrix in RAM, it
would still take several weeks to solve the system.  However, since
we don't have computers with that much available RAM, most of the
matrix would have to be stored on the hard drive, so the computation
would probably take between $6$ months to a year.

The point is that even the next generation of computers will
struggle with solving arbitrary linear systems of this size in a
reasonable period of time.  However, if we take advantage of the
sparse structure of the tri-diagonal matrix, we can solve the linear
system, even with a modest modern computer.  This is because all of
those zeros don't need to be stored and we don't need to do as many
operations to row reduce the tri-diagonal system.

Let's first compute the spatial complexity of the above system when
considered as a sparse matrix.  There are three diagonals that have
roughly $100,\!000$ non-zero entries.  That's $300,\!000$
double-precision floating point numbers, which is about 2.4 MB (Less
storage than your favorite song).  As a result, it will easily
fit into the computer's RAM.  Furthermore, the temporal complexity for solving
a tri-diagonal matrix is $O(n)$. Let's see how long it takes to
solve the system for random data:

\begin{lstlisting}[style=python]
: from scipy.sparse import linalg as sparla
: from timer import timer
: D = sp.rand(3, 100000)
: b = sp.rand(1, 100000)
: A = spar.spdiags(D,[-1,0,1],100000,100000)
: def solSys():
....: return sparla.spsolve(A,b)
: with timer() as t:
....: t.time(solSys)
....: t.printTimes()
\end{lstlisting}

\begin{problem}
Write a function that returns a full $n\times n$
tri-diagonal array with $2$'s along the diagonal and $-1$'s along
the two sub-diagonals above and below the diagonal. Hint: Use the \li{la.toeplitz()} method. Note that this is the second derivative matrix that we discussed at the beginning of this lab.
\end{problem}

\begin{problem}
Write another function that builds the same array as above, but as a sparse array. You must build this as a sparse matrix from the beginning. Hint: Use the \li{spar.spdiags()} method.
\end{problem}

\begin{problem}
Solve the linear system $Ax = b$ where $A$ is the $n\times n$
tri-diagonal array from the above two problems and $b$ is randomly
generated.  How high can you go for each method?  Make a table for
several different values of $n$ and the time it took to solve for
each run.  What conclusions can you draw?
\end{problem}

\begin{problem}
Using the sparse array above and the method \li{la.eigs()}, calculate the smallest eigenvalue $\lambda$ of the array as the array's size goes to infinity. What value does $\lambda n^2$ approach?  Hint: It's the square of an important number. This is related to operator theory: the second derivative operator has this eigenvalue in certain cases.
\end{problem}

\section*{Other Sparse Commands}

One important method of sparse array objects is the \li{nonzero()} method, which is related to the number of nonzero entries in an array.  This number is important because it is an indicator of the amount of time and space that is required to operate on the sparse array. You should be aware that there is some overhead to using and storing the sparse array data structure. Sparsely represented arrays are very beneficial when the number of nonzero entries is relatively small compared to the total number of entries. When the array has many nonzero entries, a sparse representation becomes disadvantageous. To see this, create and execute a script with the following code:
\begin{lstlisting}[style=python]
: A = sp.rand(600,600); B = spar.csc_matrix(A)
: def square(A): return sp.power(A, 2)
: with timer() as t:
....: t.time(square, A)
....: t.time(square, B)
\end{lstlisting}

Run the script and note the two different runtimes. Notice that it takes much longer to square the sparse array. This is because the sparse array data structure is optimized for arrays that are actually sparse. The array $A$ is entirely nonzero. Thus, you incur the overhead of the sparse array representation without any benefits since there are no entries you are not required to store or compute. To summarize, only use a sparse array when your array is in fact sparse. Using sparse arrays for mostly nonzero arrays will negatively impact performance and memory requirements.

Just as with dense arrays, we can pre-allocate sparse arrays. Sometimes it is necessary to create sparse matrices that do not have a nice banded pattern.  We initialize a sparse array just like any other array.  The most efficient sparse array for pre-allocation is LIL.  Once you are done constructing you sparse array and wish to perform calculations, you should convert to a more efficient sparse array (CSR or CSC).
\begin{lstlisting}[style=python]
: Z = spar.lil_matrix((400,300))
<400x300 sparse matrix of type '<type 'numpy.float64'>'
        with 0 stored elements in LInked List format>
: Z[1,34] = 23
: Z[23,32] = 56
: Z[2,:] = 13.2
\end{lstlisting}

This code snippet creates a $400 \times 300$ LIL sparse array.  We can then work with the sparse array as though it were a dense array.  When the array is initialized all of the entries
are assumed to be zero.


\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[top=1in, bottom=1in, left=0.6in, right=0.6in]{geometry}
\usepackage{lipsum}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[hidelinks]{hyperref}
\usepackage{graphicx}
\usepackage[
backend=biber,
style=numeric,
doi=false,
url=false,
maxbibnames=9
]{biblatex}
\addbibresource{sysbio2018_blackboard.bib}

%\newcommand{\mymatrix}[1]{{\boldsymbol{#1}}}
\newcommand{\mymatrix}[1]{#1}
\newcommand{\myvector}[1]{{\boldsymbol{#1}}}
\newcommand{\fluxcone}{\mathcal{C}}
\newcommand{\stoichmat}{\mymatrix{S}}
\newcommand{\stoichmatint}{\mymatrix{S}_{\mathsf{int}}}

\date{March 2, 2018}
\title{\textbf{SysBio 2018}\\Innsbruck, Austria}
\author{Elad Noor}
\begin{document}
\maketitle
\tableofcontents

\section{Blackboard session: Metabolic Engineering}

\subsection{Introduction}
\subsubsection{Linear Programming (LP)}
Every Linear Programming problem can be converted to a canonical form that is:
\begin{eqnarray}
	\textsf{\textbf{Primal}}&&\nonumber\\
	&\textsf{maximize}&\myvector{c}^\top\myvector{x}\nonumber\\
	&\textsf{subject to}&\mymatrix{A}\myvector{x} \leq \myvector{b}\nonumber\\
	&\textsf{and}&\myvector{x}\geq \myvector{0}\,.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\end{eqnarray}

The dual is a symmetrical linear problem described by the following constraints
\begin{eqnarray}
	\textsf{\textbf{Dual}}&&\nonumber\\
	&\textsf{minimize}&\myvector{b}^\top\myvector{y}\nonumber\\
	&\textsf{subject to}&\mymatrix{A}^\top\myvector{y} \geq \myvector{c}\nonumber\\
	&\textsf{and}&\myvector{y}\geq 0\,.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\end{eqnarray}

If the primal has an optimal solution ($\myvector{x}^*$), then the dual will also have an optimal solution ($\myvector{y}^*$) and their values will be equal:
\begin{eqnarray}
	\textsf{\textbf{Strong duality theorem}}&&\nonumber\\
	&&\myvector{c}^\top\myvector{x}^* = \myvector{b}^\top\myvector{y}^*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\end{eqnarray}

Linear Programming problems can be solved very efficiently using established algorithms such as Simplex. Many good solvers exist for LP, including open source options such as glpk, CLP, and the internal R-project solver.

\subsubsection{Mixed-Integer Linear Programming (MILP)}
If a LP contains variables that can only have integer values (or in a more specific case, boolean values), the problem cannot be solved using the standard methods. In fate, MILP problems in general are NP-complete, i.e. computationally hard. The free solvers are typically not good enough for solving MILPs. Nevertheless, advanced commercial solvers such as CPLEX and Gurobi are quite efficient in solving even large MILPs in reasonable time, and provide free licenses for academics.

\subsection{Convex analysis of a metabolic flux cone}
The basic idea underlying most metabolic constraint-based models, is mass-balance combined with the steady-state assumption. Formally, given a stoichiometric matrix $\mymatrix{S}$, where rows represent metabolites and column represent reactions, and given a flux vector $\myvector{v}$, the rate of change in metabolite levels ($\myvector{c}$) is given by the equation.
\begin{eqnarray}\label{eq:dynamic-mass-balance}
	&\textsf{\textbf{Dynamic mass-balance}} &\nonumber\\
    &&\frac{d\myvector{c}}{dt} = \stoichmat \myvector{v}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\end{eqnarray}
For the subset of rows in $\stoichmat$ that represent internal metabolites (e.g. intracellular metabolites) which is denoted $\stoichmatint$, the pseudo steady-state assumption states that their concentrations do not change over time.
\begin{eqnarray}\label{eq:pseudo-steady-state}
	&\textsf{\textbf{Pseudo steady-state}} &\nonumber\\
	&&\stoichmatint \myvector{v} = 0~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\end{eqnarray}
Note that external metabolites are not constrained in this way. For example, the concentration of extracellular glucose in the medium decreases gradually while cells are growing at steady-state. Therefore, pseudo steady-state flux solutions are contained within the null-space (or kernel) of $\stoichmatint$, i.e. $\myvector{v} \in \ker(\stoichmatint)$.

\subsubsection{The flux cone}
Typically, many of the reactions in our model will be considered irreversible, due to thermodynamic constraints (and we will elaborate on this topic in section \ref{sec:thermodynamics}). Therefore, a subset of the fluxes will be constrained to be positive:
\begin{eqnarray}\label{eq:irreversible}
\forall i \in I_{irr} ~~ v_i \geq 0
\end{eqnarray}

Therefore, the set of possible flux solutions would be all fluxes that satisfy both equation \ref{eq:pseudo-steady-state} and \ref{eq:irreversible}. Explicitly, it would be the intersection of all the hyperplanes that represent rows in $\stoichmatint$ described by $S_{j*}\cdot \myvector{v} = 0$ and all half-spaces corresponding to irreversible reactions $\myvector{e_i} \cdot \myvector{v} \geq 0$ (where $\myvector{e_i}$ is the unit vector corresponding to reaction $i$). This intersection is an unbound convex polyhedron denoted the steady-state \emph{flux cone} or $\fluxcone$ \cite{marashi_analysis_2012}.

It is important to note, that the flux cone is unbounded in some directions, i.e. it can extend to infinity. Therefore, we typically ignore the absolute values of the fluxes (represented, for example, by $||\myvector{v}||$) and consider only the relative values -- i.e. the direction towards which the vector is pointing. It is often easier to only consider normalized vectors, such as ones whose biomass rate is set to 1.

\begin{eqnarray}\label{eq:fluxcone}
	&\textsf{\textbf{Flux cone}} &\nonumber\\
	&&\fluxcone = \{\myvector{v} \in \mathbb{R}^n~|~\stoichmatint\myvector{v} = 0 ~\wedge ~ v_i \geq 0~~\forall i \in I_{irr}\}~~~~~~~~~~~~~~~~~
\end{eqnarray}

\subsubsection{Elementary Flux Modes}
Elementary Flux Modes (EFMs) are defined as the set of all non-decomposable vectors in the flux cone. A non-decomposable vector is one whose \textit{support} (the set of reactions with non-zero flux) is minimal -- i.e. the flux cone does not contain any vectors whose support is a proper subset of it. EFMs also form a convex basis for the flux cone \cite{schilling_theory_2000}:
\begin{eqnarray}
\forall \myvector{v} \in \fluxcone:~ \exists \lambda_j \geq 0 ~~\text{s.t.}~~ \myvector{v} = \sum_j \lambda_j~\myvector{e}^j
\end{eqnarray}
where $\{\myvector{e}^j\}_j$ is the set of all EFMs. Figure \ref{fig:toy1} illustrates a model with 10 reactions and 9 EFMs.
\begin{figure}[ht!]
	\begin{center}
	\includegraphics[width=0.8\textwidth]{toy1}
	\caption{A toy example with 6 internal reactions and 4 exchange reactions. The flux cone is spanned by 9 EFMs.}\label{fig:toy1}
	\end{center}
\end{figure}

EFMs are closely related to Extreme Pathways (EPs) and distinguishing them requires more subtle definitions. For further reading you can refer to \cite{klamt_two_2003}.

\subsubsection{Classification of EFMs}
Even in our small toy example in Figure \ref{fig:toy1}, we already encounter a common problem that arises when dealing with EFMs -- i.e. cycles. In this example, it's quite obvious that the two EFMs that consist of combining two opposing reactions is futile and should be ignored. Larger networks, however, might have much larger cycles that are harder to identify and get rid of.

First, we need to distinguish between \textit{primary exchange} and \textit{currency exchange} reactions. The former are the standard reactions that exchange nutrients between the cell/compartment and its environment (sugar import, CO$_2$ export, etc.). \textit{Currency exchange} reactions do not, in fact, exchange material between compartments, but are rather abstract representations of energy dissipation. A canonical example would be the ATP maintenance reaction that is present in most metabolic models: ATP + H$_2$O $\rightharpoonup$ ADP + P$_i$.

Now, we can define the three types of EFMs (\cite{price_extreme_2002}, see Figure \ref{fig:efm_types}):
\begin{description}
	\item[I] -- \textit{Primary systemic EFMs} have at least one active primary exchange flux.
	\item[II] -- \textit{Futile cycles} have no active primary exchange fluxes, and at least one active currency exchange flux.
	\item[III] -- \textit{Internal cycles} have no active exchange fluxes (neither primary nor currency).
\end{description}

\begin{figure}[ht!]
	\begin{center}	\includegraphics[width=0.7\textwidth]{extreme_pathway_types}
	\caption{The three types of Elementary Flux Modes.}\label{fig:efm_types}
	\end{center}
\end{figure}

To illustrate the three types of pathways, we present another toy model with one currency exchange reaction and two primary exchange reactions (Figure \ref{fig:toy2}). This model contains all the three types of EFMs.
\begin{figure}[ht!]
	\begin{center}
	\includegraphics[width=0.5\textwidth]{toy2}
	\caption{A small toy model with 8 internal reactions, 2 primary exchange reactions, and one currency exchange reaction. In this simple network, one can identify all three types of extreme pathways. The Type I pathway (green) is typically the type of solution that most constrain-based models are seeking. The Type II pathway ($A \rightarrow B \rightarrow C \rightarrow A$, blue) is a typical futile cycle, since it does not involve any primary exchange reactions, but does \emph{waste} ATP. The Type III pathway ($A \rightarrow C \rightarrow D \rightarrow A$, red) is called internal since none of its reactions are exchange reactions. An internal cycle will never be thermodynamically feasible.
	}\label{fig:toy2}
	\end{center}
\end{figure}

In the next section, we will see how this three type classification is useful to separate thermodynamically feasible pathways from infeasible ones.

\subsection{Thermodynamics}\label{sec:thermodynamics}
A fundamental principle in enzymatic catalysis is the notion that enzymes can accelerate the rate of a reaction, but not change its thermodynamic equilibrium, therefore they have no effect on the \textit{direction} of flux.

\subsubsection{The Gibbs free energy of reaction}

The second law of thermodynamics states that entropy can never decrease over time in an isolated system. In living cells, which are open systems in constant pressure, temperature, and pH, entropy can be replaced by the concept of transformed Gibbs free energy of reaction \cite{alberty_biochemical_2006-1}, denoted $\Delta_r G'$. Then, for a chemical reaction to be feasible, it must impose a negative change in $\Delta_r G'$:
\begin{eqnarray}
\Delta_r G' < 0
\end{eqnarray}
For the purpose of constraint-based modeling, it is enough to understand how each $\Delta_r G'$ is affected by its reactant concentration. We typically use the assumption that biochemical reactions occur in dilute aqueous solutions (i.e. the activity coefficient of uncharged species is unity). In this case, the transformed Gibbs free energy of reaction $j$ is given by:
\begin{eqnarray}\label{eq:thermo1}
\Delta_r G'_j &=& \Delta_r G'^\circ_j + R\,T\cdot\ln(Q'_j) \\
Q'_j &=& \prod_i c_i^{S_{ij}}
\end{eqnarray}
where $R$ is the gas constant, $T$ is the temperature (in Kalvin), $Q'$ is the biochemical reaction quotient, $c_i$ is the molar concentration of a reactant (substrate of product) and $S_{ij}$ are the corresponding stoichiometric coefficients (i.e. a row in $\stoichmatint$). $\Delta_r G'^\circ_j$ represents that $\Delta_r G'_j$ in standard conditions, which is typically defined as 1 M for each one of the reactants. One way to measure $\Delta_r G'^\circ_j$, is to let the reaction run until it reaches equilibrium, and measure the concentrations of all reactants. The value of $Q'$ in equilibrium is called the \textit{equilibrium constant} and is denoted $K'$. Also, since know that at equilibrium $\Delta_r G'_j = 0$, we can solve for $\Delta_r G'^\circ_j$ and get:
\begin{eqnarray}
\Delta_r G'^\circ_j &=& -R\,T\cdot\ln(K'_j)
\end{eqnarray}
For further reading on thermodynamics of biochemical reactions, see \cite{alberty_recommendations_2011, noor_note_2013}.

Finally, we would like to rewrite equations \ref{eq:thermo1} in a more convenient way that corresponds well with our linear algebra notation. First, one can notice that taking the log from $Q'$ makes it a linear function of the log-concentration values: $\ln(Q'_j) = \sum_i S_{ij} \cdot \ln(c_i)$. In addition, from now on we will use a matrix notation for calculating the vector of all reaction Gibbs energies ($\myvector{\Delta_r G'}$):
\begin{eqnarray}
\myvector{\Delta_r G'} = \myvector{\Delta_r G'^\circ} + R\,T\cdot\stoichmat^\top \myvector{x}
\end{eqnarray}
where we define $\myvector{x}$ as the vector of log-concentrations, i.e. $x_i = \ln(c_i)$.

\subsubsection{Irreversibility is an approximation}
In pure physics terms, every chemical reaction should be reversible. There are cases, where the equilibrium is so far from unity, which makes it impossible for the reaction to reach equilibrium in practice. In these cases, it is sometimes convenient to assume that the reaction is irreversible and complete ignore the reverse direction. In defining the flux cone, this assumption can significantly reduce the solution space and number of EFMs.

Most thermodynamics-aware models, however, cannot make this assumption. Every reaction must have a defined equilibrium constant, no matter how extreme it might be. Therefore, from now on, we assume $\Delta_r G'^\circ_j$ has a finite value, even for reactions that are defined as irreversible in our model. There is no universal threshold for $\Delta_r G'^\circ_j$ which make a reaction irreversible. It is context dependent and a topic of much debate \cite{mavrovouniotis_identification_1993-1, henry_thermodynamics-based_2007, noor_integrated_2012}.

Beyond $\Delta_r G'^\circ_j$, probably the most important parameters for determining the reversibility are the range of concentrations allowed for each of the reactants. Basically, a reversible reaction should have both positive and negative values to its $\Delta_r G'_j$, depending on the chosen reactant concentrations within their predefined ranges. If we denote the vectors of lower and upper bounds as $\myvector{b}^L$ and $\myvector{b}^U$, respectively, the constraint on $\myvector{x}$ would be:
\begin{eqnarray}
\ln(\myvector{b}^L) \leq \myvector{x} \leq \ln(\myvector{b}^U)
\end{eqnarray}
It is easy to see, that the lowest value for $\Delta_r G'_j$ is achieved when all substrate concentrations are set to their value in $\myvector{b}^U$, and all product concentrations to $\myvector{b}^L$. The highest value of $\Delta_r G'_j$ occurs in the opposite extreme. Then, it become straightforward to check if a reaction is, by itself, reversible or not.

\subsubsection{Thermodynamic Feasibility of EFMs}
Given a specific elementary flux mode $\myvector{e}^j$ (or any flux vector $\myvector{v}$, for that matter), we would like to know whether it is thermodynamically feasible according to the second law. First, every one of the support reactions must be feasible in the direction defined by $\myvector{e}^j$. However, the same metabolite can be a substrate for one reaction, and a product for another and therefore there is inter-dependence between the $\Delta_r G'_j$ values of these reactions. Sometimes, even though a series of reactions can be individually feasible, their combination is infeasible. This has been termed a \textit{distributed bottleneck} by \textcite{mavrovouniotis_identification_1993-1} in 1993.

Using Linear Programming, it is relatively simple to test thermodynamic feasibility:
\begin{eqnarray}\label{eq:feasible_lp}
\textsf{Find}&\myvector{x}&\nonumber\\
\textsf{such that}&&\nonumber\\
\myvector{\Delta_r G'} &\equiv& \myvector{\Delta_r G'^\circ} + RT \cdot \stoichmat^\top \myvector{x} \nonumber\\
\myvector{\Delta_r G'} &<& 0\nonumber\\
\ln(\myvector{b}^L) &\leq& \myvector{x} ~\leq~ \ln(\myvector{b}^U)
\end{eqnarray}

\subsubsection{Max-min Driving Force}
In order to provide a more quantitative measure for the thermodynamic feasibility of a given pathway, the Max-min Driving Force (MDF) method provides a relatively simple measure. We adjust the Linear Program in Equation \ref{eq:feasible_lp}, by adding a margin variable ($B$) and maximizing its value:
\begin{eqnarray}
\textsf{MDF} &\equiv& \max_{B,\myvector{x}}{B} \nonumber\\
\textsf{such that}&&\nonumber\\
\myvector{\Delta_r G'} &\equiv& \myvector{\Delta_r G'^\circ} + RT \cdot \stoichmat^\top \myvector{x} \nonumber\\
\myvector{\Delta_r G'} &<& -B\nonumber\\
\ln(\myvector{b}^L) &\leq& \myvector{x} ~\leq~ \ln(\myvector{b}^U)
\end{eqnarray}
If the MDF is positive, then there exists a set of concentrations $\myvector{x}$ (inside the allowed range) such that $\myvector{\Delta_r G'} \leq -B < 0$, i.e. this pathway is thermodynamically feasible. The larger $B$ is, the ``more feasible'' it is, since we can keep all reactions farther away from equilibrium. From non-equilibrium thermodynamic theory, we know that reactions that are far from equilibrium tend to be more efficient (as described by the flux-force relationship \cite{beard_relationship_2007-1}). An example for MDF calculation is given in Figure \ref{fig:mdf}.

An online interface for running MDF analysis can be found here: \url{http://equilibrator.weizmann.ac.il/pathway/}.

\begin{figure}[ht!]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{mdf}
		\caption{(left) Schematic comparison between two pathways. Each pathway starts and ends with the same compounds, employs five enzymes and carries the same net flux. The kinetic parameters of all enzymes in both pathways, as well as enzyme and metabolite concentrations, are assumed to be identical. (right) Energetic profile of Embden-Meyerhof-Parnas glycolysis. Dashed black line corresponds to $\Delta_r G'^\circ$ values (metabolite concentrations of 1M) of pathway reactions at pH 7.5. Red line corresponds to $\Delta_r G'$ values of pathway reactions after an optimization procedure that maximizes the driving force of the thermodynamic bottleneck reactions. Figure is from \textcite{noor_pathway_2014}.
		}\label{fig:mdf}
	\end{center}
\end{figure}

\subsubsection{Thermodynamic Flux Balance Analysis (TFBA)}

Thermodynamic FBA (also known as Thermo\-dynamic-based Metabolic Flux Analysis \cite{henry_thermodynamics-based_2007}) was designed to deal with thermodynamically infeasible flux solutions within the framework of FBA:
\begin{eqnarray}
\textsf{\textbf{FBA}} && \nonumber\\
\myvector{v^*} &=& \mathrm{arg\max_v} {~\myvector{c}^\top\myvector{v}}\nonumber\\
\textsf{such that:} && \nonumber\\
\stoichmatint \myvector{v} &=& \myvector{0}  \label{eq:fba1} \\
\myvector{v}_{LB} &\leq& \myvector{v} ~\leq~ \myvector{v}_{UB} \label{eq:fba2}
\end{eqnarray}
where $\myvector{c} \in \mathbb{R}^r$ is the objective function, and the constants are the internal stoichiometric matrix $\stoichmatint \in \mathbb{R}^{m \times r}$. $\myvector{v}_{LB}$ and $\myvector{v}_{UB}$ are the lower and upper bounds on the fluxes, typically relevant only for exchange fluxes.

TFBA adds another two sets of variables -- the boolean flux indicators ($\myvector{y} \in \{0, 1\}^r$), and the log-concentrations ($\myvector{x} \in \mathbb{R}^r$). Then, the following constraints are added to the Linear Problem (which is now actually a Mixed-Integer Linear Problem -- MILP):
\begin{eqnarray}
\textsf{\textbf{TFBA}} && \nonumber\\
\myvector{v^*} &=& \mathrm{arg\max_v} {~\myvector{c}^\top\myvector{v}}\nonumber\\
\textsf{such that:} && \nonumber\\
\stoichmatint \myvector{v} &=& \myvector{0} \nonumber\\
\myvector{v}_{LB} &\leq& \myvector{v} ~\leq~ \myvector{v}_{UB} \nonumber\\
0 &\leq& M \myvector{y} - \myvector{v} ~\leq~ M  \label{eq:tfba1}\\
0 &<& M \myvector{y} + \myvector{\Delta_r G'} ~<~ M \label{eq:tfba2} \\
\myvector{\Delta_r G'} &=& \myvector{\Delta_r G'^\circ} + RT \cdot \stoichmat^\top \myvector{x} \label{eq:tfba3}\\
\ln(\myvector{b}^L) &\leq& \myvector{x} ~\leq~ \ln(\myvector{b}^U) \label{eq:tfba4}
\end{eqnarray}
The new constants are the vector of standard Gibbs energies of reaction $\myvector{\Delta_r G'^\circ}$ (in units of kJ/mol), the gas constant $R$ = 8.31 J/mol/K and temperature $T$ = 300 K. $M$ which is a very large number (larger than any of the possible flux and Gibbs free energy). Note also the difference between the internal stoichiometric matrix ($\stoichmatint$) used for the pseudo steady-state assumption, versus the full stoichiometric matrix ($\stoichmat$) used in the Gibbs free energy calculation.

Equations \ref{eq:tfba3}-\ref{eq:tfba4} should be familiar, and are exactly the same as in previous sections. To understand how the other two constraints (\ref{eq:tfba1}-\ref{eq:tfba2}) enforce thermodynamic feasibility, we consider three possible cases for each reaction $j$ separately:
\begin{enumerate}
	\item $v_j > 0$ : the only possible value that $y_j$ can have is 1, otherwise, $My_j - v_j$ would be negative and violate constraint \ref{eq:tfba1}. Therefore, constraint \ref{eq:tfba2} becomes $0 < M + \Delta_r G'_j < M$, which means that $\Delta_r G'_j < 0$.
	\item $v_j < 0$ : the only possible value that $y_j$ can have is 0, otherwise, $My_j - v_j$ would be larger than $M$ and violate constraint \ref{eq:tfba1}. Therefore, constraint \ref{eq:tfba2} becomes $0 < \Delta_r G'_j < M$, which means that $\Delta_r G'_j > 0$.
	\item $v_j = 0$ : both $0$ and $1$ are possible solutions for $y_j$. Therefore, there are no constraints on $\Delta_r G'_j$.
\end{enumerate}
Summarizing these 3 cases, one can concisely write:
\begin{eqnarray}
\forall j:v_j = 0~\vee~\text{sign}(v_j) = -\text{sign}(\Delta_r G'_j)\,,
\end{eqnarray}
which is exactly the second law of thermodynamics.

\subsubsection{Loopless Flux Balance Analysis (ll-FBA)}
The loopless algorithm \cite{schellenberger_elimination_2011} is very similar to TFBA, except that there are no actual thermodynamic values. This way, thermodynamically infeasible internal (Type III) cycles are eliminated, while all other pathways are kept \cite{noor_proof_2012}. The set of equations describing ll-FBA are:
\begin{eqnarray}
\textsf{\textbf{ll-FBA}} && \nonumber\\
\myvector{v^*} &=& \mathrm{arg\max_v} {~\myvector{c}^\top\myvector{v}}\nonumber\\
\textsf{such that:} && \nonumber\\
\stoichmatint \myvector{v} &=& \myvector{0} \label{eq:llfba1} \\
\myvector{v}_{LB} &\leq& \myvector{v} ~\leq~ \myvector{v}_{UB} \\
0 &\leq& M \myvector{y} - \myvector{v} ~\leq~ M
\label{eq:llfba2} \\
0 &<& M \myvector{y} + \myvector{\Delta_r G'} ~<~ M \label{eq:llfba3} \\
\myvector{\Delta_r G'} &\in& \ker{(\stoichmat)}^\perp \label{eq:llfba5}
\end{eqnarray}
here, $\myvector{\Delta_r G'}$ is not constrained by the $\myvector{\Delta_r G'}^\circ$ and the metabolite concentrations, but is only required to be orthogonal to the null-space of $\stoichmat$ (or, equivalently, to be in $\mathrm{image}(\stoichmat^\top)$).

Why does ll-FBA not eliminate type II cycles from the set of flux solutions? These futile cycles ``waste'' resources such as ATP, but are thermodynamically feasible. They are only considered ``cycles'' because we chose to give co-factors a special status (namely, they are external metabolites that do not need to be kept at steady-state). In other words, type II EFMs are in the null-space of $\stoichmatint$, but not in the null-space of $\stoichmat$ (just like type I EFMs). Therefore, it is possible to assign negative $\Delta_r G'$ values to all reactions in a type II EFMs.

Interestingly, this argument can be applied also to the reverse of such futile cycle. Running a futile cycle in reverse is sometimes called an Energy Generating Cycle (EGC), and is obviously unrealistic. Furthermore, unlike type III cycles that do not affect the biomass rate in standard FBA, EGCs have the potential to increase the maximal biomass yield and pose a more serious problem in FBA models \cite{fritzemeier_erroneous_2017-2}. This is one of the cases where TFBA differs from ll-FBA, as it prevents the use of EGCs completely, while ll-FBA doesn't.

\subsection{Metabolic engineering algorithms}

\subsubsection{Phenotypic Phase Plane (PPP)}
This useful concept derived from FBA, is a term for 2D projections on the flux polytope. Typically, one chooses specific reaction (e.g. exchange of succinate) for the y-axis and the FBA objective (i.e. biomass rate) for the x-axis. The projection of the flux polytope onto this plane creates a convex polygon shape and helps to visualize how important a certain flux is for creating biomass. Often for cases where the selected flux is a fermentation product, the PPP is referred to as a \textit{production envelope}. It is standard practice to present strain-design results by overlaying two or more PPPs, e.g. a knockout strain versus the wild-type (Figure \ref{fig:ppp}). Using such comparisons, it is easy to see if the knockout is useful for forcing a cell to produce the desired chemical byproduct, by coupling its production to the biomass rate.

\begin{figure}[ht!]
	\begin{center}
		\includegraphics[width=0.7\textwidth]{ppp}
		\caption{Phenotypic phase planes for anaerobic succinate production from glucose based on the \textit{E. coli} core model. (a) wild-type strain (light gray) vs. triple-deletion mutant (ACKr, ATPS4r, FUM) resulting in a design without growth-coupling (purple). (b) wild-type strain vs. triple-deletion mutant (ACALD, PYK, ME2) resulting in a partially growth-coupled design. (c) wild-type strain vs. double-deletion mutant (ACALD, LDH\_D) resulting in a fully growth-coupled design. Figure is from \textcite{machado_co-evolution_2015}.}\label{fig:ppp}
	\end{center}
\end{figure}

\subsubsection{OptKnock and its derivatives}
\textcite{burgard_optknock_2003-1} phrased the OptKnock optimization problem first as a bi-level mixed-integer linear optimization problem. The inner problem is standard FBA, maximizing an objective function $\myvector{c}_\textsf{in}^\top \myvector{v}$. This encodes the assumption that each knockout strain we consider for the outer problem, evolves to maximize the objective (which is typically set to be the biomass function). Then, we choose the one knockout that maximizes the outer objective $\myvector{c}_\textsf{out}^\top \myvector{v}$ -- typically the secretion rate of the desired product (e.g. succinate).
\begin{eqnarray}
\textsf{\textbf{OptKnock}} && \nonumber\\
\max_{\myvector{y}}~~\myvector{c}_{\textsf{out}}^\top \myvector{v}&&\nonumber\\
\textsf{subject to}&&\nonumber\\
&\max_{\myvector{v}}~\myvector{c}_{\textsf{in}}^\top \myvector{v}&\nonumber\\
&\textsf{subject to}&\nonumber\\
&&\stoichmatint \myvector{v} = \myvector{0} \nonumber\\
&&v_{\textsf{LB},\,j} \cdot y_j \leq v_j \leq y_j \cdot v_{\textsf{UB},\,j} \nonumber\\
&\myvector{y} \in \{0, 1\}^r \nonumber\\
&\sum_j{(1-y_j)} \leq K & \label{eq:optknock}
\end{eqnarray}
where, $K$ is the maximum number of allowed knockouts. The inner LP is a standard FBA optimization problem, except that a few of the reactions are ``knockout out'' -- when a reaction indicator is set to 0 by the external LP it forces the corresponding flux to zero, i.e. $y_j = 0 \rightarrow v_j = 0$. In addition, we set $v_{\textsf{LB},\,biomass} > 0$ and $y_{biomass} = 1$, since we only want to consider mutants that show at least some minimal growth. Finally, the external LP places an upper bound on the total number of knockouts, which is useful for two reasons: (1) making sure we don't have too many knockouts that would make the genetic manipulation impractical, and (2) to reduce the computational load required to solve the MILP. Nevertheless, even for small $K$, solving the bi-level MILP  directly is not tractable for large networks (such as the genome-scale \textit{E. coli} model). Fortunately, \textcite{burgard_optknock_2003-1} provided a solution, by converting into a single-level MILP using duality theory.

The dual of the inner optimization problem in \ref{eq:optknock} is:
\begin{eqnarray}
\min_{\myvector{\lambda},\myvector{\mu},\myvector{\omega}} ~\sum_j y_j (v_{\textsf{UB},\,j} \cdot \mu_j - v_{\textsf{LB},\,j} \cdot \omega_j)&\nonumber\\
\textsf{subject to}&\nonumber\\
&\stoichmatint^\top \myvector{\lambda} + \myvector{\mu} - \myvector{\omega} \geq \myvector{c}_\textsf{in} \nonumber\\
&\myvector{\mu}, \myvector{\omega} \geq 0\,.
\end{eqnarray}
Since duality theory states if the optimums solutions of the primal and dual problems are bounded, their objective function values must be equal to one another, so ensuring optimality can be achieved by equating the two objectives: $\myvector{c}_\textsf{in}^\top \myvector{v} = \sum_j y_j (v_{\textsf{UB},\,j} \cdot \mu_j - v_{\textsf{LB},\,j} \cdot \omega_j)$. However, using this equation as a constraint in the outer optimization problem would make it non-linear, since it contains products of two variables ($y_j \cdot \mu_j$ and $y_j \cdot \omega_j$). We can, however, use the fact that $y_j$ are binary in order to cast this expression into a linear form.

First, we introduce a new set of auxiliary variables $\myvector{m} \in \mathbb{R}^r$, and constrain them to be $m_j = y_j \cdot \mu_j$ by adding the following:
\begin{eqnarray}
	0 \leq &m_j& \leq y_j \cdot M \nonumber\\
	\mu_j - (1 - y_j) \cdot M \leq &m_j& \leq \mu_j\,.
\end{eqnarray}
As before, $M$ is a constant larger than any possible value of $\mu_j$. Considering the two cases for $y_j$, one can easily see that $y_j = 0 ~\rightarrow~ m_j = 0$ and $y_j = 1 ~\rightarrow~ m_j = \mu_j$, which is what we wanted to achieve. We do exactly the same for $\myvector{\omega}$ and another set of variables $\myvector{u} \in \mathbb{R}^r$. Then we can rewrite the dual objective as: $\sum_j y_j (v_{\textsf{UB},\,j} \mu_j - v_{\textsf{LB},\,j} \omega_j) = \myvector{v}_\textsf{UB}^\top \myvector{m} - \myvector{v}_\textsf{LB}^\top\myvector{u}$.

Using this notion, we get a single-level MILP problem for OptKnock:
\begin{eqnarray}\label{eq:optknock2}
\textsf{\textbf{OptKnock}} & \nonumber\\
\max_{\myvector{y},\myvector{v},\myvector{\lambda},\myvector{\mu},\myvector{\omega},\myvector{m},\myvector{u}}~~\myvector{c}_\textsf{out}^\top \myvector{v}&\nonumber\\
\textsf{subject to}&\nonumber\\
&\stoichmatint \myvector{v} = \myvector{0} \nonumber\\
&v_{\textsf{LB},\,j} \cdot y_j \leq v_j \leq y_j \cdot v_{\textsf{UB},\,j} \nonumber\\
&\myvector{c}_\textsf{in}^\top \myvector{v} = \myvector{v}_\textsf{UB}^\top \myvector{m} - \myvector{v}_\textsf{LB}^\top\myvector{u} \nonumber\\
&\myvector{c}_\textsf{in} \leq \stoichmatint^\top \myvector{\lambda} + \myvector{\mu} - \myvector{\omega} \nonumber\\
&0 \leq \myvector{\mu}, \myvector{\omega} \nonumber\\
&0 \leq \myvector{m} \leq \myvector{y} \cdot M \nonumber\\
&\myvector{\mu} - (1 - \myvector{y}) \cdot M \leq \myvector{m} \leq \myvector{\mu} \nonumber\\
&0 \leq \myvector{u} \leq \myvector{y} \cdot M \nonumber\\
&\myvector{\omega} - (1 - \myvector{y}) \cdot M \leq \myvector{u} \leq \myvector{\omega} \nonumber\\
&\sum_j{(1-y_j)} \leq K
\end{eqnarray}

\subsubsection{Derivatives of OptKnock}

A comprehensive review by \textcite{machado_co-evolution_2015} lists the many published algorithms for \textit{in silico} strain design that followed after OptKnock (see Figure \ref{fig:optknock_derivatives}).
\begin{figure}[ht!]\label{fig:optknock_derivatives}
	\begin{center}
		\includegraphics[width=0.75\textwidth]{optknock_derivatives.png}
	\end{center}
	\caption{A timeline of all constraint-based strain design methods that followed OptKnock (figure is from \cite{machado_co-evolution_2015}).}
\end{figure}

Robust\-Knock is an improvement for Opt\-Knock, introduced by \textcite{tepper_predicting_2010-1} in 2010, which accounts for competing pathways that might reduce the chemical production rates. One common issue with standard Opt\-Knock, is that it only looks at the maximal rate of the chemical production rate: $\max \myvector{c}_\textsf{out}^\top \myvector{v}$. However, as visualized by the production envelope in Figure \ref{fig:ppp}a, there might be redundancy in the solution space, where the same biomass rate can be achieved without any production of the desired chemical. Robust\-Knock addresses this problem by performing a max-min optimization, i.e. maximizing the lowest value of $\myvector{c}_\textsf{out}^\top \myvector{v}$ over all $\myvector{v}$ that satisfy the inner LP. This method yields solutions like in Figure \ref{fig:ppp}b-c.

\printbibliography

\end{document}


\subsection{Expected utility}

If an agent faces an uncertain world they can make decisions under uncertainty. For example, how would an agent value £10 relative to a 50% chance of £0 and a 50% chance of £20?

There are many different attitudes an agent could have – we need a form which can capture these. A standard approach is expected utility.

We start by taking

\(E[u(x)]=\)

Subjective expected utility


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS622: Theory of Formal Languages
% Copyright 2014 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 1}

Let $A = \{a,b\}$.
Construct the minimal deterministic finite automaton that is able to accept the language $aA^*b$.

\subsection*{Solution}

Based on Hopcraft's Algorithm \cite{hopcroft1971n}, we start by $Q_0 = L = \{aA^*b\}$ and apply symbols $\{a,b\}\in A$ to go to $a^{-1}L$ and $b^{-1}L$ respectively.
We take advantage of the following property of prefixes.

\begin{equation}\label{eq1}
a^{-1}LK = (a^{-1}L)K \cup (L \cap \{\lambda\})a^{-1}K
\end{equation}

Let $L$ and $K$ be defined as $aA^*$ and $b$, respectively.
Using Eq. \ref{eq1}, we'll have

\begin{equation}\label{eq2}
\begin{aligned}
a^{-1}aA^*b &= (a^{-1}aA^*)b \cup (aA^* \cap \{\lambda\})a^{-1}b\\
&= A^*b \cup \emptyset \emptyset\\
&= A^*b
\end{aligned}
\end{equation}

\begin{equation}\label{eq3}
\begin{aligned}
b^{-1}aA^*b &= (b^{-1}aA^*)b \cup (aA^* \cap \{\lambda\})b^{-1}b\\
&= \emptyset \cup \emptyset \lambda\\
&= \emptyset
\end{aligned}
\end{equation}

Therefore, $Q_1 = \{aA^*b, A^*b, \emptyset\}$.
In a similar fashion,

\begin{equation}
\begin{aligned}
a^{-1}A^*b &= (a^{-1}A^*)b \cup (A^* \cap \{\lambda\})a^{-1}b\\
&= A^*b
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
b^{-1}A^*b &= (b^{-1}A^*)b \cup (A^* \cap \{\lambda\})b^{-1}b\\
&= A^*b \cup \{\lambda\}
\end{aligned}
\end{equation}

and evidently, $a^{-1}\emptyset = b^{-1}\emptyset = \emptyset$.
Therefore, $Q_2 = Q_1 \cup \{A^*b \cup \{\lambda\}\}$.

\begin{equation}
\begin{aligned}
a^{-1}(A^*b\cup\{\lambda \}) &= a^{-1}A^*b \cup a^{-1}\lambda\\
&= A^*b
\end{aligned}
\end{equation}

\begin{equation}
\begin{aligned}
b^{-1}(A^*b\cup\{\lambda \}) &= b^{-1}A^*b \cup b^{-1}\lambda\\
&= A^*b \cup \{\lambda \}
\end{aligned}
\end{equation}

Thus, $Q_3 = Q_2 = Q_L = \{aA^*b, \emptyset, A^*b, A^*b\cup\{\lambda \} \}$.
$Q_L$ would be set of states of the machine $\mathcal{M}_L$ that can recognize the language $aA^*b$.
The automaton $\mathcal{M}_L$ is defined by Table \ref{tab1}.
Also, Figure \ref{fig1} depicts graph of $\mathcal{M}_L$ where $q_0$, $q_1$, $q_2$ and $q_3$ represent $\emptyset$, $aA^*b$, $A^*b$ and $A^*b\cup\{\lambda \}$, respectively.

\begin{table}\centering
	\begin{tabular}[H!]{|c||c|c|c|c|}
		\hline
		Input & $aA^*b$ & $A^*b$ & $\emptyset$ & $A^*\cup \{\lambda \}$\\
		\hline
		a & $A^*b$ & $A^*b$& $\emptyset$& $A^*b$\\
		b & $\emptyset$ & $A^*b\cup \{\lambda \}$& $\emptyset$ & $A^*b \cup \{\lambda \}$\\
		\hline
	\end{tabular}
	\caption{Directed graph of the automaton $\mathcal{M}_L$}\label{tab1}
\end{table}

\begin{figure}\centering
	\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,semithick]
		\tikzstyle{final}=[circle,thick,draw=black,fill=gray!40,text=black]
		\node[state,initial]	(1) 					{$q_1$};
		\node[state]			(0) [left of = 1]		{$q_0$};
		\node[state]			(2) [right of = 1]		{$q_2$};
		\node[state, final]		(3) [right of = 2]		{$q_3$};
		\path
			(0) edge [loop left]	node {a,b}	 (0)
			(1) edge [bend left]	node {b}	 (0)
				edge [bend left]	node {a}	 (2)
			(2) edge [bend left]	node {b}	 (3)
				edge [loop above]	node {a}	 (2)
			(3) edge [loop right]	node {b}	 (3)
				edge [bend left]	node {a}	 (2);
	\end{tikzpicture}
	\caption{Directed graph of the automaton $\mathcal{M}_L$}\label{fig1}
\end{figure}


\documentclass[twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{outlines}
\usepackage{amsmath}
\usepackage{gensymb}
\title{Ch. 22 Notes}
\author{John Yang}
\setcounter{section}{+20}

\begin{document}
\maketitle
\section{Sinusoidal AC Circuit Analysis}
\subsection{Representations of a complex variable}
\begin{outline}
\1 Rectangular, Polar, Exponential \[z=x+iy=r\angle\theta=re^{i\theta}\] where \[x=r\cos\theta\]\[y=r\sin\theta\]\[\tan\theta=\dfrac{\text{Im}}{\text{Re}}=\dfrac{y}{x}\]\[z=r\angle\theta=r\cos\theta+ir\sin\theta=re^{i\theta}\]

\end{outline}
\subsection{Arithmetic Operations with Complex Variables}
\begin{outline}
\1 Adding and subtracting: Add real and add imaginary separately
\1 Multiplication: \[z_1z_2=r_1r_2e^{i(\theta_1+\theta_2)}\]\[z_1z_2=(r_1\angle\theta_1)(r_2\angle\theta_2)=r_1r_2\angle(\theta_1+\theta_2)\]\[z_1z_2=(x_1+iy_1)(x_2+iy_2)\]\[=(x_1x_2-y_1y_2)+i(x_1y_2+x_2y_1)\]
\1 Division: \[\dfrac{z_1}{z_2}=\dfrac{r_1e^{i\theta_1}}{r_2e^{i\theta_2}}=\dfrac{r_1}{r_2}\angle(\theta_1-\theta_2)\]\[\dfrac{z_1}{z_2}=\dfrac{r_1\angle\theta_1}{r_2\angle\theta_2}=\dfrac{r_1}{r_2}\angle(\theta_1-\theta_2)\]
\1 Complex conjugates: \[z=x+iy\]\[z^*=x-iy\]
\end{outline}
\subsection{Complex potential differences and currents: Phasors}
\begin{outline}
\1 Sinusoidally oscillating potential differences and currents: \[V(t)=V_0\cos(\omega t+\theta)\]\[I(t)=I_0\cos(\omega t+\phi)\]
\end{outline}
\subsection{The Potential difference and current Phasors for resistors, Inductors, and Capacitors}
\begin{outline}
\1 Resistor: \[V=IR\]\[V(t)=RI(t)\]
\1 Inductor: \[V=L\dfrac{dI}{dt}\]
\1 Capacitor: \[C=\dfrac{Q}{V}\]\[I=C\dfrac{dV}{dt}\]
\1 Impedances: \[V=IZ\]
    \2 Resistor: \[Z_R=R\]
    \2 Inductor: \[Z_L=i\omega L\]
    \2 Capacitor: \[Z_C=\dfrac{1}{i\omega C}\]
\1 Impedance is measured in Ohms. Impedances for capacitors and inductors are imaginary numbers. Impedance is not a phasor.
\end{outline}
\subsection{Series and parallel combinations of impedances}
\begin{outline}
\1 Impedances in series combine like resistors in series
\1 Impedances in parallel combine like resistors in parallel
\end{outline}
\subsection{Complex independent AC voltage sources}
\begin{outline}
\1 Complex voltage sources: \[V_{\text{source}}(t)=V_0\cos(\omega t)+iV_0\sin(\omega t)\]\[V_{\text{source}}(t)=V_0e^{i(\omega t)}\]\[V_{\text{source}}(t)=V_0\angle(\omega t)\]
\end{outline}
\subsection{Power absorbed by circuit elements in AC Circuits}
\begin{outline}
\1 Average power absorbed by a circuit element: \[\langle P\rangle=\dfrac{1}{2}V_0I_0\cos\beta\]where\[\beta=\theta-\phi\]
\1 $\cos\beta$ is the power factor.
\1 Peak values divided by $\sqrt 2$ are known as effective values of potential difference and current, also known as rms values. \[V_{\text{rms}}=\dfrac{V_0}{\sqrt 2}\]\[I_{\text{rms}}=\dfrac{I_0}{\sqrt 2}\]\[\langle P\rangle=V_{\text{rms}}I_{\text{rms}}\cos\beta\]
\1 Multimeters read rms values and not peak values
\1 For resistors, $\cos\beta=1$
\1 For capacitors, the average power is 0 because \(\beta=-\dfrac{\pi}{2}\)
\1 For inductors, the average power is 0 because \(\beta=\dfrac{\pi}{2}\)
\end{outline}
\subsection{A Filter circuit}
\begin{outline}
\1 Filter circuits let certain frequencies pass relatively unimpeded and filter out or eliminate one or another range of frequencies.
\end{outline}
\subsection{A Series RLC Circuit}
\begin{outline}
\1 For fixed $L$ and $C$, the numerical value of the resistance affects the shape of the graph of $\langle P\rangle$ vs. $\omega$. The smaller the resistance $R$, the more sharply peaked the curve.
\end{outline}

\end{document}


\documentclass{stdlocal}
\begin{document}
\section{Mathematical Proofs} % (fold)
\label{sec:proofs}
  \begin{lemma*}[Monte Carlo Integration Estimates Value of Integral]
    Choose the same setting as in the above definition \ref{definition:monte-carlo-integration}.
    In this case for all $n\in\setNatural$, the Monte Carlo integration $\mathrm{MCI}_n(f)$ is a Monte Carlo method and the following statements for the expectation value and standard deviation are fulfilled.
    \[
      \expect \mathrm{MCI}_n(f) = \integral{U}{}{f}{λ}
      \separate
      \stddev\boxBrackets{\mathrm{MCI}_n(f)} \leq \sqrt{\frac{λ(U)}{n} \integral{U}{}{f^2}{λ}}
    \]
  \end{lemma*}
  \begin{proof}[Lemma \ref{lemma:monte-carlo-integration} on page \pageref{lemma:monte-carlo-integration}]
    Let $p$ be the probability density of $X_n$.
    Because the random variables are uniformly distributed on $U$, we can express it as follows.
    \[
      \function{p}{U}{[0,\infty)}
      \separate
      p(x) \define \frac{1}{λ(U)}
    \]
    By using substitution and chaining from propositions \ref{proposition:substitution} and \ref{proposition:chaining}, the expectation value can be directly computed.
    \[
      \begin{aligned}[t]
        \expect \mathrm{MCI}_n(f)
        &= \expect \boxBrackets{ \frac{λ(U)}{n} \sum_{k=1}^n f\circ X_k }
        = \frac{λ(U)}{n} \sum_{k=1}^n \expect(f\circ X_k) \\
        &= λ(U) \integral{U}{}{f(x) p(x)}{λ(x)}
        = \integral{U}{}{f}{λ}
      \end{aligned}
    \]
    For the standard deviation, first the variance will be observed.
    Since the sequence of random variables is stochastically independent, the sum can be taken out of the argument.
    Afterwards, we again apply substitution and chaining.
    \[
      \begin{aligned}
        \var \mathrm{MCI}_n(f) &= \var\boxBrackets{ \frac{λ(U)}{n} \sum_{k=1}^n f\circ X_k } = \frac{λ(U)^2}{n^2} \sum_{k=1}^n \var\roundBrackets{f\circ X_k} \\
        &= \frac{λ(U)^2}{n^2} \sum_{k=1}^n \expect\roundBrackets{f\circ X_k}^2 - \boxBrackets{\expect\roundBrackets{f\circ X_k}}^2 \\
        &\leq \frac{λ(U)^2}{n^2} \sum_{k=1}^n \expect\roundBrackets{f\circ X_k}^2 = \frac{λ(U)^2}{n} \integral{U}{}{f^2(x) p(x)}{λ(x)} \\
        &= \frac{λ(U)}{n} \integral{U}{}{f^2}{λ}
      \end{aligned}
    \]
    The inequality is now inferred by the definition of the standard deviation which proofs the lemma.
    \[
      \stddev\boxBrackets{\mathrm{MCI}_n(f)} = \sqrt{\var \mathrm{MCI}_n(f)} \leq \sqrt{\frac{λ(U)}{n} \integral{U}{}{f^2}{λ}}
    \]
  \end{proof}

  \begin{lemma*}[Pseudorandom Sequences are Ultimately Periodic]
    Let $\mathscr{G}\define (S,T,U,G)$ be a PRNG and $s_0\in S$ its initial state.
    Then the respective pseudorandom sequence $(u_n)_{n\in\setNatural}$ is ultimately periodic.
    In this case, for the period ρ and the transient τ the following holds.
    \[
      1 \leq ρ + τ - 1 \leq \# S
    \]
    In particular, if $T$ is bijective $(u_n)$ will be periodic.
  \end{lemma*}
  \begin{proof}[Lemma~\ref{lemma:pseudorandom-sequences-periodicity} on page \pageref{lemma:pseudorandom-sequences-periodicity}]
    Let $(s_n)_{n\in\setNatural}$ be the respective sequence of states and $N\define \# S$ the number of different states.
    $T$ maps all elements of $S$ to at most $N$ other elements of $S$.
    Therefore at least the element $s_N$ has to be mapped to an element $s_k$ for $k\in\setNatural$ with $k\leq N$ which was already reached.
    % Hence, there exist $n,k\in\setNatural$ with $k\leq n\leq N$ such that $T(s_n) = s_k$.
    Hence, we conclude the following.
    \[
      \exists n,k\in\setNatural, k\leq n\leq N: \quad T(s_n) = s_k
    \]
    % Assume $T$ maps $s_n$ to a state $s_k$ with $k\in\setNatural$ and $k < n$.
    We choose $n$ and $k$ appropriately and define the following values.
    \[
      ρ \define n - k + 1
      \separate
      τ \define k
    \]
    Now let $i\in\setNatural_0$ be arbitrary and apply the definition.
    We get the following chain of equations which show that $(u_n)$ is ultimately periodic.
    \[
      \begin{aligned}
        u_{τ+i+ρ} &= u_{n+1+i} = G \circ T^{n+1+i}(s_0) = G \circ T^i\circ T^{n+1}(s_0) \\
        &= G \circ T^i(s_k) = G \circ T^i \circ T^k(s_0) = G \circ T^{i+k}(s_0) = u_{k+i} = u_{τ+i}
      \end{aligned}
    \]
    The inequality can be shown by directly inserting the values into the definition.
    \[
      1 \leq ρ + τ - 1 = n \leq N = \# S
    \]
    This proofs the given lemma.
  \end{proof}

  \begin{lemma*}[Equidistributed Pseudorandom Sequences]
    Let $\mathscr{G}\define (S,T,U,G)$ be a PRNG with $s_0\in S$ as its seed value and $(u_n)_{n\in\setNatural}$ the respective pseudorandom sequence with transient τ and period ρ.
    Furthermore, let μ be a probability measure on $(U,\mathscr{P}(U))$.
    Then the following statements are equivalent.
    \begin{enumerate}[label=(\roman*)]
      \item $(u_n)$ is equidistributed with respect to μ.
      \item For all $u\in U$ the following is true.
        \[
          \frac{1}{ρ} \cdot \#\set{n\in\setNatural}{τ\leq n < ρ+τ, u_n = u} = μ(\set{u}{})
        \]
    \end{enumerate}
  \end{lemma*}
  \begin{proof}[Lemma~\ref{lemma:equidistribution} on page \pageref{lemma:equidistribution}]
    Because $U$ is a finite set, every measurable function $\function{X}{U}{\setReal}$ can be described as a linear combination of characteristic functions with respect to some real coefficients $α_u$ for all $u \in U$ in the following way.
    \[
      X = \sum_{u\in U} α_u \mathds{1}_{\set{u}{}}
    \]
    Hence, without loss of generality, it suffices to take only characteristic functions into account.
    Let $u\in U$ be arbitrary.
    The right-hand side of the definition will then result in the following.
    \[
      \integral{U}{}{\mathds{1}_{\set{u}{}}}{μ} = μ(\set{u}{})
    \]
    Applying the characteristic function together with the properties of a periodic sequence to the left-hand side of the definition, looks as follows.
    \[
      \begin{aligned}
        \lim_{n\to\infty} \frac{1}{n} \sum_{k=1}^n \mathds{1}_{\set{u}{}}(u_k)
        &= \lim_{n\to\infty} \frac{1}{n} \sum_{k=1}^{τ-1} \mathds{1}_{\set{u}{}}(u_k) + \lim_{n\to\infty} \frac{1}{n}\sum_{k=τ}^{τ+n-1} \mathds{1}_{\set{u}{}}(u_k) \\
        &= \frac{1}{ρ} \sum_{k=τ}^{τ+ρ-1} \mathds{1}_{\set{u}{}}(u_k) \\
        &= \frac{1}{ρ} \cdot \#\set{n\in\setNatural}{τ\leq n < ρ+τ, u_n = u}
      \end{aligned}
    \]
    This shows the desired equivalence and proofs the lemma.
  \end{proof}

  \begin{lemma*}[Corresponding Vector Sequences are Ultimately Periodic]
    Let $U$ be a non-empty set of values and $(u_n)_{n\in\setNatural}$ be an ultimately periodic sequence in $U$ with period ρ and transient τ.
    In this case, every corresponding $k$-dimensional vector sequence $(v_n)_{n\in\setNatural}$ with translation $t$ is ultimately periodic with period $ρ'$ and transient $τ'$ defined as follows.
    \[
      ρ' \define \frac{ρ}{\mathrm{gcd}(ρ,k)}
      \separate
      τ' \define \ceilBrackets{\frac{\max(0,τ-1-t)}{k}} + 1
    \]
  \end{lemma*}
  \begin{proof}[Lemma \ref{lemma:vector-sequences-periodicity} on page \pageref{lemma:vector-sequences-periodicity}]
    Choose $n\in\setNatural_0$ and $i\in\setNatural$ with $i\leq k$ to be arbitrary.
    We denote with $v^{(i)}_n$ the $i$.~coordinate of the $n$.~vector.
    By definition the following equality holds.
    \[
      v^{(i)}_{τ' + n + ρ'} = u_{t + (τ'+n+ρ'-1)k + i}
    \]
    Observing the index, we separate it into three parts.
    One for the index, one for the transient one for the period.
    \[
      t+(τ' + n + ρ' - 1)k + i = \underbrace{(t + τ'k - k + 1)}_{\reverseDefine \tilde{τ}} + \underbrace{(nk + i - 1)}_{\reverseDefine \tilde{n}}  + \underbrace{ρ'k}_{\reverseDefine \tilde{ρ}}
    \]
    The period part has to be a multiple of the period ρ of $(u_n)$ as can be seen in the following.
    Hence, $\tilde{ρ}$ has the property of a period.
    \[
      \tilde{ρ} = ρ'k = \frac{ρk}{\mathrm{gcd}(ρ,k)} = ρ \frac{k}{\mathrm{gcd}(ρ,k)}
    \]
    To apply the periodicity of $(u_n)$, the transient part has to be bigger or equal to the transient τ of $(u_n)$.
    \[
      \tilde{τ} = t + τ'k - k + 1 = 1 + t + k \ceilBrackets{\frac{\max(0,τ-1-t)}{k}} \geq τ
    \]
    Inserting the results and applying the periodicity of $(u_n)$, we can conclude that the corresponding vector sequence has to be ultimately periodic as well.
    \[
      v^{(i)}_{τ' + n + ρ'} = u_{\tilde{τ} + \tilde{n} + \tilde{ρ}} = u_{\tilde{τ} + \tilde{n}} = u_{t + (τ' + n - 1)k + i} = v^{(i)}_{τ' + n}
    \]
    Due to the shown statements, $ρ'$ and $τ'$ are indeed the smallest possible values such that this equation holds and can therefore be denoted as period and transient of $(v_n)$ respectively.
  \end{proof}
% section proofs (end)
\end{document}


%%%%%%%%%%%%%%%% arm kinematics %%%%%%%%%%%%%%

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\columnwidth]{chapters/img/robot_arm.png}
    \caption{Placeholder image for the robotic arm?.}
    \label{fig:robotic_manipulator_general}
\end{figure}

\section*{Manipulator Kinematics} %Niklas
Kinematics is what describes the motion of rigid bodies and points in space.

\subsection*{Backgroud}
\subsection*{Rigid body transformation}
In general any position can be described by translation along three axes and rotation along these same axes. These translations/rotations can be described by a matrix
\begin{equation}
    T =
    \begin{bmatrix}
        R & d \\
        \bf{0} & 1
    \end{bmatrix}
\end{equation}
where \(R\) is a 3 x 3 rotation matrix and \(d\) a 3 x 1 translation matrix. This implies that any transformation could be characterized by six parameters, three for the translation and three for the rotation. \cite{spong}

\subsection*{Denavit-Hartenberg Convention}
A common approach for selecting the coordinate frames of reference for each joint in a robotic arm is the Denavit-Hartenberg convention. This allows the transformation matrix for each joint to be expressed as
\begin{equation}
    A_i = Rot_{z}(\theta_i) \cdot Trans_{z}(d_i) \cdot Trans_{x}(a_i) \cdot Rot_{z}(\alpha_i)
    \label{eqn:DH-transformation}
\end{equation}
that consists of the four basic transformations
\begin{equation}
    Rot_z(\theta_i) =
    \begin{bmatrix}
        cos(\theta_i) & -sin(\theta_i) & 0 & 0 \\
        sin(\theta_i) & cos(\theta_i) & 0 & 0 \\
        0 & 0 & 1 & 0 \\
        0 & 0 & 0 & 1
    \end{bmatrix}
\end{equation}
\begin{equation}
    Trans_z(d_i) =
    \begin{bmatrix}
        1 & 0 & 0 & 0 \\
        0 & 1 & 0 & 0 \\
        0 & 0 & 1 & d_i \\
        0 & 0 & 0 & 1
    \end{bmatrix}
\end{equation}
\begin{equation}
    Trans_x(\alpha_i) =
    \begin{bmatrix}
        1 & 0 & 0 & \alpha_i \\
        0 & 1 & 0 & 0 \\
        0 & 0 & 1 & 0 \\
        0 & 0 & 0 & 1
    \end{bmatrix}
\end{equation}
\begin{equation}
    Rot_z(\theta_i) =
    \begin{bmatrix}
        1 & 0 & 0 & 0 \\
        0 & cos(\alpha_i) & -sin(\alpha_i) & 0 \\
        0 & sin(\alpha_i) & cos(\alpha_i) & 0 \\
        0 & 0 & 0 & 1
    \end{bmatrix}.
\end{equation}

Where the parameters \(\theta_i\), \(a_i\), \(d_i\) and \(\alpha_i\), known as DH-parameters, characterize each joint. To allow the transformation for each joint to be represented by only four parameters, compared to the six parameters that is required in general, there are some restrictions on how the coordinate frames can be chosen. To comply with the DH-convention, the following must be satisfied.
\begin{itemize}
    \item The axis \(x_{1}\) is perpendicular to the axis \(z_0\)
    \item The axis \(x_{1}\) intersects the axis \(z_0\)
\end{itemize}
where it is assumed that two frames are given, frame 0 and frame 1, and the transformation from equation \ref{eqn:DH-transformation} transforms a coordinate from frame 1 into a coordinate in frame 0.\cite{spong}

\subsection*{Kinematic Chain} %A1*A2*...
A robotic manipulator can be described by a set of joints with links between them where a homogeneous transformation matrix \(A_i\) that describes the transformation with respect to the previous joint exists for each joint. This means that a transformation that describes the position and orientation of joint \(j\) with respect to a joint \(i\) to a can be found by a transformation matrix \cite{spong}
\begin{equation}
    \begin{cases}
        T_j^i = A_{i+1}A_{i+2}...A_{j-1}A_{j}, \text{  if \(i < j\) } \\
        T_j^i = I, \text{  if \(i = j\) } \\
        T_j^i = (T_i^j)^{-1}, \text{  if \(i > j\) }
    \end{cases}
    \label{eqn:Kinematic_chain_spong}
\end{equation}

\subsection*{Forward Kinematics}
Forward kinematics is the problem of finding the position and orientation of the end effector.

Since a manipulator can be seen as a kinematic chain the problem of finding the position and orientation of the end effector can be solved by finding the transformation matrices in equation \ref{eqn:Kinematic_chain_spong}.

\subsection*{Inverse Kinematics}
The problem of finding the joint states required for achieving a desired pose is called inverse kinematics. For some simple kinematic chains an analytical solution exists, but in general a numerical approach might be required.

%we have for joints, three needed for position. geometric approach for finding the position. then joint for is used to control the pitch of the EOF

\subsection*{Workspace}
A robotic manipulator will not be able to reach all points in space since it has a fixed size. It will not even be able to reach all mathematically reachable points since each joint (usually) have some restrictions on how it can rotate/translate. The physically reachable space is defined as the workspace of the manipulator. %source might be needed here
\\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% Kinematics Implementation %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Implementation}

\subsection*{Denavit-Hartenberg Convention}
In table \ref{tab:DH-table} the DH-parameters that was used for the robotic manipulator, seen in figure \ref{fig:robotic_manipulator_general}, are listed.
\begin{table}[H]
    \centering
    \caption{The Denavit-Hartenberg parameters used for the robotic manipulator.}
    \begin{tabular}{c | c c c c c}
        \(i\) & \(\theta_i\) [rad] & \(d_i\) [mm] & \(a_i\) [mm] & \(\alpha_i\) [rad] \\
        \hline
        \(1\) & \(\theta_1\) & \(75\) & \(0\) & \(\pi / 2\) \\
        \(2\) & \(\theta_2\) & \(0\) & \(67.5\) & \(0\) \\
        \(3\) & \(\theta_3\) & \(0\) & \(67.5\) & \(0\) \\
        \(4\) & \(\theta_4\) & \(0\) & \(65\) & \(0\) \\
    \end{tabular}
    \label{tab:DH-table}
\end{table}

\subsection*{Forward Kinematics}
The transformation matrices from equation \ref{eqn:DH-transformation} for each joint can be multiplied together
\begin{equation}
    T_{end} = A_1 \cdot A_2 \cdot A_3 \cdot A_4
\end{equation}
where \(A_1\) - \(A_4\) are calculated from equation \ref{eqn:DH-transformation} to find the pose \(T_{end}\) of the end effector and thereby solving the forward kinematic problem.

\subsection*{Inverse Kinematics}
To solve the inverse kinematics for the position of a three joint manipulator a geometric method can be used to find an analytical solution. The required angles to achieve a desired position \((x_d, y_d, z_d)\) in an elbow-up configuration are \cite{Lec14_mit_Manipulation}

\begin{equation}
    \begin{cases}
        \theta_1 = Atan(x, y) \\
        \theta_3 = Atan(D, +\sqrt{1 - D^2}) \\
        \theta_2 = Atan(\sqrt{x_d^2 + y_d^2}, z_d - d_1) \\- Atan(a_2 + a_3 \cdot D, a_3 \cdot \sqrt{1 - D^2}) %fix
    \end{cases}
\end{equation}
where \(Atan(x, y)\) is the two argument arctangent function \cite{inverse_tangent_wolfram} and

\begin{equation}
    D = \frac{x_d^2 + y_d^2 + (z_d - d_1)^2 - a_2^2 - a_3^2}{2 a_2 a_3}
\end{equation}
and \(a_2\), \(a_3\), and \(d_1\)   are the corresponding DH-parameters.

\subsection*{Workspace}
The robotic manipulator seen in figure \ref{fig:robotic_manipulator_general} has the following limitations for the first three joints
\begin{equation}
    \begin{cases}
        -100^\circ < \theta_1 < 100^\circ \\
        -14^\circ < \theta_2 < -194^\circ \\
        -104^\circ < \theta_3 < 104^\circ
    \end{cases}
    \label{eqn:workspace_limits_angles}
\end{equation}
due to limitations on how much each joint can rotate and limitations such as cables and safety concerns. A simulated 2D slice of the workspace can be seen in figure \ref{fig:workspace_simulated}. The full workspace can be formed by rotating this 2D slice around the z-axis according to the limitations on \(\theta_1\) from equation \ref{eqn:workspace_limits_angles}.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\columnwidth]{chapters/img/workspace.eps}
    \caption{A simulated 2D slice of the workspace for the three jointed manipulator.}
    \label{fig:workspace_simulated}
\end{figure}
%chapters/img/workspace.eps

%%%%%%%%%%%%%%%% arm kinematics end %%%%%%%%%%%%%%

\section*{Base}

The motors will be connected to tracks on the robot and can therefore be modelled as two wheels connected with a rod, as seen in figure \ref{fig:base_math_model}.\\
Deriving a mathematical model is straight forward if we can read the encoders from the motors. From the encoders we would be able to get both the length the robot has traveled and more importantly the individual speed each motor rotates with. The speed for the individual motor is given by:

\begin{equation}
    v_m=\frac{2\pi r_{wh}/N_{enc}}{\Delta t}
    \label{eq:base_system_eq1}
\end{equation}

\noindent Where $N_{enc}$ is the number of encoders on the motor, $r_{wh}$ is the radius of the driving wheel and thickness of the track and $\Delta t$ is the time between the previous encoder reading and the most recent one. By doing this for both the left and right motor we can find how fast the robot is moving along the line by:

\begin{equation}
    \overline{v}= \frac{v_L+v_R}{2}(-\cos \theta \hat{i}+ \sin \theta \Hat{j})
\end{equation}

\noindent Where the $y$-axis parallel to the line. In a similar fashion the angular velocity can be calculated using:

\begin{equation}
    \Dot{\theta} = \frac{v_R-v_L}{2r_{b}}
    \label{eq:base_system_eq2}
\end{equation}

\noindent Where $r_{b}$ is the distance from the center of the base to the wheels. From this and figure \ref{fig:base_math_model} it's easy to see that the change of angle then becomes:

\begin{equation}
    \theta = sin^{-1}\left(\frac{V_L-V_R}{2r_{b}}t\right)
\end{equation}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\columnwidth]{chapters/img/base_math_model.eps}
    \caption{Modelling of the base as two wheels connected with a rod, where $\theta$ is the angle to the line}
    \label{fig:base_math_model}
\end{figure}
%base_math_model.eps

Given the use of small-angles approximation\footnote{Which will be assumed since the robot will be operating on a grid with hard coded functions for turning at QR-codes}, the angular velocity can be written as:

\begin{equation}
    \theta = \frac{V_L-V_R}{2r_{b}}t
\end{equation}

\noindent Using equations \eqref{eq:base_system_eq1}-\eqref{eq:base_system_eq2} a system can be built for simulations in matlab, using rate of change and max/min value limiters for simulations of the motors physical restrictions.

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

\section*{Camera vision and calibration}
In this section it is outlined the theory behind the camera vision system.
How points in a 3D-space are projected on a 2D-plane, calibration and distortion correction.
\subsection*{Camera model}
\subsection*{Distortion}