% !TEX root = hott_intro.tex

\section{Propositions, sets, and the higher truncation levels}
\sectionmark{Truncation levels}\label{chap:hierarchy}

\index{truncated type|(}
\index{truncation level|(}
%Not all types have interesting higher groupoid structure. For example, we will see below that two natural numbers can only be equal in at most one way. Voevodsky articulated a useful notion to detect the homotopical complexity of types, which allows us to distinguish between contractible types (also called \emph{$(-2)$-types}), \emph{propositions} (also called \emph{$(-1)$-types}), \emph{sets} (\emph{$0$-types}), and \emph{$k$-types} for higher $k$.

%We will see [later] that there are types that are not $k$-types for any $k$.

\subsection{Propositions and subtypes}

\index{proposition|(}
\begin{defn}
A type $A$ is said to be a \define{proposition} if there is a term of type\index{is-prop(A)@{$\isprop(A)$}}
\begin{equation*}
\isprop(A)\defeq\prd{x,y:A}\iscontr(x=y).
\end{equation*}
Given a universe $\UU$, we define $\prop_\UU$\index{Prop@{$\prop$}} to be the type of all small propositions, i.e.,
\begin{equation*}
  \prop_\UU\defeq\sm{X:\UU}\isprop(X).
\end{equation*}
\end{defn}

\begin{eg}\label{eg:prop_contr}
  Any contractible type is a proposition by \cref{ex:prop_contr}\index{contractible type!is a proposition}\index{is a proposition!contractible type}. In particular, the unit type is a proposition.

  However, propositions do not need to be inhabited: the empty type is also a proposition, since\index{empty type!is a proposition}\index{is a proposition!empty type}
\begin{equation*}
\prd{x,y:\emptyt}\iscontr(x=y)
\end{equation*}
follows from the induction principle of the empty type.
\end{eg}

In the following lemma we prove that in order to show that a type $A$ is a proposition, it suffices to show that any two terms of $A$ are equal. In other words, propositions are types with \define{proof irrelevance}.

\begin{thm}\label{lem:isprop_eq}
  Let $A$ be a type. Then the following are equivalent:
  \begin{enumerate}
  \item The type $A$ is a proposition.
  \item Any two terms of type $A$ can be identified, i.e., there is a dependent function\index{is-prop'(A)@{$\isprop'(A)$}}\index{is-prop(A)@{$\isprop(A)$}!is-prop(A) iff is-prop'(A)@{$\isprop(A)\leftrightarrow\isprop'(A)$}}
    \begin{equation*}
      \isprop'(A)\defeq\prd{x,y:A}\id{x}{y}.
    \end{equation*}
  \item The type $A$ is contractible as soon as it is inhabited, i.e., there is a function\index{is-prop(A)@{$\isprop(A)$}!is-prop(A) iff A to is-contr(A)@{$\isprop(A)\leftrightarrow(A\to\iscontr(A))$}}
    \begin{equation*}
      A \to \iscontr(A).
    \end{equation*}
  \item The map $\const_\ttt : A\to\unit$ is an embedding.\index{is-prop(A)@{$\isprop(A)$}!is-prop(A) iff is-emb(const star)@{$\isprop(A)\leftrightarrow\isemb(\const_\ttt)$}}
  \end{enumerate}
\end{thm}

\begin{proof}
  To show that (i) implies (ii), let $A$ be a proposition. Then its identity types are contractible, so the center of contraction of $\id{x}{y}$ is identification $\id{x}{y}$, for each $x,y:A$.

  To show that (ii) implies (iii), suppose that $A$ comes equipped with $p:\prd{x,y:A}\id{x}{y}$. Then for any $x:A$ the dependent function $p(x):\prd{y:A}\id{x}{y}$ is a contraction of $A$. Thus we obtain the function
  \begin{equation*}
    \lam{x}(x,p(x)):A\to\iscontr(A).
  \end{equation*}

  To show that (iii) implies (iv), suppose that $A\to\iscontr(A)$. We first make the simple observation that
  \begin{equation*}
    (X\to \isemb(f))\to \isemb(f)
  \end{equation*}
  for any map $f:X\to Y$, so it suffices to show that $A\to\isemb(\const_\ttt)$. However, assuming we have $x:A$, it follows by assumption that $A$ is contractible. Therefore, it follows by \cref{ex:contr_equiv} that the map $\const_\ttt:A\to\unit$ is an equivalence. Since it is an equivalence, it is an embedding by \cref{cor:emb_equiv}.

  To show that (iv) implies (i), note that if $A\to\unit$ is an embedding, then the identity types of $A$ are equivalent to contractible types and therefore they must be contractible.
\end{proof}

In the following lemma we show that propositions are closed under equivalences.

\begin{lem}\label{lem:prop_equiv}
Let $A$ and $B$ be types, and let $e:\eqv{A}{B}$. Then we have\index{proposition!closed under equivalences}
\begin{equation*}
\isprop(A)\leftrightarrow\isprop(B).
\end{equation*}
\end{lem}

\begin{proof}
We will show that $\isprop(B)$ implies $\isprop(A)$. This suffices, because the converse follows from the fact that $e^{-1}:B\to A$ is also an equivalence.

Since $e$ is assumed to be an equivalence, it follows by \cref{cor:emb_equiv} that
\begin{equation*}
\apfunc{e} : (x=y)\to (e(x)=e(y))
\end{equation*}
is an equivalence for any $x,y:A$. If $B$ is a proposition, then in particular the type $e(x)=e(y)$ is contractible for any $x,y:A$, so the claim follows from \cref{thm:contr_equiv}.
\end{proof}

  In set theory, a set $y$ is said to be a subset\index{subset} of a set $x$, if any element of $y$ is an element of $x$, i.e., if the condition
  \begin{equation*}
    \forall_z (z\in y)\to (z\in x)
  \end{equation*}
  holds. We have already noted that type theory is different from set theory in that terms in type theory come equipped with a \emph{unique} type. Moreover, in set theory the proposition $x\in y$ is well-formed for any two sets $x$ and $y$, whereas in type theory the judgment $a:A$ is only well-formed if it is derived using the postulated inference rules. Because of these differences we must find a different way to talk about subtypes.

  Note that in set theory there is a correspondence between the subsets of a set $x$, and the \emph{predicates} on $x$. A predicate on $x$ is just a proposition $P(z)$ that varies over the elements $z\in x$. Indeed, if $y$ is a subset of $x$, then the corresponding predicate is the proposition $z\in y$. Conversely, if $P$ is a predicate on $x$, then we obtain the subset
  \begin{equation*}
    \{z\in x\mid P(z)\}
  \end{equation*}
  of $x$. Now we have the right idea of subtypes in type theory: they are families of propositions.

\begin{defn}
A type family $B$ over $A$ is said to be a \define{subtype}\index{subtype} of $A$ if for each $x:A$ the type $B(x)$ is a proposition. When $B$ is a subtype of $A$, we also say that $B(x)$ is a \define{property}\index{property} of $x:A$.
\end{defn}

We will show in \cref{thm:subtype} that a type family $B$ over $A$ is a subtype of $A$ if and only if the projection map $\proj 1:\big(\sm{x:A}B(x)\big)\to A$ is an embedding.

\index{proposition|)}

\subsection{Sets}

\index{set|(}
\begin{defn}
  A type $A$ is said to be a \define{set} if it comes equipped with a term of type
  \index{is-set(A)@{$\isset(A)$}}\index{is a set}
\begin{equation*}
\isset(A)\defeq \prd{x,y:A}\isprop(\id{x}{y}).
\end{equation*}
\end{defn}

\begin{lem}
A type $A$ is a set if and only if it satisfies \define{axiom K}\index{axiom K}, i.e., if and only if it comes equipped with a term of type\index{is-set(A)@{$\isset(A)$}!is-set(A) iff axiom-K(A)@{$\isset(A)\leftrightarrow\axiomK(A)$}}
\begin{equation*}
\axiomK(A)\defeq\prd{x:A}\prd{p:\id{x}{x}}\id{\refl{x}}{p}.
\end{equation*}
\end{lem}

\begin{proof}
If $A$ is a set, then $\id{x}{x}$ is a proposition, so any two of its elements are equal.
This implies axiom $K$.

For the converse, if $A$ satisfies axiom $K$, then for any $p,q:\id{x}{y}$ we have $\id{\ct{p}{q^{-1}}}{\refl{x}}$, and hence $\id{p}{q}$. This shows that $\id{x}{y}$ is a proposition, and hence that $A$ is a set.
\end{proof}

\begin{thm}\label{lem:prop_to_id}
Let $A$ be a type, and let $R:A\to A\to\UU$ be a binary relation on $A$ satisfying
\begin{enumerate}
\item Each $R(x,y)$ is a proposition,
\item $R$ is reflexive, as witnessed by $\rho:\prd{x:A}R(x,x)$,
\item There is a map
  \begin{equation*}
    R(x,y)\to (x=y)
  \end{equation*}
  for each $x,y:A$.
\end{enumerate}
Then any family of maps
\begin{equation*}
\prd{x,y:A}(\id{x}{y})\to R(x,y)
\end{equation*}
is a family of equivalences. Consequently, the type $A$ is a set.
\end{thm}

\begin{proof}
Let $f:\prd{x,y:A}R(x,y)\to(\id{x}{y})$.
Since $R$ is assumed to be reflexive, we also have a family of maps
\begin{equation*}
\pathind_x(\rho(x)):\prd{y:A}(\id{x}{y})\to R(x,y).
\end{equation*}
Since each $R(x,y)$ is assumed to be a proposition, it therefore follows that each $R(x,y)$ is a retract of $\id{x}{y}$. Therefore it follows that $\sm{y:A}R(x,y)$ is a retract of $\sm{y:A}x=y$, which is contractible. We conclude that $\sm{y:A}R(x,y)$ is contractible, and therefore that any family of maps
\begin{equation*}
  \prd{y:A}(x=y)\to R(x,y)
\end{equation*}
is a family of equivalences.

Now it also follows that $A$ is a set, since its identity types are equivalent to propositions, and therefore they are propositions by \cref{lem:prop_equiv}.
\end{proof}

\begin{defn}
  A map $f:A\to B$ is said to be \define{injective}\index{injective function} if for any $x,y:A$ there is a map
  \begin{equation*}
    (f(x)=f(y))\to (x=y).
  \end{equation*}
\end{defn}

\begin{cor}\label{cor:is-emb-is-injective}
  Any injective map into a set is an embedding.\index{is an embedding!injective map into a set}
\end{cor}

\begin{proof}
  Let $f:A\to B$ be an injective map between sets. Now consider the relation
  \begin{equation*}
    R(x,y)\defeq (f(x)=f(y)).
  \end{equation*}
  Note that $R$ is reflexive, and that $R(x,y)$ is a proposition for each $x,y:A$. Moreover, by the assumption that $f$ is injective, we have
  \begin{equation*}
    R(x,y)\to (x=y)
  \end{equation*}
  for any $x,y:A$. Therefore we are in the situation of \cref{lem:prop_to_id}, so it follows that the map $\apfunc{f} : (x=y)\to (f(x)=f(y))$ is an equivalence.
\end{proof}

\begin{thm}\label{thm:eq_nat}
The type of natural numbers is a set.\index{is a set!natural numbers}\index{natural numbers!is a set}
\end{thm}

\begin{proof}
We will apply \cref{lem:prop_to_id}. Note that the observational equality $\EqN:\N\to(\N\to\UU)$ on $\N$ (\cref{defn:obs_nat}) is a reflexive relation by \cref{ex:obs_nat_eqrel}, and moreover that $\EqN(n,m)$ is a proposition for every $n,m:\N$ (proof by double induction).
Therefore it suffices to show that
\begin{equation*}
\prd{m,n:\N}\EqN(m,n)\to (\id{m}{n}).
\end{equation*}
This follows from the fact that observational equality is the \emph{least} reflexive relation, which was shown in \cref{ex:obs_nat_least}.
\end{proof}

\begin{comment}
\begin{thm}[Hedberg]\label{thm:dec_eq}
Any type with decidable equality is a set.
\end{thm}

\begin{proof}
Let $A$ be a type, and let $d:\prd{x,y:A}(\id{x}{y})+\neg(\id{x}{y})$ be the witness that $A$ has decidable equality.
We first construct a reflexive binary relation $E:A\to A\to\type$ such that each $E(x,y)$ is a proposition.
For every $x,y:A$, we first define a type family $E'(x,y):((\id{x}{y})+\neg(\id{x}{y}))\to\type$ by
\begin{align*}
E'(x,y,\inl(p)) & \defeq \unit \\
E'(x,y,\inr(p)) & \defeq \emptyt.
\end{align*}
Note that $E'(x,y,q)$ is a proposition for each $x,y:A$ and $q:(\id{x}{y})+\neg(\id{x}{y})$.
Now we set $E(x,y)\defeq E'(x,y,d(x,y))$. Then $E$ is clearly reflexive, and a family of propositions.
Therefore it remains to show that $E$ implies identity.

Since $E$ is defined as an instance of $E'$, it suffices to construct a term of type
\begin{equation*}
\prd{x,y:A}\prd{q:(\id{x}{y})+\neg(\id{x}{y})} E'(q)\to (\id{x}{y}).
\end{equation*}
By induction of disjoint sums, it suffices to construct terms of types
\begin{align*}
& \prd{x,y:A}\prd{p:\id{x}{y}} \unit\to (\id{x}{y}) \\
& \prd{x,y:A}\prd{p:\neg(\id{x}{y})} \emptyt\to (\id{x}{y}).
\end{align*}
In the first case, we take $\lam{x}\lam{y}\lam{p}{t}p$, and the second case is by induction on the empty type.
\end{proof}
\end{comment}
\index{set|)}

