\subsection{Metric topology}\label{subsec:metric_topology}

\begin{definition}\label{def:metric_space}\mcite[248]{Engelking1989}
  A \term{metric space} is a set \( X \) along with a nonnegative real-valued function \( \rho: X \times X \to [0, \infty) \), called a \term{metric}, also called the \term{distance function}, such that
  \begin{thmenum}[series=def:metric_space]
    \thmitem[def:metric_space/M1]{M1} \( \rho(x, y) = 0 \iff x = y \)
    \thmitem[def:metric_space/M2]{M2}(symmetry) \( \rho(x, y) = \rho(y, x) \)
    \thmitem[def:metric_space/M3]{M3}(triangle inequality) \( \rho(x, y) \leq \rho(x, z) + \rho(z, y) \)
  \end{thmenum}

  If instead of \ref{def:metric_space/M1} we have the weaker condition
  \begin{thmenum}[resume=def:metric_space]
    \thmitem[def:metric_space/pseudometric_identity]{M1'} \( \forall x \in X, \rho(x, x) = 0 \),
  \end{thmenum}
  we call \( \rho \) a \term{pseudometric} and \( (X, \rho) \) a \term{pseudometric space}.

  \begin{thmenum}
    \thmitem{def:metric_space/subspace} If \( A \subseteq X \) is a set, then the restriction \( (A, \rho{\rvert_A}) \) is a metric space and it is called a \term{subspace} of \( X \).

    \thmitem{def:metric_space/ball} Define the function
    \begin{balign*}
       & B: X \times (0, \infty) \to \pow(X),                   \\
       & B(x, r) \coloneqq \{ y \in X \colon \rho(x, y) < r \}.
    \end{balign*}

    The set \( B(x, r) \) is called an \term{open ball} or simply \term{ball} with \term{center} \( x \) and \term{radius} \( r \).

    The ball \( B = B(0, 1) \) is called the \term{unit ball}.

    \thmitem{def:metric_space/closed_ball} The set
    \begin{equation*}
      \overline{B(x, r)} \coloneqq \cl(B(x, r))
    \end{equation*}
    is called the \term{closed ball} with center \( x \) and radius \( r \).

    \thmitem{def:metric_space/sphere} The set
    \begin{equation*}
      S(x, r) \coloneqq \fr{B(x, r)}
    \end{equation*}
    is called the \term{sphere} with center \( x \) and radius \( r \).

    \thmitem{def:metric_space/bounded_set} A set \( A \subseteq X \) is called \term{bounded} if it is contained in some ball \( B(x, r) \).

    \thmitem{def:metric_space/bounded_sequence} A \hyperref[def:sequence]{sequence} \( \{ x_k \}_{k=1}^\infty \subseteq X \) is called \term{bounded} if the corresponding set \( \{ x_k \colon k = 1, 2, \ldots \} \) is \hyperref[def:metric_space/bounded_set]{bounded}.

    \thmitem{def:metric_space/bounded_metric} If every set is bounded, we say that the metric itself is bounded.

    \thmitem{def:metric_space/bounded_function} We say that a function \( f: S \to X \) from a set \( S \) to a metric space \( (X, \rho) \) is \term{bounded} if its image \( f(S) \) is a bounded set in \( (X, \rho) \).

    \thmitem{def:metric_space/diameter} Define the function
    \begin{balign*}
       & \diam: \pow(X) \to [0, \infty],                             \\
       & \diam(A) \coloneqq \sup \{ \rho(x, y) \colon x, y \in A \},
    \end{balign*}
    whose values include the nonnegative extended real \hyperref[def:extended_real_numbers]{numbers}.

    If it exists, we call the number \( \diam(A) \) the \term{diameter of \( A \)}.

    \thmitem{def:metric_space/distance} Define the function
    \begin{balign*}
       & \op{dist}: X \times \pow(X) \to [0, \infty),                    \\
       & \op{dist}(x, A) \coloneqq \inf \{ \rho(x, a) \colon a \in A \}.
    \end{balign*}

    We call the number \( \op{dist}(x, A) \) the \term{distance from the point \( x \) to the set \( A \)}. We use the convention that the infimum of an empty set of real numbers is \( +\infty \), hence \( \op{dist}(x, \varnothing) = \infty \).
  \end{thmenum}
\end{definition}

\begin{proposition}\label{thm:pseudometric_to_metric}
  Let \( (X, \rho) \) be a \hyperref[def:metric_space]{pseudometric space}. Define the equivalence relation
  \begin{equation*}
    x \cong y \iff \rho(x, y) = 0.
  \end{equation*}

  Then the following metric on the \hyperref[thm:equivalence_partition]{quotient set} \( M \coloneqq X / \cong \)
  \begin{balign*}
     & \rho: M \times M \to [0, \infty)    \\
     & \rho([x], [y]) \coloneqq \rho(x, y)
  \end{balign*}
  is well-defined.
\end{proposition}
\begin{proof}
  The function \( \rho \) is well-defined since, if \( x \) and \( y \) both belong to the same equivalence class \( [x] \), then \( \rho(x) = \rho(y) \). Thus, \( \rho \) does not depend on the choice of representatives.

  Additionally, \( \rho \) is a metric since \( \rho([x], [y]) = 0 \) implies that \( [x] = [y] \), that is, \( \rho(x, y) = 0 \).
\end{proof}

\begin{proposition}\label{rem:bounded_set_metric_order_equivalence}
  A set \( A \) in a metric space \( (X, \rho) \) is \hyperref[def:metric_space/bounded_set]{bounded} if and only if the set \( \{ \rho(a, b) \colon a, b \in A \} \) is bounded as a \hyperref[def:partially_ordered_set_extremal_points/upper_and_lower_bounds]{partially ordered set}.
\end{proposition}

\begin{definition}\label{def:metric_topology}\mcite[249]{Engelking1989}
  Let \( (X, \rho) \) be a metric space. We define the \term{metric topology}\( \mscrT \) , also called the \term{induced topology}, as the \hyperref[def:topological_space]{topology} generated by the \hyperref[def:topological_local_base]{neighborhood system}
  \begin{equation}\label{def:metric_topology/integer_base}
    \mathcal{B}(x) \coloneqq \{ B(x, \tfrac 1 n) \colon n = 1, 2, 3, \ldots \}.
  \end{equation}

  If for some topological space \( (X, \mscrT) \) there exists a metric such that \( \mscrT \) is its induced topology, we say that the topology \( \mscrT \) is \term{metrizable}.

  It is often conventional to consider the alternative (larger) base
  \begin{equation}\label{def:metric_topology/real_base}
    \mathcal{B}'(x) \coloneqq \{ B(x, \varepsilon) \colon \varepsilon > 0 \}.
  \end{equation}
\end{definition}
\begin{proof}
  This is indeed a neighborhood system as it satisfies \ref{thm:topological_local_base_axioms/BP1}-\ref{thm:topological_local_base_axioms/BP3}:

  \begin{refenum}
    \refitem{thm:topological_local_base_axioms/BP1} Every point \( x \) belongs to any ball centered at \( x \).

    \refitem{thm:topological_local_base_axioms/BP2} Fix \( x \in X \) and two balls \( B(x, \tfrac 1 n) \) and \( B(x, \tfrac 1 m) \). Then
    \begin{equation*}
      B(x, \tfrac 1 {\max\{ n, m \}}) \subseteq B(x, n) \cap B(x, m).
    \end{equation*}

    \refitem{thm:topological_local_base_axioms/BP3} Fix \( x, y \in X \) and let \( x \in B(y, \tfrac 1 n) \), i.e. \( \rho(x, y) < \tfrac 1 n \).

    \begin{figure}
      \centering
      \text{\todo{Add diagram}}\iffalse\begin{mplibcode}
        u := 1cm;
        r := sqrt(2) / 2;

        pair v;
        v := (1, 1);

        beginfig(1);
        draw fullcircle scaled 3u;
        dotlabel.bot("$y$", origin);

        draw fullcircle scaled 1u shifted (-r * u * v);
        dotlabel.bot("$x_1$", -r * u * v);

        draw fullcircle scaled 1u shifted (r/2 * u * v);
        dotlabel.bot("$x_2$", r/2 * u * v);
        endfig;
      \end{mplibcode}\fi
      \caption{There is a nested ball around every point in an open ball}\label{def:metric_topology/nested_balls}
    \end{figure}

    Define \( m \) to be the the smallest positive integer such that
    \begin{equation*}
      \tfrac 1 m \leq \min\{ \rho(x, \tfrac 1 n), \tfrac 1 n - \rho(x, \tfrac 1 n) \}.
    \end{equation*}

    Note that \( m \) exists since the positive integers are \hyperref[def:well_founded_relation]{well-founded}.

    Let \( z \in B(x, \tfrac 1 m) \). There are two cases:
    \begin{itemize}
      \item If \( \rho(x, y) \leq \tfrac 1 {2n} \), then
            \begin{balign*}
              \rho(z, y)
              \leq
              \rho(z, x) + \rho(x, y)
              <
              \tfrac 1 m + \rho(x, y)
              \leq
              \tfrac 1 n + \tfrac 1 n
              \leq
              2 \tfrac 1 {2n}
              =
              \tfrac 1 n.
            \end{balign*}

      \item If \( \rho(x, y) > \tfrac 1 {2n} \), then
            \begin{balign*}
              \rho(z, y)
              \leq
              \rho(z, x) + \rho(x, y)
              <
              \tfrac 1 m + \rho(x, y)
              \leq
              (\tfrac 1 n - \rho(x, y)) + \rho(x, y)
              =
              \tfrac 1 n.
            \end{balign*}
    \end{itemize}

    In both cases, \( B(x, \tfrac 1 m) \subseteq B(y, \tfrac 1 n) \).
  \end{refenum}
\end{proof}

\begin{proposition}\label{thm:def:metric_topology/properties}
  The metric topology \( \mscrT \) on \( X \) induced by \( \rho \) has the following properties:
  \begin{thmenum}
    \thmitem{thm:def:metric_topology/properties/ball_is_open} For every point \( x \in X \) and any radius \( r > 0 \), the ball \( B(x, r) \) is an open set and, hence, a neighborhood of \( x \).
    \thmitem{thm:def:metric_topology/properties/first_countable} \( \mscrT \) is first-countable.
    \thmitem{thm:def:metric_topology/properties/sequential} \( \mscrT \) is sequential.
    \thmitem{thm:def:metric_topology/properties/hausdorff} \( \mscrT \) is Hausdorff.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:metric_topology/properties/ball_is_open} Obvious from \fullref{def:metric_topology}.

  \SubProofOf{thm:def:metric_topology/properties/first_countable} Since \fullref{def:metric_topology} involves generating a topology using a neighborhood system of countable local neighborhoods, \( \mscrT \) is first-countable.

  \SubProofOf{thm:def:metric_topology/properties/sequential} Follows from \fullref{thm:def:metric_topology/properties/first_countable} and \fullref{thm:first_countable_spaces_are_sequential}.

  \SubProofOf{thm:def:metric_topology/properties/hausdorff} Let \( x, y \in X \) be distinct points. Define
  \begin{equation*}
    r \coloneqq \dfrac 1 2 \rho(x, y),
  \end{equation*}
  so that
  \begin{equation*}
    B(x, r) \cap B(y, r) = \varnothing.
  \end{equation*}
\end{proof}

\begin{definition}\label{def:metric_uniformity}
  Let \( (X, \rho) \) be a metric space.

  We define the \term{metric uniformity} \( \mscrV \), also called the \term{induced uniformity}, as the \hyperref[def:uniform_space]{uniformity} generated by the countable \hyperref[thm:uniform_space_base_axioms]{base}
  \begin{equation}\label{def:metric_uniformity/integer_base}
    \mathcal{B} \coloneqq \{ V_n \colon n = 1, 2, \ldots \},
  \end{equation}
  where
  \begin{equation*}
    V_n \coloneqq \rho^{-1}([0, \tfrac 1 n)).
  \end{equation*}

  As for the \hyperref[def:metric_topology]{metric topology}, we can instead consider the base
  \begin{equation}\label{def:metric_uniformity/real_base}
    \mathcal{B}' \coloneqq \{ \rho^{-1}([0, \varepsilon)) \colon \varepsilon > 0 \}.
  \end{equation}
\end{definition}
\begin{proof}
  Each relation \( V_r \) is obviously an entourage by \ref{def:metric_space/M1} and \ref{def:metric_space/M2}. We will prove that \( \mathcal{B} \) is indeed a uniform space base.

  \begin{refenum}
    \refitem{thm:uniform_space_base_axioms/BU1} For nonnegative integers \( n, m \) we have
    \begin{equation*}
      V_n \cap V_m
      =
      \{ (x, y) \in X \times X \colon \rho(x, y) < \tfrac 1 n \T{and} \rho(x, y) < \tfrac 1 m \}
      =
      V_{\max\{ n, m \}}.
    \end{equation*}

    Pick any integer \( k \geq \max\{ n, m \} \), so that
    \begin{equation*}
      V_k \subseteq V_n \cap V_m.
    \end{equation*}

    \refitem{thm:uniform_space_base_axioms/BU2} Fix \( V_n \in V \) and \( m \coloneqq 2n \). By the triangle inequality, we have that if \( \rho(x, y) < m \) and \( \rho(y, z) < m \), then
    \begin{equation*}
      \rho(x, z) \leq \rho(x, y) + \rho(y, z) < \tfrac 1 m + \tfrac 1 m = \tfrac 1 n.
    \end{equation*}

    Thus,
    \begin{equation*}
      V_m + V_m
      =
      \left\{ (x, z) \colon \exists y \in X: \rho(x, y) < \tfrac 1 m \T{and} \rho(y, z) < \tfrac 1 m \right\}
      \subseteq
      V_n
    \end{equation*}

    \refitem{thm:uniform_space_base_axioms/BU3} \ref{def:metric_space/M1} implies that
    \begin{equation*}
      \bigcap \mscrB = \lim_{n \to \infty} \rho^{-1}([0, \tfrac 1 n)) = \Delta_X.
    \end{equation*}
  \end{refenum}