\subsection{General truncation levels}

\begin{defn}
We define $\istrunc{} : \Z_{\geq-2}\to\UU\to\UU$ by induction on $k:\Z_{\geq -2}$, taking\index{is-trunc k(A)@{$\istrunc{k}(A)$}}
\begin{align*}
\istrunc{-2}(A) & \defeq \iscontr(A) \\
\istrunc{k+1}(A) & \defeq \prd{x,y:A}\istrunc{k}(\id{x}{y}).\qedhere
\end{align*}
For any type $A$, we say that $A$ is \define{$k$-truncated}\index{k-truncated type@{$k$-truncated type}|see {truncated type}}, or a \define{$k$-type}\index{k-type@{$k$-type}}, if there is a term of type $\istrunc{k}(A)$. We say that a map $f:A\to B$ is $k$-truncated if its fibers are $k$-truncated.\index{k-truncated map@{$k$-truncated map}|see {truncated map}}\index{truncated map}
\end{defn}

%For the rest of this section, let $k:\Z_{\geq-2}$.

\begin{thm}\label{thm:istrunc_next}
If $A$ is a $k$-type, then $A$ is also a $(k+1)$-type.\index{is-trunc k(A)@{$\istrunc{k}(A)$}!is-trunc k(A) to is-trunc k+1(A)@{$\istrunc{k}(A)\to\istrunc{k+1}(A)$}}
\end{thm}

\begin{proof}
We have seen in \cref{eg:prop_contr} that contractible types are propositions. This proves the base case.
For the inductive step, note that if any $k$-type is also a $(k+1)$-type, then any $(k+1)$-type is a $(k+2)$-type, since its identity types are $k$-types and therefore $(k+1)$-types.
\end{proof}

\begin{thm}\label{thm:ktype_eqv}
If $e:\eqv{A}{B}$ is an equivalence, and $B$ is a $k$-type, then so is $A$.\index{truncated type!closed under equivalences}
\end{thm}

\begin{proof}
We have seen in \cref{ex:contr_equiv} that if $B$ is contractible and $e:\eqv{A}{B}$ is an equivalence, then $A$ is also contractible. This proves the base case.

For the inductive step, assume that the $k$-types are stable under equivalences, and consider $e:\eqv{A}{B}$ where $B$ is a $(k+1)$-type. In \cref{cor:emb_equiv} we have seen that
\begin{equation*}
\apfunc{e}:(\id{x}{y})\to(\id{e(x)}{e(y)})
\end{equation*}
is an equivalence for any $x,y$. Note that $\id{e(x)}{e(y)}$ is a $k$-type, so by the induction hypothesis it follows that $\id{x}{y}$ is a $k$-type. This proves that $A$ is a $(k+1)$-type.
\end{proof}

\begin{cor}\label{cor:emb_into_ktype}
If $f:A\to B$ is an embedding, and $B$ is a $(k+1)$-type, then so is $A$.\index{truncated type!closed under embeddings}
\end{cor}

\begin{proof}
By the assumption that $f$ is an embedding, the action on paths
\begin{equation*}
\apfunc{f}:(\id{x}{y})\to (\id{f(x)}{f(y)})
\end{equation*}
is an equivalence for every $x,y:A$. Since $B$ is assumed to be a $(k+1)$-type, it follows that $f(x)=f(y)$ is a $k$-type for every $x,y:A$. Therefore we conclude by \cref{thm:ktype_eqv} that $\id{x}{y}$ is a $k$-type for every $x,y:A$. In other words, $A$ is a $(k+1)$-type.
\end{proof}

\begin{thm}
Let $B$ be a type family over $A$. Then the following are equivalent:\index{truncated family of types}
\begin{enumerate}
\item For each $x:A$ the type $B(x)$ is $k$-truncated. In this case we say that the family $B$ is \define{$k$-truncated}.
\item The projection map
\begin{equation*}
\proj 1 : \Big(\sm{x:A}B(x)\Big)\to A
\end{equation*}
is $k$-truncated.
\end{enumerate}
\end{thm}

\begin{proof}
By \cref{ex:proj_fiber} we obtain equivalences
\begin{equation*}
\eqv{\fib{\proj 1}{x}}{B(x)}
\end{equation*}
for every $x:A$. Therefore the claim follows from \cref{thm:ktype_eqv}.
\end{proof}

\begin{thm}\label{thm:trunc_ap}
Let $f:A\to B$ be a map. The following are equivalent:
\begin{enumerate}
\item The map $f$ is $(k+1)$-truncated.
\item For each $x,y:A$, the map
\begin{equation*}
\apfunc{f} : (x=y)\to (f(x)=f(y))
\end{equation*}
is $k$-truncated.
\end{enumerate}
\end{thm}

\begin{proof}
First we show that for any $s,t:\fib{f}{b}$ there is an equivalence
\begin{equation*}
\eqv{(s=t)}{\fib{\apfunc{f}}{\ct{\proj 2(s)}{\proj 2(t)^{-1}}}}
\end{equation*}
We do this by $\Sigma$-induction on $s$ and $t$, and then we calculate
\begin{align*}
(\pairr{x,p}=\pairr{y,q}) & \eqvsym \Eqfib_f((x,p),(y,q)) \\
  & \jdeq \sm{\alpha:x=y} p=\ct{\ap{f}{\alpha}}{q} \\
  & \eqvsym \sm{\alpha:x=y} \ct{\ap{f}{\alpha}}{q}=p \\
& \eqvsym \sm{\alpha:x=y} \ap{f}{\alpha}=\ct{p}{q^{-1}} \\
& \jdeq \fib{\apfunc{f}}{\ct{p}{q^{-1}}}.
\end{align*}
By these equivalences, it follows that if $\apfunc{f}$ is $k$-truncated, then for each $s,t:\fib{f}{b}$ the identity type $s=t$ is equivalent to a $k$-truncated type, and therefore we obtain by \cref{thm:ktype_eqv} that $f$ is $(k+1)$-truncated.

For the converse, note that we have equivalences
\begin{align*}
\fib{\apfunc{f}}{p} & \eqvsym ((x,p)=(y,\refl{f(y)})).
\end{align*}
It follows that if $f$ is $(k+1)$-truncated, then the identity type $(x,p)=(y,\refl{f(y)})$ in $\fib{f}{f(y)}$ is $k$-truncated for any $p:f(x)=f(y)$. We conclude by \cref{thm:ktype_eqv} that the fiber $\fib{\apfunc{f}}{p}$ is $k$-truncated.
\end{proof}

\begin{cor}\label{cor:prop_emb}
A map is an embedding if and only if its fibers are propositions.\index{is an embedding!-1-truncated map@{$(-1)$-truncated map}}
\end{cor}

\begin{cor}\label{thm:subtype}
A type family $B$ over $A$ is a subtype if and only if the projection map
\begin{equation*}
\proj 1 : \Big(\sm{x:A}B(x)\Big)\to A
\end{equation*}
is an embedding.
\end{cor}

\begin{thm}
Let $f:\prd{x:A}B(x)\to C(x)$ be a family of maps. Then the following are equivalent:
\begin{enumerate}
\item For each $x:A$ the map $f(x)$ is $k$-truncated.
\item The induced map
\begin{equation*}
\tot{f}:\Big(\sm{x:A}B(x)\Big)\to\Big(\sm{x:A}C(x)\Big)
\end{equation*}
is $k$-truncated.
\end{enumerate}
\end{thm}

\begin{proof}
This follows directly from \cref{lem:fib_total,thm:ktype_eqv}.
\end{proof}

\begin{exercises}
\exercise
  \begin{subexenum}
  \item Show that $\succN:\N\to\N$ is an embedding.\index{succ N@{$\succN$}!is an embedding}\index{is an embedding!succ N@{$\succN$}}
  \item Show that $n\mapsto m+n$ is an embedding, for each $m:\N$.\index{add N@{$\addN$}!add N(m) is an embedding@{$\addN(m)$ is an embedding}}\index{is an embedding!add N(m)@{$\addN(m)$}} Moreover, conclude that there is an equivalence
    \begin{equation*}
      \fib{\addN(m)}{n}\simeq (m\leq n).
    \end{equation*}
  \item Show that $n\mapsto mn$ is an embedding\index{mul N@{$\mulN$}!mul N(m) is an embedding if m>0@{$\mulN(m)$ is an embedding if $m>0$}}\index{is an embedding!mul N(m) for m > 0@{$\mulN(m)$ for $m>0$}}, for each $m>0$ in $\N$. Conclude that the divisibility relation\index{d {"| n}@{$d\mid n$}!is a proposition if d>0@{is a proposition if $d>0$}}\index{is a proposition!d {"| n} for d>0@{$d\mid n$ for $d>0$}}
    \begin{equation*}
      d\mid n
    \end{equation*}
    is a proposition for each $d,n:\N$ such that $d>0$.
  \end{subexenum}
\exercise \label{ex:diagonal}Let $A$ be a type, and let the \define{diagonal} of $A$ be the map $\delta_A:A\to A\times A$ given by $\lam{x}(x,x)$.
\begin{subexenum}
\item Show that
\begin{equation*}
{\isequiv(\delta_A)}\leftrightarrow{\isprop(A)}.
\end{equation*}
\item Construct an equivalence $\eqv{\fib{\delta_A}{(x,y)}}{(x=y)}$ for any $x,y:A$.
\item Show that $A$ is $(k+1)$-truncated if and only if $\delta_A:A\to A\times A$ is $k$-truncated.
\end{subexenum}
\exercise \label{ex:istrunc_sigma}
\begin{subexenum}
\item Let $B$ be a type family over $A$. Show that if $A$ is a $k$-type, and $B(x)$ is a $k$-type for each $x:A$, then so is $\sm{x:A}B(x)$. Conclude that for any two $k$-types $A$ and $B$, the type $A\times B$ is also a $k$-type. Hint: for the base case, use \cref{ex:contr_in_sigma,ex:contr_equiv}.
\item Show that for any $k$-type $A$, the identity types of $A$ are also $k$-types.
\item Show that any maps $f:A\to B$ between $k$-types $A$ and $B$
is a $k$-truncated map.
\item Use \cref{ex:proj_fiber} to show that for any type family $B:A\to \UU$, if $A$ and $\sm{x:A}B(x)$ are $k$-types, then so is $B(x)$ for each $x:A$.
\end{subexenum}
\exercise \label{ex:eq_bool}Show that $\bool$ is a set by applying \cref{lem:prop_to_id} with the observational equality on $\bool$ defined in \cref{ex:obs_bool}.
\exercise \label{ex:set_coprod}Show that for any two $(k+2)$-types $A$ and $B$, the disjoint sum $A+B$ is again a $(k+2)$-type. Conclude that $\mathbb{Z}$ is a set.
\exercise Use \cref{ex:contr_retr,ex:retr_id} to show that if $A$ is a retract of a $k$-type $B$, then $A$ is also a $k$-type.
\exercise Show that a type $A$ is a $(k+1)$-type if and only if the map $\const_x:\unit\to A$ is $k$-truncated for every $x:A$.
\exercise Consider a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
A \arrow[rr,"h"] \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
& X
\end{tikzcd}
\end{equation*}
with $H: f \htpy g \circ h$, and suppose that $g$ is $k$-truncated. Show that $f$ is $k$-truncated if and only if $h$ is $k$-truncated.
% Suppose that $h$ is $k$-truncated and surjective. Show that $f$ is $(k+1)$-truncated if and only if $g$ is $(k+1)$-truncated.
\end{exercises}
\index{truncated type|)}
\index{truncation level|)}


% !TEX root = index.tex
\maketitle
\tableofcontents
\newpage

\setcounter{section}{-1}
\section{Introduction \& Motivation}
\epigraph{In mathematics you don't understand things. You just get used to them.}{John von Neumann}

Cohomology was introduced by Poincare in a series of papers named \emph{Analysis Situs} and now forms the basis of modern Algebraic Topology.
To a topological space $X$ we can associate a sequence of vector spaces denoted
\begin{align*}
    \check H^i(X)
\end{align*}
for each $i \in \Z_{\ge 0}$ called it's \textbf{Cech Cohomology} (pronounced \emph{check cohomology}). In a very loose sense, the dimension of  $\check H^i(X)$ measures the $i^{th}$ dimensional holes in $X$.\\

Why care about the $i^{th}$ dimensional holes? We can use these to rigorously distinguish between spaces. For example, most proofs of the fact that $\R^m$ is not homeomorphic\footnote{Homeomorphism is the isomorphism for topological spaces.} to $\R^n$ if $m \neq n$ use some cohomology computation. We'll also see that a torus $S^1 \times S^1$ has two \emph{1-dimensional holes} which distinguishes it from a sphere $S^2$ which has none.

\begin{thm}
  Two topological spaces $X$, $Y$ are homeomorphic only if
  \begin{align*}
    \check H^i(X) \cong \check H^i(Y)
  \end{align*} for all non-negative integers $i$.
\end{thm}
\begin{remark}
  The above statement is not an \emph{if and only if} statement. The other direction is easily shown to be false. You'll be able to come up with examples by yourself in a couple of days.
\end{remark}

Computing the cohomology requires multiple steps. The goal of this class is to develop the relevant machinery and actually do some cohomology computations.
\begin{align*}
 \xymatrix@R-2pc{
 X \ar@{~>}[r] & \U \ar@{~>}[r] & \L^\bullet(\U) \ar@{~>}[r] & \check H^*(X)\\
 \mbox{Topological Space} & \mbox{Good cover of } X & \mbox{Cech Complex of } \U & \mbox{Cech Cohomology of } X
 }
\end{align*}

\noindent \textbf{Note:} The number of stars (*) on the problems indicate their difficulty level. The non-starred marked problems are compulsory, the starred problems are optional.

% We'll gradually define Cech cohomology. The strategy is the following.
% \begin{enumerate}
% 	\item For a space $ X$, find a good open cover $ \U$
% 	\item Find the Cech complex (to be defined) $ \L^\bullet(\U)$ associated $ \U$
% 	\item Find the cohomology of the complex $H^*(\L^\bullet(\U))$ which then equals the singular cohomology of the space $H^*(X, \F)$.
% \end{enumerate}
%
% \begin{remark}
% 	When $X$ is a nice enough space (which includes most spaces you can think of) the Cech cohomology $\check H^i(X; \F)$ equals another purely topological invariant of $X$ called the \textbf{singular homology}. And thus Cech cohomology provides a purely algebraic way of computing a topological invariant.
% \end{remark}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{physics}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\relpenalty=10000
\binoppenalty=10000

\begin{document}
\section*{Geometric inequalities}
	\begin{enumerate}
		\item %vorrat 48
		Prove the inequalities for $x \in \left(\frac{-\pi}{2},\frac{\pi}{2}\right)$
		$$|\tan x| \geq |x| \geq |\sin x| \geq  \frac{2}{\pi}|x|$$
		\item %vorrat 49
		Prove the inequalities for $0 < \alpha < \beta < \frac{\pi}{2}$
		$$\frac{\sin \beta}{\sin \alpha} < \frac{\beta}{\alpha} < \frac{\tan \alpha}{\tan \beta}$$
		\item % vorrat 52
		Prove the inequalities where $a,b,c$ are lengths of triangles sides
		\begin{enumerate}
			\item $a^2 + b^2 + c^2 < 2 (ab + bc + ca)$
			\item $a^4 + b^4 + c^4 < 2 (a^2b^2 + b^2c^2 + c^2a^2)$
		\end{enumerate}
		\item % vorrat 53
		Prove that $R \geq 2r$ given that $R$ is the radius of circumcircle and $r$ is the radius of incircle of a triangle.
		\item \emph{(Ptolemy's inequality).}
		Prove the inequality for any four points $A,B,C,D$ on a plane
		$$AC \cdot BD \leq AB \cdot CD + AD \cdot BC$$
		When does the equality hold?
		\item \emph{(Erdős-Mordell inequality).} % vorrat 62
		Let $P$ be a point in the interior of $\triangle ABC$ and $X$, $Y$,$Z$ projections of $P$ to $BC$,$AC$,$AB$ respectively. Prove that
		$$PA+PB+PC \geq 2(PX+PY+PZ)$$
		When does the equality hold?
		\item %vorrat 63
		Points $A,B,C,X,Y,Z,P$ are defined as before. Prove that
		$$AP \cdot BC + BP \cdot AC + CP \cdot AB \geq 4S_{\triangle ABC} $$
		\item %vorrat 64
		Points $A,B,C,X,Y,Z,P$ are defined as before. Prove that
		$$AP \cdot XP + BP \cdot YP + CP \cdot ZP \geq 2(XP \cdot YP + YP \cdot ZP + ZP \cdot XP) $$
		\item %vorrat 65, imo 96
		Given a convex hexagon $ABCDEF$ such that $AB \parallel DE$, $BC \parallel EF$, $CD \parallel FA$ and with perimeter $P$. Let $R_A, R_C, R_E$ be the radii of circles $FAB,BCD,DEF$ respectively. Prove that
		$$R_A + R_C + R_C \geq \frac{P}{2} $$

	\end{enumerate}
\end{document}


\chapter{Geostrophic balance and coastal flow}
\label{chap:geostrophic}

\includegraphics[width=6.5in]{figs/Geostrophic/HaidaEddy}

\section{Geostrophic balance}

The second balance that involves the Coriolis force is the \Wikiref{Geostrophic balance}, and is when the Coriolis force is in ``balance'' with the pressure gradient:

\begin{equation}
    \frac{du}{dt} = \overunderbraces{\br{2}{geostrophic}}
    {- \frac{1}{\rho_0}\frac{dP}{dx} & +fv & + \frac{1}{\rho}\frac{d\tau_{xz}}{dz}}
    {&\br{2}{Ekman}}
\end{equation}
\begin{equation}
    \frac{dv}{dt} = \overunderbraces{\br{2}{geostrophic}}
    {- \frac{1}{\rho_0}\frac{dP}{dy} & -fu & + \frac{1}{\rho}\frac{d\tau_{yz}}{dz}}
    {&\br{2}{Ekman}}
\end{equation}
\begin{marginfigure}
    \includegraphics{figs/Geostrophic/BalanceSketch}
    \caption{Sketch of geostrophic force-balance for a sea-level high and a sea-level low in the northern hemisphere, looking from above, and from the south for the high.  The flow will be clockwise around a hight and counter-clockwise.}
    \label{fig:BalanceSketch}
\end{marginfigure}
in which case the equations of motion simplify to:
\begin{eqnarray*}
    -\frac{1}{\rho_0}\frac{dP}{dx} & \approx & -fv\\
    -\frac{1}{\rho_0}\frac{dP}{dy} & \approx & +fu\\
\end{eqnarray*}
In the northern hemisphere ($f>0$) this says that water turns to the right as in moves from high pressure to low.  This gives a clockwise circulation around an isolated high, or a counter-clockwise around an isolated low (\fref{fig:BalanceSketch})

Crucially, if we know the pressure gradient, we can directly calculate the geostrophic flow perpendicular to that pressure gradient from the equations above.  So, suppose the high in the cover figure is 16 cm, over 100 km.  The pressure difference due to this flow gives us an estimate of the velocity (assuming $f = 10^{-4}\ \mathrm{rad\,s^{-1}}$)
\begin{equation}
  |u| = \left|\frac{1}{f\rho} \frac{dP}{dx}\right| = \frac{1}{f\rho}\frac{\Delta P}{\Delta x} = \frac{g}{f} \frac{\eta_C - \eta_E}{\Delta x} \approx 0.16 \ \mathrm{m\,s^{-1}}
\end{equation}

The equation applies everywhere in the fluid, and is only made complex by the presence of water with different densities making the flow have a pressure gradient that changes with depth.  An observation of this can be seen in \fref{fig:InteriorEddy}, where there is a sea-surface high in the middle, but the pressure gradient inside the eddy is the other direction.  This causes the pressure gradient force to decrease with depth, and hence the geostrophic flow decays with depth as well.

\begin{figure}
\begin{center}
    \includegraphics[width=2in]{figs/Geostrophic/InteriorEddy}
    \caption{Observations of density inside an Eddy.  Note how the density contours tend to counteract the high sea-surface height.  Blue circles with crosses are into the page, and with dots are out of the page, and the size is sketched to be proportional to the speed of the flow.}
    \label{fig:InteriorEddy}
\end{center}
\end{figure}

As discussed in \fref{chap:EquationofState}, calculating the pressure gradient is simply a matter of calculating the pressure at two locations, and differencing, where the pressure is given by:
\begin{equation}
  P(z) / \rho_0 = \frac{1}{\rho_0}\int_z^\eta \rho g\ \mathrm{d}z \approx g\eta + \frac{1}{\rho_0}\int_z^0 \rho g\ \mathrm{d}z
\end{equation}
So imagine one flank of the eddy, and that the eddy is made of two densities $\rho_1 < \rho_2$ (\fref{fig:CounterPressure}).  In the upper layer
\begin{equation}
  -\frac{1}{\rho_0} \frac{dP}{dx}(z=z_1) = -\frac{1}{\Delta x} g (\eta_B - \eta_A).
\end{equation}
In the lower layer, things are a bit more complicated, so we just do it step by step:
\begin{equation}
  P_A(z=z_2) = \rho_1 g (\eta_A - Z_A) + \rho_2 g (Z_A - z_2)
\end{equation}
\begin{equation}
  P_B(z=z_2) = \rho_1 g (\eta_B - Z_B) + \rho_2 g (Z_B - z_2)
\end{equation}
So the total pressure gradient in the lower layer is approximated as
\begin{equation}
  -\frac{1}{\rho_0}\frac{P_B - P_A}{\Delta x} = \overbrace{-\frac{1}{\Delta x} g (\eta_B - \eta_A)}^{Surface} - \overbrace{\frac{1}{\Delta x} g \frac{\left(\rho_2 - \rho_1\right)}{\rho_0} (Z_B - Z_A)}^{Internal}
  \label{eq:InternalExternal}
\end{equation}

According to the geostrophic balance, then, the flow in the upper layer is into the page, and the one in the bottom layer is either into the page, but more slow, or even out of the page.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics{figs/Geostrophic/CounterPressure}
    \caption{Two-layer approximation of pressure gradients in an eddy.}
    \label{fig:CounterPressure}
  \end{center}
\end{figure}

\subsection{Thermal wind}

The example above makes it clear that lateral density differences can drive velocity differences in the vertical if the flow is in geostrophic balance.  This can easily be extended to the continuous case with some  manipulation of the momentum equations.  If we take the vertical derivative of the x- and y-momentum equations we arrive at:
\begin{eqnarray}
    -\frac{1}{\rho_0}\frac{d^2 P}{dx dz} & = & -f \frac{dv}{dz} \\
    -\frac{1}{\rho_0}\frac{d^2 P}{dy dz} & = & f \frac{du}{dz}
\end{eqnarray}
but $P = \int_z^{\eta}\rho g \mathrm{d}z$,  so we get the \Wikiref{thermal wind} relation.
\begin{eqnarray}
    -\frac{1}{\rho_0}\frac{d \rho}{dx} & = & -f \frac{dv}{dz} \\
    -\frac{1}{\rho_0}\frac{d \rho}{dy} & = & f \frac{du}{dz}
\end{eqnarray}
so, if we know the horizontal pressure gradient, we can immediately say how the velocity changes with depth (though we do not know the absolute velocity).

\subsection{Level of no motion and ``dynamic height''}

Back in the pre-satellite days, all oceanographers would have would be measurements of the density under the ocean, like those in \fref{fig:InteriorEddy}.  Estimating the sea-surface height was impossible; imagine detecting 15 cm over 100 km in an ocean that is otherwise also moving due to tides, surface waves, relative to a poorly-constrained geoid.  However, oceanographers noted that most ocean currents, like the eddy, had tilted density surfaces, and that the currents at depth go to approximately zero.

Under these assumptions, oceanographers would assume that the pressure gradient at a ``level of no motion'' was zero, so that the geostrophic flow was zero there as well. In order to get this level of no motion the  pressure  gradients inferred from the interior density measurements were assumed to be balanced by a pressure gradient from the surface at that depth.  So, for instance, in the simple two-layer case the \fref{eq:InternalExternal}, the \emph{Surface} term was equal and opposite in sign to the \emph{Internal} term, if the deep layer was assumed to be not moving.  Note that this allows an estimate of the surface pressure gradient, and using the geostrophic balance, the surface velocities.  This surface pressure gradient is of course supplied by the sealevel interface being tilted, and  calculating these gradients is like calculating a ``dynamic height'' of the ocean.

An example in \fref{fig:DynamicHeight}, calculated in the 1980s looks strikingly similar to the sea surface heights in \fref{chap:winddriven}.  In particular, where there is Ekman convergence in the subtropics there is a high of up to 2 m, and a low in the sub-polar regions where there is Ekman divergence.  The highs and lows are displaced to the west sides of the basins.  The steepest change in dynamic height correspond to the strongest currents, again largely concentrated on the west side of the basins, and associated with the Gulf Stream, Kurishio, East Australian current etc.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics{figs/Geostrophic/DynamicHeight1500}
    \includegraphics{figs/Geostrophic/DynamicHeight2500}
    \caption{Dynamic height with a level of no motion at 1500 m (top) and 2500 m (bottom).  Note these are quite similar, strongly indicative that the dynamics are largely above these depths.  }
    \label{fig:DynamicHeight}
  \end{center}
\end{figure}

Just to summarize again how this is done:
\begin{enumerate}
    \item An interior pressure gradient is calculated at two ``stations'' as a function of depth using the density data from CTDs or other measurements.
    \item At a ``depth of no motion'' the pressure gradient is assumed to be zero, so a surface pressure gradient is added to the two points to make that be the case.
    \item If there are multiple stations in a line the surface pressure gradient is integrated to make a dynamic height with an arbitrary zero at some location.
\end{enumerate}

We will come back to \emph{why} the dynamic height looks the way it does in the next chapter.

\section{Coastal upwelling, complete picture}

Using the Ekman and geostrophic balances we are now in a position to explain the coastal upwelling example in detail (i.e. \fref{fig:PerlinFig3a}, one snapshot in detail in \fref{fig:ShelfDetail}).

\begin{figure}[hbt]
  \begin{center}
    \includegraphics{figs/Geostrophic/ShelfDetail}
    \caption{Data from \fref{fig:PerlinFig3a}.}
    \label{fig:ShelfDetail}
  \end{center}
\end{figure}

First, the wind starts to blow towards the south; this sets up a southwards windstress $\tau_y^w$ (\fref{fig:CoastalStep1}, red circle).  When this flow is in a steady-state Ekman balance there will be an equal and opposite Coriolis force to the north (\fref{fig:CoastalStep1}, magenta circle).  That implies an offshore Ekman transport to the west given by
\begin{equation}
  u_{Ek}D = \frac{\tau_y^w}{\rho_0 f}
\end{equation}

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=2.85in]{figs/Geostrophic/CoastalStep1}
    \caption{On a continental slope, initially at rest, a wind stress blows from north to south (dotted red circle)}
    \label{fig:CoastalStep1}
  \end{center}
\end{figure}

The offshore Ekman transport drops the sealevel at the coast (\fref{fig:CoastalStep2}, surface interface).   This creates a surface pressure gradient force that acts to push water back towards the coast (\fref{fig:CoastalStep2}, red arrow).  Again, in \emph{steady state} geostrophic balance, the pressure gradient force is balanced by a Coriolis force offshore, which must be generated by a geostrophic velocity to the south.  Note that this evolves in time with the deep water \emph{initially} moving onshore, but then being turned south (to the right) by the Coriolis force.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=2.85in]{figs/Geostrophic/CoastalStep2}
    \caption{After the Ekman transport has been active for a while, the sea level drops at the coast (green surface), and an interior geostrophic balanced flow is set up.}
    \label{fig:CoastalStep2}
  \end{center}
\end{figure}

At this point the flow cannot be in steady state because we are transporting water offshore in the surface Ekman layer, but not having any onshore transport to replace it.  Without this return flow, the sea surface will continue to increase its tilt.  The return flow happens in the bottom Ekman layer.  The interior geostrophic flow moves south, so there is a corresponding bottom stress to the north $\tau_y^B$(\fref{fig:CoastalStep3}, red circle at bottom).  In a steady Ekman balance, this implies a Coriolis force to the south, which implies a net Ekman transport onshore (\fref{fig:CoastalStep3}, blue arrows, bottom), given by
\begin{equation}
  u_{Ek}D = \frac{\tau_y^B}{\rho_0 f}
\end{equation}

The offshore Ekman transport drops the sealevel at the coast (\fref{fig:CoastalStep3})
\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=2.85in]{figs/Geostrophic/CoastalStep3}
    \caption{}
    \label{fig:CoastalStep3}
  \end{center}