\end{proof}

\begin{proposition}\label{thm:metric_topology_coincides_with_uniform_topology}
  The \hyperref[def:metric_topology]{metric topology} and the \hyperref[def:uniform_topology]{uniform topology} from the \hyperref[def:metric_uniformity]{metric uniformity} coincide.
\end{proposition}

\begin{theorem}\label{thm:countable_uniform_base_implies_metrizable}\mcite[thm. 8.1.21]{Engelking1989}
  A uniform space \( X \) is metrizable if and only \( w(X) \leq \aleph_0 \).
\end{theorem}

\medskip

\begin{definition}\label{def:isometry}\mcite[253]{Engelking1989}
  Let \( (X, \rho) \) and \( (Y, \nu) \) be two \hyperref[def:metric_space]{metric spaces}. We say that the function \( f: X \to Y \) is a \term{distance preserving map} or \term{isometry} or \term{isometric embedding} if
  \begin{equation*}
    \forall x, y \in X, \rho(x, y) = \nu(f(x), f(y)).
  \end{equation*}

  If \( f \) is bijective, we say that \( X \) and \( Y \) are \term{isometric}.
\end{definition}

\begin{proposition}\label{thm:isometry_is_injective}
  An \hyperref[def:isometry]{isometry} \( f: (X, \rho) \to (Y, \nu) \) is always injective.