\end{figure}

When is the whole flow balanced?  When $\tau^B = \tau^W$ so that the transport in the bottom Ekman layer is the same as the transport in the surface Ekman layer.  So, if we know the wind stress, and have a good estimate of the bottom \Wikiref{drag co-efficient}, $C_D\approx10^{-3}$, then we can guess what the mean geostrophic velocity is, and hence the size of the sea surface tilt.  For the Oregon shelf, $\tau_y^W\approx -0.15\ \mathrm{N\,m^{-2}}$ (minus means to the south).  Than means that the geostrophic velocity $v_g \approx 0.35\ \mathrm{m\,s^{-1}}$ in order for $\rho_0 C_D |v_g|^2$ to be large enough to provide the bottom stress to match.  This is approximately the same as the observations near the bottom.

We can further calculate the sea-surface height drop across the 20-km width of the shelf from $g \frac{d\eta}{dx} = f v_g$ or $\Delta \eta =  0.08\ \mathrm{m}$ across the shelf.

\subsection{Internal dynamics on shelf}

The final thing we notice about the shelf observations is that the  flow is slower towards the sea-floor than at the surface.  This is also largely explained by the geostrophic balance.  Note that the isopycnals tilt up towards the coast, counteracting the surface pressure gradient (also sketched in \fref{fig:CoastalTilt}).  This means that the surface tilt is actually larger than we calculated above if the bottom velocity is to remain $0.38\ \mathrm{m\,s^{-1}}$, leading to faster velocities near the surface.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=2.85in]{figs/Geostrophic/CoastalTilt}
        \caption{Sketch of pressure gradient changes if there are titled isopycnals (cyan line) in the section, as observed in the coastal data.  The tilted isopycnal counteracts the surface tilt, slowing the barotropic flow.  }
    \label{fig:CoastalTilt}
  \end{center}
\end{figure}

\clearpage
\section{Exercises}

\paragraph{Geostrophic balance two stations}

Calculate the geostrophic flow at two stations 50 km apart, assuming $f=10^{-4}\ \mathrm{rad\,s^{-1}}$.
Approximate the density measured at each location as 50-m blocks of homogeneous density:

\begin{tabular}{l|cc}
  $z\ \mathrm{[m]}$ & $\rho_A \mathrm{[kg\,m^{-3}]}$ & $\rho_B \mathrm{[kg\,m^{-3}]}$ \\
  \hline
  0--50 & 1021& 1020 \\
  50--100& 1022& 1021\\
  100--150& 1024& 1023\\
  150--200& 1025& 1024\\
\end{tabular}

\begin{itemize}
  \item If the surface pressure gradient is zero, what is the internal pressure gradient at 50, 100, 150, and 200 m? (do not drop too many decimal places!)
  \item What is the direction and strength of the \emph{geostrophic} velocity at the 4 depths?
  \item If there is a pressure gradient, what must its direction and strength be so that the geostrophic flow is zero at 200 m?
  \item How large must the surface difference be at the two stations?
  \item What is the new flow at the other depths in the presence of this surface gradient?
\end{itemize}

\paragraph{Geostrophic flow Gulf Stream}

Consider the temperature section across the Gulf Stream (\fref{fig:GulfStreamExample}).  Assuming a level of no motion at approximately 1500 m, what does
\begin{itemize}
    \item the sea-surface tilt look like?
    \item the interior velocity look like (just a sketch)?
    \item where would you expect the Gulf Stream to be the fastest?
\end{itemize}

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=1.8in]{figs/Geostrophic/GulfStreamLoc}
    \includegraphics[width=2.7in]{figs/Geostrophic/GulfStreamTemp}
    \caption{Location of measurements in the Gulf Stream, and plot of temperature across the section \citep{johnsetal95}.}
    \label{fig:GulfStreamExample}
  \end{center}
\end{figure}


\subsection{Indirect utility functions}

The normal utility function is:

\(U=f(\mathbf x)\)

We have our demand:

\(x_{di}=x_{di}(I, \mathbf p)\)

\subsection{The indirect utility function}

We can plut this in to get:

\(U=g(I, \mathbf p)\)


\section{Glossary}
\smallskip \hrule height 2pt \smallskip

 \begin{itemize}
 	\item \textbf{complementary CDF} (CCDF, or survival function) - can ask how often the random variable is above a particular level.
		Has applications in statistical hypothesis testing, for example, because the one-sided p-value
			is the probability of observing a test statistic at least as extreme as the one observed.
 	\item \textbf{confidence interval} - An interval that represents the expected range of an estimator if
		an experiment is repeated many times.
	\item \textbf{effect size} - any statistic that quantitatively measures the strength of a phenomenon.
	\item \textbf{jitter} - random noise added to data for purposes of visualization.
 	\item \textbf{oversampling} - The technique of increasing the representation of a sub-population in order to avoid errors due to small sample sizes.
	\item \textbf{p-value} - the \underline{\textbf{p}}robability that, using a given statistical model, the statistical summary
		(such as the sample mean difference between two compared groups) would be the same as or more extreme
		than the actual observed results
	\item \textbf{percentile rank} - The percentage of values in a distribution that are less than or equal to a given value.
	\item \textbf{quantizing} - The opposite of smoothing is discretizing, or quantizing.
	\item \textbf{rank} The index where an element appears in a sorted list.
	\item \textbf{raw moment} - A statistic based on the sum of data raised to a power.
		E.g. the correlation between two variables, the regression coefficient in a regression, the mean difference, ...
	\item \textbf{standard error}: - The RMSE of an estimate, which quantifies variability due to sampling error
		(but not other sources of error).
	\item \textbf{standard score} A value that has been standardized so that it is expressed in standard deviations from the mean.

 \end{itemize}


% Created 2020-07-14 mar 10:56
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{amssymb}
\DeclareMathOperator{\shift}{q}
\DeclareMathOperator{\diff}{p}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{Control computarizado - Asignación de polos}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Control computarizado - Asignación de polos},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

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

\section{2-dof controller}
\label{sec:org70b8192}

\begin{frame}[label={sec:orga124e5e}]{Two-degree-of-freedom controller}
\begin{center}
\includegraphics[width=0.8\linewidth]{../../figures/2dof-block-explicit-no-delay}
\end{center}
\end{frame}

\section{Problem 5.3}
\label{sec:orga34f588}
\begin{frame}[label={sec:org4d6f90f}]{Åström \& Wittenmark problem 5.3}
Consider the system given by the pulse-transfer function
\[ H(z) = \frac{z+0.7}{z^2 -1.8z + 0.81} \]
Use polynomial design (RST) to determine a controller such that the closed-loop system from command input to output has the characteristic polynomial
\[ A_c(z) = z^2 - 1.5z + 0.7. \]
Let the observer polynomial have as low order as possible, and place all observer poles in the origin (deadbeat observer). Consider three cases
\begin{description}
\item[{(a)}] Positional control with cancellation of the process zero
\item[{(b)}] Positional control with no cancellation of the zero
\item[{(c)}] Incremental controller with  no cancellation of the zero
\end{description}
\end{frame}

\begin{frame}[label={sec:org368a5ad}]{Why cancel the process zero?}
Bode plots of closed-loop systems (from reference signal to output) with and without cancellation of the process zero:

\begin{center}
\includegraphics[width=0.6\linewidth]{../../figures/aw5_3_bode}
\end{center}
\end{frame}

\begin{frame}[label={sec:org91db4f3}]{Preliminary exercise}
Which of the closed-loop responses below  corresponds to (I) Positional control with zero cancellation (II), Positional control without zero cancellation, (III) Incremental control without zero cancellation.
\begin{center}
\includegraphics[width=0.45\linewidth]{../../figures/aw5_3_refstep}
\includegraphics[width=0.45\linewidth]{../../figures/aw5_3_diststep}
\end{center}
\end{frame}
\end{document}


% !TEX root = main.tex
%----------------------------------------------------------------------

%----------------------------------------------------------------------
\chapter{The Bivariate Normal Distribution}

%The normal distribution is based on the Gaussian integral
%\[
%\int_{-\infty}^{\infty} e^{-x^2} = \sqrt{\pi},
%\]

%----------------------------------------------------------------------
\section{Bivariate transformations}

% definition
\begin{definition}\label{def:jacobian}
Let $h:\R^2\to\R^2$ and let $(u,v) = h(x,y)$. The \emph{Jacobian determinant} of the transformation $h$ is the determinant of its $2\times 2$ matrix of partial derivatives:
\[
J = \begin{vmatrix}
\displaystyle\frac{\partial u}{\partial x} & \displaystyle\frac{\partial u}{\partial y} \\[2ex]
\displaystyle\frac{\partial v}{\partial x} & \displaystyle\frac{\partial v}{\partial y} \\[2ex]
\end{vmatrix}
\]
\end{definition}

% theorem: continuous
\begin{theorem}\label{thm:bivariate_transformation_pdf}
Let $U$ and $V$ be jointly continuous random variables, let $f_{U,V}$ be their joint PDF, let $g:\R^2\to\R^2$ be an injective transform over the support of $f_{U,V}$ and let $(X,Y) = g(U,V)$. Then the joint PDF of $X$ and $Y$ is given by
\[
f_{X,Y}(x,y) = |J| f_{U,V}\big[ g^{-1}(x,y) \big]
\]
where $J$ is the Jacobian determinant of the transformation $g^{-1}$,
\[
J =
\begin{vmatrix}
\displaystyle\frac{\partial u}{\partial x} & \displaystyle\frac{\partial u}{\partial y} \\[2ex]
\displaystyle\frac{\partial v}{\partial x} & \displaystyle\frac{\partial v}{\partial y} \\[2ex]
\end{vmatrix}
\]
where $(u,v) = g^{-1}(x,y)$.
\end{theorem}

% remark
\begin{remark}
The absolute value $|J|$ is a scale factor, which ensures that the transformed PDF $f_{X,Y}(x,y)$ integrates to one.
\end{remark}

% example: jointly continuous
\begin{example}
Let $U$ and $V$ be continuous random variables, and let $X=U+V$ and $Y=U-V$.
\ben
\it Find the joint PDF of $X$ and $Y$ in terms of the joint PDF of $U$ and $V$.
\it If $U,V\sim\text{Exponential}(1)$ are independent, find the joint PDF of $X$ and $Y$.
\een
\end{example}

\begin{solution}
\ben

\it % <<< (i)
\bit
\it The transformation $g:\R^2\to\R^2$ is defined by $g(u,v) = (u+v,u-v)$.
\it To compute the inverse transformation $g^{-1}:\R^2\to\R^2$, we solve the equations %$x=u+v$ and $y=u-v$ for $u$ and $v$.
\[
x=u+v \quad\text{and}\quad y=u-v.% \quad \text{for $u$ and $v$.}
\]
\it This yields $u = \frac{1}{2}(x+y)$ and $v = \frac{1}{2}(x-y)$.
\it Thus the inverse transformation is
\[
(u,v) = g^{-1}(x,y) = \left[\frac{1}{2}(x+y),\frac{1}{2}(x-y)\right].
\]
\eit
The Jacobian determinant is given by
\[
J =
\begin{vmatrix}
\displaystyle\frac{\partial u}{\partial x} & \displaystyle\frac{\partial u}{\partial y} \\[2ex]
\displaystyle\frac{\partial v}{\partial x} & \displaystyle\frac{\partial v}{\partial y} \\
\end{vmatrix}
=
\begin{vmatrix}
1/2	& 1/2 \\
1/2 & -1/2 \\
\end{vmatrix}
= -\frac{1}{4} - \frac{1}{4} = -\frac{1}{2}.
\]
Hence the joint PDF of $X$ and $Y$ is
\begin{align*}
f_{X,Y}(x,y)
	& = |J|f_{U,V}(u,v) \\
	& = \left|-\frac{1}{2}\right| f_{U,V}\left[\frac{1}{2}(x+y),\frac{1}{2}(x-y)\right] \\
	& = \frac{1}{2}f_{U,V}\left[\frac{1}{2}(x+y),\frac{1}{2}(x-y)\right].
\end{align*}

\it % <<< (ii)
Let $U$ and $V$ be independent with $U,V\sim\text{Exponential}(1)$.

By independence, the joint PDF of $U$ and $V$ is
\[
f_{U,V}(u,v)
	= \begin{cases}
		e^{-(u+v)}		& u,v > 0 \\
		0				& \text{otherwise.}
\end{cases}
\]
To compute the support of $f_{X,Y}$, since $u>0$ and $v>0$ we have $x>0$, so
\bit
\it $\min(y) = \min(u-v) = -x$ (which occurs when $u=0$ and $v=x$), and
\it $\max(y) = \max(u-v) = x$ (which occurs when $u=x$ and $v=0$).
\eit
Thus, substituting for $u+v = \frac{1}{2}(x+y) + \frac{1}{2}(x-y) = x$, we obtain
\[
f_{X,Y}(x,y) = \begin{cases}
	\frac{1}{2}e^{-x}	& \text{for } x > 0 \text{ and } -x < y < x, \\
	0					& \text{otherwise.}
\end{cases}
\]
\een
\end{solution}

%----------------------------------------------------------------------
\section{The bivariate normal distribution}
%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%\section{The sum of two random variables}
%%----------------------------------------------------------------------
%% theorem
%\begin{theorem}\label{thm:convolution}
%Let $X$ and $Y$ be jointly continuous random variables, and let $f_{X,Y}$ be their joint PDF. The random variable $X+Y$ has the following PDF:
%\[
%f_{X+Y}(t)
%	= \int_{-\infty}^{\infty} f_{X,Y}(x,t-x)\,dx
%	= \int_{-\infty}^{\infty} f_{X,Y}(t-y,y)\,dy.
%\]
%\end{theorem}
%
%\begin{proof}
%Let $A = \{(x,y):x+y\leq z\}\subset\R^2$. Then
%\[
%\prob(X+Y\leq z)
%	= \iint_A f(x,y)\,dxdy
%	= \int_{x=-\infty}^{\infty} \int_{y=-\infty}^{z-x}f_{X,Y}(x,y)\,dy\,dx
%\]
%We change the variable of integration (in the inner integral), making the substitution $y=t-x$:
%\begin{align*}
%F_{X+Y}(z) = \prob(X+Y\leq z)
%	& = \int_{x=-\infty}^{\infty} \int_{t=-\infty}^{z}f_{X,Y}(x,t-x)\,dt\,dx \\
%	& = \int_{t=-\infty}^{z} \int_{x=-\infty}^{\infty}f_{X,Y}(x,t-x)\,dx\,dt
%\end{align*}
%where the final equality follows by reversing the order of integration. Thus the PDF of $X+Y$ is
%\[
%f_{X+Y}(t) = \int_{-\infty}^{\infty} f_{X,Y}(x,t-x)\,dx \qquad\text{as required.}
%\]
%\end{proof}
%
%% corollary
%\begin{corollary}\label{cor:convolution_independent}
%If $X$ and $Y$ are independent, the PDF of $X+Y$ is the \emph{convolution} of the marginal PDFs:
%\[
%f_{X+Y}(t)
%	= \int_{-\infty}^{\infty} f_X(x)f_Y(t-x)\,dx
%	= \int_{-\infty}^{\infty} f_X(t-y)f_Y(y)\,dy.
%\]
%\end{corollary}

%\begin{remark}
%The function $f_{X+Y}$ is called the \emph{convolution} of $X$ and $Y$, and is often written as $f_{X+Y}=f_X\ast f_Y$.
%\end{remark}

%%----------------------------------------------------------------------
%\section{The sum of two standard normal variables}
%%----------------------------------------------------------------------
%% lemma: sums of normal random variables}
%\begin{lemma}\label{lem:sum_of_standard_normal_variables}
%If $U,V\sim N(0,1)$ are independent, then $U+V\sim N(0,2)$.
%\end{lemma}
%
%\begin{proof}
%Since $U$ and $V$ are independent, their joint PDF is
%\[
%f(u,v)=f_U(u)f_V(v) = \frac{1}{2\pi}\exp\left(-\frac{1}{2}(u^2 + v^2)\right) \qquad  u,v\in\R.
%\]
%
%Let $W=U+V$. Since $U$ and $V$ are independent, by Corollary~\ref{cor:convolution_independent} we have
%\begin{align*}
%f_W(w)
%	& = \int_{-\infty}^{\infty} f_U(u)f_V(w-u)\,du \\
%	& = \frac{1}{2\pi} \int_{-\infty}^{\infty} \exp\left[-\frac{1}{2}\left(u^2 + (w-u)^2\right)\right]\,du \\
%	& = \frac{1}{2\pi} e^{-\frac{1}{4}w^2} \int_{-\infty}^{\infty} \exp\left[-\left(u-\frac{w}{2}\right)^2\right]\,du
%\end{align*}
%We change the variable of integration, by making the substitution $t = \displaystyle\sqrt{2}\left(u-\frac{w}{2}\right)$:
%\[
%f_W(w)
%	= \frac{1}{2\sqrt{\pi}} e^{-\frac{1}{4}w^2} \int_{-\infty}^{\infty} \frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}w^2}\,dv
%	= \frac{1}{2\sqrt{\pi}} e^{-\frac{w^2}{4}},
%\]
%which is the PDF of the $N(0,2)$ distribution
%\end{proof}
%
%Lemma~\ref{lem:sum_of_standard_normal_variables} is a special case of the following theorem.
%% theorem

%%----------------------------------------------------------------------
%\section{The standard bivariate normal distributrion}
%%----------------------------------------------------------------------

\begin{theorem}\label{thm:sum_of_normal_variables}
if $X\sim N(\mu_1,\sigma_1^2)$ and $Y\sim N(\mu_2,\sigma_2^2)$ are independent, then
\[
X+Y\sim N(\mu_1+\mu_2, \sigma_1^2+\sigma_2^2).
\]
\end{theorem}

%The following corollary will be useful later on:
% corollary
\begin{corollary}\label{cor:lin_comb_std_normal}
If $U,V\sim N(0,1)$ are independent, then $aU+bV\sim N(0,a^2+b^2)$ for all $a,b\in\R$.
\end{corollary}
%If $U$ and $V$ are independent standard normal variables, their joint PDF of is
%
%\[
%f(u,v)=f_U(u)f_V(v) = \frac{1}{2\pi}\exp\left(-\frac{1}{2}(u^2 + v^2)\right) \qquad  u,v\in\R.
%\]

% definition: standard bivariate normal
\begin{definition}\label{def:standard_bivariate_normal}
A pair of random variables $U$ and $V$ have the \emph{standard bivariate normal distribution} if their joint PDF $f:\R^2\to[0,\infty)$ can be written as
\[
f_{U,V}(u,v) = \frac{1}{2\pi\sqrt{1-\rho^2}}\exp\left(-\frac{1}{2(1-\rho^2)}\big(u^2 - 2	\rho uv + v^2\big)\right)
\]
where $\rho$ is a constant satisfying $-1 < \rho < 1$.
\end{definition}

% definition: bivariate normal
\begin{definition}\label{def:bivariate_normal}
A pair of random variables $X$ and $Y$ are said to have \emph{bivariate normal distribution} with means $\mu_1$ and $\mu_2$, variances $\sigma_1^2$ and $\sigma_2^2$ and correlation $\rho$, if their joint PDF can be written as
%\small
\[
f_{X,Y}(x,y)
	= \frac{1}{2\pi\sigma_1\sigma_2\sqrt{1-\rho^2}}
		\exp\left(-\frac{1}{2(1-\rho^2)}\left[\left(\frac{x-\mu_1}{\sigma_1}\right)^2
			-2\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\left(\frac{y-\mu_2}{\sigma_2}\right)
				+\left(\frac{y-\mu_2}{\sigma_2}\right)^2 \right]\right)
\]
\normalsize
\end{definition}

% technical result
The following lemma can be used to derive many properties of the bivariate normal distribution.
\begin{lemma}\label{lem:trick}
Let $U,V\sim N(0,1)$ be independent, let $\rho\in(-1,+1)$. Then the random variables
\begin{align*}
X & = \mu_1 + \sigma_1 U, \\
Y & = \mu_2 + \sigma_2\big(\rho U +\sqrt{1-\rho^2}V\big)
\end{align*}
have bivariate normal distribution with means $\mu_1$ and $\mu_2$, variances $\sigma_1^2$ and $\sigma_2^2$, and correlation $\rho$.
\end{lemma}