\end{proposition}
\begin{proof}
  If \( f(x) = f(x') \), then by \fullref{def:metric_space/M1}, \( x = x' \).
\end{proof}

\begin{definition}\label{def:category_of_metric_spaces}
  Metric spaces and monotone maps form a subcategory of \( \cat{Unif} \) (see \fullref{def:category_of_uniform_spaces}). We denote this category by \( \cat{Met} \).
\end{definition}

\begin{definition}\label{def:equivalent_metrics}
  Two metrics \( \rho \) and \( \nu \) on the set \( X \) are said to be \term{equivalent} if \( \rho \) and \( \nu \) have the same \hyperref[def:metric_topology]{metric topology}. They are said to be \term{strongly equivalent} if there exist constants \( \alpha, \beta \in \BbbR \) such that for every \( x, y \in X \) we have
  \begin{equation*}
    \alpha \nu(x, y) \leq \rho(x, y) \leq \beta \nu(x, y).
  \end{equation*}
\end{definition}

\begin{remark}\label{rem:metric_space_convergence}
  All types of convergence from \fullref{subsec:topological_nets}, \fullref{subsec:topological_continuity} and \fullref{subsec:uniform_spaces} hold in metric spaces using the \hyperref[def:metric_topology]{metric topology} and \hyperref[def:metric_uniformity]{metric uniformity} structure.

  It is conventional to prefer the bases \fullref{def:metric_topology/real_base} and \fullref{def:metric_uniformity/real_base} to the bases \fullref{def:metric_topology/integer_base} and \fullref{def:metric_topology/integer_base}.

  For example, given two metric spaces \( X \) and \( Y \), continuity of \( f: X \to Y \) at \( x_0 \in X \) (see \fullref{def:local_continuity}) is usually written using the \enquote{epsilon-delta notation} as
  \begin{equation*}
    \forall \varepsilon > 0 \ \exists \delta > 0 : \rho_X(x, x_0) < \delta \implies \rho_Y(f(x), f(x_0)) < \varepsilon
  \end{equation*}
  for any \( x \in X \).
\end{remark}

\begin{definition}\label{def:translation_invariant_metric}
  A \hyperref[def:metric_space]{metric} \( \rho \) on a \hyperref[def:magma]{magma} \( G \) is said to be \term{left translation-invariant} if
  \begin{equation*}
    \rho(ax, ay) = \rho(x, y) \quad\forall a, x, y \in G
  \end{equation*}
  and \term{right translation-invariant} if
  \begin{equation*}
    \rho(xa, ya) = \rho(x, y) \quad\forall a, x, y \in G.
  \end{equation*}

  If \( \rho \) is both left and right translation invariant (e.g. for commutative magmas), we simply say that \( \rho \) is \term{translation invariant}.
\end{definition}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage[pdfborder={0 0 0}]{hyperref}
\usepackage{csquotes}
\newcommand\set[1]{\left\{#1\right\}}

\begin{document}
\section{Exercise 8}

\subsection{Royal Flush}
10, jack, queen, king, ace in one suit
\[ \sum_{s \in \set{\clubsuit, \diamondsuit, \diamondsuit, \spadesuit}} \frac{5}{52} \cdot \frac{4}{51} \cdot \frac{3}{50} \cdot \frac{2}{49} \cdot \frac{1}{48} = 4 \cdot \frac{120}{311875200} \approx 1.5390\cdot 10^{-6} \]
Consider one suit. It is okay to pick one of five cards. Then 4 admissible cards are left. Then 3 cards. Then 2. Then 1. For four suits.

\subsection{Straight Flush}
5 cards in same suit with consecutive rank.

%\[ \sum_{s \in \set{\clubsuit, \diamondsuit, \diamondsuit, \spadesuit}} \frac{13}{52} \cdot \frac{4}{51} \cdot \frac{3}{50} \cdot \frac{2}{49} \cdot \frac{1}{48} = 4 \cdot \frac{312}{311875200} = 4.0016\cdot 10^{-6} \]
%Card 1 must match one specific color, the second card must be one of 4 neighboring cards of same suit. This continues to 5 cards.
%
%Now we need to exclude the royal flush:
%\[ 4 \cdot \frac{312}{311875200} - 4 \cdot \frac{120}{311875200} = \frac{768}{311875200} \approx 2.4625 \cdot 10^{-6} \]
%
%This result is wrong according to Wikipedia (should be more likely).

The ranks can begin with 1, 2, 3, \dots up to 10. There are 4 suits.
\[ \frac{4 \cdot 10}{311875200} \]

\subsection{Poker / Four of a kind}
4 cards of same rank.
\[ \frac{1}{1} \cdot \frac{3}{51} \cdot \frac{2}{50} \cdot \frac{1}{49} = \frac{6}{124950} \]

\subsection{Full House}
3 cards of same rank and 2 cards of same rank.
\[ \frac11 \cdot \frac{3}{51} \cdot \frac{2}{50} \cdot \frac{48}{49} \cdot \frac{3}{48} = \frac{864}{5997600} \approx 0.0001 \]
The first choice is arbitrary, the second choice must be of same rank (3 of same suit are left, 51 cards left), as well as the third choice.
The fourth choice excludes 4 cards of same rank (the fourth card must not be picked!), hence $52 - 3 - 1 = 48$. The fifth choice must be of same rank (there are 3 suits of same rank left).

\subsection{Flush}
5 cards of same suit
\[ \frac{1}{1} \cdot \frac{12}{51} \cdot \frac{11}{50} \cdot \frac{10}{49} \cdot \frac{9}{48} = \frac{11880}{5997600} \approx 0.0020 \]

Verified with \href{https://en.wikipedia.org/wiki/Poker_probability}{Wikipedia: Poker probability}

\section{Whiteboard solution}

The solutions do not necessarily respect the previous solutions (a Royal Flush is a Flush).

\[ \mathbb P(\text{\enquote{Royal Flush}}) = \frac{{4 \choose 1} {47 \choose 1}}{{52 \choose 5}} \approx 1.539 \cdot 10^{-6} \]
There are $4$ different Royal Flushs (because of $4$ suits).

\[ \Omega = \{A \subseteq \{1, \dots, 4\} \times \{2, \dots, 14\} \,|\, |A| = 5\} \]

\[ \mathbb P(\text{\enquote{Straight Flush}}) = \frac{{4 \choose 1} {9 \choose 1}}{{52 \choose 5}} = \frac{36}{{52 \choose 5}} \approx 0.0000138 \]

\[ \mathbb P(\text{\enquote{Poker}}) = \frac{{13 \choose 1} {48 \choose 1}}{{52 \choose 5}} \approx 0.0002401 \]
\[ \mathbb P(\text{\enquote{Full House}}) = \frac{{13 \choose 1} {4 \choose 3} {12 \choose 1} {4 \choose 2}}{{52 \choose 5}} \approx 0.00144 \]
\[ \mathbb P(\text{\enquote{Flush}}) = \frac{{4 \choose 1} {13 \choose 5}}{{52 \choose 5}} \]
minus Royal Flush: $- \frac{36}{{52 \choose 5}} - \frac{4}{{52 \choose 5}} \approx 0.001965$

\end{document}


% !TEX root = main.tex

%-------------------------------------------------
\section{Correlation}\label{sec:correlation}

%-----------------------------
\subsection{Product moments}
%Let $X$ and $Y$ be random variables on the same probability space.

\begin{definition}\label{def:prod_rvs}
The \emph{product} of $X$ and $Y$ is the random variable
\[
\begin{array}{rlcl}
XY : 	& \Omega & \to 		& \R \\
		& \omega & \mapsto	& X(\omega)Y(\omega).
\end{array}
\]
\end{definition}

\begin{definition}
\ben
\it % << discrete
If $X$ and $Y$ are jointly discrete, the \emph{product moment} of $X$ and $Y$ is
\[
\expe(XY) = \sum_{i=1}^{\infty}\sum_{j=1}^{\infty} x_iy_j\, f_{X,Y}(x_i,y_j)
\]
whenever this sum exists.
\it % << cts
If $X$ and $Y$ are jointly continuous, the \emph{product moment} of $X$ and $Y$ is
\[
\expe(XY) = \int_{-\infty}^{\infty}\int_{-\infty}^{\infty} xy\, f_{X,Y}(x,y)\,dx\,dy
\]
whenever this integral exists.
\een
\end{definition}
%

%-----------------------------
\subsection{Covariance}
The covariance of $X$ and $Y$ is the product moment of the \emph{centred} variables $X-\expe(X)$ and $Y-\expe(Y)$.

\begin{definition}\label{def:covariance}
The \emph{covariance} of $X$ and $Y$ is
\[
\cov(X,Y) = \expe\big(\big[X-\expe(X)\big]\big[Y-\expe(Y)\big]\big).
\]
\end{definition}

\begin{remark}
Note that $\cov(X,Y)=\cov(Y,X)$ and $\cov(X,X)=\var(X)$.
\end{remark}

In the same way that $\var(X)=\expe(X^2)-\expe(X)^2$ we have the following convenient expression for $\cov(X,Y)$.
\begin{lemma}\label{lem:covariance-formula}
%\[
$\cov(X,Y) = \expe(XY) - \expe(X)\expe(Y)$.
%\]
\end{lemma}
\begin{proof}
Expand the product in Definition~\ref{def:covariance} then apply the linearity of expectation.
\end{proof}

\begin{lemma}\label{lem:var_of_sum}
For random variables $X_1,X_2,\ldots,X_n$,
\[
\var\left(\sum_{i=1}^n X_i\right) = \sum_{i=1}^n\sum_{j=1}^n \cov(X_i,X_j)
%= \sum_{i=1}^n \var(X_i) + 2\sum_{i=1}^{n}\sum_{j=i+1}^n \cov(X_i,X_j).
\]
\end{lemma}
\begin{proof}
Let $Y=\sum_{i=1}^n X_i$. Then
\begin{align*}
\var(Y)
	= \expe(Y^2)-\expe(Y)^2
	& = \expe\left[\left(\sum_{i=1}^n X_i\right)^2\right] - \left[\expe\left(\sum_{i=1}^n X_i\right)\right]^2 \\
	& = \expe\left[\sum_{i=1}^n \sum_{j=1}^n X_iX_j\right] - \left[\sum_{i=1}^n \expe(X_i)\right]^2 \\
	& = \sum_{i=1}^n\sum_{j=1}^n \expe(X_iX_j) - \sum_{i=1}^n\sum_{j=1}^n \expe(X_i)\expe(X_j) \\
	& = \sum_{i=1}^n\sum_{j=1}^n \cov(X_i,X_j)
\end{align*}
\end{proof}

\begin{exercise}\label{exe:covar_bilinear}
Show that covariance is a \emph{bilinear} operator, in the sense that
\[
\cov(aX_1+bX_2,cY_1+dY_2) = ac\cov(X_1,Y_1) + ad\cov(X_1,Y_2) + bc\cov(X_2,Y_1) + cd\cov(X_2,Y_2).
\]
\end{exercise}

%-----------------------------
\subsection{Correlation}

\begin{definition}
$X$ and $Y$ are said to be \emph{correlated} if $\cov(X,Y)\neq 0$ or equivalently if
\[
\expe(XY)\neq\expe(X)\expe(Y),
\]
otherwise they are said to be \emph{uncorrelated}.
\end{definition}

% lem: independent => correlated
\begin{lemma}\label{lem:indept_implies_uncorrelated}
If $X$ and $Y$ are independent, they are uncorrelated.
\end{lemma}
\begin{proof}
Let $X$ and $Y$ be jointly continuous (the discrete case is similar).
\par
Because $X$ and $Y$ are independent, $f_{X,Y}(x,y)=f_X(x)f_Y(y)$ for all $x,y\in\R$. Hence
\begin{align*}
\expe(XY)
	& = \int_{-\infty}^{\infty}\int_{-\infty}^{\infty} xy\, f_{X,Y}(x,y)\,dx\,dy \\
	& = \int_{-\infty}^{\infty}\int_{-\infty}^{\infty} xy\, f_X(x)f_Y(y)\,dx\,dy \text{\quad(by independence),}\\
	& = \left(\int_{-\infty}^{\infty}x\,f_X(x)\,dx\right)\left(\int_{-\infty}^{\infty}y\,f_Y(y)\,dy\right) \\
	& = \expe(X)\expe(Y).
\end{align*}
\end{proof}

% variance of sum = sum of variances
\begin{lemma}\label{lem:var_of_indept_sum}
If $X_1,X_2,\ldots,X_n$ are pairwise uncorrelated,
\[
\var\left(\sum_{i=1}^n X_i\right) = \sum_{i=1}^n\var(X_i).
\]
\end{lemma}
\begin{proof}
Because the $X_i$ are pairwise uncorrelated, $\cov(X_i,X_j)=0$ whenever $i\neq j$, so by Lemma~\ref{lem:var_of_sum},
\[
\var\left(\sum_{i=1}^n X_i\right) = \sum_{i=1}^n\cov(X_i,X_i) = \sum_{i=1}^n\var(X_i).
\]
\end{proof}

% example: negative binomial
\begin{example}
Let $X_1,\ldots,X_r$ be independent with each $X_i\sim\text{Geometric}(p)$, the distribution of the number of failures before the first success in a sequence of independent Bernoulli trials where the probability of success is $p$. Find the mean and variance of $Y = \sum_{i=1}^r X_i$.
\begin{solution}
Since $X_i\sim\text{Geometric}(p)$, we know that
\[
\expe(X_i)=\frac{1-p}{p} \quad\text{and}\quad \var(X_i)=\frac{1-p}{p^2}.
\]
By the linearity of expectation,
\[
\expe(Y) = \expe(X_1)+\expe(X_2)+\ldots+\expe(X_r) = \frac{r(1-p)}{p}
\]
and because the $X_i$ are independent,
\[
\var(Y) = \var(X_1)+\var(X_2)+\ldots+\var(X_r) = \frac{r(1-p)}{p^2}
\]
In this case $Y$ has the so-called \emph{negative binomial} distribution with parameters $r$ and $p$. This is the distribution of the number of failures before the $r$th success in a sequence of independent Bernoulli trials in which the probability of success is $p$.
\end{solution}
\end{example}

%-----------------------------
\subsection{Correlation coefficient}
The correlation coefficient is the product moment of the \emph{standardized} variables $\displaystyle\frac{X-\expe(X)}{\sqrt{\var(X)}}$ and $\displaystyle\frac{Y-\expe(Y)}{\sqrt{\var(Y)}}$.

\begin{definition}\label{def:correlation_coefficient}
The \emph{correlation coefficient} of $X$ and $Y$ is
\[
\rho(X,Y) = \expe\left[\left(\frac{X-\expe(X)}{\sqrt{\var(X)}}\right)\left(\frac{Y-\expe(Y)}{\sqrt{\var(Y)}}\right)\right]
\]
\end{definition}

By the linearity of expectation, we have the following convenient expression for $\rho(X,Y)$.
\begin{lemma}
The correlation coefficient of $X$ and $Y$ can be written as
\[
\rho(X,Y) = \frac{\cov(X,Y)}{\sqrt{\var(X)\cdot\var(Y)}}
\]
\end{lemma}
\begin{proof}
Follows easily from Lemma~\ref{lem:covariance-formula}.
\end{proof}

Note that $\rho(X,Y)=0$ whenever $X$ and $Y$ are uncorrelated. In fact the correlation coefficient satisfies the inequality $|\rho(X,Y)|\leq 1$ and thus provides a \emph{standardized} measure of the (linear) dependence between $X$ and $Y$. To prove this we need the following result from mathematical analysis.% called the \emph{Cauchy-Schwarz inequality}.

%First we need the following technical result (which we shall not prove).
%% lemma
%\begin{lemma}\label{lem:pos_rv_expe_zero}
%If $X\geq 0$ and $\expe(X)=0$ then $\prob(X=0)=1$.
%\end{lemma}
%\begin{proof}
%Proof by contradiction: let $X\geq 0$ with $\expe(X)=0$, and suppose that $\prob(X>0)>0$.
%\bit
%\it Because the CDF $\prob(X\leq x)$ is right-continuous, there exists $\epsilon>0$ such that $\prob(X>\epsilon)>0$.
%\it This implies that $X\geq \epsilon I(X>\epsilon)$.
%\it Taking the expected value of both sides, $\expe(X)\geq \epsilon\,\prob(X>\epsilon) > 0$ (by monotonicity).
%\eit
%This is a contradiction, so we conclude that $\prob(X>0)=0$.
%\end{proof}

% thm: cauchy-schwarz
\begin{theorem}[Cauchy-Schwarz inequality for random variables]
For any two random variables $X$ and $Y$,
\[
\expe(XY)^2 \leq \expe(X^2)\expe(Y^2)
\]
with equality if and only if $\prob(Y=aX)=1$ for some $a\in\R$.
\end{theorem}

\begin{theorem}\label{thm:bounds_on_rho}
The correlation coefficient satisfies the inequality
\[
|\rho(X,Y)|\leq 1,
\]
with equality if and only if $\prob(Y=aX+b)=1$ for some $a,b\in\R$.
\end{theorem}
\begin{proof}
Apply the Cauchy-Schwarz inequality to $X-\expe X$ and $Y-\expe Y$:
\begin{align*}
\cov(X,Y)^2
	& =		\expe\big((X-\expe X)(Y-\expe Y)\big) \\
	& \leq 	\expe\big((X-\expe X)^2\big)\expe\big((Y-\expe Y)^2\big) \\
	& = 	\var(X)\var(Y),
\end{align*}
with equality if and only if there exists $a\in\R$ such that
\[
\prob\big[Y-\expe Y = a(X-\expe X)] = 1.
\]
Hence,
\[
|\rho(X,Y)| = \left|\frac{\cov(X,Y)}{\sqrt{\var(X)\var(Y)}}\right| \leq 1
\]
with equality if and only if $\prob(Y = aX + b) = 1$,  where $b = \expe Y - a\expe X$.
\end{proof}

%----------------------------------------------------------------------
\begin{exercise}
\begin{questions}
%----------------------------------------
%==========================================================================
\question
Let $X$ and $Y$ be two random variables having the same distribution but which are not necessarily independent. Show that
$
\cov(X+Y,X-Y)=0
$
provided that their distribution has finite mean and variance.

\begin{answer}
Perhaps the simplest method is the following: let $U=X+Y$ and $V=X-Y$. Then
\begin{align*}
\cov(X+Y,X-Y)
	& = \expe(UV) - \expe(U)\expe(V) \\
	& = \expe\big[(X+Y)(X-Y)\big] - \expe(X+Y)\expe(X-Y) \\
	& = \expe(X^2 - Y^2) - \big[\expe(X)+\expe(Y)\big]\big[\expe(X)-\expe(Y)\big]	\qquad\text{(by the linearity of expectation)} \\
	& = \expe(X^2) - \expe(Y^2) - \expe(X)^2 +\expe(X)\expe(Y) - \expe(Y)\expe(X) + \expe(Y)^2 \quad\text{(by linearity again)} \\
	& = \big[\expe(X^2) - \expe(X)^2\big] - \big[\expe(Y^2) - \expe(Y)^2\big] \\
	& = \var(X) - \var(Y).
\end{align*}
Since $X$ and $Y$ have the same distribution, their variances must be equal, so $\cov(X+Y,X-Y)=0$.
\end{answer}

%==========================================================================
\question
Consider a fair six-sided die whose faces show the numbers $-2,0,0,1,3,4$. The die is independently rolled four times. Let $X$ be the average of the four numbers that appear, and let $Y$ be the product of these four numbers. Compute $\expe(X)$, $\expe(X^2)$, $\expe(Y)$ and $\cov(X,Y)$.
\begin{answer}
Let $X_1,X_2,X_3,X_4$ be independent discrete random variables on the set $\{-2,0,0,1,3,4\}$. Each $X_i$ is identically distributed according to the following PMF:
\[\begin{array}{|c|ccccc|}\hline
k			& -2		& 0		& 1		& 3		& 4 	\\ \hline
\prob(X_i=k)		& 1/6	& 1/3	& 1/6	& 1/6 	& 1/6	\\ \hline
\end{array}\]
Hence for $i=1,2,3,4$,
\begin{align*}
\expe(X_i)		& = \frac{1}{6}(-2+0+0+1+3+4) = 1, \\
\expe(X_i^2)	& = \frac{1}{6}(4+0+0+1+9+16) = \frac{30}{6} = 5, \\
\var(X_i)		& = \expe(X_i^2)-\expe(X_i)^2 = 4.
\end{align*}

Let $X=\frac{1}{4}(X_1+X_2+X_3+X_4)$. Then
\[
\expe(X) 	= \frac{1}{4}\big(\expe(X_1)+\ldots+\expe(X_4)\big) = 1
\]
By independence,
\[
\var(X) 	= \frac{1}{16}\big(\var(X_1)+\ldots+\var(X_4)\big) = 1
\]
so $\expe(X^2)	= \var(X) + \expe(X)^2 = 2$.

and $Y=X_1 X_2 X_3 X_4$. By independence,
\begin{align*}
\expe(Y)
	& = \expe(X_1 X_2 X_3 X_4) \\
	& = \expe(X_1)\expe(X_2)\expe(X_3)\expe(X_4) \\
	& = 1 \\
\end{align*}
and because the $X_i$ are identically distributed,
\begin{align*}
\expe(XY)
	& = \frac{1}{4}\expe\big(\expe(X_1+X_2+X_3+X_4)X_1X_2X_3X_4\big) \\
	& = \expe(X_1^2)\expe(X_2)\expe(X_3)\expe(X_4) \\
	& = \expe(X_1^2) = 5 \\
\end{align*}
so $\cov(XY) = \expe(XY) - \expe(X)\expe(Y) = 4$.
\end{answer}

%==========================================================================
\question
A fair die is rolled twice. Let $U$ denote the number obtained on the first roll, let $V$ denote the number obtained on the second roll, let $X=U+V$ denote their sum and let $Y=U-V$ denote their difference. Compute the mean and variance of $X$ and $Y$, and compute $\expe(XY)$. Check whether $X$ and $Y$ are uncorrelated. Check whether $X$ and $Y$ are independent.
\begin{answer}
Let $U,V\sim\text{Uniform}\{1,2,3,4,5,6\}$ be independent (and identically distributed) random variables, and define $X=U+V$ and $Y=U-V$.
\begin{align*}
\expe(X)	& = \expe(U) + \expe(V) = 7 \\
\expe(Y)	& = \expe(U) - \expe(V) = 0 \\
\intertext{By independence,}
\var(X)		& = \var(U) + \var(V) = 35/6 \\
\var(Y)		& = \var(U) + \var(V) = 35/6 \\
\intertext{Because $U$ and $V$ are identically distributed, and}
XY 			& = (U+V)(U-V) = U^2 - V^2 \\
\intertext{it follows that}
\expe(XY)	& = \expe(U^2) - \expe(V^2) = 0 \\
\intertext{$X$ and $Y$ are uncorrelated, since}
\cov(X,Y)	& = \expe(XY) - \expe(X)\expe(Y) =0 \\
\intertext{However $X$ and $Y$ are not independent, because (for example)}
\prob(Y=0)		& \neq \prob(Y=0|X=12)=1
\end{align*}
\end{answer}

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


\documentstyle[12pt]{article}
\begin{document}
\title{Linear First Order Differential Equations}
\maketitle
\section{The variation of constants method}

We start with the homogeneous equation
$$y'+p(t)y=0.$$
To solve this, we simply divide by $y$,
$$y'/y+p(t)=0,$$
and then integrate
$$\ln\alpha|y|+\int^t p(s)\,ds=K,$$
where $K$ is an integration constant.
We take the exponential on both sides:
$$|y|\exp(\int^t p(s)\,ds)=e^K.$$
This yields
$$y=\pm e^K\exp(-\int^t p(s)\,ds).$$
We define a new constant $C=\pm e^K$, so we can put the solution in the
form
\begin{equation}
y=C\exp(-\int^t p(s)\,ds).\label{e1}
\end{equation}

Now we look at an inhomogeneous equation
\begin{equation}
y'+p(t)y=g(t).\label{e2}
\end{equation}
The idea of the variation of constants method is to look for a solution in
a form similar to (\ref{e1}). Obviously, something has to change since the
equation has changed. The change is that the constant $C$ is replaced by
a function $C(t)$. So we set
$$y=C(t)\exp(-\int^t p(s)\,ds).$$
We differentiate using the product and chain rules to find
$$y'=C'(t)\exp(-\int^t p(s)\,ds)-C(t)p(t)\exp(-\int^t p(s)\,ds),$$
and
$$y'+p(t)y=C'(t)\exp(-\int^t p(s)\,ds).$$
Thus our differential equation becomes
$$C'(t)\exp(-\int^t p(s)\,ds)=g(t),$$
so that
$$C'(t)=g(t)\exp(\int^t p(s)\,ds).$$
We can then find $C(t)$ by integrating this equation.

The result is evidently the same as what we found using the integrating factor
method, but the ideas leading to the result were different. While for first
order linear equations the integrating factor method and the variation
of constants method are the same, the difference is in how they can be
generalized. The integrating factor method can be generalized to some nonlinear
equations of first order. The variation of constants method, on the other
hand, can be generalized to linear equations of higher order and to linear
systems.

\section{The method of undetermined coefficients}

This method is more limited in scope; it applies only to the special case
of (\ref{e2}), where $p(t)$ is a constant and $g(t)$ has some special form.
The advantage of the method is that it does not require any integrations and
is therefore quick to use. The homogeneous equation
$$y'+\lambda y=0$$
has the solution
$$y_h=Ce^{-\lambda t}.$$
To solve the inhomogeneous equation
$$y'+\lambda y=g(t),$$
it suffices to find one particular solution $y_p(t)$. If $y_p(t)$ is any
particular solution, then the general solution is
$$y(t)=y_p(t)+Ce^{-\lambda t}.$$

The idea behind the method of undetermined coefficients is to look for
$y_p(t)$ which is of a form like that of $g(t)$. This is possible only for
special functions $g(t)$, but these special cases arise quite frequently in
applications.

We start with the case where $g(t)$ is an exponential:
$$g(t)=Ae^{\alpha t}.$$
We look for $y(t)$ in a similar form
$$y(t)=ae^{\alpha t}.$$
This leads to
$$y'=a\alpha e^{\alpha t},\quad y'+\lambda y=(\alpha+\lambda)ae^{\lambda t}.$$
So the differential equation becomes
$$(\alpha+\lambda)ae^{\alpha t}=Ae^{\alpha t}.$$
We can solve this to find $a=A/(\alpha+\lambda)$. This leads to the particular
solution
$$y_p(t)={A\over \alpha+\lambda}e^{\alpha t},$$
and the general solution
\begin{equation}
y(t)={A\over \alpha+\lambda}e^{\alpha t}+Ce^{-\lambda t}.\label{e3}
\end{equation}

Example: Find the general solution of the equation
$$y'+2y=e^t.$$
The solution of the homogeneous equation is $C\exp(-2t)$, and we look
for a particular solution in the form $y_p=ae^t$. Setting $y=ae^t$ in
the equation, we find
$$ae^t+2ae^t=e^t,$$
leading to $a=1/3$. The general solution is
$$y={1\over 3}e^t+Ce^{-2t}.$$

Why did this work? The idea is simply that if $y$ is an exponential, then so
is $y'$, and so if both $y$ and $g$ are exponentials, then all terms in
the equation are exponentials and we can hope to obtain a solution by setting
coefficients equal to each other.

There are some other classes of functions for which this works. For instance,
if $y$ is a polynomial of degree $n$, then $y'$ is a polynomial of degree
$n-1$. If $g$ is a polynomial, we can therefore look for polynomial solutions.
Consider
$$y'+2y=t^2.$$
The right hand side is a polynomial of degree 2, so we look for a solution
in the same form $y=at^2+bt+c$. This leads to $y'=2at+b$, and
$$y'+2y=2at^2+(2a+2b)t+b+2c=t^2.$$
To satisfy this, we want to set
$$2a=1,\quad 2a+2b=0,\quad b+2c=0.$$
This leads to $a=1/2$, $b=-1/2$, $c=1/4$.
So a particular solution is
$$y_p={t^2\over 2}-{t\over 2}+{1\over 4}.$$
The general solution is
$$y={t^2\over 2}-{t\over 2}+{1\over 4}+Ce^{-2t}.$$

We note that the solution (\ref{e3}) breaks down if $\alpha=-\lambda$, since
it would involve a division by zero. More generally, if the equation reads
$$y'+\lambda y=g(t),$$
and $g(t)=\exp(\alpha t)P_n(t)$, with $P_n(t)$ an $n$th degree polynomial,
then we can find a particular solution $y_p(t)=\exp(\alpha t)Q_n(t)$, where
$Q_n(t)$ is some other $n$th degree polynomial as long as $\alpha\neq -\lambda$.
In the two examples above, we had $\lambda=-2$ and $\alpha=1$, $\alpha=0$,
respectively, so $\alpha\neq -\lambda$. If, on the other hand,
$\alpha=-\lambda$, we have to modify the procedure. The modification is
simply to include an extra factor $t$ in the solution. That is, instead of
setting $y_p=\exp(\alpha t)Q_n(t)$, you set $y_p=t\exp(\alpha t)Q_n(t)$.

{\bf Examples:}

1.
$$y'+2y=te^{-2t}.$$
Here $\lambda=2$ and $\alpha=-2$, so $\alpha=-\lambda$. The right hand side
is a first degree polynomial times $e^{-2t}$. So we look for a solution of
the form
$$y=te^{-2t}(at+b)=e^{-2t}(at^2+bt).$$
We find
$$y'=e^{-2t}(-2at^2+(2a-2b)t+b),$$
so that
$$y'+2y=e^{-2t}(2at+b)=te^{-2t}.$$
We compare coefficients to find $a=1/2$, $b=0$.
The general solution of the equation is
$$y={1\over 2}t^2e^{-2t}+Ce^{-2t}.$$

2.
$$y'+2y=te^t.$$
In this case $\lambda=2$ and $\alpha=1$, so $\alpha\neq -\lambda$, and we
do not need the extra factor $t$. So we look for a solution of the form
$$y=e^t(at+b).$$
This leads to
$$y'+2y=e^t(3at+3b+a)=te^t,$$
so we need
$$3a=1,\quad 3b+a=0,$$
leading to $a=1/3$, $b=-1/9$.
The general solution is
$$y=({t\over 3}-{1\over 9})e^t+Ce^{-2t}.$$

3.
$$y'=t.$$
In this case $\lambda=\alpha=0$, and the right hand side is a first degree
polynomial, so we look for a particular solution of the form $y=t(at+b)=at^2
+bt$. We find
$$y'=2at+b=t,$$
leading to $a=1/2$, $b=0$. The general solution is
$$y={t^2\over 2}+C.$$
\end{document}


%
% Chapter 5.1
%

\section*{5.1 Areas Between Curves}

The area \(a\) bounded by the curves \(y=f(x), y=g(x)\), and the lines \(x=a, x=b\), where \(f\) and \(g\) are continuous and \(f(x) \geq g(x)\) for all \(x\) in \([a, b]\), is
\[ A = \lim_{n \to \infty} \sum_{i=1}^n[f(x_i^*)-g(x_i^*)]\delta x \Leftrightarrow \int_a^b[f(x)-g(x)]dx \]
The area between the curves \(y=f(x)\) and \(y=g(x)\) and between \(x=a\) and \(x=b\) is
\[ A = \int_a^b \left | f(x)-g(x) \right | dx \]


\section{Temperature Solver}
The flow law, Equation \eqref{kin.eq.flowlaw}, depends on the temperature of ice. It is, therefore, necessary to determine how the distribution of ice temperatures changes with a changing ice sheet configuration. The thermal evolution of the ice sheet is described by
\begin{equation}
  \label{temp.eq.temp_z}
  \frac{\pd T}{\pd t}=\frac{k}{\rho c}\nabla^2T-\vec{u}\cdot\vec\nabla T+\frac\Phi{\rho c}-w\frac{\pd T}{\pd z},
\end{equation}
where $T$ is the absolute temperature, $k$ is the thermal conductivity of ice, $c$ is the specific heat capacity and $\Phi$ is the heat generated due to internal friction. In the $\sigma$--coordinate system, Equation \eqref{temp.eq.temp_z}, becomes
\begin{equation}
  \label{temp.eq.temp}
  \frac{\pd T}{\pd t} = \frac{k}{\rho cH^2}\frac{\pd^2T}{\pd\sigma^2} - \vec{u}\cdot\vec\nabla T + \frac{\sigma g}c\frac{\pd \vec{u}}{\pd\sigma}\cdot\vec\nabla s + \frac1H\frac{\pd T}{\pd\sigma}\left(w-w_{\text{grid}}\right)
\end{equation}
The terms represents (1) vertical diffusion, (2) horizontal advection, (3) internal heat generation due to friction and (4) vertical advection and a correction due to the sigma coordinate system. Let's rewrite \eqref{temp.eq.temp} to introduce some names:
\begin{equation}
  \label{temp.eq.temp2}
  \frac{\pd T}{\pd t} = a\frac{\pd^2T}{\pd\sigma^2} +b(\sigma) + \Phi(\sigma) + c(\sigma)\frac{\pd T}{\pd\sigma},
\end{equation}
where
\begin{subequations}
  \begin{align}
    a&=\frac{k}{\rho cH^2} \\
    \label{temp.eq.hadv}
    b(\sigma)&=-\vec{u}\cdot\vec\nabla T\\
    \Phi(\sigma)&=\frac{\sigma g}c\frac{\pd \vec{u}}{\pd\sigma}\cdot\vec\nabla s \\
    c(\sigma)&=\frac1H\left(w-w_{\text{grid}}\right)
  \end{align}
\end{subequations}

\subsection{Vertical Diffusion}
Discretisation of $\pd^2T/\pd\sigma^2$ is slightly complicated because the vertical grid is irregular. Using Taylor series the central difference formulas are
\begin{subequations}
  \begin{align}
    \label{temp.eq.d1}
    \left.\frac{\pd T}{\pd\sigma}\right|_{\sigma_{k-1/2}}&=\frac{T_k-T_{k-1}}{\sigma_k-\sigma_{k-1}}\\
    \intertext{and}
    \label{temp.eq.d2}
    \left.\frac{\pd T}{\pd\sigma}\right|_{\sigma_{k+1/2}}&=\frac{T_{k+1}-T_k}{\sigma_{k+1}-\sigma_k}\\
    \intertext{The second partial derivative is then, also uning central differences:}
    \label{temp.eq.d3}
    \left.\frac{\pd^2 T}{\pd\sigma^2}\right|_{\sigma_k} &= \frac{\left.{\pd T}/{\pd\sigma}\right|_{\sigma_{k+1/2}} - \left.{\pd T}/{\pd\sigma}\right|_{\sigma_{k-1/2}}}{1/2\left(\sigma_{k+1}-\sigma_{k-1}\right)}\\
    \intertext{Inserting \eqref{temp.eq.d1} and \eqref{temp.eq.d2} into \eqref{temp.eq.d3}, we get:}
    \label{temp.eq.d4}
    &=\frac{2(T_{k+1}-T_k)}{(\sigma_{k+1}-\sigma_k)(\sigma_{k+1}-\sigma_{k-1})}-\frac{2(T_k-T_{k-1})}{(\sigma_k-\sigma_{k-1})(\sigma_{k+1}-\sigma_{k-1})}
  \end{align}
\end{subequations}
Finally, the terms of equation \eqref{temp.eq.d4} are rearranged:
\begin{multline}
  \label{temp.eq.dsigma2}
  \left.\frac{\pd^2 T}{\pd\sigma^2}\right|_{\sigma_k} = \frac{2T_{k-1}}{(\sigma_k-\sigma_{k-1})(\sigma_{k+1}-\sigma_{k-1})} - \frac{2T_k}{(\sigma_{k+1}-\sigma_k)(\sigma_k-\sigma_{k-1})}\\
  + \frac{2T_{k+1}}{(\sigma_{k+1}-\sigma_k)(\sigma_{k+1}-\sigma_{k-1})}
\end{multline}

\subsection{Horizontal Advection}
The horizontal advection term, $- \vec{u}\cdot\vec\nabla T$ is solved using an upwinding scheme. Let's start with the 1--dimensional case. The method discussed can be straightforwadly extented to 2D. As always, the temperature function is expressed as a Taylor series.
\begin{subequations}
  \begin{align}
    \label{temp.eq.taylor1}
    T(x+\Delta x) &=T(x)+\Delta xT'(x)+\frac{\Delta x^2}2T''(x)+\ldots\\
    \intertext{If we subsitute $\Delta x$ with $2\Delta x$, Equation \eqref{temp.eq.taylor1}}
    \label{temp.eq.taylor2}
    T(x+2\Delta x) &=T(x)+2\Delta xT'(x)+2\Delta x^2T''(x)+\ldots
  \end{align}
\end{subequations}
From \eqref{temp.eq.taylor1} and \eqref{temp.eq.taylor2} we can construct a difference formula where the $\mathcal{O}(\Delta x^2)$ error is cancelled, by multiplying \eqref{temp.eq.taylor1} with 4 and substracting the result from \eqref{temp.eq.taylor2}:
\begin{subequations}
  \begin{align}
    \label{temp.eq.forward_h3}
    T_+'(x)&=\frac{4T(x+\Delta x)-T(x+2\Delta x)-3T(x)}{2\Delta x}\\
    \intertext{and similarly for the backward difference:}
    T_-'(x)&=-\frac{4T(x-\Delta x)-T(x-2\Delta x)-3T(x)}{2\Delta x}
    \end{align}
\end{subequations}
So the horizontal advection term in one dimensions becomes:
\begin{equation}
  b_x = -u_x\frac{\pd T}{\pd x}=\frac{-u_x}{2\Delta x}
  \begin{cases}
    -(4T_{i-1}-T_{i-2}-3T_i) & \text{when $u_x>0$} \\
    4T_{i+1}-T_{i+2}-3T_i & \text{when $u_x<0$} \\
  \end{cases}
\end{equation}
A similar expression is found for $b_y$ by simply substituting $y$ for $x$. Finally, the combined horizontal advection term, is simply
\begin{equation}
  b=-\vec{u}\cdot\vec\nabla T=-\left(u_x\frac{\pd T}{\pd x}+u_y\frac{\pd T}{\pd y}\right)=b_x+b_y=b_1+b_2T_i
\end{equation}

\subsection{Heat Generation}
Taking the derivative of \eqref{kin.eq.vert_velo_sigma} with respect to $\sigma$, we get
\begin{equation}
  \frac{\pd u_x}{\pd\sigma} = -2(\rho g)^nH^{n+1}|\vec\nabla s|^{n-1}\frac{\pd s}{\pd x}A(T^\ast)\sigma^n
\end{equation}
Thus,
\begin{equation}
\begin{split}
  \Phi(\sigma) &= \frac{\sigma g}c\frac{\pd \vec{u}}{\pd\sigma}\cdot\vec\nabla s  = \frac{\sigma g}c\left(\frac{\pd u_x}{\pd \sigma}\frac{\pd s}{\pd x} + \frac{\pd u_y}{\pd \sigma}\frac{\pd s}{\pd y}\right)\\
       &= -2(\rho g)^nH^{n+1}|\vec\nabla s|^{n-1}\frac{\sigma g}cA(T^\ast)\sigma^n \left(\left(\frac{\pd s}{\pd x}\right)^2+\left(\frac{\pd s}{\pd y}\right)^2\right) \\
       &= -\frac2{c\rho}(g\sigma\rho)^{n+1}\left(H|\vec\nabla s|\right)^{n+1}A(T^\ast)
\end{split}
\end{equation}

The constant factor $\frac2{c\rho}(g\sigma\rho)^{n+1}$ is calculated during initialisation in the subroutine \texttt{init\_temp}. This factor is assigned to array \texttt{c1(1:upn)}. \texttt{c1} also includes various scaling factors and the factor $1/16$ to normalise $\mathcal{A}$.

The next factor, $\left(H|\vec\nabla s|\right)^{n+1}$ is calculated in the subroutine \texttt{finddisp}:
\begin{equation}
  {c_2}_{i,j} = \left(\tilde{H}_{i,j}\sqrt{\tilde{S_x}_{i,j}^2+\tilde{S_y}_{i,j}^2}\right)^{n+1},
\end{equation}

The final factor is found by averaging over the neighbouring nodes:
\begin{equation}
  \mathcal{A}_{i,j}=4A_{i,j}+2(A_{i-1,j}+A_{i+1,j}+A_{i,j-1}+A_{i,j+1})+(A_{i-1,j-1}+A_{i+1,j-1}+A_{i+1,j+1}+A_{i-1,j+1})
\end{equation}

\subsection{Vertical Advection}\label{temp.sec.vert_ad}
The vertical advection term, $\pd T/\pd\sigma$ is solved using the central difference formula for unevenly spaced nodes:
\begin{equation}
  \frac{\pd T}{\pd\sigma}=\frac{T_{k+1}-T_{k-1}}{\sigma_{k+1}-\sigma_{k-1}}
\end{equation}

\subsection{Boundary Conditions}
At the upper boundary, ice temperatures are set to the surface temperature, $T_{\text{surf}}$. The ice at the base is heated by the geothermal heat flux and sliding friction:
\begin{equation}
  \left.\frac{\pd T}{\pd\sigma}\right|_{\sigma=1}=-\frac{GH}k-\frac{H\vec{\tau}_b\cdot\vec{u}(1)}k,
\end{equation}
where $\vec{\tau}_b=-\rho gH\vec\nabla s$ is the basal shear stress and $\vec{u}(1)$ is the basal ice velocity. Ice temperatures are held constant if they reach the pressure melting point of ice, i.e.
\begin{equation}
  T^\ast=T_{\text{pmp}} \quad\text{if $T\ge T_{\text{pmp}}$}.
\end{equation}
Excess heat is then used to formulate a melt rate, $S$:
\begin{equation}
  \label{temp.eq.meltrate}
  S=\frac{k}{\rho L}\left(\frac{\pd T^\ast}{\pd z}-\frac{\pd T}{\pd z}\right),
\end{equation}
where $L$ is the specific latent heat of fusion. Finally, basal temperatures are held constant, if the ice is floating:
\begin{equation}
  \frac{\pd T(1)}{\pd t}  = 0.
\end{equation}

\subsection{Putting it all together}
Equation \eqref{temp.eq.temp} is solved for each ice column. The horizontal dependency of the horizontal advection term, \eqref{temp.eq.hadv}, is resolved by iterating the vertical solution. Putting the individual terms together using a fully explicit finite differences scheme, Equation \eqref{temp.eq.temp2} becomes
\begin{subequations}
  \begin{multline}
    \label{temp.eq.temp3a}
    \frac{T_{k,t+1}-T_{k,t}}{\Delta t} = \left(\frac{2aT_{k-1,t}}{(\sigma_k-\sigma_{k-1})(\sigma_{k+1}-\sigma_{k-1})} - \frac{2aT_{k,t}}{(\sigma_{k+1}-\sigma_k)(\sigma_k-\sigma_{k-1,t})}\right. \\
    \left.+ \frac{2aT_{k+1,t}}{(\sigma_{k+1}-\sigma_k)(\sigma_{k+1}-\sigma_{k-1})}\right)+{b_1}_{k,t}+{b_2}_kT_{k,t}+\Phi_k+c_k\frac{T_{k+1,t}-T_{k-1,t}}{\sigma_{k+1}-\sigma_{k-1}}
\end{multline}
and similarly the fully implicit scheme
  \begin{multline}
    \label{temp.eq.temp3b}
    \frac{T_{k,t+1}-T_{k,t}}{\Delta t} = \left(\frac{2aT_{k-1,t+1}}{(\sigma_k-\sigma_{k-1})(\sigma_{k+1}-\sigma_{k-1})} - \frac{2aT_{k,t+1}}{(\sigma_{k+1}-\sigma_k)(\sigma_k-\sigma_{k-1,t+1})}\right. \\
    \left.+ \frac{2aT_{k+1,t+1}}{(\sigma_{k+1}-\sigma_k)(\sigma_{k+1}-\sigma_{k-1})}\right)+{b_1}_{k,t+1}+{b_2}_kT_{k,t+1}+\Phi_k+c_k\frac{T_{k+1,t+1}-T_{k-1,t+1}}{\sigma_{k+1}-\sigma_{k-1}}
\end{multline}
\end{subequations}
Taking the average of Equations \eqref{temp.eq.temp3a} and \eqref{temp.eq.temp3b} gives the \emph{Crank--Nicholson scheme}. The resulting equation is then rearranged and terms of $T_{k-1,t+1}$, $T_{k,t+1}$ and $T_{k+1,t+1}$ are combined to give the tri--diagonal system
\begin{equation}
  \alpha_kT_{k-1,t+1}+\beta_kT_{k,t+1}+\gamma_kT_{k+1,t+1}=\delta_k
\end{equation}
where, for $k=2,N-1$
\begin{subequations}
  \begin{align}
    \alpha_k &= -\frac12\frac{2a\Delta t}{(\sigma_k-\sigma_{k-1})(\sigma_{k+1}-\sigma_{k-1})}+\frac12\frac{c_k\Delta t}{\sigma_{k+1}-\sigma_{k-1}} \\
    \beta_k &= 1+\frac12\frac{2a\Delta t}{(\sigma_{k+1}-\sigma_k)(\sigma_k-\sigma_{k-1})}-\frac12{b_2}_k\Delta t=1-\alpha_k-\gamma_k-\frac12{b_2}_k\Delta t\\
    \gamma_k &= -\frac12\frac{2a\Delta t}{(\sigma_{k+1}-\sigma_k)(\sigma_{k+1}-\sigma_{k-1})}-\frac12\frac{c_k\Delta t}{\sigma_{k+1}-\sigma_{k-1}} \\
    \delta_k &= -\alpha_kT_{k-1,t}+(2-\beta_k)T_{k,t}-\gamma_kT_{k+1,t}+\frac12({b_1}_{k,t}+{b_1}_{k,t+1})\Delta t+\Phi_k\Delta t
  \end{align}

\subsubsection{Boundary Conditions}
At the upper boundary:
\begin{equation}
  \alpha_1=0,\quad\beta_1=1,\quad\gamma_1=0,\quad\delta_1=T_{\text{surf}}
\end{equation}
\end{subequations}

The lower boundary condition is somewhat more complicated. Here we only look at the case when the temperature is below the pressure melting point of ice. BC for floating ice and temperatures at the pressure melting point of ice are trivial. The geothermal heat flux is applied at the lower boundary, i.e. Equation \eqref{temp.eq.d2} becomes
\begin{equation}
  \label{temp.eq.d2-lb}
  \left.\frac{\pd T}{\pd\sigma}\right|_{\sigma_{k+1/2}}=-\frac{GH}k
\end{equation}
Assuming that $\sigma_k-\sigma_{k-1}=\sigma_{k+1}-\sigma_k=\Delta\sigma$ and inserting \eqref{temp.eq.d1} and \eqref{temp.eq.d2-lb} into \eqref{temp.eq.d3}, the second partial derivative becomes
\begin{equation}
  \left.\frac{\pd^2 T}{\pd\sigma^2}\right|_{\sigma_N} = \left(-\frac{GH}k-\frac{T_N-T_{N-1}}{\Delta\sigma}\right)/\Delta\sigma=-\frac{GH}{k\Delta\sigma}-\frac{T_N-T_{N-1}}{\Delta\sigma^2}
\end{equation}
Inserting the new conduction term and replacing the derivative of the vertical advection term with the Neuman boundary condition, Equation \eqref{temp.eq.temp3a} becomes
\begin{subequations}
  \begin{equation}
    \frac{T_{N,t+1}-T_{N,t}}{\Delta t} = -a\left(\frac{GH}{k\Delta\sigma}+\frac{T_{N,t}-T_{N-1,t}}{\Delta\sigma^2}\right)+{b_1}_{N,t}+{b_2}_NT_{N,t}+\Phi_N-c_N\frac{GH}k
  \end{equation}
  and similarly for Equation \eqref{temp.eq.temp3b}
  \begin{multline}
    \frac{T_{N,t+1}-T_{N,t}}{\Delta t} = -a\left(\frac{GH}{k\Delta\sigma}+\frac{T_{N,t+1}-T_{N-1,t+1}}{\Delta\sigma^2}\right)+{b_1}_{N,t+1}+{b_2}_NT_{N,t+1}\\
    +\Phi_N-c_N\frac{GH}k
  \end{multline}
\end{subequations}
The elements of the tri--diagonal system at the lower boundary are then
\begin{subequations}
  \begin{gather}
    \alpha_N =-\frac{a\Delta t}{2(\sigma_N-\sigma_{N-1})^2}\\
    \beta_N = 1-\alpha_N+\frac12{b_2}_N\Delta t\\
    \gamma_N = 0 \\
    \begin{split}
      \delta_N =&-\alpha_NT_{N-1,t}+(2-\beta_N)T_{N,t}-a\frac{GH\Delta t}{k(\sigma_N-\sigma_{N-1})}\\
      &+\frac12({b_1}_{N,t}+{b_1}_{N,t+1})\Delta t+\Phi_N\Delta t-c_N\frac{GH\Delta t}k
    \end{split}
  \end{gather}
\end{subequations}


\chapter{Finance}

\section{Asset}

If \(x \in \Real\) then \(cash(x)\) is an asset.

If \(t \in \Real\) and \(x\) is an asset,
then \(delay(t,x)\) is an asset.

If \(p \in [0,1]\) and \(x\) is an asset,
then \(chance(p,x)\) is an asset.

See also \emph{Composing contracts: an adventure in financial engineering} \cite{SpjContract}.

\section{Value function}

Let \(V\) be the value function.

\(V(cash(x)) = x\).

\(V(chance(p,x)) = p \cdot V(x)\).

\(V(delay(t,x)) = V(x)/(1+r)^t\).

\section{Time preference}

Let \(r\) be the interest rate.
Let \(x\) be an amount.

The \emph{future value} of \(x\) at time \(t\) is \((1+r)^t x\).

The \emph{present value} of \(delay(t,x)\) is \(x / (1+r)^t\).

\section{Risk-free interest rate}

\emph{Your} risk-free interest rate is
the highest government-guaranteed interest you can get.

\section{Perpetuity}

A perpetuity is \(\sum_{t \in \Nat} delay(t,p) \).

Let \(r\) be the risk-free interest rate.
Let \(p\) be an installment.
Let all installments be the same.

The present value of such perpetuity is \(\frac{p}{r}\).

\section{No-arbitrage}


\section{Taylor Expanding the Doppler operator \\ (and why this is a bad idea)}
\label{sec:taylor}
%
An alternative way to tackle the problem is to linearize the Doppler
operator via a Taylor expansion in $\alpha$ about $\alpha=0$:
%
\begin{align}
    \label{eq:taylor:I}
    I(\xi, x, y)
        &=
        I_0(\xi_0, x, y) \Bigg|_{\alpha=0}
        +
        \frac{\mathrm{d}I_0(\xi_0, x, y)}{\mathrm{d}\alpha} \Bigg|_{\alpha=0}
            \Delta\alpha(x, y)
        +
        \frac{1}{2}\frac{\mathrm{d}^2I_0(\xi_0, x, y)}{\mathrm{d}\alpha^2}
            \Bigg|_{\alpha=0} \Delta\alpha(x, y)^2
        +
        ...
\end{align}
%
Since the dependence of Equation~(\ref{eq:xi0}) on $\alpha$ is trivial,
the derivatives of the spectrum $I_0(\xi_0)$ with respect to
$\alpha$ are simply
%
\begin{align}
    \frac{\mathrm{d}^nI_0(\xi_0, x, y)}{\mathrm{d}\alpha^n} &=
    \dfrac{\mathrm{d}^nI_0(\xi_0, x, y)}{\mathrm{d}\xi_0^n} \nonumber\\ &\equiv
    I_0^{(n)}(\xi_0, x, y)
    \quad.
\end{align}
%
Given this result, and noting that $\xi_0 = \xi$ when $\alpha = 0$,
we may re-write Equation~(\ref{eq:taylor:I}) as
%
\begin{proof}{Taylor}
    \label{eq:taylor:ISum}
    I(\xi, x, y)
        &=
        \sum_{n=0}^\infty
            \frac{I_{0}^{(n)}(\xi, x, y)}{n!}
            \Delta\alpha(x, y)^n
        \quad ,
\end{proof}
%
The utility of this expression is that the velocity dependence of the spectrum
is now entirely encoded in the terms $\Delta\alpha(x, y)^n$, which are
\emph{independent of wavelength}. We can further decouple the spatial
dependence from the spectral dependence by expressing the intensity field
as an expansion over spherical harmonics $Y_{lm}(x, y)$ in the sky-projected
coordinates:
%
\begin{align}
    I_0(\xi, x, y) = \sum_{l=0}^\infty\sum_{m=-l}^{l} a_{lm}(\xi) Y_{lm}(x, y)
    \quad .
\end{align}
%
Equation~(\ref{eq:taylor:ISum}) now reads
%
\begin{align}
    \label{eq:taylor:IYlm}
    I(\xi, x, y)
        &=
        \sum_{n=0}^\infty
            \sum_{l=0}^\infty\sum_{m=-l}^{l}
                \frac{a_{lm}^{(n)}(\xi)}{n!}
                Y_{lm}(x, y)\Delta\alpha(x, y)^n
            \quad .
\end{align}
%
Finally, integrating this equation over the visible disk of
the star, we arrive at an equation for the observed spectrum:
%
\begin{align}
    \label{eq:taylor:S}
    S(\xi)
        &=
        \sum_{n=0}^\infty
            \sum_{l=0}^\infty\sum_{m=-l}^{l}
                \frac{a_{lm}^{(n)}(\xi)}{n!}
                \iint\limits_{\mathcal{S}(x, y)}
                Y_{lm}(x, y)\Delta\alpha(x, y)^n
                \mathrm{d}{\mathcal{S}(x, y)}
            \quad .
\end{align}
%
By expanding the spectrum in both the spectral and spatial dimensions, we
have effectively decoupled the two. However, Equation~(\ref{eq:taylor:S})
is impractical for three reasons. First, the surface integrals can be very
difficult to solve (although we will show later how these integrals may be
approximated analytically). Second, it requires knowledge of high order
derivatives of the spectrum, which may not always be easy or convenient to
compute. Third, and most important, the series expansion in $n$ is typically
extremely slow to converge and is therefore only practically useful in the
limit that the Doppler shift is much smaller than the typical width of a
spectral line; see the notebook link next to Equation~(\ref{eq:taylor:ISum})
for an example.


\section{Probability Theory}

\subsection{Exercise 1}
(a) Four character strings consisting of H and T (16 total).

(b) $\mathbb{N}$

(c) $\mathbb{R}^+$

(d) $\mathbb{R}^+ / 0$

(e) $\frac{i}{n}$ for $i = 0, ..., n$.

\subsection{Exercise 4}
(a) $P(A) + P(B) - P(A \cap B)$

(b) $P(A) + P(B) - 2P(A \cap B)$

(c) Same as (a).

(d) $1 - P(A \cap B)$

\subsection{Exercise 6}
We have $u + w = 1 \implies u^2 + 2uw + w^2 = 1$. However, it is not possible for
$u^2, 2uw, w^2$ to all equal $\frac{1}{3}$; thus, there are no such $u, w$.

\subsection{Exercise 12}
(a) Just a case of countable additivity.

(b) Split $\cup_{i = 1}^{\infty} A_i$ into $\cup_{i = 1}^{n} A_i$ and $\cup_{i = n + 1}^{\infty} A_i$.
Taking $n \to  \infty$ sends the probability of the latter union to 0, leaving the
desired result.

\subsection{Exercise 13}
No, since $P(B^c) = 1 - P(B) \implies P(B) = \frac{3}{4}$ and $\frac{3}{4} + \frac{1}{3} > 1$.

\subsection{Exercise 18}
This exercise (based on the provided answer) seems to assume that the balls are distinguishable.
There are $n^n$ different arrangements of balls in cells (each ball has $n$ options). If
exactly one cell must remain empty, then exactly one other cell must have two balls. There are
$n$ choices for the empty cell and $n - 1$ choices for the double cell. The two balls to go into
the double cell can be chosen in $\binom{n}{2}$ ways, and the remaining balls can be ordered in
$(n - 2)!$ ways (since they are distinguishable), hence giving the provided result.

\subsection{Exercise 21}
There are $\binom{2n}{2r}$ ways to choose $2r$ shoes from the collection. If we want to ensure that
no pairs of shoes are drawn, we should only draw one shoe from each of the $n$ pairs, which we can do
in $\binom{n}{2r}$ ways. For each of the $2r$ shoes we can either choose the right or left shoe from
the pair (assuming distinguishability).

\subsection{Exercise 23}
We use \href{https://en.wikipedia.org/wiki/Vandermonde%27s_identity}{Vandermonde's identity} with $m = n = r$ to get
\begin{align*}
        \sum_{k = 0}^n \bigg(\frac{1}{2^n} \binom{n}{k} \bigg)^2 = \frac{1}{4^n} \binom{2n}{n}
\end{align*}

\subsection{Exercise 25}
Depends on how you define the sample space, see \href{https://en.wikipedia.org/wiki/Boy_or_Girl_paradox#Analysis_of_the_ambiguity}{Wikipedia} for a lengthy discussion.

\subsection{Exercise 26}
We take the complement of the probability that we roll a 6 in the first 5 rolls:
$1 - \sum_{k = 0}^{4} \frac{5^k}{6^{k+1}}$.

\subsection{Exercise 35}
From the definition of conditional probability and the fact that $P(B) > 0$,
we have that $P(\cdot | B) > 0$ and $P(S | B) = \frac{P(B)}{P(B)} = 1$. Countable additivity
follows from $A \cap B$ and $A' \cap B$ being disjoint if $A$ and $A'$ are.

\subsection{Exercise 36}
To solve the first part, we take the complement of the probability that the target was hit fewer than
two times, which is
\begin{align*}
        1 - \bigg(\frac{4}{5}\bigg)^{10} - 10\bigg(\frac{4}{5}\bigg)^9 \frac{1}{5} \approx 0.624
\end{align*}
If we let $A$ be the event that the target was hit at least once, and $B$ be the event that the target was
hit at least twice, then we see that $A \cap B = B$. The conditional probability is thus
\begin{align*}
        \frac{1 - \bigg(\frac{4}{5}\bigg)^{10} - 10\bigg(\frac{4}{5}\bigg)^9 \frac{1}{5}}{1 - \bigg(\frac{4}{5}\bigg)^{10}} \approx 0.699
\end{align*}


\section{Sets and Relations}

\frame{
{Part 1: Sets and Relations}

\tableofcontents[currentsection,hideallsubsections, firstsection=2, sections={2-4}]
}

\subsection{Sets: Definitions}
\begin{frame}
  \frametitle{Mathematical Set}

  \structure{Mathematical Sets} are useful when talking about proofs. Last class we have already used some sets: \bigskip

  \begin{itemize}
    \item $\mathbb{N}$ -- Set of natural (non negative) numbers;
    \item $\mathbb{Z}$ -- Set of integer numbers;
    \item $\mathbb{R}$ -- Set of real numbers;
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Characteristics of sets}

  \begin{itemize}
    \item  \structure{Mathematical Sets} can mix different "types" of objects:
    \begin{itemize}
    \item \{7, ``Aranha'', $\pi/2$, TRUE\}
    \end{itemize}\bigskip

    \item \structure{Mathematical Sets} do not have a concept of "order":
    \begin{itemize}
      \item \{7, ``Aranha'', $\pi/2$, TRUE\} =   \item \{TRUE, 7, $\pi/2$, ``Aranha''\}
    \end{itemize}\bigskip

    \item \structure{Mathematical Sets} do not contain duplicates:
    \begin{itemize}
    \item \{7, $\pi$\} = \{7, $\pi$, 7\}
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Set: Membership}

  The fundamental property of a set is \structure{membership}, represented by the symbol $\in$. Note that membership is \alert{not} recursive!\vfill

  \begin{columns}
    \column{.5\textwidth}
  A = \{7,TRUE,$\pi$\}
  \begin{itemize}
  \item $7 \in A$
  \item 7 is an element of $A$,
  \item $3 \notin A$
  \end{itemize}\bigskip

  \column{.5\textwidth}
  B = \{7, $\mathbb{Z}$, 3\}
  \begin{itemize}
    \item $7 \in B$
    \item $7 \in \mathbb{Z}$
    \item $\mathbb{Z} \in B$
    \item \alert{$1 \notin B$}
  \end{itemize}
\end{columns}
\end{frame}

\begin{frame}{Set: Subsets}

  {\larger
  \begin{block}{Subset}
    \begin{itemize}
    \item $A \subset B$ means that every element of A is also an element of B
    \item $A\subset B \text{ equiv }\forall x, x\in A \rightarrow x\in B$
    \item $\mathbb{Z} \subset \mathbb{R}, \mathbb{R} \subset \mathbb{C}, \{3\} \subset \{5,3,7\}$
    \end{itemize}
  \end{block}

  \begin{block}{Important!}
    \begin{itemize}
    \item $A \subset A$
    \item $\forall X\text{ is a set}, \varnothing \subset X$
    \end{itemize}
  \end{block}
  }
\end{frame}

\begin{frame}
  \frametitle{Difference between Membership and Subset}

  \begin{columns}[T]
    \column{0.5\textwidth}
    \structure{Membership} ($\in, \notin$) indicates if one member is part of a set. \structure{Subset} ($\subset, \not\subset$) indicates if one set contains the members of other sets.

    \column{0.5\textwidth}
  {\larger
  \begin{itemize}
  \item $3 \in \{3,5,6\}$
  \item $3 \not\subset \{3,5,6\}$
  \end{itemize}
  \bigskip

  \begin{itemize}
  \item $\{3\} \subset \{3,5,6\}$
  \item $\{3\} \notin \{3,5,6\}$
  \end{itemize}
  \bigskip

  \begin{itemize}
    \item $\{3\} \in \{5, 6, \{3\}\}$
  \end{itemize}
  }
  \end{columns}
\end{frame}

\begin{frame}{Power Set}
  The \structure{Power Set} of A is a special set composed of ALL subsets of A.

  \begin{equation*}
    POW(A) = \forall x \subset A, x \in POW(A)
  \end{equation*}

  For example:

  \begin{equation*}
    POW(\{T,F\}) = \{\{T\},\{F\},\{T,F\},\varnothing\}
  \end{equation*}

  The definition of power set means that for any set $A$ contained in $B$, $A$ is an element of POW(B):

  \begin{equation*}
    \mathbb{N} \in POW(\mathbb{R}), \mathbb{N} \subset \mathbb{R}, \mathbb{N} \notin \mathbb{R}
  \end{equation*}
\end{frame}

\begin{frame}{Operations on Sets}

  Finally, You should be familiar with the regular operations on sets:
  \bigskip

    \begin{itemize}
    \item Union: $A \cup B \rightarrow x \in A \lor x \in B$\bigskip
    \item Intersection: $A \cap B \rightarrow x \in A \land x \in B$\bigskip
    \item Subtraction: $A - B \rightarrow x \in A \land x \notin B$\bigskip
    \item Complement: $\overline{A} = D - A$, where $D$ is the \structure{domain}\\ (the "everything" set or "parent" set of interest).
    \end{itemize}
\end{frame}

\subsection{Sets and Proofs}

\begin{frame}{Sets, Proofs and Predicates}

  Sets are used often as parts of proofs. We used sets a little bit when we talked about the \structure{Well Ordering Principle}.\bigskip

  It is common to use predicates to determine what elements are members of a set. For example, $P(X)$ be a predicate that defines $A$. If $P(X)$ is true for a certain $X$, then $X \in A$.

  \begin{block}{Example 1}
    \begin{itemize}
    \item $A = x \in \mathbb{N}, \{x < 12 \text{ AND } x \text{ is prime}\}$
    $\implies A = \{2,3,5,7,11\}$
    \end{itemize}
    A is the set of "prime numbers smaller than 12"
  \end{block}

  \begin{block}{Example 2}
    \begin{itemize}
    \item $B = x \in \mathbb{N}, \{x \text{ is prime AND } x+2 \text{ is prime}\} \implies$\\
    $B = \{3 (5), 5 (7), 11 (13), 17 (19), 29 (31), \ldots\}$
    \end{itemize}
    B is the set of "prime numbers x where x+2 is also prime".
  \end{block}
\end{frame}

\begin{frame}{Sets and Proofs Example (1)}
{Prove that the empty set is a subset of every set.}

  \begin{proof}
    Proof by construction:

  \begin{enumerate}

  \item $A \subset B$ means that $\forall x, x \in A \rightarrow x \in B$\bigskip

  \item If $A = \varnothing$ then $x \in A$ is FALSE for $\forall x$, so we can replace ``$\forall x \in A$'' with FALSE in {\bf (1)}\bigskip

\item The statement FALSE $\rightarrow x \in B$ is always TRUE.\\ \hfill (remember that FALSE $\rightarrow X$ is always TRUE)\bigskip

  \item Therefore, $\varnothing \subset B$ is TRUE $\forall B$
  \end{enumerate}
  \end{proof}
\end{frame}

\begin{frame}{Sets and Proofs Example (2)}
{Proof that Union and Intersection are Distributive}

  \begin{equation*}
    A \cup (B \cap C) \iff (A \cup B) \cap (A \cup C)
  \end{equation*}

  \begin{proof}
    Proof by sequence of "IFF"s:
    \begin{enumerate}
    \item \structure{$x \in A \cup (B \cap C)$} {\bf iff}
    \item $x \in A \lor x \in (B \cap C)$ {\bf iff} \hfill (definition of union)
    \item $x \in A \lor (x \in B \land x \in C)$ {\bf iff} \hfill
      (definition of intersection)
    \item $(x \in A \lor x \in B) \land (x\in A \lor x \in C)$ {\bf
      iff} \hfill (distributive prop.)
    \item $(x \in A\cup B) \land (x \in A\cup C)$ {\bf iff}\hfill
      (definition of union)
    \item \structure{$x \in (A\cup B)\cap (A\cup C)$}  \hfill
      (defintion of intersection)

    \end{enumerate}
  \end{proof}
\end{frame}

\subsection{Binary Relations}
\begin{frame}
  \frametitle{Definition of Binary Relations}
  \structure{Binary Relations} define an association of elements from one set (the {\bf domain}) to another set (the {\bf co-domain}). We see (binary) relations in many different situations:\bigskip

  \begin{itemize}
    \item Functions are a special case of binary relations: f(x) = y.
    \begin{itemize}
      \item A function associates the set of inputs with the sets of outputs;
    \end{itemize}\bigskip

    \item Operations such as set membership can be expressed as binary relations.
    \begin{itemize}
      \item For example, the predicate $P(x): x is prime$ defines a binary relation from $\mathbb{N}$ to \{TRUE, FALSE\}
    \end{itemize}\bigskip

    \item "Relational Databases" (for example, SQL) are also based on the idea of binary relations
    \begin{itemize}
      \item Key of X, member of a table, joint key, etc;
    \end{itemize}
    \end{itemize}
\end{frame}

\subsection{Binary Relation Definitons}

\begin{frame}{Example of Binary Relation}

  \begin{columns}[t]
    \column{0.4\textwidth}
    Let's consider the binary relation from the set of students {\bf (D)} that are registered to the set of subjects {\bf (J)}.\bigskip

    Components of a binary relation:
    \begin{itemize}
    \item \structure{Domain}: Set of Students;
    \item \structure{Co-domain}: Set of Classes;
    \item \structure{Relation Graph}:
    \begin{itemize}
      \item Vertices: Union of Domain and Co-domain;
      \item Edges: Directed edges from Domain to Co-domain;
    \end{itemize}
    \end{itemize}\bigskip

    \column{0.6\textwidth}

    \begin{center}
      \includegraphics[width=0.6\textwidth]{../img/relations}
    \end{center}\pagenote{Relation Graph Image from MIT OCS}
    Representation of a binary relation:
    \begin{itemize}
    \item $R($Jason$) = \{6.042, 6.012\}$
    \item Jason $R$ 6.042
    \item $R(\{$Jason, Yihui$\}) = \{6.042, 6.012, 6.004\}$
    \end{itemize}
  \end{columns}
\end{frame}

\begin{frame}{Relations and Inverse Relations}

  If we think of a binary relation as a directed graph, the \structure{Inverse Relation} is the relation defined by the same graph when the edges are reversed:\bigskip

  Relation $R$:
  \begin{equation*}
    R(X) ::= {j \in J | \exists d \in X. d R j}
  \end{equation*}

  Reverse Relation $R^{-1}$:
  \begin{equation*}
    R^{-1}(Y) ::= {d \in S | \exists j \in Y. d R j}
  \end{equation*}
  \bigskip

  \begin{columns}
    \column{0.5\textwidth}
    \begin{itemize}
      \item $R(Jason) = \{6.042, 6.012\}$
      \item $R^{-1}(6.012) = \{Jason, Yihui\}$
    \end{itemize}
    \column{0.5\textwidth}
    \begin{center}
      \includegraphics[width=0.6\textwidth]{../img/relations}
    \end{center}
  \end{columns}
\end{frame}

\begin{frame}{Composite Relations}

  \begin{columns}[t]

    \column{0.5\textwidth}
    If we have a relation $V$ from set $P$ to set $D$, and a relation $R$ from set $D$ to set $J$, then we can define a \structure{composite} relation $R\circ V$ from set $P$ to set $J$ (we can also use $R(V)$).\bigskip

    \begin{itemize}
    \item $R(V(X))$ or $(R\circ V)(X)$
    \item $R(V(\text{FTL})) = \{6.003\}$
    \begin{itemize}
      \item professor FTL super{\bf V}ises Joan;\bigskip

      \item Joan is {\bf R}egistered to class 6.003;
    \end{itemize}
    \end{itemize}

    \column{0.5\textwidth}
    \begin{center}
      \includegraphics[width=0.8\textwidth]{../img/composite_relation}
    \end{center}

  \end{columns}
\end{frame}

\begin{frame}{Types of Binary Relations}

  We can classify a binary relation based on the number of degrees (arrows) in the relation graph. Imagine a relation $R$ from $X$ to $Y$ (i.e.: $R(X) = Y$)\bigskip

  Classification of $R$ based on $Y$:
  \begin{itemize}
  \item {\bf Surjection}: Every element in $Y$ has $\geq 1$ in-arrows. (Every Y has {\bf one or more} X)
  \item {\bf Injection}: Every element in $Y$ has $\leq 1$ in-arrows. (Every Y has {\bf one or less} X)
  \end{itemize}\smallskip

  Classification of $R$ based on $X$:
  \begin{itemize}
  \item {\bf Total}: Every element in $X$ has $\geq 1$ out arrows. (Every $X$ has {\bf one or more} $Y$)
  \item {\bf Function}: Every element in $X$ has $\leq 1$ out arrows. (Every $X$ has {\bf one or less $Y$})
  \end{itemize}\smallskip

  An important definition:
  \begin{itemize}
  \item {\bf Bijection}: Every element of $X$ has {\bf exactly one} element of $Y$, {\bf and vice versa}.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Types of Binary Relations: Examples}

    \begin{block}{Example 1: $g: \mathbb{R}\times\mathbb{R} \rightarrow \mathbb{R}, g(x,y) = 1/(x-y)$}
      \begin{itemize}
      \item This is a {\bf function}, because each pair $(x,y)$ has at most one output.
      \item This is not {\bf total}, because not every pair $(x,y)$ has an output: $(x = y)$ has no output.
      \end{itemize}
    \end{block}

    \begin{block}{Example 2: $g_o: \mathbb{R}\times\mathbb{R} - \{x,y|x=y\} \rightarrow \mathbb{R}, g_o(x,y) = 1/(x-y)$}
      \begin{itemize}
      \item $g$ and $g_o$ are similar relations, but defined on different domains.
      \item The domain of $g_o$ removes all $(x,y)$ where $x = y$
      \item Because of this, $g_0$ is {\bf function} (every pair has at most one output) and {\bf total} (every pair has at least one output).
      \end{itemize}

    \end{block}

\end{frame}

% \begin{frame}
%   \frametitle{Size of Finite Sets}
%
%   {\larger
%     We can use the characteristics of relations to estimate the
%     size of sets (domains and co-domains).
%
%     \vfill
%
%     \begin{itemize}
%     \item A bijection B $\rightarrow |A| = |B|$
%     \item A function surjection B $\rightarrow |A| \geq |B|$
%     \item A total injection B $\rightarrow |A| \leq |B|$
%     \end{itemize}
%   }
% \end{frame}
%
% \begin{frame}
%   \frametitle{Set Size Example: Finite power sets and binary strings}
%
%   {\larger
%     What is the size of the Power Set of a \structure{finite} set?
%
%     \vfill
%
%     \begin{itemize}
%     \item Make a bijection between the power set and the binary string
%     \item Calculate the size of a binary string
%     \item Establish equality
%     \end{itemize}
%
%   }
% \end{frame}


\subsection{Variation of Parameters for Systems}
\noindent
Let $X$ be a fundamental matrix for the homogeneous system.
\begin{equation*}
	\vec{x_h}' = A\vec{x_h}
\end{equation*}
That is,
\begin{equation*}
	\vec{x_h} = X\vec{c}
\end{equation*}
where
\begin{equation*}
	\vec{c} = \begin{bmatrix}
	C_1 \\
	\vdots \\
	C_n
	\end{bmatrix}
\end{equation*}
and the entries of matrix $A$ can be any continuous functions of $t$.

\noindent
We are looking for the particular solution $\vec{x_p}$, to the system
\begin{equation*}
	\vec{x} = A\vec{x} + \vec{f}
\end{equation*}
where $\vec{x_p}$ is of the form
\begin{equation*}
	\vec{x_p} = X\vec{v}
\end{equation*}
where $\vec{v}$ is a vector of functions of $t$ that we'll have to find.

\noindent
Differentiating $\vec{x_p}$,
\begin{equation*}
	\vec{x_p}' = X\vec{v}' + X'\vec{v}
\end{equation*}
From the system we're trying to solve we know that
\begin{equation*}
	X\vec{v}' + X'\vec{v} = A(X\vec{v}) + \vec{f}
\end{equation*}
Since $X' = AX$,
\begin{equation*}
	X\vec{v}' = \vec{f}
\end{equation*}

\noindent
Since the columns of $X$ are always linearly independent, we know that $X^{-1}$ always exists.\\
Multiplying by $X^{-1}$,
\begin{equation*}
	\vec{v}' = X^{-1}\vec{f}
\end{equation*}
Integrating with respect to $t$,
\begin{equation*}
	\vec{v} = \int{X^{-1}\vec{f} \mathrm{d}t}
\end{equation*}
So,
\begin{equation*}
	\vec{x_p} = X\int{X^{-1}\vec{f} \mathrm{d}t}
\end{equation*}
and
\begin{equation*}
	\vec{x} = X\vec{c} + X\int{X^{-1}\vec{f} \mathrm{d}t}
\end{equation*}

\ifodd\includeLinearSystemsExamples\input{./linearSystems/heterogeneousSystems/variationOfParameters_example.tex}\fi


\section{Pitch rate command system}

Reducing the state space model in Equation~\ref{eq:sslon} by eliminating the velocity and pitch angle states result in the following model.

\begin{equation}
    \begin{aligned}
        A_{red}&=\begin{bmatrix}
            -0.05167 &   0.9792 \\
            -0.6256  & -0.2485
        \end{bmatrix} &
        B_{red}&=\begin{bmatrix}
            -0.0002308 \\
            -0.01541
        \end{bmatrix} \\\\
        C_{red}&=I_2 &
        D_{red}&=0_{2,1}
    \end{aligned}
\end{equation}

Simulating the step response for both models yields similar results. It is clear in the response of the full model that the short period is the dominant eigenmotion during the first few seconds. Since only the initial response of the system in relevant for the CAP and Gibson dropback criterion, it is fine to continue using the reduced model for the rest of the assignment.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.6\textwidth]{figures/pc_acre_step.pdf}
    \caption{Pitch rate elevator step response for both the full and reduced model.}
    \label{fig:pc_acre_step}
\end{figure}

A feedback loop is needed in order to satisfy the short period natural frequency and damping requirements. Figure~\ref{fig:pc_loop1} shows the control loop for solving this problem. The system $G$ is the open loop state space system and $K$ is the to be determined gain matrix.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.4\textwidth]{figures/pc_loop1.pdf}
    \caption{Feedback loop with gain in the loop.}
    \label{fig:pc_loop1}
\end{figure}

The build-in matlab function \texttt{place(...)} is used to calculate the gain $K_{reduced}$ using pole placement. The resulting gain is shown in Equation~\ref{eq:gaink}.

\begin{equation}
    \label{eq:gaink}
    K_{reduced}=\begin{bmatrix}
                    K_{\alpha} & K_q
                \end{bmatrix}
               =\begin{bmatrix}
                    -449.0440 & -151.8325
                \end{bmatrix}
\end{equation}

The new closed loop system matrix $A$ can then be calculated using Equation~\ref{eq:aclosed} and the rest of the matrices are the same as that of the open loop system. The new closed loop system can be found in Equation~\ref{eq:closedloopsystem}.

\begin{equation}
    \label{eq:aclosed}
    A_{closed} = A_{open}-B_{open}K
\end{equation}

\begin{equation}
    \label{eq:closedloopsystem}
    \begin{aligned}
        A_{closed}&=\begin{bmatrix}
            -0.1553 &  0.9442 \\
            -7.544  & -2.588 \\
        \end{bmatrix} &
        B_{closed}&=\begin{bmatrix}
            -0.0002308 \\
            -0.01541
        \end{bmatrix} \\\\
        C_{closed}&=I_2 &
        D_{closed}&=0_{2,1}
    \end{aligned}
\end{equation}

Calculating the damping and natural frequency of the closed loop system shows that the parameters match the required $\zeta=0.5$ and $\omega_n=0.03V\approx 2.74$
\begin{table}[h!]
    \centering
    \begin{tabular}{ c c c c c }
         Poles         & $\zeta$        & $\omega_n$ \\ \hline \hline
         $1.37 + 1.37i$ & $\e{5.00}{-1}$ & $2.74$     \\
         $1.37 - 1.37i$ & $\e{5.00}{-1}$ & $2.74$     \\ \hline
    \end{tabular}
    \caption{Longitudinal eigenmotions poles, damping rations, natural frequencies, periods and time to half amplitude.}
\end{table}

If small angle approximations are assumed it is possible to calculate the angle of attack using the following equation.

\begin{equation}
    \alpha = \frac{w}{V}
\end{equation}

Here is $w$ the vertical airspeed component and V the total airspeed. Thus the angle of attack change in case of a vertical gust may be calculated by dividing the gust speed by the free stream velocity. This angle of attack may be passed as an initial condition to a simulation in order to observe the aircraft's behavior during such a gust. For a severe gust with $w=4.572\ [m\ s^{-1}]$ the initial angle of attack in the assigned flight condition is,

\begin{equation}
    \alpha_0=\frac{4.572\ [m\ s^{-1}]}{300\ [ft\ s^{-1}]}=\frac{4.572\ [m\ s^{-1}]}{91.44\ [m\ s^{-1}]} = 0.05\ [rad]
\end{equation}

The simulation results for both the open and closed loop systems using this initial condition and zero inputs is shown in Figure~\ref{fig:pc_vertgust}. As it can be seen the closed loop system quickly dampens the disturbance due the vertical gust, in contrast to the open loop system which continues oscillating significantly longer.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.6\textwidth]{figures/pc_vertgust.pdf}
    \caption{Angle of attack during a $w=4.572\ [m\ s^{-1}]$ vertical gust for the open and closed loop systems.}
    \label{fig:pc_vertgust}