% proof
\begin{proof}
To find the joint PDF of $X$ and $Y$, let $g(u,v)$ denote the transformation:
\[
g(u,v) = \big[\mu_1 + \sigma_1 u, \mu_2 + \sigma_2(\rho u +\sqrt{1-\rho^2}v)\big].
\]
The inverse transformation is
\[
g^{-1}(x,y)
	= \left(\frac{x-\mu_1}{\sigma_1}, \frac{1}{\sqrt{1-\rho^2}}
  \left[\left(\frac{y-\mu_2}{\sigma_2}\right) -\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\right]\right)
\]
The joint PDF of $X$ and $Y$ is $f_{X,Y}(x,y) = |J|f_{U,V}(u,v)$, where $J$ is the Jacobian determinant of the inverse transformation:
\[
J=\begin{vmatrix}
\displaystyle\frac{\partial u}{\partial x}  & \displaystyle\frac{\partial u}{\partial y}  \\[2ex]
\displaystyle\frac{\partial v}{\partial x}  & \displaystyle\frac{\partial v}{\partial y}
\end{vmatrix}
=
\begin{vmatrix}
\displaystyle\frac{1}{\sigma_1}  		& \displaystyle0 \\[2ex]
\displaystyle\frac{1}{\rho\sigma_1}	& \displaystyle\frac{1}{\sigma_2\sqrt{1-\rho^2}}
\end{vmatrix}
=
\frac{1}{\sigma_1\sigma_2\sqrt{1-\rho^2}}
\]

Because $U$ and $V$ are independent,
\[
f_{U,V}(u,v)=f_U(u)f_V(v) = \frac{1}{2\pi}\exp\left(-\frac{1}{2}(u^2 + v^2)\right) \qquad  u,v\in\R.
\]
and since
\begin{align*}
u^2 +v^2
	&  = \left(\frac{x-\mu_1}{\sigma_1}\right)^2
			+ \frac{1}{1-\rho^2}\left[
				\left(\frac{y-\mu_2}{\sigma_2}\right)^2
					- 2\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\left(\frac{y-\mu_2}{\sigma_{2}}\right)
						+\rho^2\left(\frac{x-\mu_1}{\sigma_1}\right)^2 \right]  \\
	& =  \frac{1}{1-\rho^2}\left[\left(\frac{x-\mu_1}{\sigma_1}\right)^2
				-2\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\left(\frac{y-\mu_2}{\sigma_2}\right)
					+\left(\frac{y-\mu_2}{\sigma_2}\right)^2\right]
\end{align*}
it follows that
%\small
\[
f_{X,Y}(x,y)
	= \frac{1}{2\pi\sigma_1\sigma_1\sqrt{1-\rho^2}}
		\exp\left(\frac{-1}{2(1-\rho^2)}\left[\left(\frac{x-\mu_1}{\sigma_1}\right)^2
			-2\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\left(\frac{y-\mu_2}{\sigma_2}\right)
				+\left(\frac{y-\mu_2}{\sigma_2}\right)^2 \right]\right)
\]
\normalsize
as required.
\end{proof}

The following theorem shows that if $X$ and $Y$ have bivariate normal distribution, then any linear combination of $X$ and $Y$ is normally distributed.
% theorem: linear combination
\begin{theorem}\label{thm:lin_comb_bivar_normal}
Let $X$ and $Y$ have bivariate normal distribution with means $\mu_1$ and $\mu_2$, variances $\sigma_1^2$ and $\sigma_2^2$, and correlation $\rho$. Then
%If $X\sim N(\mu_1,\sigma_1^2)$ and $Y\sim N(\mu_2,\sigma_2^2)$ then
\[
aX + bY \sim N\big(a\mu_1 + b\mu_2, a^2\sigma_1^2 + 2ab\sigma_1\sigma_2\rho + b^2\sigma_2^2\big)
\]
\end{theorem}
\begin{proof}
Let $Z=aX+bY$, let $U$ and $V$ be independent standard normal random variables, and let
\begin{align*}
X' & = \mu_1 + \sigma_1 U \\
Y' & = \mu_2 + \sigma_2\big(\rho U +\sqrt{1-\rho^2}V\big)
\end{align*}

By Lemma~\ref{lem:trick}, $X$ and $Y$ have the same joint distribution as $X'$ and $Y'$, so $Z=aX+bY$ has the same distribution as
\[
Z' = aX'+bY' = (a\mu_1 + b\mu_2) + (a\sigma_1 + b\sigma_2\rho)U + b\sigma_2\sqrt{1-\rho^2} V
\]

Because $U,V\sim N(0,1)$ are independent, it follows by Corollary~\ref{cor:lin_comb_std_normal} that
\[
Z' \sim N\left(a\mu_1 + b\mu_2, a^2\sigma_1^2 + 2ab\sigma_1\sigma_2\rho + b^2\sigma_2^2\right),
\]
so $Z=aX+bY$ has normal distribution, as required.
\end{proof}

%----------------------------------------------------------------------
\section{Properties of the bivariate normal distribution}
%----------------------------------------------------------------------

% theorem: properties
\begin{theorem}
Let $X$ and $Y$ have bivariate normal distribution with means $\mu_1$ and $\mu_2$, variances $\sigma_1^2$ and $\sigma_2^2$, and correlation $\rho$. Then
\ben
\it $X\sim N(\mu_1,\sigma_1^2)$ and $Y\sim N(\mu_2,\sigma_2^2)$,
\it $\rho$ is the correlation coefficient of $X$ and $Y$, and
\it $X$ and $Y$ are independent if and only if $\rho=0$.
\een
\end{theorem}

% proof
\begin{proof}
%By Lemma~\ref{lem:trick}, let us write
Let $U,V\sim N(0,1)$ and define
\begin{align*}
X & = \mu_1 + \sigma_1 U \\
Y & = \mu_2 + \sigma_2\big(\rho U +\sqrt{1-\rho^2}V\big)
\end{align*}

\ben
\it % (i)
In the proof of Theorem~\ref{thm:lin_comb_bivar_normal}:
\bit
\it taking $a=1$ and $b=0$ yields $X\sim N(\mu_1,\sigma_1^2)$, and
\it taking $a=0$ and $b=1$ yields $Y\sim N(\mu_2,\sigma_2^2)$.
\eit
\it % (ii)
Using the fact that $\cov(aX+b,cY+d)=ac\cov(X,Y)$ for all $a,b,c,d\in\R$,
\begin{align*}
\cov(X,Y)
	& = \cov\big[\mu_1 + \sigma_1 U, \mu_2 + \sigma_2(\rho U + \sqrt{1-\rho^2}V)\big] \\
	& = \sigma_1\sigma_2\cov(U, \rho U + \sqrt{1-\rho^2}V) \\
	& = \sigma_1\sigma_2\big[\rho\expe(U^2) + \sqrt{1-\rho^2}\expe(UV)\big] \\
	& = \sigma_1\sigma_2\rho.
\end{align*}
Thus $\rho = \displaystyle\frac{\cov(X)}{\sqrt{\var(X)}\sqrt{\var(Y)}}$ as required.

\it % (iii)
If $X$ and $Y$ are independent, they are uncorrelated. If $X$ and $Y$ are uncorrelated then $\rho=0$, so the joint PDF of $X$ and $Y$ satisfies
\begin{align*}
f_{X,Y}(x,y)
	& = \frac{1}{2\pi\sigma_1\sigma_2}\exp\left(-\frac{1}{2}\left[\left(\frac{x-\mu_1}{\sigma_1}\right)^2 + \left(\frac{y-\mu_2}{\sigma_2}\right)^2 \right]\right) \\
	& = \frac{1}{\sqrt{2\pi}\sigma_1}\exp\left(-\frac{1}{2}\left(\frac{x-\mu_1}{\sigma_1}\right)^2\right)
			\times \frac{1}{\sqrt{2\pi}\sigma_2}\exp\left(-\frac{1}{2}\left(\frac{y-\mu_2}{\sigma_2}\right)^2\right) \\
	& = f_X(x)f_Y(y).
\end{align*}
Because this holds for all $x,y\in\R$, it follows that $X$ and $Y$ are independent.
\een
\end{proof}

%----------------------------------------------------------------------
\section{Conditional distributions}
%----------------------------------------------------------------------

% theorem
\begin{theorem}
Let $X$ and $Y$ have bivariate normal distribution with means $\mu_1$ and $\mu_2$, variances $\sigma_1^2$ and $\sigma_2^2$, and correlation $\rho$.
Then the conditional distribution of $Y$ given $X=x$ is also normal, with conditional mean and variance given by
\begin{align*}
\expe(Y|X=x)	& = \mu_2 + \rho\left(\frac{\sigma_2}{\sigma_1}\right)(x - \mu_1), \\[2ex]
\var(Y|X=x) 	& = \sigma_2^2(1-\rho^2),
\end{align*}
and the conditional mean and variance of $Y$ given $X$ is
\begin{align*}
\expe(Y|X)	& = \expe(Y) + \frac{\cov(X,Y)}{\var(X)}\big[X - \expe(X)\big], \\[2ex]
\var(Y|X) 	& = \var(Y)(1-\rho^2).
\end{align*}
\end{theorem}

% proof
\begin{proof}
Let $U,V\sim N(0,1)$ be independent, and define the random variables
\begin{align*}
X & = \mu_1 + \sigma_1 U, \\
Y & = \mu_2 + \sigma_2\big[\rho U +\sqrt{1-\rho^2}V\big] \\
	& = \mu_2 + \sigma_2\left[\rho \left(\frac{X-\mu_1}{\sigma_1}\right) +\sqrt{1-\rho^2}V\right].
\end{align*}
If $X$ is fixed at $x$, then $Y$ is a linear transformation of $V$, so the conditional distribution of $Y$ given that $X=x$ is a normal distribution.
Furthermore, since $\expe(V)=0$ and $\var(V)=1$ we have
\begin{align*}
\expe(Y|X=x) 	& = \mu_2 + \rho\left(\frac{\sigma_2}{\sigma_1}\right)(x - \mu_1) \\[2ex]
\var(Y|X=x)	& = \sigma_2^2(1-\rho^2)
\end{align*}
as required.
\end{proof}

% remarks
\begin{remark}
\bit
\it
Given $X=x$, the conditional mean is obtained by adjusting $\expe(Y)$ by an amount proportional to the difference between $X=x$ and its mean $\expe(X)$. The size of this adjustment is determined by (1) the size of $Y$ relative to $X$, expressed by the ratio $\sigma_2/\sigma_1$, and (2) the correlation coefficient $\rho$, which quantifies the linear dependence between $X$ and $Y$. Note that $\rho=0$ implies that $\expe(Y|X=x)=0$ for all $x$ (which is not surprising, given that uncorrelated normal variables are independent).
\it
The conditional variance $\var(Y|X=x)$ quantifies the variability in $Y$ that is \emph{not} explained by the fact that $X$ takes the value $x$. The squared correlation coefficient $\rho^2$ thus quantifies the proportion of the overall variance $\var(Y)$ accounted for by the fact that $X=x$.
\it
This idea of `explained' and `unexplained' variance is apparent in the law of total variance:
\[
\var(Y) = \expe\big[\var(Y|X)\big] + \var\big[\expe(Y|X)\big]
\]
	\bit
	\it $\var(Y)$ is the total variance,
	\it $\expe\big[\var(Y|X)\big]$ is the variance explained by $X$, and
	\it $\var\big[\expe(Y|X)\big]$ is the variance not explained by $X$.
	\eit
\eit
\end{remark}

%----------------------------------------------------------------------
\section{Mulivariate transformations}
%----------------------------------------------------------------------

% definition
\begin{definition}\label{def:jacobian}
Let $h:\R^n\to\R^n$ be a transformation of $n$ variables, and let $(x_1,x_2,\ldots,x_n) = h(y_1,y_2,\ldots,y_n)$. The \emph{Jacobian} (or \emph{Jacobian determinant}) of the transformation $h$ is the determinant of its $n\times n$ matrix of partial derivatives:
\[
J =
\begin{vmatrix}
\displaystyle\frac{\partial x_1}{\partial y_1} & \displaystyle\frac{\partial x_1}{\partial y_2} & \cdots &  \displaystyle\frac{\partial x_1}{\partial y_n}\\[2ex]
\displaystyle\frac{\partial x_2}{\partial y_1} & \displaystyle\frac{\partial x_2}{\partial y_2} & \cdots &  \displaystyle\frac{\partial x_2}{\partial y_n}\\[2ex]
\vdots & \vdots & \ddots & \vdots \\[2ex]
\displaystyle\frac{\partial x_n}{\partial y_1} & \displaystyle\frac{\partial x_n}{\partial y_2} & \cdots &  \displaystyle\frac{\partial x_n}{\partial y_n}\\[2ex]
\end{vmatrix}
\]
\end{definition}