\end{figure}

The $T_{\theta_2}$ time constant cannot be modified by pole placement or by some other control loop structure. The reason is because it is located in the numerator of the transfer function and it's not possible to change the numerator using a feedback loop gain.

Placing the lead-lag filter inside of the loop on the forward path will make it end up in both the numerator and denominator of the closed loop transfer function. Thus making it impossible to replace the zero without without changing the damping ratio and natural frequency of the closed loop system.

To modify $T_{\theta_2}$ a lead-lag prefilter can be placed before the closed loop system. The pole in the prefilter is used to cancel the current zero with $T_{\theta_2}$ in the closed loop, and the prefilter's zero end up in the overall transfer function, thus replacing the original zero.

\begin{equation}
    \frac{q(s)}{\delta_{el}(s)}=\frac{1+T_{\theta_2,new}}{1+T_{\theta_2,old}} \cdot
        \frac{k_q \left(1+T_{\theta_2,old}\right)}{
        s^2+2\zeta\omega_ns+\omega_n^2}
        =
        \frac{k_q \left(1+T_{\theta_2,new}\right)}{
        s^2+2\zeta\omega_ns+\omega_n^2}
\end{equation}

Even with the prefilter the controller will still not track the reference pitch rate signal properly, there will be a steady state error that needs to be compensated for. The reason is because the signal coming from the feedback loop is scaled by the gain $K$ and thus the reference signal needs to be scaled as well.

For this assignment the \texttt{rscale(...)} function which can be found at \cite{matlabrscale} is used. The final form of the precompensator is thus,

\begin{equation}
    N \cdot \frac{1+T_{\theta_2,new}}{1+T_{\theta_2,old}}
\end{equation}

Where $N$ is the scaling factor from \texttt{rscale(...)}.

% Category B: Those nonterminal Flight Phases that are normally accomplished using
% gradual maneuvers and without precision tracking, although accurate
% flight-path control may be required. Included in this Category are:

Figure~\ref{fig:pc_cap} shows the level 1 Control Anticipation Parameter criteria for Category B flight phases. According to \cite{milstd1797a}, Category B flights phases are normally accomplished using gradual maneuvers and without precision tracking, which includes cruise flight. As it can be seen, the controller satisfies the requirement.

\begin{equation}
    CAP=\frac{g\omega^2T_{\theta_2}}{V} \approx 0.1168
\end{equation}

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.4\textwidth]{figures/pc_cap.pdf}
    \caption{Control Anticipation Parameter criteria for Category B flight phases and the location for the controlled system in the plot.}
    \label{fig:pc_cap}
\end{figure}

Figure~\ref{fig:pc_cap} shows the pitch rate and pitch angle step response of the controlled system. The maximum pitch rate $q_m$ and steady state value $q_s$ are marked in the plot.
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.8\textwidth]{figures/pc_pitch_resp.pdf}
    \caption{Pitch rate and pitch angle step response of the controlled system.}
    \label{fig:pc_pitch_resp}