% theorem: continuous
\begin{theorem}\label{thm:transf_injective_joint_continuous}
Let $X=(X_1,X_2,\ldots,X_n)$ be a vector of continuous random variables, let $f_X(x_1,x_2\ldots,x_n)$ be their joint PDF, let $g:\R^n\to\R^n$ be an injective transformation, and let $Y=(Y_1,Y_2,\ldots,Y_n)$ be the continuous random vector defined by $Y=g(X)$, i.e.\
\[
(Y_1,Y_2,\ldots,Y_n) = g(X_1,X_2,\ldots,X_n)
\]
If the Jacobian of the inverse transformation $g^{-1}$ is continuous and non-zero over the range of the transformation, the joint PDF of $Y$ is
\[
f_Y(y_1,y_2,\ldots,y_n) = |J| f_X(x_1,x_2,\ldots,x_n)
\]
where
\[
(x_1,x_2,\ldots,x_n)=g^{-1}(y_1,y_2,\ldots,y_n)
\]
is the solution of $(y_1,y_2,\ldots,y_n)=g(x_1,\ldots,x_n)$.
\end{theorem}

% remark
\begin{remark}
The scale factor $|J|$ ensures that $f_Y(y_1,y_2,\ldots,y_n)$ integrates to one.
\end{remark}

%==================================================================================================
\section{The multivariate normal distribution}
%==================================================================================================
The bivariate normal distribution can be formulated using matrix notation. Let
\[
\mathbf{Z}		= \begin{pmatrix} X \\ Y \end{pmatrix},\quad
\mathbf{\mu}	= \begin{pmatrix} \mu_1 \\ \mu_2 \end{pmatrix}\quad\text{and}\quad
\Sigma			= \begin{pmatrix} \sigma^2_1 & \rho\sigma_1\sigma_2 \\ \rho\sigma_1\sigma_2 & \sigma^2_2 \end{pmatrix}
\]

The matrix $\Sigma$ is called the \emph{variance-covariance} matrix.
\bit
\it The determinant of $\Sigma$ is
\[
|\Sigma| = (1-\rho^2)\sigma^2_1\sigma^2_2.
\]
\it The inverse of $\Sigma$ is
\[
\Sigma^{-1} = \frac{1}{(1-\rho^2)\sigma^2_1\sigma^2_2}
\begin{pmatrix}
	\sigma^2_2  				& -\rho\sigma_1\sigma_2 \\
	-\rho\sigma_1\sigma_2	& \sigma^2_1
\end{pmatrix}.
\]
\eit

Let $\mathbf{z} = (x,y)^T$ and consider the quadratic form
\begin{align*}
\big(\mathbf{z}-\mathbf{\mu})^T \Sigma^{-1}(\mathbf{z}-\mathbf{\mu}\big)
	& = 	\frac{1}{(1-\rho^2)\sigma^2_1\sigma^2_2}\
			\begin{pmatrix} x-\mu_1 \\ y-\mu_2	\end{pmatrix}^T
			\begin{pmatrix} \sigma^2_2 & -\rho\sigma_1\sigma_2 \\ -\rho\sigma_1\sigma_2 & \sigma^2_1 \end{pmatrix}
			\begin{pmatrix} x-\mu_1 \\ y-\mu_2	\end{pmatrix} \\
	& = \frac{1}{(1-\rho^2)}\left[\left(\frac{x-\mu_1}{\sigma_1}\right)^2
			 -2\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\left(\frac{y-\mu_2}{\sigma_2}\right)
			 	+\left(\frac{y-\mu_2}{\sigma_2}\right)^2\right]
\end{align*}

The PDF $f(x,y)$ of the bivariate normal distribution can therefore be written as
\[
f(x,y) = \frac{1}{2\pi|\Sigma|^{1/2}}\exp\left(-\frac{1}{2}(\mathbf{z}-\mathbf{\mu})^T \Sigma^{-1} (\mathbf{z}-\mathbf{\mu})\right).
\]

Using this notation, we define the \emph{multivariate normal distribution} as follows.

% definition: multivariate normal
\begin{definition}
A random vector $\mathbf{Z}=(X_1,X_2,\ldots,X_n)^T$ is said to have \emph{multivariate normal distribution} with mean vector $\mathbf{\mu}= (\mu_1,\mu_2,\ldots,\mu_n)^T$ and variance-covariance matrix
\[
\Sigma			= \begin{pmatrix}
	\sigma^2_1		 			& \rho_{12}\sigma_1\sigma_2  	& \ldots & \rho_{1n}\sigma_1\sigma_n \\
	\rho_{21}\sigma_1\sigma_2 	& \sigma^2_2					& \ldots & \rho_{2n}\sigma_2\sigma_n \\
	\vdots						& \vdots						& \ddots & \vdots \\
	\rho_{n1}\sigma_1\sigma_n		 	& \rho_{n2}\sigma_n\sigma_2	& \ldots & \sigma^2_n \\
	\end{pmatrix}
\]
if its joint density function can be written as
\[
f(\mathbf{z}) = \frac{1}{2\pi|\Sigma|^{1/2}}\exp\left(-\frac{1}{2}(\mathbf{z}-\mathbf{\mu})^T \Sigma^{-1} (\mathbf{z}-\mathbf{\mu})\right)
\]
\end{definition}

where $\mu_i = \expe(X_i)$, $\sigma_i^2 = \var(X_i)$ and $\rho_{ij} = \displaystyle\frac{\expe(X_iX_j)}{\sigma_i\sigma_j}$.

\begin{exercise}
\begin{questions}
%----------------------------------------
% GS p100
\question
Let $X$ and $Y$ have standard bivariate normal distribution, with joint PDF given by
%\[
%f(x,y)
%	= \frac{1}{2\pi\sigma_1\sigma_2\sqrt{1-\rho^2}}
%		\exp\left(-\frac{1}{2(1-\rho^2)}\left[\left(\frac{x-\mu_1}{\sigma_1}\right)^2
%			-2\rho\left(\frac{x-\mu_1}{\sigma_1}\right)\left(\frac{y-\mu_2}{\sigma_2}\right)
%				+\left(\frac{y-\mu_2}{\sigma_2}\right)^2 \right]\right)
%\]
\[
f(x,y) = \frac{1}{2\pi\sqrt{1-\rho^2}}\exp\left(-\frac{1}{2(1-\rho^2)}(x^2 - 2\rho xy + y^2)\right)
\]
where $\rho$ is a constant satisfying $-1 < \rho < 1$.
\begin{parts}
\part
Check that $f(x,y)$ is indeed a joint PDF, by verifying that $f(x,y)\geq 0$ and $\displaystyle\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} f(x,y)\,dxdy = 1$.
\begin{answer}
TODO
\end{answer}
\part
Check that $\cov(X,Y) = \displaystyle\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} xy f(x,y)\,dxdy = \rho$.
\begin{answer}
TODO
\end{answer}
\part
Show that if $X$ and $Y$ are uncorrelated, then they are independent.
\begin{answer}
TODO
\end{answer}
\end{parts}
%----------------------------------------
\question
% GS p110
Let $X$ and $Y$ have standard bivariate normal distribution. Find the conditional distribution of $Y$ given $X=x$, and hence show that $\expe(Y|X) = \rho X$.
\begin{answer}
The conditional distribution of $Y$ given $X=x$ is $N(\rho x, 1-\rho^2)$.
\end{answer}
%----------------------------------------
% GS 4.7.5
\question
Let $X$ and $Y$ have standard bivariate normal distribution. Show that $X$ and $\displaystyle Z=\frac{Y-\rho X}{\sqrt{1-\rho^2}}$ are independent standard normal random variables.
\begin{answer}
TODO
\end{answer}
%----------------------------------------
% GS 4.7.6
\question
Let $X$ and $Y$ have standard bivariate normal distribution, and let $Z=\max\{X,Y\}$. Show that $\expe(Z)=\sqrt{(1-\rho)/\pi}$ and $\expe(Z^2)=1$.
\begin{answer}
TODO
\end{answer}
%----------------------------------------
\question
Let $U,V\sim N(0,1)$. Show that the random variables $X = U+V$ and $Y = U-V$ are independent.

\begin{answer}
\bit
\it The transformation is $g(u,v) = (u+v,u-v)$.
\it To compute the inverse transformation, consider $x=u+v$ and $y=u-v$.
\it Solving these, we obtain $u = \frac{1}{2}(x+y)$ and $v = \frac{1}{2}(x-y)$.
\it Thus the inverse transformation is $(u,v) = g^{-1}(x,y) = \left(\frac{1}{2}(x+y),\frac{1}{2}(x-y)\right)$
\eit

The Jacobian determinant of $g^{-1}(x,y)$ is
\[
J =
\begin{vmatrix}
\frac{\partial u}{\partial x}  & \frac{\partial u}{\partial y}  \\
\frac{\partial v}{\partial x}  & \frac{\partial v}{\partial y}
\end{vmatrix}
=
\begin{vmatrix}[r]
\frac{1}{2}   &  \frac{1}{2} \\
\frac{1}{2}  & -\frac{1}{2}
\end{vmatrix}
=
-\frac{1}{2}
\]
The joint PDF of $U$ and $V$ is
\[
f(u,v) = \frac{1}{2\pi \sqrt{1-\rho ^{2}}} \exp\left( -\frac{u^2 + v^2 - 2\rho uv}{2(1-\rho^2)} \right)
\]

Now,
\begin{align*}
u^2 + v^2 - 2\rho uv
	& = \left(\frac{1}{2}(x+y)\right)^2 +\left(\frac{1}{2}(x-y)\right)^2 - 2\rho\left(\frac{1}{2}(x+y)\right)\left(\frac{1}{2}(x-y)\right) \\
	& = \frac{1}{2}x^2(1-\rho ) + \frac{1}{2}y^2(1+\rho )
\end{align*}

The joint PDF of $X$ and $Y$ is therefore
\begin{align*}
f(x,y)
	& = \frac{1}{2} \frac{1}{2\pi\sqrt{1-\rho ^{2}}}\exp\left( -\frac{x^{2} (1-\rho )}{4(1-\rho ^{2} )} -\frac{y^{2} (1+\rho )}{4(1-\rho ^{2})}\right) \\
	& = \frac{1}{\sqrt{4\pi(1+\rho)}}\exp\left(-\frac{x^{2}}{4(1+\rho)}\right) \times \frac{1}{\sqrt{4\pi(1-\rho)}}\exp\left(-\frac{y^{2}}{4(1-\rho )}\right) \\
\end{align*}
This is the product of the PDF of a $N\big(0,2(1+\rho)\big)$ variable and the PDF of a $N\big(0,2(1-\rho)\big)$ variable. Thus $X$ and $Y$ are independent.
\end{answer}

\question
Let $X$ and $Y$ have bivariate normal distribution with means $\mu_1$ and $\mu_2$, variances $\sigma_1^2$ and $\sigma_2^2$, and correlation $\rho$. Show that the conditional distribution of $Y$ given $X=x$ is
\[
N\left(\mu_2 + \rho\left(\frac{\sigma_2}{\sigma_1}\right)(x-\mu_1), \sigma_2^2(1-\rho^2)\right).
\]
\begin{answer}
TODO
\end{answer}
%----------------------------------------
\question
\begin{parts}
\part % << (i)
Let $X$ and $Y$ be jointly continuous random variables, and let $f_{X,Y}$ be their joint PDF. Show that the PDF of the random variable $X+Y$ can be written as
\[
f_{X+Y}(t)
	= \int_{-\infty}^{\infty} f_{X,Y}(x,t-x)\,dx
	= \int_{-\infty}^{\infty} f_{X,Y}(t-y,y)\,dy.
\]
\begin{answer}
Let $A = \{(x,y):x+y\leq z\}\subset\R^2$. Then
\[
\prob(X+Y\leq z)
	= \iint_A f(x,y)\,dxdy
	= \int_{x=-\infty}^{\infty} \int_{y=-\infty}^{z-x}f_{X,Y}(x,y)\,dy\,dx
\]
We change the variable of integration (in the inner integral), making the substitution $y=t-x$:
\begin{align*}
F_{X+Y}(z) = \prob(X+Y\leq z)
	& = \int_{x=-\infty}^{\infty} \int_{t=-\infty}^{z}f_{X,Y}(x,t-x)\,dt\,dx \\
	& = \int_{t=-\infty}^{z} \int_{x=-\infty}^{\infty}f_{X,Y}(x,t-x)\,dx\,dt