\end{figure}

Figure~\ref{fig:pc_cap} shows the satisfactory range of values to meet the Gibson dropback criterion. As it can be seen the controlled system satisfies the criteria.
\begin{align}
    \label{eq:gibvals}
    \frac{DB}{q_s} &= T_{\theta_2} - \frac{2\zeta_{sp}}{\omega_{n_{sp}}} \approx 0.1099 \\
    \frac{q_m}{q_s} &= \frac{1.4126}{1} = 1.4126
\end{align}

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.4\textwidth]{figures/pc_gib.pdf}
    \caption{Pitch rate and pitch angle step response of the controlled system.}
    \label{fig:pc_gib}
\end{figure}

\clearpage


\documentclass[12pt]{cdblatex}
\usepackage{examples}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% ============================================================================================
\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   import cdblib
   checkpoint_file = 'tests/semantic/output/example-14.json'
   cdblib.create (checkpoint_file)
   checkpoint = []
\end{cadabra}
\egroup

\clearpage

% ============================================================================================
\section*{Example 14 The Weyl tensor is conformally invariant}

This example shows that the Weyl tensor is conformally invariant. That is, for a pair of
metrics $g$ and $\overline{g}$ related by a conformal transformation,
$\overline{g}_{a b} = \phi g_{a b}$ then $\overline{C}^{a}_{b c d} = C^{a}_{b c d}$
or equally $\overline{C}_{a b c d} = \phi C_{a b c d}$.

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,u,v,w#}::Indices (position=independent).

   \partial{#}::PartialDerivative.

   g_{a b}::Metric.
   g^{a b}::InverseMetric.
   g_{a}^{b}::KroneckerDelta.

   GammaU := \Gamma^{a}_{b c} ->  1/2 g^{a d} (   \partial_{b}{g_{d c}}
                                                + \partial_{c}{g_{b d}}
                                                - \partial_{d}{g_{b c}}).

   GammaD := \Gamma_{a b c} ->  1/2 (   \partial_{b}{g_{a c}}
                                      + \partial_{c}{g_{b a}}
                                      - \partial_{a}{g_{b c}}).

   Rabcd := R_{a b c d} ->   \partial_{c}{\Gamma_{a b d}}
                           - \partial_{d}{\Gamma_{a b c}}
                           + \Gamma_{e a d} \Gamma^{e}_{b c}
                           - \Gamma_{e a c} \Gamma^{e}_{b d}.

   Rab     := R_{a b} -> g^{c d} R_{a c b d}.

   Rscalar := R -> g^{a b} R_{a b}.

   # Weyl in 4-dimensions

   Cabcd := R_{a b c d} - (1/2) (R_{a c} g_{b d} - R_{a d} g_{b c})
                        - (1/2) (g_{a c} R_{b d} - g_{a d} R_{b c})
                        + (R/6) (g_{a c} g_{b d} - g_{a d} g_{b c}).

   {\partial_{a b}{\phi},\partial_{a}{\phi},\phi}::SortOrder.
   {\partial_{a b}{g_{c d}},\partial_{a}{g_{b c}},g_{a b},g^{a b}}::SortOrder.

   substitute (Cabcd,Rscalar)
   substitute (Cabcd,Rab)
   substitute (Cabcd,Rabcd)
   substitute (Cabcd,GammaU)
   substitute (Cabcd,GammaD)

   distribute     (Cabcd)

   sort_product   (Cabcd)
   rename_dummies (Cabcd)
   canonicalise   (Cabcd)

   # this is the Weyl tensor on the base metric
   baseC := @(Cabcd).

   conformal := {g_{a b} -> \phi g_{a b}, g^{a b} -> (1/phi) g^{a b}}.

   substitute     (Cabcd, conformal)
   product_rule   (Cabcd)
   distribute     (Cabcd)
   product_rule   (Cabcd)
   distribute     (Cabcd)

   map_sympy      (Cabcd, "simplify")

   sort_product   (Cabcd)
   rename_dummies (Cabcd)
   canonicalise   (Cabcd)

   # this is the Weyl tensor on the conformal metric
   confC := @(Cabcd).

   # their difference, should be zero
   diff := @(confC) - \phi @(baseC).  # cdb (ex-14.diff.100,diff)

   distribute     (diff)
   sort_product   (diff)
   rename_dummies (diff)
   canonicalise   (diff)  # cdb (ex-14.diff.101,diff)

   # this trick is not essential but it does reduce the number of terms in diff
   substitute     (diff,$\partial_{a}{\partial_{b}{g_{c d}}} -> g_{c d b a}$)
   substitute     (diff,$\partial_{a}{g_{b c}} -> 0$)
   substitute     (diff,$g_{c d b a} -> \partial_{a}{\partial_{b}{g_{c d}}}$)  # cdb (ex-14.diff.102,diff)

   # standard expressions in 4-d
   substitute     (diff,$g_{a b} g^{a b} -> 4$,repeat=True)          # cdb (ex-14.diff.201,diff)
   substitute     (diff,$g_{a b} g^{c b} -> g_{a}^{c}$,repeat=True)  # cdb (ex-14.diff.202,diff)
   substitute     (diff,$g_{b a} g^{b c} -> g_{a}^{c}$,repeat=True)  # cdb (ex-14.diff.203,diff)
   substitute     (diff,$g_{a}^{a} -> 4$,repeat=True)                # cdb (ex-14.diff.204,diff)
   substitute     (diff,$g^{a}_{a} -> 4$,repeat=True)                # cdb (ex-14.diff.205,diff)
   eliminate_kronecker (diff)                                        # cdb (ex-14.diff.206,diff)

   # need a second round since the above block introduces new terms that match those just eliminated
   substitute     (diff,$g_{a b} g^{a b} -> 4$,repeat=True)          # cdb (ex-14.diff.301,diff)
   substitute     (diff,$g_{a b} g^{c b} -> g_{a}^{c}$,repeat=True)  # cdb (ex-14.diff.302,diff)
   substitute     (diff,$g_{b a} g^{b c} -> g_{a}^{c}$,repeat=True)  # cdb (ex-14.diff.303,diff)
   substitute     (diff,$g_{a}^{a} -> 4$,repeat=True)                # cdb (ex-14.diff.304,diff)
   substitute     (diff,$g^{a}_{a} -> 4$,repeat=True)                # cdb (ex-14.diff.305,diff)
   eliminate_kronecker (diff)                                        # cdb (ex-14.diff.306,diff)

   sort_product   (diff)
   rename_dummies (diff)
   canonicalise   (diff)  # cdb (ex-14.diff.400,diff)

   checkpoint.append (baseC)
   checkpoint.append (confC)
\end{cadabra}

\clearpage

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.102}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.201}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.202}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.203}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.204}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.205}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.206}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.301}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.302}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.303}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.304}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.305}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*[\hskip2.0cm\hfill]{ex-14.diff.306}}
\end{dgroup*}

\begin{dgroup*}
   \Dmath*{ \Delta = \Cdb*{ex-14.diff.400}}
\end{dgroup*}

\clearpage

% ============================================================================================
% export to json format

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   for i in range( len(checkpoint) ):
      cdblib.put ('check{:03d}'.format(i),checkpoint[i],checkpoint_file)
\end{cadabra}
\egroup

\end{document}