\end{align*}
where the final equality follows by reversing the order of integration. Thus the PDF of $X+Y$ is
\[
f_{X+Y}(t) = \int_{-\infty}^{\infty} f_{X,Y}(x,t-x)\,dx \qquad\text{as required.}
\]
\end{answer}
\part % << (ii)
Hence, or otherwise, show that if $U,V\sim N(0,1)$ are independent, then $U+V\sim N(0,2)$. (This is a special case of Theorem~\ref{thm:sum_of_normal_variables}.)
\begin{answer}
By part (a), if two random variables $X$ and $Y$ are independent, the PDF of $X+Y$ is the \emph{convolution} of the marginal PDFs:
\[
f_{X+Y}(t)
	= \int_{-\infty}^{\infty} f_X(x)f_Y(t-x)\,dx
	= \int_{-\infty}^{\infty} f_X(t-y)f_Y(y)\,dy.
\]
$U$ and $V$ are independent, so their joint PDF is
\[
f(u,v)=f_U(u)f_V(v) = \frac{1}{2\pi}\exp\left(-\frac{1}{2}(u^2 + v^2)\right) \qquad  u,v\in\R.
\]
Let $W=U+V$. Then because $U$ and $V$ are independent,
\begin{align*}
f_W(w)
	& = \int_{-\infty}^{\infty} f_U(u)f_V(w-u)\,du \\
	& = \frac{1}{2\pi} \int_{-\infty}^{\infty} \exp\left[-\frac{1}{2}\left(u^2 + (w-u)^2\right)\right]\,du \\
	& = \frac{1}{2\pi} e^{-\frac{1}{4}w^2} \int_{-\infty}^{\infty} \exp\left[-\left(u-\frac{w}{2}\right)^2\right]\,du
\end{align*}
We change the variable of integration, by making the substitution $t = \displaystyle\sqrt{2}\left(u-\frac{w}{2}\right)$:
\[
f_W(w)
	= \frac{1}{2\sqrt{\pi}} e^{-\frac{1}{4}w^2} \int_{-\infty}^{\infty} \frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}w^2}\,dv
	= \frac{1}{2\sqrt{\pi}} e^{-\frac{w^2}{4}},
\]
which is the PDF of the $N(0,2)$ distribution
\end{answer}
\end{parts}

%----------------------------------------
\end{questions}
\end{exercise}
%----------------------------------------------------------------------


\subsection{Frequent patterns}

\subsubsection{Frequent patterns}

A frequent itemset is one where the support is above a minimum.

We know that if an itemset is frequent, then all its subsets are also frequent.

We look for frequent patterns, \(F\), between the items \(I\).

An example of a frequent pattern is \(\{a,b\}\).


%gln
\section{General linear group}

As the most prominent example of a functor, we will now discuss the following construction. Let $R$, again, denote a unital commutative ring.
\begin{defi}
Let $\mathfrak{F}$ be a finite category and $\mathrm{UCRng}$ the category of unital commutative rings. The bifunctor
$$\bao{rrcl}
\mathrm{Gl} : &\mathfrak{F} \times \mathrm{UCRng} &\longrightarrow &\mathrm{Grp},\\
& (I,R) &\longmapsto& Gl(I,R) := Gl_{|I|}(R),\\
\ea$$
is a pairing of a finite set $I$, a ring $R$ and its group of $R$ module automorphism on the generic module $R^I$.
\end{defi}
\begin{coro}
We  immediately get
$$\bao{rcl}
Gl_0(R) &=& Gl(\emptyset,R) = \{1_R\} \ \mathrm{and}\\
Gl_1(R) &=& Gl(\mathcal{P}(\emptyset),R)\\ &=& R^\times = U(R)\\ &:=& \{r \in R : \exists r' \in R,\ r r' = 1\}.\\
\ea\\$$
\end{coro}
Both statements are clearly a consequence of our above definition.
\begin{theo}
Let $I \in \mathfrak{F}$ be fixed with $|I| = i$ and let $R \in \mathrm{UCRng}$ be an ID then
$$\mathrm{Gl}_i \simeq \mathrm{Hom}_{\mathrm{UCRng}}(\mathbb{Z}[X,Y]/\left<\det X \cdot Y - 1\right>,\ \cdot\ )$$
as functors.
\end{theo}
Before we proceed in proofing the theorem, let us dwell in the above formular for a moment. Firstly, $X$ stands for $\{X_{kl} : 1 \leq k, l \leq i\}$ - a set of $i^2$ variables and
$$\mathbb{Z}[X,Y]/\left<\det X \cdot Y - 1\right> = \mathbb{Z}[X_{kl},Y : 1 \leq k, l \leq i]/\left<\det \left(\bao{ccc}
X_{11} & \ldots & X_{1i}\\
\vdots&&\vdots\\
X_{i1}&\ldots&X_{ii}\ea\right) \cdot Y - 1\right>$$
for the ring of the polynomial ring over $\mathbb{Z}$ in $i^2 + 1$ indeterminants, factored out the ideal generated by $\det X \cdot Y - 1$. Thus, the right hand side claims that the class of all (unital commutative) ring homomorphisms into some ring $R$ are functorially isomorphic to $i$-th general linear group $\mathrm{Gl}_i(R)$. %In classical algebraic geometry we would get the zero set:

%$$Z(\det X \cdot Y - 1) = \left\{x \in \mathbb{A}^{i^2}_R \simeq M_i(R) : \det x \in \mathrm{Gl}_1(R)\right\}$$
%and its spectral interpretation:
%$$\mathfrak{Z}(\det X \cdot Y - 1) = \left\{\prm \in \mathrm{Spec}(\mathbb{Z}[X,Y]/\left<\det X Y - 1\right>) : \det X Y - 1 \in \prm\right\}$$

Given some ring homomorphism $\varphi : A := \mathbb{Z}[X,Y]/\left<\det X Y - 1\right> \longrightarrow R$, we get
$$\mathrm{Spec}(\varphi) = \left[\prm \in \mathrm{Spec}(R) \longmapsto \varphi^{-1}(\prm) := \{f \in A : \varphi(f) \in \prm\}\right],$$
in particular
$$\varphi^{-1}(\prm) \in \mathrm{Spec}(A).$$
Clearly, $\mrm{Spec}(A)$ contains the elements
$$\prm_\mu :=  \left\{\sum_{kl} \lambda_{kl} (X_{kl} - \mu_{kl}) : \mu = \left(\bao{ccc}\mu_{11}&\ldots&\mu_{1i}\\\vdots&&\vdots\\\mu_{i1}&\ldots&\mu_{ii}\\\ea\right), \lambda_{kl} \in A,\ \lambda_\mu (X - \mu) \in \ker eval_{\mu}\right\}$$
mapping the monomial $X$ to $\mu$. As $A$ is a UFD and $(X_{kl} - \mu_{kl})$ cannot have any other divisors we conclude $$\ker eval_\mu \in \spec{A},$$
as $\det \mu \cdot Y - 1 \in R[Y]$ has roots in $R$ (i.e.: for all $\det \mu \in \mrm{Gl}_1(R)$) and more importantly, $(0) \in \spec{R}$.
Here, we are using the following diagram:
$$\xymatrix{
A \ar[rr]^{eval_\mu}\ar[rrd]_f&&R[Y]\ar[d]^{eval_{\det \mu^{-1}}}\\
&&R.\\
}$$
Thus, the pairing
$$\bao{rrcl}
V :& \mrm{Gl}_i(R) \subset \affr{i^2+1} &\longrightarrow& \spec{\zz[X,Y]},\\
&\left(\mu_{k,l},\frac{1}{\det \mu}\right)_{1\leq k,l \leq i} &\longmapsto& \left<X_{kl} - \mu_{kl}, Y - \det \mu^{-1}\right>
\ea$$
lets us consider all points in $\mrm{Gl}_i(R)$ as an algebraic set also as (families of) prime ideals in $\zz[X,Y]$.


\section{How fast will Raft elect a leader when split votes are possible?}
\label{leaderelection:split:total}

Given a split vote rate, we can estimate the total election time.
Raft will elect a leader as soon as an election term
successfully completes without a split vote. When a split vote occurs,
it's likely that all servers have reset their timers, since servers do
this when they grant a vote (this isn't quite true when logs
differ; see Section~\ref{leaderelection:logsdiff}). Thus, the
next election term has the same probability of success as an entirely
new election and will take just as long. In other words, each election
term is essentially memoryless, and the number of election terms
required in an election can be modeled as a geometric distribution,
where the probability of success is the probability that a split vote
does not occur. Therefore, Raft elections are expected to complete in
$\dfrac{1}{1-\text{split vote rate}}$ election terms on average.

If a split vote occurs in a particular election term, the election term
takes about $1+M_s$ time units plus a one-way network latency to reset
the server's election timers. We do not include the time for the
candidate to record its own vote on disk, since this time can be
overlapped with the RequestVote messages (with this optimization, the
candidate may not count its own vote towards leadership until the vote
is durably recorded). After the vote is split, the cluster must wait
another election timeout before the next election term begins. This
repeats for each split vote, then the time for an election with no split
votes (from Section~\ref{leaderelection:nosplit}) is additional. Thus, the
total time for an election, $E_s$, is:
\begin{align*}
E_s &= \Big(\sum_\text{split votes} \text{time for split vote}\Big)
 +
  \Big(\text{time for election with no split vote}\Big) \\
%
E_s &= \Big(\sum_\text{split votes} (1 + M_s + L)\Big)
 +
  \Big(1 + M_s + 2L + W - U(0,\dfrac{1}{2})\Big) & \\
%
\Ex[E_s] &= \Big((\frac{1}{1-\text{split vote rate}} - 1) \times
            (1 + \frac{1}{s+1} + \Ex[L])\Big)
 +
  \Big(1 + \frac{1}{s+1} + 2\Ex[L] + \Ex[W] - \dfrac{1}{4}\Big) \\
%
\Ex[E_s] &= \frac{1}{1-\text{split vote rate}} \times
            \Big(1 + \frac{1}{s+1} + \Ex[L]\Big)
            + \Ex[L] + \Ex[W] - \dfrac{1}{4}
\end{align*}
where $L$ is the one-way network latency and $W$ is the latency for a
durable disk write.

Howard~\cite{Howard:2014} suggests an optimization to decrease the time
for an election after split votes occur. The optimization separates
followers' timeouts from candidates' timeouts, where candidates select
smaller timeouts from a distribution with a smaller range. This results
in faster iterations once split votes have occurred, though it risks
additional split votes. The remainder of this chapter does not use this
optimization.

\begin{figure}
\centering
\includegraphics[height=5.5in]{leaderelection/overall}
\hspace{-2em}
\vcaption[expected overall election time]{
The expected total election times for various clusters,
as defined by $\Ex[E_s]$, with a fixed one-way network latency.
It excludes the time to write to stable
storage (which is usually negligible). The timeout range and
expected overall election time are presented as multiples of the one-way
network latency ($l$), since $l$ is typically fixed in a given
deployment.
}
\label{fig:leaderelection:theory:overall}
\end{figure}

Figure~\ref{fig:leaderelection:theory:overall} plots the expected time to
elect a leader when the network latency is fixed, by combining the
formula for $\Ex[E_s]$ with the formula for $Pr(D_{c,s} \leq l)$.
From the graphs, a Raft cluster with a sufficiently broad timeout range
will usually elect a leader within 20 times the one-way network latency,
even when running with a bare majority of available servers. This
suggests that most datacenter Raft deployments should be able to achieve
typical leader election times under \SI{100}{\milli\second}. Even worst
case global deployments, with one-way latencies of
\SI{200}{\milli\second},
should be able to typically elect leaders within \SI{4}{seconds}. (Election
times may be larger if some servers are deployed on other planets.)

Each of the curves has a knee. If the timeout range is chosen to be too
short, too many servers time out before others are able to collect
votes, resulting in poor election times. Once timeout ranges are
sufficiently large (about 3--8 times the network latency, depending on
the cluster), the curves become linear with a slight upward slope:
elections complete after few or no split votes, but they must wait
longer for each timeout to elapse.

The graphs provide insight into how to configure election timeouts: a
conservative setting is probably best in practice. The minimum point on
the graphs represents the best average election time possible for each
given cluster configuration. However, attaining this minimum time is
quite risky, since the minimum is close to the knee in the curve. If the
network latency turns out to be slightly higher than anticipated in
practice, that might push the system into the left region of the graph
where election times skyrocket. It is better to configure systems
farther to the right, trading off a slightly higher average election
time in exchange for a more robust system. Thus, we recommend using a
timeout range that is ten times the one-way network latency (even if the
true network latency is five times greater than anticipated, most clusters would
still be able to elect a leader in a timely manner).