\section{Covering Spaces}
\subsection{Definitions and Examples}
\begin{definition}
    Let $p:\hat{X}\to X$ be a map.
    An open subset $U\subset X$ is evenly covered if there exists a set $\Delta_U$ with discrete topology and a homeomorphism $\psi:p^{-1}(U)\cong\Delta_U\times U$ such that the diagram
    \[
        \begin{tikzcd}
            p^{-1}(U)\arrow{r}{\psi}\arrow[swap]{d}{p}&\Delta_U\times U\arrow{dl}{(\delta,u)\mapsto u}\\
            U&
        \end{tikzcd}
    \]
    commutes.
\end{definition}
For $\delta\in\Delta_U$, we write $U_\delta=\{\delta\}\times U\subset\Delta_U\times U$.
We can identify it as a subspace of $p^{-1}(U)$ via $\psi^{-1}$.
Then, we can canonically identify $\Delta_U$ with $p^{-1}(\{x\})$ for any $x\in U$.
Also note that we have
$$p^{-1}(U)\cong\Delta_U\times U\cong\coprod_{\delta\in\Delta_U}U$$
\begin{definition}
    If every point in $X$ has an evenly covered neighbourhood, then $p$ is a covering map and $\hat{X}$ is a covering space of $X$.
\end{definition}
\begin{example}
    1. Take $X=I, \Delta_X=\{1,2,3\}, \hat{X}=\Delta_X\times X$ and take $p:\hat{X}\to X$ to be the projection on second coordinate.\\
    But if we take $\hat{X}=I\sqcup[0,1/2)$, then the obvious projection is not a covering map as it fails at the inverse of a small neighbourhood of $1/2$.\\
    2. Let $\hat{X}=\mathbb R$ and $S^1\subset\mathbb C$ the unit circle.
    We take $p:t\mapsto e^{2\pi it}$.
    This is a covering map.
    Indeed, if $U\subsetneq S^1$ is a proper open subset and $z_1\in S^1\setminus U$, then we can choose a branch of the logarithm well-defined on $S^1\setminus\{z_1\}$.
    Write this choice of branch as $\log$.
    Now every point $\hat{z}\in p^{-1}(U)$ can be uniquely written as $\hat{z}=k+\log(z)/(2\pi i)$ for some integer $k$, which induces the homeomorphism $p^{-1}(U)\cong\mathbb Z\times U$.
    So $p$ is a covering map.\\
    3. Let $\hat{X}=X=S^1\subset C$ and $p(z)=z^n$ for some $n\in\mathbb Z_+$ is a covering map.
    Indeed, choose a local branch for $\sqrt[n]{\cdot}$ on a small enough open proper subset $U\subsetneq S^1$ allows us to write $\hat{z}\in p^{-1}(U)$, uniquely, $\hat{z}=e^{2\pi ik/n}\sqrt[n]{z}$ for $k\in\mathbb Z/n\mathbb Z$.
    So $\Delta_{S^1}=\mathbb Z/n\mathbb Z$ works.\\
    4. Let $\hat{X}=S^2$ and $G=\mathbb Z/2\mathbb Z$ acting on $S^2$ by the antipodal map $(x,y,z)\mapsto (-x,-y,-z)$.
    Take $X=\hat{X}/G$.
    We can identify $X$ with the real projective plane $\mathbb{RP}^2$.
    The quotient map $p$ is easily a covering map.
    For $x\in\hat{X}$, take a small enough neighbourhood $U$ of $x$ such that $(-U)\cap U=\varnothing$.
    Let $V=p(U)$, then $p^{-1}(V)=U\cap(-U)$, so we can just take $\Delta_U=\mathbb Z/2\mathbb Z$.
\end{example}
\begin{definition}
    A covering map $p:\hat{X}\to X$ is $n$-sheeted (where $n\in\mathbb N\cup\{\infty\}$) if $|p^{-1}(\{x\})|=n$ for every $x\in X$.
    If such an $n$ exists, we call $n$ the degree of $p$.
\end{definition}
\subsection{Lifting Properties}
We want to connect fundamental group and covering spaces.
This is done by introducing the notion of lifting.
\begin{definition}
    Let $p:\hat{X}\to X$ be a convering map and $f:Y\to X$ a map.
    A lift of $f$ to $\hat{X}$ is a map $\hat{f}:Y\to\hat{X}$ such that the diagram
    \[
        \begin{tikzcd}
            &\hat{X}\arrow{d}{p}\\
            Y\arrow[swap]{r}{f}\arrow[dashed]{ur}{\hat{f}}&X
        \end{tikzcd}
    \]
    commutes.
\end{definition}
\begin{definition}
    A space $X$ is locally path connected if for any $x\in X$ and open set $U\ni x$, there is a neighbourhood $x\ni V\subset U$ that is path-connected.
\end{definition}
\begin{lemma}[Uniqueness of Lifting]\label{lift_unique}
    Let $p:\hat{X}\to X$ be a covering map and $\hat{f}_1,\hat{f}_2:Y\to\hat{X}$ lifts of $f:Y\to X$.
    If $Y$ is connected and locally path connected, and there is $y_0\in Y$ such that $\hat{f}_1(y_0)=\hat{f}_2(y_0)$, then $\hat{f}_1=\hat{f}_2$.
\end{lemma}
However, there exists connected but not locally path-connected space, like the topologist's comb.
\begin{proof}
    We will show that the following set
    $$S=\{y\in Y|\hat{f}_1(y)=\hat{f}_2(y)\}$$
    equals $Y$ by showing it is open and closed.
    The result then follows by the connectedness of $Y$ and the fact that $y_0\in S$ (so $S$ is nonempty).\\
    Let $y_1\in Y$ be arbitrary and $U$ be an evenly covered open neighbourhood of $f(y_1)$.
    Choose $y\in V\subset f^{-1}(U)$ be a path-connected open neighbourhood of $y_1$.
    We shall show that $V\subset S$ if $y_1\in S$ and $V\subset Y\setminus S$ if $y_1\notin S$, which implies what we want.
    To see this, choose any $y\in V$, then there is a path $\alpha:I\to V$ such that $\alpha(0)=y_1$ and $\alpha(1)=y$.
    Then $\hat{f}_i\circ\alpha$ is a path in $\hat{X}$ connecting $\hat{f}_i(y_1)$ and $\hat{f}_i(y)$.
    But $p\circ\hat{f}_i\circ\alpha=f\circ\alpha$ as $\hat{f}_i$ are lifts.
    The image of $f\circ\alpha$ is contained in $f(V)\subset U$ by design.
    This tells us $\hat{f}_i\circ\alpha$ is a path in $p^{-1}(U)\cong\Delta_U\times U$.
    But $\Delta_U$ is discrete, so for each $i$, $\hat{f}_i(y_1)$ and $\hat{f}_i(y)$ must in fact lie in the same copy of $U$ as they must be in the same path component.
    We are actually done.
    Indeed, if $y_1\in S$, then $\hat{f}_1(y_1)=\hat{f}_2(y_1)$, so the copies of $U$ those paths $\hat{f}_i\circ\alpha$ lie on are actually the same.
    This forces $\hat{f}_i(y)$ to be equal since if we denote that particular copy as $U'$ then we have a homeomorphism $p':U'\to U$ by restricting the covering map which gives
    $$\hat{f}_1(y)=(p')^{-1}\circ f(y)=\hat{f}_2(y)$$
    If $y_1\notin S$ but $y\in S$, then by reversing the argument for $y,y_1$ shows $y_1\in S$ which is false.
    (Alternatively, one can argue by observing that each copy of $U$ contains a unique point of $p^{-1}\circ f(\{y_1\})$).
\end{proof}
\begin{definition}
    Let $\gamma:I\to X$ be a path from $x_0$ and $p:\hat{X}\to X$ be a covering map.
    A lift of $\gamma$ at $\hat{x}_0\in\hat{X}$ is a lift $\hat{\gamma}:I\to\hat{X}$ of $\gamma$ such that $\hat{\gamma}(0)=\hat{x}_0$.
\end{definition}
In particular, $p(\hat{x}_0)=x_0$ necessarily.
In the special case where $\gamma$ is contained in an evenly covered open set, its lift is just picking one of the copies of the open set containing $\hat{x}_0$ in the pre-image under the covering map and carve the same path there.
In fact, we can say more.
\begin{lemma}[Path-Lifting Lemma]\label{path_lift}
    Let $p:\hat{X}\to X$ be a covering map and $\gamma:I\to X$ be a path from $x_0$.
    For any $\hat{x}_0\in p^{-1}(x_0)$, there exists a unique lift $\hat{\gamma}$ of $\gamma$ from $\hat{x}_0$.
\end{lemma}
\begin{proof}
    The uniqueness follows from Lemma \ref{lift_unique}.
    For existence, we consider
    $$S=\{t\in I:\gamma|_{[0,t]}\text{ has a lift at $\hat{x}_0$ to $\hat{X}$}\}$$
    and we will show it is open and closed.
    As $0\in S$, we know $S\neq\varnothing$, therefore it shall imply what we want.\\
    Let $t_0\in I$ and $U$ be an evenly covered neighbourhood of $\gamma(t_0)$ and let $V\subset\gamma^{-1}(U)$ be an open interval containing $t_0$.
    Again we will show $t_0\in S$ implies $V\subset S$ and $t_0\notin S$ implies $V\subset I\setminus S$ which gives the result.
    Let $t\in V$ and suppose first that $t_0\in S$.
    If $t\le t_0$ then automatically $t\in S$.
    Otherwise $t>t_0$.
    Denote the lift of $\gamma|_{[0,t_0]}$ by $\hat{\gamma}|_{[0,t_0]}$.
    But then since $U\supset f(V)$ is evenly covered, we can just pick the copy of $U$ where $\hat{\gamma}|_{[0,t_0]}(t_0)$ resides in and continue $\hat{\gamma}$ there by following $\gamma$.
    More precisely, suppose that copy is $U'$ and $p':U'\to U$ is the homeomoephism by restricting $p$, then
    $$[0,t]\ni s\mapsto\begin{cases}
        \hat\gamma(s)\text{, for $s\in[0,t_0]$}\\
        (p')^{-1}\circ\gamma(s)\text{, for $s\in[t_0,t]$}
    \end{cases}$$
    which works as a lift of $\gamma|_{[0,t]}$.
    So $t\in S$.\\
    If $t_0\notin S$ and $t\in V$, then if $t\in S$ and $t\ge t_0$ we get an immediate contradiction.
    But if $t<t_0$ and $t\in S$, we can extend the lift on $[0,t]$ in the way we just described to $t_0$ which shows $t_0\in S$, another contradiction.
    So we are done.
\end{proof}
\begin{lemma}
    If $p:\hat{X}\to X$ is a covering map and $X$ is path-connected, then $p$ is an $n$-sheeted cover for some $n\in\mathbb N\cup\{\infty\}$.
\end{lemma}
Actually we can do something stronger:
We can prove that if $x,y\in X$ then there is a bijection between $p^{-1}(\{x\})$ and $p^{-1}(\{y\})$.
In fact, this is exactly what we shall prove.
\footnote{I think this lemma is trivial by considering the set of points $x$ such that $p^{-1}(x)$ has a given (fixed) cardinality which can be easily shown to be both open and closed, but the lecturer prefers to use path-lifting lemma.}
\begin{proof}
    Let $\gamma$ be a path in $X$ from $x$ to $y$.
    For any $\hat{x}\in p^{-1}(\{x\})$, then there is a unique path $\hat{\gamma}_{\hat{x}}$ lifting $\gamma$ with starting point $\hat{x}$.
    This gives a map $\psi:p^{-1}(\{y\})\to p^{-1}(\{y\})$ via $\psi(\hat{x})=\hat{\gamma}_{\hat{x}}(1)$.
    We shall show that it has an inverse $\phi$ defined in a similar way but using the path $\bar\gamma$ from $y$ to $x$.
    So $\phi(\hat{y})=\widehat{(\bar\gamma)}_{\hat{y}}(1)$.
    We shall show that $\phi\circ\psi=\operatorname{id}_{p^{-1}(\{x\})}$, the other side is completely analogous.
    Indeed,
    $$\phi\circ\psi(\hat{x})=\phi(\hat{\gamma}_{\hat{x}}(1))=\widehat{(\bar\gamma)}_{\hat{\gamma}_{\hat{x}}(1)}(1)$$
    But then $\hat{\gamma}_{\hat{x}}\cdot\widehat{(\bar\gamma)}_{\hat{\gamma}_{\hat{x}}(1)}$ is a lift of $\gamma\cdot\bar\gamma$, but so is $\hat{\gamma}_{\hat{x}}\cdot\overline{(\hat{\gamma}_{\hat{x}})}$ hence by the uniqueness of lifts they have common endpoints.
    In particular, $\widehat{(\bar\gamma)}_{\hat{\gamma}_{\hat{x}}(1)}(1)=\hat{x}$ as desired.
\end{proof}
Here comes a lemma that really links together fundamental groups and covering maps
\begin{lemma}[Homotopy Lifting Lemma]\label{homotopy_lifting}
    Let $p:\hat{X}\to X$ be a covering map and $f_0:Y\to X$ a map from a locally path-connected space.
    Suppose $F:Y\times I\to X$ is a homotopy with $\forall y\in Y,F(y,0)=f_0(y)$ and there exists a lifting $\hat{f}_0:Y\to\hat{X}$ of $f_0$.
    Then there exists a unique lifting $\hat{F}:Y\times I\to\hat{X}$ with $\hat{F}(y,0)=\hat{f}_0(y)$ for any $y\in Y$.
\end{lemma}
\begin{remark}
    In the special case where $Y$ is a one-point space we reproduce Lemma \ref{path_lift}.
\end{remark}
\begin{proof}
    For each $y\in Y$, the homotopu $F$ defines a path $\gamma_y(t)=F_{y,t}$.
    By Lemma \ref{path_lift}, each $\gamma_t$ has a unique lift $\hat{\gamma}_t$ from $\hat{f}_0(y)$.
    So we essentially need $\hat{F}(y,t)=\hat{\gamma}_t(y)$.
    It remains to show that such an $\hat{F}$ has to be continuous.
    The trick is to construct on open subsets of $Y\times I$ a differently constructed lift $\tilde{F}$ which is a priori continuous, and then show that $\hat{F}$ agrees with $\tilde{F}$ on these open sets.
    Fix $y_0\in Y$.
    The goal is to find a neighbourhood $V$ of $y_0$ and a lifting of $F$ on $V\times I$.
    For any $t\in I,F(y_0,t)\in X$ has an evenly covered neighbourhood $U_t\subset X$.
    Then by continuity and the definition of product topology $F^{-1}(U_t)$ contains an open neighbourhood of $(y_0,t)\in Y\times I$ of the form $V_t\times[(t-\epsilon_t,t+\epsilon_t)\cap I]$ for $\epsilon_t>0$ and $V_t\ni y_0$ is open.
    As $Y$ is locally path-connected we might as well assume $V_t$ are path-connected.
    As $\{y_0\}\times I$ is compact, there is a finite set $T\subset I$ such that $\{(t_i-\epsilon_{t_i},t_i+\epsilon_{t_i}):t_i\in T\}$ covers $I$.
    We then take $V=\bigcap_{t_i\in T}V_{t_i}$ which is open and path-connected.
    Take $J_i=(t_i-\epsilon_{t_i},t_i+\epsilon_{t_i})\cap I$.
    Then we know that $F(V\times J_i)$ is contained in an evenly covered open subset $U_i$ of $X$ for each $i$.
    Let $U_i'$ be the unique copy of $U_i$ in $p^{-1}(U_i)$ such that $\hat{F}(\{y_0\}\times J_i)=\hat{\gamma}_{y_0}(J_i)\subset U_i'$.
    Let $p_i:U_i'\to U_i$ be the homeomorphism between them.
    Now for $(y,t)\in V\times I$, we define $\tilde{F}(y,t)=p_i^{-1}\circ F(y,t)$ for $t\in J_i$.
    It is quite obvious it is well-defined, but let's prove it.
    Suppose $t\in J_i\cap J_j$ and let $\alpha$ be a path in $V$ from $y_0$ to $y$.
    Define $\alpha_t(s)=F(\alpha(s),t)$.
    Then $p_i^{-1}\circ \alpha_t$ and $p_j^{-1}\circ\alpha_t$ are lifts of $\alpha_t$.
    Furthermore they have the same initial values at $\hat{F}(y_0,t)$, so they are equal by Lemma \ref{lift_unique}.
    Consequently $p_i^{-1}\circ F(y,t)=p_j^{-1}\circ F(y,t)$ as desired.
    So $\tilde{F}$ is a well-defined and hence continuous by its definition.\\
    As $V$ is path-connected and $\tilde{F}(\cdot,0)$ is a lift of $f_0$ that agrees with $\hat{f}_0$ at $y_0$, we have $\forall y\in V,\hat{F}(y,0)=\hat{f}_0(y)$ by Lemma \ref{lift_unique}.
    Also, for each $y\in V$, $\hat{F}(y,\cdot)$ is a lift of $\gamma_y$ at $\hat{f}_0(y)$.
    Therefore $\tilde{F}(y,t)=\hat{\gamma}_y(t)$ again by Lemma \ref{lift_unique}, thus $\tilde{F}=\hat{F}$ on $V\times I$.
    In particular, $\hat{F}$ is continuous in $V\times I$, hence it is continuous.
\end{proof}
\subsection{Lifting and Fundamental Groups}
\begin{lemma}
    Let $p:\hat{X}\to X$ be a covering map and $F:I\times I\to X$ be a homotopy of paths.
    Then any lift $\hat{F}$ of $F$ is also a homotopy of paths.
\end{lemma}
\begin{proof}
    As $F$ is a homotopy of paths, $F(0,\cdot)$ and $F(1,\cdot)$ are constant paths in $X$.
    Therefore $\hat{F}(0,\cdot)$ and $\hat{F}(1,\cdot)$ are lifts those constant paths , hence constant.
\end{proof}
\begin{lemma}
    Let $p:\hat{X}\to X$ be a covering map with $\hat{x}\in \hat{X}$ and $x=p(\hat{x})$, then the induced homeomorphism $p_\ast:\pi_1(\hat{X},\hat{x})\to\pi_1(X,x)$ via $[\hat\gamma]\mapsto [p\circ\hat\gamma]$ is injective.
\end{lemma}
\begin{proof}
    Suppose $[\hat{\gamma}]\in\ker p_\ast$, then $\gamma=p\circ\hat{\gamma}\simeq c_x$.
    If $F$ is a homotopy between $\gamma$ and $c_x$, we can lift it to a homotopy $\tilde{F}$ from $\hat{\gamma}$ which is a homotopy of paths by the preceding lemma.
    In particular, $\hat{F}$ is a homotopy between $\hat\gamma$ and $c_{\hat{x}}$, so $[\hat\gamma]=[c_{\hat{x}}]$ is the identity.
    This shows the result.
\end{proof}
\begin{remark}
    So we can view $\pi_1(\hat{X},\hat{x})$ as the subgroup $p_\ast(\pi(\hat{X},\hat{x}))\le\pi_1(X,x)$.
    Also note that given $[\gamma]\in\pi_1(X,x)$, we get a map $p^{-1}(\{x\})\to p^{-1}(\{x\})$ via $\hat{x}\mapsto \hat{\gamma}_{\hat{x}}(1)$ which is necessarily bijective.
    So this defines a right group action of $\pi_1(X,x)$ on $p^{-1}(\{x\})$.
    For $\hat{x}\in p^{-1}(\{x\})$, this is induced by $\hat{x}\cdot\gamma=\hat\gamma_{\hat{x}}(1)$.
    Easily $(\hat{x}\cdot\gamma)\cdot\delta=\hat{x}\cdot(\gamma\cdot\delta)$.
\end{remark}
\begin{lemma}
    Let $p:\hat{X}\to X$ be a covering map and suppose $\hat{X}$ is path-connected.
    Let $x\in X$, and let $\Pi$ denote the set of right cosets of $p_\ast(\pi_1(\hat{X},\hat{x}))$ in $\pi_1(X,x)$.
    Then the map $\Pi\to p^{-1}(\{x\})$ via $p_\ast(\pi_1(\hat{X},\hat{x}))[\gamma]\mapsto \hat{x}\cdot\gamma$ is a bijection.
    Further, this bijection satisfies
    $$p_\ast(\pi_1(\hat{X},\hat{x}))[\gamma][\delta]\mapsto\hat{x}\cdot(\gamma\cdot\delta)$$
    where $\gamma,\delta$ are loops based at $x$.
\end{lemma}
\begin{proof}
    If $[\delta]\in p_\ast(\pi_1(\hat{X},\hat{x}))$, then $\hat{x}\cdot\delta=\hat{x}$.
    This obviously implies the map is well-defined.
    To prove this is a bijection, we shall use the Orbit-Stabiliser Theorem.
    Note that the stabliser of $\hat{x}$ on this right action is the set of loops $[\gamma]$ at $x$ such that $\hat\gamma_{\hat{x}}(1)=\hat{x}$ where $\hat{\gamma}_{\hat{x}}$ is the lift of $\gamma$ from $\hat{x}$.
    But then $\hat\gamma_{\hat{x}}$ is a loop, so $\gamma\in p_\ast(\pi_1(\hat{X},\hat{x}))$.
    This means that the stabiliser of $\hat{x}$ is exactly $p_\ast(\pi_1(\hat{X},\hat{x}))$.
    It remains to show that the action is transitive.
    Let $\hat{y}\in p^{-1}(\{x\})$, then there is a path $\hat{\gamma}$ from $\hat{x}$ to $\hat{y}$, so $p\circ\hat{\gamma}$ is a loop based at $x$ and $\hat{x}\cdot\gamma=\hat{y}$.
\end{proof}
\begin{remark}
    So the degree of the covering map $p$ is just the index of $p_\ast(\pi_1(\hat{x},\hat{x}))$ in $\pi_1(X,x)$.
\end{remark}
\begin{example}
    Consider $\pi_1(S^1,x)$.
    The covering map $p_1:\mathbb R\to S^1$ via $t\mapsto e^{2\pi it}$ has infinite degree and $p_2:S^1\to S^1$ via $z\mapsto z^n$ has degree $n$ for any $n>1$.
    So what we conclude from the lemma above is that $\pi_1(S_1,x)$ is an infinite group with subgroups of every finite index.
    A natural guess is then $\pi_1(S^1,x)\cong\mathbb Z$.
    We will prove it later.
\end{example}
\begin{definition}
    If $p:\hat{X}\to X$ is a covering map and $\hat{X}$ is simply connected, then $\hat{X}$ is called a univeral cover of $X$.
\end{definition}
\begin{example}
    The map $t\mapsto e^{2\pi it}$ makes $\mathbb R$ a universal cover of $S^1$.
\end{example}
\begin{corollary}
    If $p:\hat{X}\to X$ is a universal cover, then for any choice of $\hat{x}\in p^{-1}(\{x\})$ there is a bijection
    $$\pi_1(X,x)\to p^{-1}(\{x\}),[\gamma]\mapsto \hat{x}\cdot\gamma$$
\end{corollary}
\begin{proof}
    Immediate from what we have discussed.
\end{proof}
So we can get a group structure on $p^{-1}(\{x\})$ which is essentially isomorphic to $\pi_1(X,x)$ and determined by $\hat{x}\cdot (\gamma\cdot\delta)=(\hat{x}\cdot\gamma)\cdot\delta$.
This allows us to actually calculate some fundamental groups.
\begin{example}
    Now we compute the fundamental group of the circle.
    Let $p:\mathbb R\to S^1$ be the usual universal cover.
    Hence there is a bijection
    $$\pi_1(S^1,1)\to p^{-1}(\{1\})=\mathbb Z\subset\mathbb R$$
    Now for $n\in\mathbb Z$, take $\tilde{\gamma}_n(t)=nt$ to be a path from $0$ to $n$ in $\mathbb R$ and let $\gamma_n=p\circ\hat{\gamma}_n$ which has to be a loop based at $1$.
    Clearly $0\cdot\gamma_n=\hat{\gamma}_n(1)=n$.
    Thus any loop in $S^1$ based at $1$ is homotopic to some $\gamma_n$.
    So the bijection becomes $[\gamma_n]\mapsto n$.
    It remains to show that it is a homomorphism.
    Indeed, for any $m\in\mathbb Z$, we know that $m+\hat{\gamma}_n$ is a lift of $\gamma_n$ from $m$ to $m+n$.
    So
    $$(0\cdot\gamma_m)\cdot\gamma_n=m\cdot\gamma_n=m+n=0\cdot\gamma_{m+n}$$
    Hence it is indeed a homomorphism, it then follows that it is an isomorphism, which means $\pi_1(S^1,1)\cong\mathbb Z$.
\end{example}
In complex analysis, we defined the winding number as, loosely speaking, the number of times a loop wraps around a certain point.
It then follows that this notion is essentially describing the homotopy class a curve is in when we put the thing in $\pi_1(S^1,1)$.
There are more applications of this idea.
\subsection{Applications of Fundamental Groups}
\begin{theorem}[No-Retraction Theorem]\label{no_retraction}
    The identity map of $S^1$ does not extend to a map $r:D^2\to S^1$ (where we view $S^1$ as $\partial D^2$).
\end{theorem}
That is, $S^1$ is not a retract of $D^2$.
We have essentially covered the proof in the introduction, where all technical details have just been covered.
\begin{proof}
    If such an $r$ exists, let $\iota:S^1\hookrightarrow D^2$ be the inclusion map, then $r\circ\iota=\operatorname{id}_{S^1}$.
    As $D^2$ is contractible, $\pi_1(D^2,1)=0$, so there is a factorisation
    $$(\operatorname{id}_{S^1})_\ast=(r\circ\iota)_\ast=r_\ast\circ\iota_\ast:\pi_1(S^1,1)\to\pi_1(D^2,1)\to\pi_1(S^1,1)$$
    But the last diagram is just $\mathbb Z\to\{0\}\to\mathbb Z$.
    This means that $(\operatorname{id}_{S^1})_\ast$ has to be constant.
    But it isn't.
    Contradiction.
\end{proof}
\begin{theorem}[Brouwer's Fixed Point Theorem]
    Any map $f:D^2\to D^2$ has a fixed point.
\end{theorem}
\begin{proof}
    Suppose not, then let $g:D^2\to S^1$ be the map given by projecting $f(x)$ through $x$ onto $S^1$.
    That is, $g(x)$ is the intersection of the line joining $x$ and $f(x)$ and $S^1$ that is closer to $x$.
    Then $g$ is continuous and $g(x)=x$ for any $x\in S^1$.
    This however means that $g$ restricts to $\operatorname{id}_{S^1}$, contradicing Theorem \ref{no_retraction}.
\end{proof}
\begin{theorem}[The Fundamental Theorem of Algebra]
    Every nonconstant polynomial $p:\mathbb C\to\mathbb C$ has a zero.
\end{theorem}
\begin{proof}
    Let $r:\mathbb C\setminus\{0\}\to S^1$ via $r(z)=z/|z|$ which is a retraction.
    For $R\ge 0$, we define $\lambda_R:S^1\to\mathbb C, \lambda_R(z)=Rz$.
    If $p$ has no zero, then we can define $f_R:r\circ p\circ \lambda_R:S^1\to S^1$.
    Easily for any $R_1,R_2\in\mathbb R_{\ge 0}$, $f_{R_1}$ and $f_{R_2}$ are homotopic, hence $(f_{R_1})_\ast=(f_{R_2})_\ast$.
    But these induced maps are all homomorphisms $\mathbb Z\to\mathbb Z$.
    Therefore all $(f_R)^\ast$ is given by multiplication by $d$ for some fixed $d\in\mathbb N\setminus\{0\}$.
    But $f_0$ is constant, therefore $d=0$ and $(f_R)_\ast$ are constantly zero.
    But for very large $R$, the leading term dominates $p$, so it is clear that $(f_R)_\ast$ is given by multiplication by $\deg p\neq 0$, contradiction.
\end{proof}
\begin{definition}
    A space $X$ is locally simply connected if, for every $x\in X$ and open neighbourhood $U$ of $x$, there exists a simply connected neighbourhood $x\in V\subset U$.
\end{definition}
\begin{example}[Non-example]
    Take the Hawaii Earing space
    $$X=\bigcup_{n=1}^\infty \left\{(x,y)\in\mathbb R^2:\left( x-\frac{1}{n} \right)+y^2=\frac{1}{n^2}\right\}$$
    then no open neighbourhood of $(0,0)$ is simply connected.
\end{example}
\begin{theorem}[Existence of Universal Cover]
    Let $X$ be a path-connected space such that $X$ is localled simply connected, then there exists a universal cover $p:\hat{X}\to X$.
\end{theorem}
\begin{proof}[Sketch of proof]
    Fix $x_0\in X$ and consider the set $\mathscr X$ of all paths $\gamma$ from $x_0$.
    Define $\hat{X}=\mathscr X/\simeq$ where $\simeq$ is the path homotopy equivalence relation.
    The intended map $p:\hat{X}\to X$ is gives by $p([\gamma])=\gamma(1)$.
    The tricky bit is to find a topology on $\hat{X}$ making it work, which -- guess what -- is skipped.
\end{proof}
\begin{example}
    Take $X$ to be the figure eight, then $\hat{X}$ looks like the Caylay graph of the free group $F_2$.
\end{example}
\subsection{The Galois Correspondence}
The idea is to classify all covering spaces using subgroups of the fundamental group, which is in certain ways analogous to the idea of Galois correspondence in Galois theory.
\begin{definition}
    Let $X$ be a path-connected space and $p_1:\hat{X}_1\to X_1, p_2:\hat{X}_2\to X$.
    An isomorphism of covering spaces is a homeomorphism $\phi:\hat{X}_1\to\hat{X}_2$ such that
    \[
        \begin{tikzcd}
            X&\hat{X}_1\arrow{dl}{\phi}\arrow[swap]{l}{p_1}\\
            \hat{X}_2\arrow{u}{p_2}&
        \end{tikzcd}
    \]
    commutes.
\end{definition}
Note also that $\phi^{-1}$ is automatically an isomorphism of covering spaces as well
If $\hat{X}_i$ are equipped with basepoints $\hat{x}_i\in\hat{X}_i$ and $\phi(\hat{x}_1)=\hat{x}_2$, we say $\phi$ is based.
\begin{remark}
    Note that $\phi$ is a lift of $p_1$ to $\hat{X}_2$, so by Lemma \ref{lift_unique}, a based isomorphism is uniquely determined by the basepoints $\phi(\hat{x}_1)=\hat{x}_2$ if $\hat{X}_1$ is connected and locally path-connected.
\end{remark}
\begin{theorem}[Galois Correspondence with Basepoints]\label{based_galois}
    Let $X$ be a path-connected, locally simply connected space with basepoint $x_0$.
    The map which sends a covering $p:\hat{X}\to X$ equipped with a basepoint $\hat{x}_0\in p^{-1}(\{x_0\})$ to the subgroup $p_\ast(\pi_1(\hat{X},\hat{x}_0))\le\pi_1(X,x_0)$ induces a bijection between the set of based isomorphism classes of path-connected covering spaces with basepoint and the set of subgroups of $\pi_1(X,x_0)$.
\end{theorem}
\begin{proof}
    Not dreadfully hard but omitted.
\end{proof}
\begin{example}
    As $\pi_1(S^1,1)=\mathbb Z$, each subgroup of $\mathbb Z$ is of the form $n\mathbb Z$ for natural number $n$ (including $0$).
    Then the usual $p:\mathbb R\to S^1, t\mapsto e^{2\pi it}$ corresponds to the subgroup $\{0\}$, and the maps $p:S^1\to S^1, z\mapsto z^n$ corresponds to the subgroups $n\mathbb Z$ for $n\neq 0$.
    Hence, these are the only path-connected covering spaces of $S^1$ up to based isomorphism.
\end{example}
\begin{corollary}
    Let $X$ be a path-connected and locally simply connected space, then any two universal covers $p_1:\hat{X}_1\to X$ and $p_2:\hat{X}_2\to X$ are isomorphic.
\end{corollary}
\begin{proof}
    Immediate.
\end{proof}
\begin{corollary}[Galois Correspondence without Basepoints]\label{unbased_galois}
    Let $X$ be a path-connected, locally simply connected space with basepoint $x_0$.
    Then the map that sends a covering $p:\hat{X}\to X$ equipped with a basepoint $\hat{x}_0\in p^{-1}(\{x_0\})$ to the subgroup $p_\ast(\pi_1(\hat{X},\hat{x}))\le\pi_1(X,x_0)$ induces a bijection between (unbased) isomorphism classes of path-connected convering spaces of $X$ and conjugacy classes of subgroups of $\pi_1(X,x_0)$.
\end{corollary}
\begin{proof}
    This map is surjective due to Theorem \ref{based_galois}.
    To see it is injective, we need to show that if $(p_1)_\ast(\pi_1(\hat{X}_1,\hat{x}_1))$ and $(p_2)_\ast(\pi_1(\hat{X}_2,\hat{x}_2))$ are conjugate subgroups of $\pi_1(X,x_0)$, then there is an (unbased) isomorphism $\phi:\hat{X}_1\to\hat{X}_2$ of covering spaces.\\
    Suppose
    $$(p_1)_\ast(\pi_1(\hat{X}_1,\hat{x}_1))=[\gamma](p_2)_\ast(\pi_1(\hat{X}_2,\hat{x}_2))[\bar\gamma]$$
    for some $[\gamma]\in\pi_1(X,x_0)$.
    Let $\widehat{\bar{\gamma}}$ be the lift of $\bar\gamma$ at $\hat{x}_2$ and $\hat{x}_2'$ be the other endpoint of $\widehat{\bar\gamma}$.
    By the last part of Lemma \ref{indep_basepoint},
    \begin{align*}
        [\gamma](p_2)_\ast(\pi_1(\hat{X}_2,\hat{x}_2))[\bar\gamma]&=\bar\gamma_\#((p_2)_\ast(\pi_1(\hat{X}_2,\hat{x}_2)))\\
        &=(p_2)_\ast(\widehat{\bar\gamma}_\#(\pi_1(\hat{X}_2,\hat{x}_2)))\\
        &=(p_2)_\ast(\pi_1(\hat{X}_2,\hat{x}_2'))
    \end{align*}
    Therefore $(p_1)_\ast(\pi_1(\hat{X}_1,\hat{x}_1))=(p_2)_\ast(\pi_1(\hat{X}_2,\hat{x}_2'))$.
    Theorem \ref{based_galois} then gives a based isomorphism $(\hat{X}_1,\hat{x}_1)\cong(\hat{X}_2,\hat{x}_2')$.
    In particular, $\hat{X}_1\cong\hat{X}_2$ as (unbased) covering spaces.
\end{proof}


\documentclass{article}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}
    \usepackage{mathrsfs}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{tikz}
    \usepackage{fancyhdr}
    \usepackage{multicol}

    \usetikzlibrary{positioning}

\pagestyle{fancy}
\fancyhf{}
\rhead{Edgar Jacob Rivera Rios - A01184125}

\renewcommand{\labelenumi}{\alph{enumi})}

\begin{document}
\section*{3.3.1}
Sales personnel for Skillings Distributors submit weekly reports listing the customer contacts made during the week.
A sample of 65 weekly reports showed a sample mean of 19.5 customer contacts per week. The sample standard deviation was 5.2.
Provide 90\% and 95\% confidence intervals for the population mean number of weekly customer contacts for the sales personnel.
\begin{align*}
  \text{Margin} &= 90\%\\
  s &= 5.2\\
  n &= 65\\
  \bar{x} &= 19.5\\
  \text{Error Margin} &= t_{\alpha/2}(s/ \sqrt{n})\\
  &=t_{.1/2}(5.2/ \sqrt{65})\\
  &=t_{.05}(0.644980619863884)\\
  &=1.669(0.644980619863884)\\
  &=1.07648105544093\\
  interval &= \bar{x} \pm \text{Error Margin}\\
  &= 19.5 \pm 1.07648105544093\\
  &= (18.4235189445591, 20.5764810554409)
\end{align*}

\begin{align*}
  \text{Margin} &= 95\%\\
  s &= 5.2\\
  n &= 65\\
  \bar{x} &= 19.5\\
  \text{Error Margin} &= t_{\alpha/2}(s/ \sqrt{n})\\
  &=t_{.05/2}(5.2/ \sqrt{65})\\
  &=t_{.025}(0.644980619863884)\\
  &=1.998(0.644980619863884)\\
  &=1.28849691076229\\
  interval &= \bar{x} \pm \text{Error Margin}\\
  &= 19.5 \pm 1.28849691076229\\
  &= (18.2115030892377, 20.7884969107623)
\end{align*}

\section*{3.3.2}
The mean number of hours of flying time for pilots at Continental Airlines is 49 hours per month (The Wall Street Journal, February 25, 2003). Assume that this mean was based on actual flying times for a sample of 100 Continental pilots and that the sample standard deviation was 8.5 hours.
\begin{enumerate}
  \item At 95\% confidence, what is the margin of error?
  \begin{align*}
    \text{Margin} &= 95\%\\
    s &= 8.5\\
    n &= 100\\
    \bar{x} &= 49\\
    \text{Error Margin} &= t_{\alpha/2}(s/ \sqrt{n})\\
    &=t_{.05/2}(8.5/ \sqrt{100})\\
    &=t_{.025}(0.85)\\
    &=1.984(0.85)\\
    &=1.68658440884845
  \end{align*}

  \item What is the 95\% confidence interval estimate of the population mean flying time for the pilots?
  \begin{align*}
    interval &= \bar{x} \pm \text{Error Margin}\\
    &= 59 \pm 1.68658440884845\\
    &= (47.3134155911515, 50.6865844088485 )
  \end{align*}

  \item The mean number of hours of flying time for pilots at United Airlines is 36 hours per month. Use your results from part (b) to discuss differences between the flying times for the pilots at the two airlines. (The Wall Street Journal reported United Airlines as having the highest labor cost among all airlines. Does the information in this exercise provide insight as to why United Airlines might expect higher labor costs?)

  It's probably because United airlines pilots fly a lot less, so the costs may stay relative high when compared to pilots who fly 10 hours more a month
\end{enumerate}

\section*{3.3.3}
The average cost per night of a hotel room in New York City is \$273 (Smart Money, March 2009). Assume this estimate is based on a sample of 45 hotels and that the sample standard deviation is \$65.
\begin{enumerate}
  \item With 95\% confidence, what is the margin of error?
  \begin{align*}
    \text{Margin} &= 95\%\\
    s &= 65\\
    n &= 45\\
    \bar{x} &= 273\\
    \text{Error Margin} &= t_{\alpha/2}(s/ \sqrt{n})\\
    &=t_{.05/2}(65/ \sqrt{45})\\
    &=t_{.025}(9.689)\\
    &=2.015(9.689)\\
    &=19.5281618831222
  \end{align*}

  \item What is the 95\% confidence interval estimate of the population mean?
  \begin{align*}
    interval &= \bar{x} \pm \text{Error Margin}\\
    &= 265 \pm 19.5281618831222\\
    &= (253.471838116878, 292.528161883122)
  \end{align*}

  \item Two years ago the average cost of a hotel room in New York City was \$229. Discuss the change in cost over the two-year period

  It seems that the average cost of hotels in New York from two years ago tends to be to the lower estimate of the actual average. Which is normal considering the inflation, which is that costs go up and the money looses value over time.
\end{enumerate}

\section*{3.3.4}
Is your favorite TV program often interrupted by advertising? CNBC presented statistics on the average number of programming minutes in a half-hour sitcom (CNBC, February 23, 2006). The following data (in minutes) are representative of their findings:
\begin{table}[h!]
  \centering
  \begin{tabular}{c c c c c c}
    20.02&22.20&21.20&21.06&22.24&20.62\\
    22.37&22.19&22.34&21.66&21.23&23.86\\
    23.82&20.30&21.52&21.52&21.91&23.1\\
    23.36&23.44&&&&\\
  \end{tabular}
\end{table}
Assume the population is approximately normal. Provide a point estimate and a 95\% confidence interval for the mean number of programming minutes during a half-hour television sitcom.

\begin{align*}
  \text{Margin} &= 95\%\\
  s &= 1.11702424230396\\
  n &= 20\\
  \bar{x} &= 21.998\\
  \text{Error Margin} &= t_{\alpha/2}(s/ \sqrt{n})\\
  &=t_{.05/2}(1.11702424230396/ \sqrt{20})\\
  &=t_{.025}(0.249774213830685)\\
  &=2.093(0.249774213830685)\\
  &=0.522783437718549\\
  interval &= \bar{x} \pm \text{Error Margin}\\
  &= 21.998 \pm 0.522783437718549\\
  &= (21.4752165622814, 22.5207834377185)
\end{align*}
\end{document}


%!TEX root = TDT4265-Summary.tex
\section{Optical flow}
Optical flow is the task of describing how pixels move in an image, thus describing motion of surfaces or objects in an image. Mathematically, you calculate the motion at each pixel position between two frames at times $t$ and $t + \Delta t$.

\subsection{Constraints}
We usually assume constant brightness between frames
\begin{equation}
    I(x,y,t) = I(x + \Delta x, y + \Delta y, t + \Delta t)
\end{equation}
where $I(\cdot)$ is image intensity. We also assume small motion
\begin{multline}
    I(x + \Delta x, y + \Delta y, t + \Delta t) =\\
    I(x,y,z) + \pd{I}{x}\Delta x + \pd{I}{y}\Delta y + \pd{I}{t} \Delta t.
\end{multline}
This gives
\begin{equation}
    \pd{I}{x} V_x + \pd{I}{y} V_y + \pd{I}{t} = 0.
\end{equation}

Determining optical flow can be done in several ways. Sections \ref{ssec:lucas-kanade} and \ref{ssec:horn-schunck} describe differential methods.

\paragraph{Aperture problem} The aperture problem comes from the fact that what a camera sees is only a small part of the world. Motion inside of this frame may appear different than it really is, if the moving object is not contained in the frame.

\subsection{Lucas--Kanade (LK) method}\label{ssec:lucas-kanade}
LK tracks interest points in a scene from one frame to the next. Can use Harris corner points as the interest points. It assumes constant flow in a neighborhood of the pixel examined, and solves the optical flow equations by least squares. Using a neighborhood reduces ambiguities and improves noise performance. But being purely local, it cannot determine flow in the interior of uniform regions.

\subsection{Horn--Schunck (HS) method}\label{ssec:horn-schunck}
A method to estimate optical flow globally. It introduces a global smoothness constraint to solve the aperture problem. It minimizes distortion in flow globally, and prefers solutions that are smoother.

\subsection{Kanade--Lucas-Tomasi (KLT) feature tracker}\label{ssec:kanade-lucas-tomasi}
KLT is a computationally fast feature extraction method that is useful for tracking in realtime video.


\documentclass{article}
\input{preamble}
\begin{document}
\section{Noise Calculation}

The assumption is that the noise of a point source with flux $F$ has a variance
 obtained by quadratically adding the variance of a background $N_0^2$ and a quantity proportional to the Poisson Noise due to the source.
\be
N^2 = N_0^2 + \alpha F
\ee
Given a source Flux, the noise has two unknown quantities $\alpha$ and $N_0$.
Therefore, it is posible to eliminate one of these variables in terms of
quantities at a standard SNR. Let us consider this standard SNR to be 5.
Then, we have
\be
\frac{F_5^2}{N_0^2 + \alpha F_5} = 25
\ee
implying we can eliminate
\be
\alpha = F_5 \left( 0.04 - \left(\frac{N_0}{F_5}\right)^2 \right)
\ee
At the linear level, the noise in the magnitude $\sigma_m$ is
\beqn
\sigma_m^2 &=& \left(\frac{2.5}{\log{(10)}}\right)^2 (N/F)^2 \\
&\approx& \left(\frac{N_0}{F }\right)^2 + \alpha \left(\frac{1}{F}\right) \\
&=& \left(\frac{N_0}{F_5}\right)^2 \left(F_5/F\right)^2 + \left( 0.04 - ({N_0/F_5})^2\right) (F_5/F) \\
&=& \gamma x^2 + (0.04 - \gamma) x
\eeqn
We could put back the initial missing factor $\approx 1$ to get
\be
\sigma_m^2  = \left(2.5/\log{(10)}\right)^2 \left( \gamma x^2  + (0.04 - \gamma ) x \right)
\ee
where
$\gamma \equiv \left( N_0/ F_5 \right)^2$ and $x \equiv F_5/F = 10.0^{0.4(m-m_5)}.$ In terms of $\gamma$ and $m_5,$ we may write the value of $\alpha$:
\be
\alpha = 10.0^{-0.4 m_5} ( 0.04 - \gamma)
\ee
We could write the Flux in terms of counts. This would be
$$
C_5 = \frac{\pi D^2 T_{exp}}{4 g h} F5
$$
Then,
\be
\gamma  = \left(\frac{\pi D^2 T_{exp} N0}{4 g h C_5}\right)^2
\ee
% \section{Rewrite}
% We will consider PSF photometry of a point source as described in the SNR document using the estimator
% \be
% \hat{S} = \sum w_i C_i
% \ee. Here, the physical signal is the Flux of an astrophysical point source in absolute units in a filter band. This means that number of counts $C$ and the variance of the estimator is
% \be
% N^2 = C/g + N_0^2
% \ee
% where g is the gain
\end{document}


\section{Macaulay Notation}

Macaulay Notation\footnote{N.b. This should all be familiar to you from your 1st Year Solid Mechanics.} is a method used for the structural analysis of Euler-Bernoulli beams and describes the beam forces, moments and deflection. The method is particularly useful for discontinuous and/or discrete loading scenarios as well as loadings that are uniformly distributed loads (u.d.l.) and/or uniformly varying loads (u.v.l.) over the span of a beam.

The\marginnote{Method} method starts with the Euler-Bernoulli beam theory and the relation between the deflection $w$ and bending moment $M$.

\begin{equation}
  \pm EI\frac{\text{d}^2 w}{\text{d}x^2} = M
\end{equation}

\noindent Where $E$ is the elastic modulus and $I$ is the second moment of area.

In terms of Macaulay Notation, $M$ is expressed in the form:

\begin{equation}
  M = M_1(x) + P_1\langle x-a_1\rangle^{b_1} + P_2\langle x-a_2\rangle^{b_2} + P_3\langle x-a_3\rangle^{b_3} + \ldots
\end{equation}

\noindent Where $M_1$ is the moment at the start of $x$ and $P_i\langle x-a_i\rangle^{b_i}$ representing elements along the beam that contribute to the moment. These contribute to the scenario when $x$ becomes greater than $a_i$:

\begin{equation}
  \langle x - a_i\rangle =
  \begin{cases}
    0 & \mathrm{if}~ x < a_i \\
    x - a_i & \mathrm{if}~ x > a_i
  \end{cases}
\end{equation}

\noindent $b_i$ is determined by the type of loading that is being applied.

For\marginnote{Shear Force} the shaft design exercise, you will be taking the shear forces and integrating them to get your bending moments for the two axes. For example, the Macaulay Notation for the Free Body Diagrams in \cref{fig-fbd} are as follows:
\begin{equation}
  S_v = R_{v1}\langle x-x_1\rangle^0 + F_{1}\langle x-x_2\rangle^0  + F_{2}\langle x-x_3\rangle^0 + R_{v2}\langle x-x_4\rangle^0
\end{equation}
\begin{equation}
  S_h = F_{3}\langle x-x_0\rangle^0 + R_{h1}\langle x-x_2\rangle^0 + R_{h2}\langle x-x_4\rangle^0
\end{equation}

From\marginnote{Shear Force Diagram} these equations, the shear force diagrams for the two axes can be generated (\cref{fig-sfd}).

\begin{figure*}[th!]

    \hfill
    \subfloat[Vertical Shear]{
        \includestandalone[width=0.45\textwidth, mode=buildnew]{03_macaulay_notation/vertical_shear}
    }
    \hfill
    \subfloat[Horizontal Shear]{
        \includestandalone[width=0.45\textwidth, mode=buildnew]{03_macaulay_notation/horizontal_shear}
    }
    \hfill

    \vspace{2em}
    \caption{Shear force diagrams}
    \label{fig-sfd}
\end{figure*}

Having\marginnote{Bending Moment}  described the shear forces in Macaulay Notation, it is then the case of integrating and determining the constant of integration to arrive at an equation that describes the bending moment at any point through the beam.
\begin{equation}
  M_v = \int S_v = R_{v1}\langle x-x_1\rangle^1 + F_{1}\langle x-x_2\rangle^1  + F_{2}\langle x-x_3\rangle^1 + R_{v2}\langle x-x_4\rangle^1
\end{equation}
\begin{equation}
  M_h = \int S_h = F_{3}\langle x-x_0\rangle^1 + R_{h1}\langle x-x_2\rangle^1 + R_{h2}\langle x-x_4\rangle^1
\end{equation}

Using\marginnote{Bending Moment Diagram} these equations, one can obtain the bending moment diagrams for the loaded beam (\cref{fig-bmd}).

\begin{figure*}[th!]

    \hfill
    \subfloat[Vertical Bending]{
        \includestandalone[width=0.45\textwidth, mode=buildnew]{03_macaulay_notation/vertical_bending}
    }
    \hfill
    \subfloat[Horizontal Bending]{
        \includestandalone[width=0.45\textwidth, mode=buildnew]{03_macaulay_notation/horizontal_bending}
    }
    \hfill

    \vspace{2em}
    \caption{Bending Moment Diagrams}\label{fig-bmd}
\end{figure*}


Software architecture is of central importance in any large software
project because it establishes predictable patterns of usage and
development~\cite{Shaw1996}. This section describes the essential
structural components of SymPy, provides justifications for the design
decisions that have been made, and gives example
user-facing code as appropriate.

\subsection{The Core}
\label{sec:core}
A computer algebra system stores mathematical expressions as data structures.
For example, the mathematical expression $x + y$ is represented as a tree with
three nodes, $+$, $x$, and $y$, where $x$ and $y$ are ordered children of $+$.
As users manipulate mathematical expressions with traditional mathematical
syntax, the CAS manipulates the underlying data structures. Symbolic
computations such as integration, simplification, etc.\ are all functions that
consume and produce expression trees.

In SymPy every symbolic expression is an instance of the class
\texttt{Basic},\footnote{Some internal classes, such as those used in the
  polynomial submodule, do not follow this rule for efficiency reasons.} the
superclass of all SymPy types providing common methods to all SymPy
tree-elements, such as traversals. The children of a node in the tree are held
in the \texttt{args} attribute. A leaf node in the expression tree
has empty \texttt{args}.

For example, consider the expression $xy + 2$:
\begin{verbatim}
>>> x, y = symbols('x y')
>>> expr = x*y + 2
\end{verbatim}
By order of operations, the parent of the expression tree for \texttt{expr} is
an addition. It is of type \texttt{Add}. The child nodes of \texttt{expr} are
\texttt{2} and \texttt{x*y}.
\begin{verbatim}
>>> type(expr)
<class 'sympy.core.add.Add'>
>>> expr.args
(2, x*y)
\end{verbatim}

Descending further down into the expression tree yields the full expression. For
example, the next child node (given by \texttt{expr.args[0]}) is
\texttt{2}. Its class is \texttt{Integer}, and it has an empty \texttt{args}
tuple, indicating that it is a leaf node.
\begin{verbatim}
>>> expr.args[0]
2
>>> type(expr.args[0])
<class 'sympy.core.numbers.Integer'>
>>> expr.args[0].args
()
\end{verbatim}
Symbols or symbolic constants, like $e$ or $\pi$, are other examples of
leaf nodes.
\begin{verbatim}
>>> exp(1)
E
>>> exp(1).args
()
>>> x.args
()
\end{verbatim}

A useful way to view an expression tree is using the \texttt{srepr} function, which
returns a string representation of an expression as valid Python code\footnote{
\label{note:dotprint}
The \texttt{dotprint} function from the \texttt{sympy.printing.dot} submodule
prints output to dot format, which can be rendered with Graphviz to
visualize expression trees graphically.}
with all the nested class constructor calls to create the given expression.
\begin{verbatim}
>>> srepr(expr)
"Add(Mul(Symbol('x'), Symbol('y')), Integer(2))"
\end{verbatim}

Every SymPy expression satisfies a key identity invariant:
\begin{verbatim}
expr.func(*expr.args) == expr
\end{verbatim}
This means that expressions are
rebuildable from their \texttt{args}.\footnote{\texttt{expr.func} is used
instead of \texttt{type(expr)} to allow the function of an expression to be
distinct from its actual Python class. In most cases the two are the same.}
Note that in SymPy the \texttt{==} operator represents exact
structural equality, not mathematical equality. This allows testing if any two
expressions are equal to one another as expression trees. For example, even
though ${(x + 1)}^2$ and $x^2 + 2x + 1$ are equal mathematically, SymPy gives
\begin{verbatim}
>>> (x + 1)**2 == x**2 + 2*x + 1
False
\end{verbatim}
because they are different as expression trees (the former is a \verb|Pow|
object and the latter is an \verb|Add| object).

Another important property of SymPy expressions is that they are immutable.
This simplifies the design of SymPy, and enables expression interning. It also
enables expressions to be hashed, which allows expressions to be used as keys
in Python dictionaries, and is used to implement caching in SymPy.

Python allows classes to override mathematical operators. The Python
interpreter translates the above \texttt{x*y + 2} to, roughly,
\verb|(x.__mul__(y)).__add__(2)|. Both \texttt{x} and \texttt{y}, returned
from the \texttt{symbols} function, are \texttt{Symbol} instances. The
\texttt{2} in the expression is processed by Python as a literal, and is
stored as Python's built in \texttt{int} type. When \texttt{2} is passed to the
\verb|__add__| method of \texttt{Symbol}, it is converted to the SymPy type
\verb|Integer(2)| before being stored in the resulting expression tree. In
this way, SymPy expressions can be built in the natural way using Python
operators and numeric literals.

%% TODO: describe how assumptions are implemented

%%
%% Extensibility
\subsection{Extensibility}

While the core of SymPy is relatively small, it has been extended to a wide variety
of domains by a broad range of contributors.
This is due, in part, to the fact that the same language, Python,
is used both for the internal implementation and the external usage by users.
All of the extensibility capabilities available to
users are also utilized by SymPy itself. This eases the transition pathway from
SymPy user to SymPy developer.

The typical way to create a custom SymPy object is to subclass an existing
SymPy class, usually \texttt{Basic}, \texttt{Expr}, or \texttt{Function}. As
it was stated before, all SymPy classes used for expression trees should be
subclasses of the base class \texttt{Basic}. \texttt{Expr} is the
\texttt{Basic} subclass for mathematical objects that can be added and
multiplied together. The most commonly seen classes in SymPy are subclasses of
\texttt{Expr}, including \texttt{Add}, \texttt{Mul}, and \texttt{Symbol}.
Instances of \texttt{Expr} typically represent complex numbers, but may also
include other ``rings'', like matrix expressions. Not all SymPy classes are
subclasses of \texttt{Expr}. For instance, logic expressions, such as
\verb|And(x, y)|, are subclasses of \texttt{Basic} but not of
\texttt{Expr}.\footnote{See section~\ref{S-suppsec:Logic} of the supplementary
  material for more information on the
  \texttt{sympy.logic} submodule.}

The \texttt{Function} class is a subclass of \texttt{Expr} which makes it
easier to define mathematical functions called with arguments. This includes
named functions like $\sin(x)$ and $\log(x)$ as well as undefined functions
like $f(x)$. Subclasses of \texttt{Function} should define a
class method \texttt{eval}, which returns an evaluated value for the function
application (usually an instance of some other class, e.g., a \texttt{Number}),
or \texttt{None} if for the given arguments it should not be
automatically evaluated.

Many SymPy functions perform various evaluations down the expression tree.
Classes define their behavior in such functions by defining a relevant
\verb|_eval_|\texttt{\textit{*}} method. For instance, an object can indicate
to the \texttt{diff} function how to take the derivative of itself by defining
the \verb|_eval_derivative(self, x)| method, which may in turn call
\texttt{diff} on its \texttt{args}. (Subclasses of \texttt{Function} should
implement the \texttt{fdiff} method instead; it returns the derivative of the function
without considering the chain rule.) The most common
\verb|_eval_|\texttt{\textit{*}} methods relate to the assumptions:
\verb|_eval_is_|\texttt{\textit{assumption}} is used to deduce
\textit{assumption} on the object.

Listing~\ref{fig:gamma-example} presents an example of this extensibility. It
gives a stripped down version of the \texttt{gamma} function $\Gamma(x)$ from
SymPy. The methods defined allow it to evaluate itself on positive integer
arguments, define the real assumption, allow it to be rewritten in terms of
factorial (with \verb|gamma(x).rewrite(factorial)|), and allow it to be
differentiated. \texttt{self.func} is used throughout instead of referencing
\texttt{gamma} explicitly so that potential subclasses of \texttt{gamma} can
reuse the methods.

\lstset{
  basicstyle=\ttfamily,
}

\begin{lstlisting}[caption={A minimal implementation of \texttt{sympy.gamma}.},label=fig:gamma-example]
from sympy import Function, Integer, factorial, polygamma

class gamma(Function):
    @classmethod
    def eval(cls, arg):
        if isinstance(arg, Integer) and arg.is_positive:
            return factorial(arg - 1)

    def _eval_is_real(self):
        x = self.args[0]
        # noninteger means real and not integer
        if x.is_positive or x.is_noninteger:
            return True

    def _eval_rewrite_as_factorial(self, z):
        return factorial(z - 1)

    def fdiff(self, argindex=1):
        from sympy.core.function import ArgumentIndexError
        if argindex == 1:
            return self.func(self.args[0])*polygamma(0, self.args[0])
        else:
            raise ArgumentIndexError(self, argindex)
\end{lstlisting}
The gamma function implemented in SymPy has many more capabilities than the
above listing, such as evaluation at rational points and series expansion.

\subsection{Performance}
\label{sec:performance}

Due to being written in pure Python without the use of extension modules,
SymPy's performance characteristics are generally poorer than that of
its commercial competitors. For many applications,
the performance of SymPy, as measured by clock cycles, memory usage, and memory
layout, is sufficient.
However, the boundaries for when SymPy's pure Python strategy becomes
insufficient are when the user requires handling of very long expressions or many
small expressions. Where this boundray lies depends on the system at hand, but tends
to be within the range of $10^4$--$10^6$ symbols for modern computers.

For this reason, a new project called SymEngine~\cite{SymEngine} has been started.
The aim of this poject is to develop a library with better performance
characteristics for symbolic manipulation. SymEngine is a pure C++ library,
which allows it fine-grained control over the memory layout of expressions.
SymEngine has thin wrappers to other languages (Python, Ruby,
Julia, etc.). Its aim is to be the fastest symbolic manipulation library. Preliminary
benchmarks suggest that SymEngine performs as well as its commercial and
open source competitors.

The development version of SymPy has recently started to use SymEngine as an
optional backend, initially in \texttt{sympy.\allowbreak{}physics.\allowbreak{}mechanics} only.
Future work will involve
allowing more algorithms in SymPy to use SymEngine as a backend.


\documentclass[11pt, a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{amsmath}
\usepackage{placeins}
\usepackage{amssymb}

\begin{document}

\title{SINGULAR VALUE DECOMPOSITION}
\date{}
\maketitle

Singular Value Decomposition (SVD) is the factorization of any $m\times n$ matrix $A$ as $A = U \Sigma V^T$ where $U$ is a $m \times m$ matrix made up of columns of mutually perpendicular unit vectors, $\Sigma$ is a diagonal matrix of the same shape as $A$ and $V^T$ is a $n \times n$ matrix made up of rows of mutually perpendicular unit vectors.

\section{Example}

Let $A$ be a $2 \times 3$ matrix as follows,

\begin{align*}
	A = \begin{pmatrix}
	3  & 0 & 0 \\
	-8 & 0 & 3
	\end{pmatrix}
\end{align*}

To calculate unit vectors of $U$, eigenvalues and eigenvectors of $AA^T$ are calculated,

\begin{align*}
	AA^T &= \begin{pmatrix}
	3  & 0 & 0 \\
	-8 & 0 & 3
	\end{pmatrix} \begin{pmatrix}
	3 & -8 \\
	0 & 0 \\
	0 & 3
	\end{pmatrix} \\
	&= \begin{pmatrix}
	9 & -24 \\
	-24 & 73
	\end{pmatrix}
\end{align*}

$AA^T - \lambda I$ must be singular if eigenvectors are non-zero. Hence,

\begin{align*}
	\begin{vmatrix}
	9 - \lambda             & -24          \\
	-24                     & 73 - \lambda
	\end{vmatrix}           & = 0          \\
	(9-\lambda)(73-\lambda) & = 576
\end{align*}

This gives $\lambda = 81, 1$ and $\hat{u} = \begin{pmatrix}
\frac{1}{\sqrt{10}} \\
\frac{-3}{\sqrt{10}}
\end{pmatrix},\begin{pmatrix}
\frac{3}{\sqrt{10}} \\
\frac{1}{\sqrt{10}}
\end{pmatrix}$ respectively.

At this step, decomposition is as follows:

\begin{align*}
	A = (\hat{u_1}, \hat{u_2}) \begin{pmatrix}
	\sqrt{\lambda_1} & 0                & 0 \\
	0                & \sqrt{\lambda_2} & 0
	\end{pmatrix} V^T \\
	A = \begin{pmatrix}
	\frac{1}{\sqrt{10}} & \frac{3}{\sqrt{10}} \\
	\frac{-3}{\sqrt{10}} & \frac{1}{\sqrt{10}}
	\end{pmatrix}      \begin{pmatrix}
	9                & 0                & 0 \\
	0                & 1                & 0
	\end{pmatrix} V^T
\end{align*}

To continue and calculate $V$, the relation $A\hat{v_i} = \sqrt{\lambda_i}\hat{u_i}$ is used. It is clear that $V$ is a $3 \times 3$ matrix.

\begin{align*}
	A\hat{v_1} &= \sqrt{\lambda_1}\hat{u_1} \\
	\begin{pmatrix}
	3  & 0 & 0 \\
	-8 & 0 & 3
	\end{pmatrix}\hat{v_1} &= \sqrt{81}\begin{pmatrix}
	\frac{1}{\sqrt{10}} \\
	\frac{-3}{\sqrt{10}}
	\end{pmatrix} \\
	\hat{v_1} &= \begin{pmatrix}
	\frac{3}{\sqrt{10}} \\
	0 \\
	\frac{-1}{\sqrt{10}} \\
	\end{pmatrix}
\end{align*}

Similarly,

\begin{align*}
	A\hat{v_2} &= \sqrt{\lambda_2}\hat{u_2} \\
	\begin{pmatrix}
	3  & 0 & 0 \\
	-8 & 0 & 3
	\end{pmatrix}\hat{v_2} &= \sqrt{1}\begin{pmatrix}
	\frac{3}{\sqrt{10}} \\
	\frac{1}{\sqrt{10}}
	\end{pmatrix} \\
	\hat{v_2} &= \begin{pmatrix}
	\frac{1}{\sqrt{10}} \\
	0 \\
	\frac{3}{\sqrt{10}} \\
	\end{pmatrix}
\end{align*}

Notice that $\hat{v_1}.\hat{v_2}$ is zero. The choices for $\hat{v_3}$ which must be perpendicular to both $\hat{v_1}$ and $\hat{v_2}$ are $(0, \pm 1, 0)^T$ and both are admissible in the decomposition as follows,

\begin{align*}
	A &= \begin{pmatrix}
	\frac{1}{\sqrt{10}} & \frac{3}{\sqrt{10}} \\
	\frac{-3}{\sqrt{10}} & \frac{1}{\sqrt{10}}
	\end{pmatrix}      \begin{pmatrix}
	9                    & 0                   & 0                    \\
	0                    & 1                   & 0
	\end{pmatrix} \begin{pmatrix}
	\hat{v_1}            & \hat{v_2}           & \hat{v_3}
	\end{pmatrix}^T \\
	A &= \begin{pmatrix}
	\frac{1}{\sqrt{10}} & \frac{3}{\sqrt{10}} \\
	\frac{-3}{\sqrt{10}} & \frac{1}{\sqrt{10}}
	\end{pmatrix}      \begin{pmatrix}
	9                    & 0                   & 0                    \\
	0                    & 1                   & 0
	\end{pmatrix} \begin{pmatrix}
	\frac{3}{\sqrt{10}}  & \frac{1}{\sqrt{10}} & 0                    \\
	0                    & 0                   & \pm 1                \\
	\frac{-1}{\sqrt{10}} & \frac{3}{\sqrt{10}} & 0                    \\
	\end{pmatrix}^T \\
	\begin{pmatrix}
	3                    & 0                   & 0                    \\
	-8                   & 0                   & 3
	\end{pmatrix} &= \begin{pmatrix}
	\frac{1}{\sqrt{10}} & \frac{3}{\sqrt{10}} \\
	\frac{-3}{\sqrt{10}} & \frac{1}{\sqrt{10}}
	\end{pmatrix}      \begin{pmatrix}
	9                    & 0                   & 0                    \\
	0                    & 1                   & 0
	\end{pmatrix} \begin{pmatrix}
	\frac{3}{\sqrt{10}}  & 0                   & \frac{-1}{\sqrt{10}} \\
	\frac{1}{\sqrt{10}}  & 0                   & \frac{3}{\sqrt{10}}  \\
	0                    & \pm 1               & 0                    \\
	\end{pmatrix}
\end{align*}

\section{Analysis}

SVD can be visualized as follows:

\begin{align*}
	A_{m \times n} &= \begin{pmatrix}
	| & | & .. & | \\
	\hat{u_1} & \hat{u_2} & .. & \hat{u_m} \\
	| & | & .. & | \\
	\end{pmatrix}_{m \times m} \begin{pmatrix}
	\sqrt{\lambda_1} & 0                & .. & 0                & 0 & .. & 0 \\
	0                & \sqrt{\lambda_2} & .. & 0                & 0 & .. & 0 \\
	0                & 0                & .. & 0                & 0 & .. & 0 \\
	0                & 0                & .. & \sqrt{\lambda_r} & 0 & .. & 0 \\
	0                & 0                & .. & 0                & 0 & .. & 0 \\
	.                & .                & .. & .                & . & .. & . \\
	.                & .                & .. & .                & . & .. & . \\
	0                & 0                & .. & 0                & 0 & .. & 0 \\
	\end{pmatrix}_{m \times n} \begin{pmatrix}
	| & | & .. & | \\
	\hat{v_1} & \hat{v_2} & .. & \hat{v_n} \\
	| & | & .. & | \\
	\end{pmatrix}_{n \times n}^T \\
	&u_i \perp u_j\ \forall\ i \neq j \\
	&v_i \perp v_j\ \forall\ i \neq j \\
	&\sqrt{\lambda_1} \geq \sqrt{\lambda_2} \geq .. \geq \sqrt{\lambda_r} > 0 \\
	&r = rank(A)
\end{align*}

The product of the last two matrices can be simplified as follows,

\begin{align*}
	A_{m \times n} &= \begin{pmatrix}
	|         & |         & .. & |         \\
	\hat{u_1} & \hat{u_2} & .. & \hat{u_m} \\
	|         & |         & .. & |         \\
	\end{pmatrix}_{m \times m} \begin{pmatrix}
	\sqrt{\lambda_1} \hat{v_1}^T \\
	\sqrt{\lambda_2} \hat{v_2}^T \\
	. \\
	. \\
	\sqrt{\lambda_r} \hat{v_r}^T \\
	0 \\
	. \\
	. \\
	0 \\
	\end{pmatrix}_{m \times n}
\end{align*}

leading to the representation of $A$ as a sum of $r$ rank-1 matrices as follows:

\begin{align*}
	A = \sum_{i=1}^r \sqrt{\lambda_i}\ \hat{u_i}\ \hat{v_i}^T
\end{align*}

Multiplying by $\hat{v_j}$ on both sides,

\begin{align*}
	A\ \hat{v_j} = \sum_{i=1}^r \sqrt{\lambda_i}\ \hat{u_i}\ \hat{v_i}^T\ \hat{v_j}
\end{align*}

Since all the $\hat{v_i}$s are mutually perpendicular to each other,

\begin{align*}
	A\ \hat{v_j} = \sqrt{\lambda_j} \hat{u_j}
\end{align*}

Note the similarity of this equation to the regular eigenvalue equation. The constraint of the same unit vector on both sides of the equation was let go in the return of universal applicability on any matrix whatsoever.

\section{Application: Matrix Compression}

Subjecting the matrix in example above to the rank-1 breakdown equation,

\begin{align*}
	A &= \sum_{i=1}^r \sqrt{\lambda_i}\ \hat{u_i}\ \hat{v_i}^T \\
	\begin{pmatrix}
	3    & 0 & 0    \\
	-8   & 0 & 3
	\end{pmatrix} &= \begin{pmatrix}
	2.7  & 0 & -0.9 \\
	-8.1 & 0 & 2.7  \\
	\end{pmatrix} + \begin{pmatrix}
	0.3  & 0 & 0.9  \\
	0.1  & 0 & 0.3
	\end{pmatrix}
\end{align*}

Note that the first matrix on the right hand side is a resonable approximation of the matrix on the left hand side and the other matrix just represents minor tweaks.

\begin{align*}
	\begin{pmatrix}
	3                   & 0 & 0                    \\
	-8                  & 0 & 3
	\end{pmatrix} &\approx \begin{pmatrix}
	2.7                 & 0 & -0.9                 \\
	-8.1                & 0 & 2.7                  \\
	\end{pmatrix} \\
	&\approx \sqrt{81} \begin{pmatrix}
	\frac{1}{\sqrt{10}} \\
	\frac{-3}{\sqrt{10}}
	\end{pmatrix} \begin{pmatrix}
	\frac{3}{\sqrt{10}} & 0 & \frac{-1}{\sqrt{10}} \\
	\end{pmatrix}
\end{align*}

The left hand side representation requires to store $mn$ entries while the right hand side only requires to store $k(m+n)$ entries. So if $k << m, n$, the right hand side is highly compressed. The right hand side is also a good approximation if k is of the order of the rank of the matrix. So for low-rank highly redundant matrices (example digital images), SVD can serve as a reasonable compression algorithm.

\section{Appendix}

\begin{align*}
	A    & = U\Sigma V^T                \\
	AA^T & = U\Sigma V^T V \Sigma^T U^T \\
\end{align*}

Since $V^TV = I$ and $\Sigma$ is diagonal.

\begin{align*}
	AA^T & = U \Sigma^2 U^T                                   \\
	AA^T & = \sum_{i = 1}^r \lambda_i\ \hat{u_i}\ \hat{u_i}^T
\end{align*}

Multiplying both sides by $\hat{u_j}$ and using the fact that all $u_i$s
are mutually perpendicular to each other.

\begin{align*}
	AA^T\ \hat{u_j} & = \sum_{i = 1}^r \lambda_i\ \hat{u_i}\ \hat{u_i}^T \ \hat{u_j} \\
	AA^T \hat{u_j}  & = \lambda_j\ \hat{u_j}
\end{align*}

It is clear that $\hat{u}$s are eigenvectors of $AA^T$. Similarly, it can be seen that $\hat{v}$s are eigenvectors of $A^TA$.
\end{document}

Singular Value Decomposition/Singular Value Decomposition.tex


\section{Problem Definition}
\label{section:problems}

The general problem of \mcproblem\ is to select the
appropriate monitoring operations to reduce
the expected uncertainty after they are performed.
Stated formally:

\begin{problem}[\mcproblem]
Given a transition matrix $P$ and an initial distribution of items $\initial$,
select a set of up to $k$ monitoring operations
to minimize the expected uncertainty $\objective$.
% $E[\uncertainty]$
\end{problem}

We study variants of the problem --
each defined for a specific type of monitoring operation.
 For simplicity, we refer to these problems
 with the same name as that of the operation type:
\variant{\parentstransitions},
\variant{\nodeitems},
\variant{\childrentransitions},
and \variant{\edgetransitions}.

Furthermore, as we saw in Section~\ref{sec:setting},
variants
\variant{\parentstransitions}\ and
\variant{\nodeitems} are equivalent: for the same set of nodes,
operations of the first type
reduce expected uncertainty as much as the second.
Therefore, in what follows,
we treat only the variant of \variant{\nodeitems}, as  our claims apply directly to
\variant{\parentstransitions}\ as well.

\todo[MM]{One issue we have not addressed is how we go from
$A_{_\shortnodeitems}(S)$ to $A_{_\shortparentstransitions}(S)$.
Does it affect our claims?}


% Copyright 2017-2018 Paul Mitchener, licensed under GNU FDL v1.3
% main author:
%   Paul Mitchener
% contributions by:
%   Markus Pflaum

\section{The category of measurable spaces and functions}\label{sec:category-measurable-spaces-functions}
\subsec{Definitions and first examples}
\begin{definition}
  Let $\Omega$ be a set. By an \emph{algebra on} $\Omega$ one understands a collection $\salgA$ of subsets of $\Omega$
  or in other words an element $\salgA\in \power{\Omega}$ such that
  \begin{axiomlist}[A]
  \item $\Omega \in \salgA$,
  \item for each $A\in \salgA$, the complement $\complement A = \Omega \setminus A $ belongs to $\salgA$,
  \item for each finite sequence $(A_k)_{k=1}^n$ of elements of $\salgA$ the union $A= \bigcup\limits_{k=1}^n A_k$
        belongs to $\salgA$.
  \end{axiomlist}
  If in addition
  \begin{axiomlist}[A]
  \setcounter{enumi}{3}
  \item for each sequence $(A_k)_{k\in \N}$ of elements of $\salgA$ the union $A= \bigcup\limits_{k\in\N} A_k$
        belongs to $\salgA$,
  \end{axiomlist}
  then the algebra $\salgA$ is called a \emph{$\sigma$-algebra}.
  A set $\Omega$ equipped with a $\sigma$-algebra $\mathscr A$ is called a
  \emph{measurable space}.  The elements of $\mathscr A$ are termed the
  \emph{measurable subsets} of $\Omega$.
\end{definition}

\begin{proposition}
  If $\salgA$ is an algebra on a set $\Omega$, the empty set and the intersection of finitely many
  elements of $\salgA$ lies in $\salgA$.
  If $\salgA$ is a $\sigma$-algebra on $\Omega$, then the intersection of countably many
  measurable sets is also measurable.
\end{proposition}
\begin{proof}
  These facts follow immediately from the axioms and the set-theoretic de Morgan's laws.
\end{proof}

\begin{examples}
\begin{environmentlist}
\item Let $\Omega$ be any set. Then the power set of $\Omega$ is a $\sigma$-algebra.
  The set $\{ \emptyset , \Omega \}$ is also a $\sigma$-algebra. These are the  largest and smallest
  $\sigma$-algebra on $\Omega$, respectively.
\item
  Let $\Omega$ be any set.  Let $\mathscr A$ be the set of all sets $A\subset \Omega$ such that $A$ or $\Omega \backslash A$ is
  a countable set.  Then $\mathscr A$ is a $\sigma$-algebra.
\end{environmentlist}
\end{examples}

\begin{remark}
  Obviously, the set of algebras on a set $\Omega$ and the set of $\sigma$-algebras on $\Omega$ are both ordered
  by set-theoretic inclusion. When talking about a ``smaller`` $\sigma$-algebra or a ``largest'' one we always
  implicitely mean in regard to set-theoretic inclusion as underlying order relation.
\end{remark}

The following two results are extremely useful when constructing examples.

\begin{proposition}
  Let $(\salgA_i)_{i\in I}$ a family of algebras on a set $\Omega$.
  Then the intersection
  $\salgA = \bigcap\limits_{i\in I} \salgA_i$ is an algebra on $\Omega$.
  If each of the $\salgA_i$ is a $\sigma$-algebra, then $\salgA$ is so, too.
\end{proposition}
\begin{proof}
  Assume first that each $\salgA_i$ is an algebra on $\Omega$.
  Obviously, $\Omega \in \salgA$ because  $\Omega \in \salgA_i$ for all $i\in I$.
  Similarly, if $A  \in \salgA$, then $A \in \salgA_i$, hence $\complement A \in \salgA_i$
  for all $i\in I$. Therefore $\complement A $ is in the intersection
  $\salgA = \bigcap\limits_{i\in I} \salgA_i$. Now assume that $(A_k)_{k=1}^n$ is a finite
  sequence of  sets belonging to $\salgA$. Then $A_k \in \salgA_i$  for $k=1,\ldots,n$ and all
  $i\in I$  which entails that $\bigcup\limits_{k=1}^n A_k$ is in each of the $\salgA_i$, hence in the
  intersection $\salgA$. The latter argument also works under the condition that each $\salgA_i$ is a $\sigma$-algebra
  to verify that for a sequence $(A_k)_{k\in\N}$  in $\salgA$ the union $\bigcup\limits_{k\in\N} A_k$
  is in $\salgA$. So the proposition is proved.
\end{proof}

\begin{corollary}
  Let $\mathscr F$ be a collection of subsets of a set $\Omega$.  Then there is a unique smallest $\sigma$-algebra,
  $\sigma (\mathscr F)$, on $\Omega$ containing $\mathcal F$.  It is called the \emph{$\sigma$-algebra} generated by
  $\mathscr F$.
\end{corollary}

\begin{proof}
  Let ${\mathcal M}$ be the family of all $\sigma$-algebras which contain $\mathscr F$.  The set of all subsets of $\Omega$ is
  certainly a $\sigma$-algebra, so ${\mathcal M} \neq \emptyset$.  Let $\sigma (\mathscr F)$ be the intersection of all
  $\sigma$-algebras in the family ${\mathcal M}$.  By the preceding proposition $\sigma (\mathscr F)$ is a $\sigma$-algebra.
  Since every element of ${\mathcal M}$ contains $\mathscr F$, the intersection
  $\sigma (\mathscr F) = \bigcap\limits_{\salgA\in \mathcal{M}}\salgA$ contains $\mathscr F$ as well. By construction,
  $\sigma (\mathscr F)$ is minimal with that property.
\end{proof}

\begin{example}
  Let $X$ be a topological space.  The $\sigma$-algebra, $\mathscr B (X)$, generated by all open subsets of $X$ is called the
  {\em Borel $\sigma$-algebra} on $X$. Its elements are the {\em Borel measurable sets} or simply the {\em Borel sets} of $X$.
  Obviously all open and all closed sets of $X$ are Borel measurable, as are all countable unions of closed sets and countable
  intersections of open sets.
\end{example}

\begin{example}
\begin{environmentlist}
\item All intervals including the half-open intervals $[a,b)$ and $(a,b]$ with $a<b$ are Borel subsets of $\R$.
\item If $X$ is a topological space with the discrete topology, then every subset of $X$ is Borel measurable.
\item If $X$ is a topological space carrying the topology $\{ X ,\emptyset \}$, then the $\sigma$-algebra
      of Borel sets is the set $\mathscr B = \{ X , \emptyset \}$.
\end{environmentlist}
\end{example}

\begin{definition}
Let $\Omega$ be a measurable space, and $Y$ a topological space.  A map $f\colon \Omega \rightarrow Y$ is termed {\em measurable} if the set $f^{-1}[U]$ is measurable for every open subset $U\subseteq Y$.
\end{definition}

\begin{proposition} \label{ime}
Let $f\colon \Omega \rightarrow Y$ be a measurable function.  Then the inverse image $f^{-1}[B]$ is measurable whenever $B\subseteq Y$ is a Borel set.
\end{proposition}

\begin{proof}
Let $\mathcal M$ be the collection of all subsets $E\subseteq Y$  such that the inverse image $f^{-1}[E] \subseteq \Omega$ is measurable.  It is easy to check the axioms required to show that $\mathcal M$ is a $\sigma$-algebra.

Since the function $f$ is measurable, the $\sigma$-algebra $\mathcal M$ contains all open sets of $Y$, and therefore all Borel sets.  Thus, by definition of the $\sigma$-algebra $\mathcal M$, the set $f^{-1}[B]$ is measurable whenever $B$ is a Borel set.
\end{proof}

\begin{definition}
Let $f\colon X\rightarrow Y$ be a mapping between topological spaces.  If $f$ is measurable with respect to the $\sigma$-algebra of all Borel sets in $X$, then we call $f$ a Borel function.
\end{definition}

Thus a function is a Borel function if the inverse image of any open set is a Borel set.  In particular, any continuous function is a Borel function.

\begin{proposition} \label{cm}
Let $f\colon \Omega \rightarrow X$ be a measurable function, and let $g\colon X\rightarrow Y$ be a Borel function.  Then the composite $g\circ f \colon \Omega \rightarrow Y$ is measurable.
\end{proposition}

\begin{proof}
Let $U\subseteq Y$ be an open set.  Then the inverse image $g^{-1}[U]$ is a Borel set, so by proposition \ref{ime} the inverse image $(g\circ f)^{-1}[U]$ is measurable.
\end{proof}

As a corollary, the composite of a measurable and a continuous function is measurable.

\begin{example} \label{char}
  Let $X$ be a measure space, and let $E\subseteq X$ be a measurable set.  Then the function
  $\chi_E \colon X\rightarrow {\mathbb C}$ given by the formula
  $$\chi_E (x) = \left\{ \begin{array}{ll} 1 & x\in E \\
  0 & x\not\in E \\
  \end{array} \right.$$
is measurable.
\end{example}

The function $\chi_E$ is called the {\em characteristic function} of $E$.

\subsec{Algebras of real and complex valued Borel measurable functions}

\begin{proposition} \label{2dm}
Let $u,v\colon X\rightarrow {\mathbb R}$ be measurable functions, and let $\Phi \colon {\mathbb R}^2 \rightarrow Y$ be continuous.  Define a function $h\colon X\rightarrow Y$ by the formula
$$h(x) = \Phi (u(x),v(x))$$
Then the function $h$ is measurable.
\end{proposition}

\begin{proof}
Define $f\colon X\rightarrow {\mathbb R}^2$ by the formula $f(x) = (u(x),v(x))$.  In view of proposition \ref{cm} it suffices to prove that the function $f$ is measurable.  Observe that:
$$f^{-1}((a,b)\times (c,d)) = u^{-1}(a,b)\cap v^{-1}(c,d)$$
so the inverse image $f^{-1}((a,b)\times (c,d))$ is measurable since $u$ and $v$ are measurable functions.

But every open set $U\subseteq {\mathbb R}^2$ is a countable union of rectangles of the form $(a,b)\times (c,d)$.  The $\sigma$-algebra axioms thus ensure that the inverse image $f^{-1}[U]$ is measurable whenever $U\subseteq {\mathbb R}^2$ is an open set.
\end{proof}

The above proposition and proof still function if the function $\Phi$ is a Borel function rather than a continuous function.

\begin{corollary} \label{comM}
Let $f\colon X\rightarrow {\mathbb C}$ be a function on a measurable space $X$.  Then the function $f$ is measurable if and only if the functions $\Re (f)$ and $\Im (f)$ are measurable.
\end{corollary}

\begin{proof}
Let $u,v\colon X\rightarrow {\mathbb R}$ be measurable functions.  Define $\Phi \colon {\mathbb R}^2\rightarrow {\mathbb C}$ by the formula $\Phi (x,y) = x+iy$.  Then the function $u+iv$ is measurable by proposition \ref{2dm}.

The converse follows immediately from proposition \ref{cm} since the functions $\Re$ and $\Im$ are continuous.
\end{proof}

\begin{corollary} \label{sum}
Let $f,g\colon X\rightarrow {\mathbb C}$ be measurable functions.  Then the functions $f+g$ and $fg$ are measurable.
\end{corollary}

\begin{proof}
In view of corollary \ref{comM} it suffices to prove this result for real-valued measurable functions.  If we define continuous functions $\Phi_1,\Phi_2 \colon {\mathbb R}^2\rightarrow {\mathbb R}$ by the formulae $\Phi_1 (s,t)=s+t$ and $\Phi_2 (s,t)=st$, then the result follows immediately from proposition \ref{2dm}
\end{proof}

\begin{proposition}
Let $f\colon X\rightarrow {\mathbb C}$ be a measurable function.  Then the function $|f|$ is measurable, and there is a measurable function $\alpha \colon X\rightarrow {\mathbb C}$ such that $|\alpha (x)| =1$ for all $x\in X$, and $f=\alpha |f|$.
\end{proposition}

\begin{proof}
Let $E = \{ x\in X \ |\ f(x)=0 \}$.  Then the set $E$ is the inverse image of a closed subset, and so measurable.  We can define a continuous function $\varphi \colon {\mathbb C}\backslash \{ 0\} \rightarrow {\mathbb C}$ by the formula $\varphi (z) = z/|z|$.  It follows from example \ref{char}, corollary \ref{sum}, and proposition \ref{cm} that the function $\alpha \colon X\rightarrow {\mathbb C}$ defined by the formula
$$\alpha (x) = \varphi (f(x) + \chi_E (x))$$
is measurable.  The formulae $|\alpha (x)| =1$ and $f=\alpha |f|$ are easy to check.
\end{proof}

\subsec{Measurable functions to the extended real line}

\begin{definition}
Let $(a_n)$ be a sequence of real numbers.  Then we define
$${\lim \sup}_{n\rightarrow \infty} a_n = {\lim \sup}_{n\rightarrow \infty} \{ a_n , a_{n+1} , a_{n+2} , \ldots \}$$
and
$${\lim \inf}_{n\rightarrow \infty} a_n = {\lim \inf}_{n\rightarrow \infty} \{ a_n , a_{n+1} , a_{n+2} , \ldots \}$$
\end{definition}

We can pass from results about $\lim \sup$ to results about $\lim \inf$, or conversely, by the observation
$${\lim \sup}_{n\rightarrow \infty} a_n = - {\lim \inf}_{n\rightarrow \infty} (-a_n)$$

It will occasionally be convenient to us to allow $\infty$ and $-\infty$ as values of limits and functions.  This is a safe enough option provided we do not attempt to do arithmetic with these symbols; for example, expressions such as `$\infty - \infty$' are completely meaningless.

However, we can form `intervals'
$$[a,\infty ] = [a ,\infty )\cup \{ \infty \}  \qquad [\infty ,b] = (\infty ,b]\cup \{ \infty \}$$
and so on.  These intervals are topological spaces.  We can also allow ourselves the inequality
$$-\infty < a < \infty$$
for all $a\in {\mathbb R}$.  The standard result about $\lim \sup$ and $\lim \inf$ can now be expressed quite simply; although a number of special cases need to be examined in the proof.

\begin{theorem}
Let $(a_n)$ be a real-valued sequence.  Then the limits
$${\lim \inf}_{n\rightarrow \infty} a_n \in [-\infty , \infty ) \qquad {\lim \sup}_{n\rightarrow \infty} a_n \in (-\infty , \infty ]$$
exist and satisfy the inequality
$${\lim \inf}_{n\rightarrow \infty} a_n \leq {\lim \sup}_{n\rightarrow \infty} a_n$$

Further, the equality
$${\lim \inf}_{n\rightarrow \infty} a_n = a = {\lim \sup}_{n\rightarrow \infty} a_n$$
holds precisely when the sequence $(a_n)$ converges to the real number $a$.
\textbf{proof to be filled in!}
\end{theorem}

Note that the number $a$ in the above result must be finite.

\begin{proposition}
Let $\Omega$ be a measurable space, and let $f\colon \Omega \rightarrow [\infty , \infty]$ be any map.  Suppose that the inverse image $f^{-1}((\alpha , \infty ])$ is measurable for every point $\alpha \in {\mathbb R}$.  Then the function $f$ is measurable.
\end{proposition}

\begin{proof}
Let
$${\mathcal M} = \{ E\subseteq [-\infty ,\infty]\ |\ f^{-1}[E] \textrm{ is measurable } \}$$

By proposition \ref{ime} the set $\mathcal M$ is a $\sigma$-algebra.  Choose points $\alpha \in {\mathbb R}$ and $\alpha_n < \alpha$ such that $\lim_{n\rightarrow \infty} \alpha_n = \alpha$.  Since the set $(\alpha_n , \infty ]$ is measurable by hypothesis, and
$$[-\infty , \alpha ) = \bigcup_{n=1}^\infty [-\infty , \alpha_n ] = \bigcup_{n=1}^\infty [-\infty , \infty ]\backslash (\alpha_n , \infty]$$
it follows that $[-\infty , \alpha )\in \Omega$.  Hence
$$(\alpha , \beta ) = [-\infty , \beta ) \cap (\alpha , \infty ] \in \Omega$$
for every point $\alpha , \beta \in {\mathbb R}$.  Since every open set in $[-\infty , \infty ]$ is a countable union of such open intervals, the collection $\mathcal M$ contains every open set.  Thus the map $f$ is measurable.
\end{proof}

\begin{corollary}
Let $f_n\colon X\rightarrow [-\infty , \infty]$ be measurable functions for $n\in {\mathbb N}$.  Then the functions
$$\sup \{ f_n \} \quad {\lim \sup}_{n\rightarrow \infty} f_n \quad \inf \{ f_n \} \quad {\lim \inf}_{n\rightarrow \infty} f_n$$
are measurable.
\end{corollary}

\begin{proof}
Let $a\in {\mathbb R}$.  Observe that the set
$$(\sup \{ f_n \})^{-1} (a,\infty ] = \bigcup_{n=1}^\infty f_n^{-1}(a,\infty ]$$
is measurable.  Hence by the above proposition, the function $\sup \{ f_n \}$ is measurable.  The formula $\inf \{ f_n \} = - \sup \{ -f_n \}$ tells us that the function $\inf \{ f_n \}$ is also measurable.

Now, for each point $x\in \Omega$, the sequence of numbers
$$g_n (x) = \sup \{ f_n (x) , f_{n+1} (x) , f_{n+2}(x) , \ldots \}$$
is monotonic increasing.  It follows that
$${\lim \sup }_{n\rightarrow \infty} f_n (x) = \inf \{ g_n (x) \}$$

We know that each function $f_n$ is measurable.  The above argument tells us that each function $g_n$ is measurable, and that the function ${\lim \sup}_{n\rightarrow \infty}f_n$ is measurable.  A similar argument tells us that the function ${\lim \inf}_{n\rightarrow \infty}g_n$ is measurable.
\end{proof}

\begin{corollary}
If $f,g\colon X\rightarrow [-\infty, \infty ]$ are measurable functions, then so are the functions $\max \{ f,g \}$ and $\min \{ f,g \}$.
\textbf{proof to be filled in!}
\end{corollary}

\begin{corollary}
The limit of a pointwise-convergent sequence of meaurable functions is measurable.
\textbf{proof to be filled in!}
\end{corollary}


\section[Basics in probability theory]{Lecture 3: Basics in probability theory}

\subsection{Discrete random variables}

Here we will shortly define random variables which is the basis of understanding probability. We will first focus on discrete random variables since we have an intuitive understanding of probability from everyday life situations, e.g. when we toss a coin or role a die.
A random variable is a variable which is subject to random effects, i.e. other than having a fixed value it can take on different values each associated with a probability. In order to specify a random variable we define a probability space $(\Omega, E, P)$. $\Omega$ is called the sampling space i.e. the set of all possible outcomes (states) of a random process. In the case of a single coin toss we can define $\Omega = \lbrace H , T\rbrace$ as the set which respresents Heads and Tails of the toss. $E$ is the set of all possible events where each event is a subsest of $\Omega$, we write $ E_i \subseteq \Omega $. Based on the possible events we define a function $P$ which maps possible events to a probability measure $P: E \rightarrow [0,1]$. This probability measure represents the likelihood of an event happening in an experiment. We say that for the conjunction of all disjoint subsets of E our probability is normalized, i.e. it sums to one: $(\forall E_i \cap E_j = \emptyset): P(\cup_i E_i) = 1$. This is equivalent to saying that $P(\Omega) = 1$. For some examples on how to utilize the probability space see the example box below. \\

\begin{bbbox}{Discrete random variables}
	\begin{small}
	\begin{tabularx}{\textwidth}{|l|l|l|X|X|} %{| l | l | l | p{5cm} | p{5cm} |}
	\hline
	Example & Distribution & $\Omega$ & $E$ & $P(X=x)$ \\ \hline
	Coin Flip & Bernoulli & $\lbrace H, T \rbrace$ & $\lbrace \rbrace$ , $\lbrace H \rbrace$ , $\lbrace T \rbrace$ , $\lbrace H,T \rbrace$ & $P(X = H) = P(X = T) = 0.5$ \\ \hline
	Die Roll & Categorical & $\lbrace 1,2,3,4,5,6 \rbrace $ & $\lbrace \mbox{"even number"} \rbrace$ , $\lbrace 5,6 \rbrace $ & $P(X = x) = \frac{1}{6}$ ; $P(X = \{5,6\}) = P(X = 5) + P(X = 6))$ \\ \hline
	Spike count & Poisson & $ \lbrace 1,2,... \rbrace $ & $\lbrace 0 \rbrace$ , $\lbrace \mbox{"at least one"} \rbrace $ & $P(X = 0) = \mbox{e}^{-\lambda}$; $P(X > 0) = 1-\mbox{e}^{-\lambda}$ \\ \hline

	\end{tabularx}
	\end{small}
\end{bbbox}

In the case of a discrete random variable $P$ is called a probability mass function (pmf). Since discrete random variables have a countable or finite number of states, or $X: \Omega \rightarrow \mathbb{N}$, the pmf directly assigns a probability to each possible event. We will later see that this is not the case for the $P$ of continuous random variables. In literature there is a redundancy in the notation of the pmf and it is useful to know that some of the expressions used are equivalent:

\begin{itemize}
\item %Rules:
\begin{align}
P(X=x)&=P_X(x)=P(x)\\
P(x)&\geq 0 \mbox{ for each } x\\
\sum_{x \in \Omega} P(x)&=1\\
P(A) = P(x \in A) &=\sum_{x \in A} P(X=x)\\
\end{align}
\item We write: $X|q \sim \mbox{Binomial}(q)$
\end{itemize}

\subsection{Moments and Expectations}
\begin{itemize}
\item Expectation (or mean): $E(X)= \sum_{x} P(X=x) x$
\end{itemize}

\begin{bbbox}{Expectation of a discrete random variable}
	The expectation of a discrete random variable is the weighted sum over all possible states of the random variable, where the weights are the probabilities of the respective state.\\

	Examples: fair coin; fair die; \\
	\begin{flalign*}
		&\text{Fair coin: } & E(coin) & = P(coin=1)\times1 + P(coin=0)\times0 = \frac{1}{2} \\
		&\text{Fair die: } & E(die) & = 1\times\frac{1}{6} + 2\times\frac{1}{6} + \dots + 6\times\frac{1}{6} = 3.5 \\
 		&\text{Poisson with rate } \lambda \text{: } & E(X) & = \sum_{x = 1}^{\infty} (x) \frac{1}{\underbrace{x!}_{(1 \times 2 \times \dots \times x)}} \mbox{e}^{-\lambda} \lambda^x \\
		& & & = \sum_{x = 1}^{\infty} \frac{1}{(x-1)!} \mbox{e}^{-\lambda} \lambda^{x-1} \lambda \\
		& & & = \lambda \sum_{x = 1}^{\infty} \frac{1}{(x-1)!} \mbox{e}^{-\lambda} \lambda^{x-1} \\
		& & & = \lambda \underbrace{\sum_{y = 0}^{\infty} \frac{1}{(y)!} \mbox{e}^{-\lambda} \lambda^{y}}_{ = 1} \;\; \vert (y = x+1) \\
		& & & = \lambda
	\end{flalign*}

\end{bbbox}

\begin{itemize}
\item Expectation of a function:  $E(f(X))= \sum_{x} P(X=x) f(x)$
\item Moments= expectation of power of $X$: $M_k= E(X^k)$
\end{itemize}

\begin{bbbox}{Example: 2nd order moment}
	We want to calculate the expectation of $Die^2$ for a fair die: \\
	\begin{align*}
		E(Die^2) &= \sum_{x = 1}^6 P(Die = x) x^2 \\
				 &= \frac{1}{6} (1 + 2 + 9 + 16 + 25 + 36) \\
				 &= \frac{1}{6} 89 \\
				 &\approx 14.66
	\end{align*}
\end{bbbox}

\begin{itemize}
\item Variance: Average (squared) fluctuation from the mean
\begin{align}
 \mbox{Var}(X)&= E((X-E(X))^2)\\
 &= E(X^2)- E(X)^2\\
 &= M_2-M_1^2
\end{align}
\end{itemize}

\begin{bbbox}{Variance}
The alternative definition of variance based on moments directly emerges from the linearity of the expectation:
	\begin{align*}
		Var(X) = E((X - E(X))^2) &= E(X^2 - 2 \times X \times E(X) + E(X)^2) \\
						&= E(X^2) - 2 \times E(X \times E(X)) + E(X)^2 \\
						&= E(X^2) - 2 \times E(X) \times E(X) + E(X)^2 \\
						&= E(X^2) - E(X)^2
	\end{align*}
\end{bbbox}

\begin{bbbox}{Why do we use the squared fluctuations from the mean for the mesaure of variance?}

	\begin{itemize}
		\item Average deviations from the mean: \\
			  $Var(X) = E(X-E(X)) = E(X) - E(X) = 0$ \\
			  As we see this measure is always zero and therefore useless for our purpose.
		\item Average abolute deviations from the mean:
			  \\$Var(X) = E(|X-E(X)|)$ \\
			  However this measure can sometimes be technically difficult and is less used in practice.
	\end{itemize}

\end{bbbox}

\begin{itemize}
\item Standard devation: Square root of variance.
\item Illustration and examples: [on board]
Aside: Difference between expectation/variance of random variable and empirical average/variance.

\end{itemize}

\subsection{Probability rules}
The course will be focused on Bayesian statistics and in that context we will be constantly using Bayes' rule. In this section we will show that Bayes' rule strictly follows from the definition of conditional probabilities. We have seen that we can measure the probability of an event $A$ with $P(A)$. Further we can measure the probability of two events $A$ and $B$ with P(A,B). We call this the joint probability of the events $A$ and $B$

\begin{align}
	P(B|A) = \frac{P(A|B) P(B)}{P(A)}
\end{align}

where $P(B|A)$ is the posterior probability of an event $B$ given we have knowledge about an event $A$. It is straightforward to show from the definition of conditional probabilities that Bayes' rule holds. We define the conditional probability of an event $B$ given knowledge about and event $A$ as

\begin{align}
	P(B|A) = \frac{P(A,B)}{P(A)}
\end{align}

where $P(A,B)$ is the  probability of observing events $A$ and $B$, we call this the joint probability of $A$ and $B$. And $P(A)$ is the probability of observing and event $A$. We can

\begin{exbox}{It is cold today, will we have rain?}
We will consider the example of interactions between rain and temperature in T\"ubingen. Lets say we have made $N = 30$ independent measurements of the weather conditions in T\"ubingen. We arbitrarily chose to divide our observations based on the temperature and whether it was raining or not. The table below reflects the number of times we made observations under a given condition, e.g. the number of observations when the temperature was below  $10\,^{\circ}\mathrm{C}$ and it was not raining was $N_{(T=0,R=0)} = 10$. \\
	\begin{tabular}{ll|c|c|r}
			& & Rain & No Rain \\
			& & $R = 1$ & $R = 0$ & $\sum_R$ \\ \hline
		Temp $> 20\,^{\circ}\mathrm{C}$: & $T = 2$ & $1$ & $3$ & 4\\ \hline
		$10\,^{\circ}\mathrm{C} <$ Temp $< 20\,^{\circ}\mathrm{C}$: & $T = 1$ & $4$ & $8$ & 12\\ \hline
		Temp $< 10\,^{\circ}\mathrm{C}$: & $T = 0$ & $4$ & $10$ & 14\\ \hline
			& $\sum_T$ & 9 & 21 \\
	\end{tabular}
We can easily see that the sum over all observations is $N = 30$ again. If we now want to calculate the probability of the temperature being below $10\,^{\circ}\mathrm{C}$ and there being no rain, we can simply do $P(T=0,R=0) = \frac{N_{(T=0,R=0)}}{N} = \frac{1}{3}$. We call this the \textbf{probability} of events $T=0$ and $R=0$. This can be done for all possible events.\\
Lets say we know the temerature is between $10$ and $20\,^{\circ}\mathrm{C}$ and we want to know how likely it is to rain. We can utilize the \textbf{conditional probability} $P(R=1|T=1)$
\end{exbox}

\begin{defbox}{Probability rules}
	\begin{itemize}
	\item {Joint distribution:} $P(X=x, Y=y)$, a list of all probabilities of all possible pairs of observations
	\item {Marginal distribution:} $P(X=x)=\sum_y P(X=x, Y=y) $
	\item {Conditional distribution:} $P(X=x|Y=y) = \frac{P(X=x, Y=y)}{P(y=y)}$
	\item $X |Y$ has distribution $P(X|Y)$, where $P(X|Y)$ specifies a 'lookup-table' of all possible $P(X=x| Y=y)$
	\end{itemize}
\end{defbox}

\begin{itemize}
\item {Conditional probability:} 'Recalculated probability of event A after someone tells you that event A happened.'
\begin{align}
P(A|B)&= \frac{P(A \cap B)}{P(B)}\\
P(A \cap B)&= P(A|B) P(B)
\end{align}
\item Examples: Rolls of a die
 [on board]
\item Bayes Rule:
\begin{align}
P(B|A) = \frac{P(A|B) P(B)}{P(A)}
\end{align}
\end{itemize}

\begin{itemize}
\item Example [on board]
\item {Joint distribution:} $P(X=x, Y=y)$, a list of all probabilities of all possible pairs of observations
\item {Marginal distribution:} $P(X=x)=\sum_y P(X=x, Y=y) $
\item {Conditional distribution:} $P(X=x|Y=y) = \frac{P(X=x, Y=y)}{P(y=y)}$
\item $X |Y$ has distribution $P(X|Y)$, where $PX(|Y)$ specifies a 'lookup-table' of all possible $P(X=x| Y=y)$
\end{itemize}
Conditioning and marginalization come up in Bayesian inference ALL the time: 'Condition on what you observe. Marginalize out the uncertainty'.

\subsection{Independence of random variables}

\begin{itemize}
 \item Intuitively, two {events are independent} if knowing that the first took places tells us nothing about the probability of the second:  $P(A|B)= P(A)$
 \item  $P(A) P(B)= P(A \cap B)$
 \item  Two {random variables} are independent if the joint p.m.f. is the product of the marginals: $P(X=x,Y=y)=P(X=x) P(Y=y)$.
 \item If $X$ and $Y$ are independent, we write $X \perp Y$. Knowing the value of $X$ does not tell us anything about $Y$.
 \item  If $X$ and $Y$ are independent, $\mbox{Cov}(X,Y)=0$.
 \end{itemize}
 Aside: Mutual information is a measure of how 'non-independent' two random variables are.

\subsection{Multivariate Distributions}
\subsubsection{Expectation and covariance}
\begin{itemize}
\item Conditional distributions are just distributions which have a (conditional) mean or variance.
\item Note: $E(X|Y)= f(Y)$. 'If I tell you what $Y$ is, what is the average value of $X$?.
%\item $E(X,Y)= \sum_{x,y} P(X=x, Y=y) (x,y)= (E(X), E(Y))$
\item Covariance is the expected value of the product of fluctuations:
\begin{align}
 \mbox{Cov}(X,Y)&= E\left((X-E(X) )(Y-E(Y) )\right)\\
 &= E(XY)- E(X)E(Y)\\
 \mbox{Var(X)}&= \mbox{Cov}(X,X)
\end{align}
 \end{itemize}
Aside: One common way to construct bivariate random variables is to have a random variable whos parameter is another random variable.

\begin{itemize}
\item $\mathbf{X},\xx$ are vector valued.
\item Mean: $E(\mathbf{X})= \sum_{\xx} \xx P(\xx)$
\item Covariance matrix:
\begin{align}
\mbox{Cov}(X_i, X_j)&= E(X_iX_j)-E(X_i) E(X_j)\\
\mbox{Cov}(\mathbf{X})&= E(\mathbf{X}\mathbf{X}^\top)-E(\mathbf{X})
E(\mathbf{X})^\top
 \end{align}
\item Conditional and marginal distributions: Can define and calculate any (multi or single-dimensional) marginals or conditional distributions we need:  $P(X_1)$, $P(X_1, X_2)$, $P(X_1, X_2, X_3 |X_4)$, etc..
\end{itemize}

\begin{bbbox}{Covariance matrix}
	\begin{align*}
		& & Cov(\mathbf{X}) &=
		\begin{pmatrix}
   		Cov(X_1,X_1) & Cov(X_2,X_1) & \cdots & Cov(X_n,X_1) \\
   		Cov(X_1,X_2) & Cov(X_2,X_2) & \cdots & Cov(X_n,X_2) \\
   		\vdots  & \vdots  & \ddots & \vdots  \\
   		Cov(X_1,X_n) & Cov(X_2,X_n) & \cdots & Cov(X_n,X_n) \\
  		\end{pmatrix} \\
		& \text{Note that: } & Cov(X_i,X_j) &= Cov(X_j,X_i) \\
		& \text{Therefore: } & Cov(\mathbf{X}) &=  Cov(\mathbf{X})^\top
	\end{align*}

\end{bbbox}

\subsection{Continuous random variables}
\begin{itemize}
\item A random variable $X$ is {continuous} if its sample space $X$ is uncountable.
\item In this case, $P(X=x)=0$ for each $x$.
\item If $p_X(x)$ is a {probability density function} for $X$, then
\begin{align}
P(a < X <b) &=\int_a^b p(x) dx\\
P(a<X <a+dx) \approx p(a) \cdot dx
\end{align}
\item The {cumulative distribution function} is $F_X(x)=P(X<x)$. We have that $p_X(x)=F'(x)$, and $F(x)=\int_{-\infty}^x p(s) ds$.
\item
More generally: If $A$ is an event, then
\begin{align}
P(A)&=P(X \in A) =\int_{x \in A}  p(x) dx\\
P(\Omega)&=P(X \in \Omega) =\int_{x \in \Omega} p(x) dx=1
\end{align}
\item Example: Uniform, Exponential, Beta  [on board]
\end{itemize}

\begin{bbbox}{PDFs of the uniform and the exponential distribution}
	Continuous uniform distribution: \\
	\begin{flalign*}
		X &\sim U(0,1) \\
		p(x) &=
			\begin{cases}
		    	1 & \text{if } x \epsilon \left[0,1\right]; \\
		    	0 & \text{else};
			\end{cases} \\
		P(a \leq X \leq b) &= \int_a^b p(x) dx \\
						   &= \left[ x \right]_a^b = b-a\\
		P(0 \leq X \leq 1) &= 1;
	\end{flalign*} \\
	Exponential Distribution with rate $\lambda$:
	\begin{flalign*}
		X &\sim Exp(\lambda) \\
		p(x) &= \lambda \mbox{e}^{-\lambda x} \text{where } x \epsilon \left[0,\infty\right);\\
		P(a \leq X \leq b) &= \int_a^b \lambda \mbox{e}^{-\lambda x} dx \\
						   &= \left[ - \mbox{e}^{-\lambda x} \right]_a^b\\
						   &= \mbox{e}^{-\lambda a} - \mbox{e}^{-\lambda b} \\
		P(0 \leq X \leq \infty) &= \mbox{e}^{-\lambda 0} - \mbox{e}^{-\lambda \infty} = 1
	\end{flalign*} \\
\end{bbbox}

\subsubsection{Expectations and variance}
\begin{itemize}
\item Mean: $E(X)= \int_x x  \cdot p(x) dx$\\
\item Variance: $\mbox{Var}(X)= E(X^2)- E(X)^2$
\item Example: Uniform, Exponential [on board]
\item If $X$ has pdf $p(x)$, then $X | (X \in A)$ has pdf
\begin{align}
p_{X|A}(x)=\frac{p(x)}{P(A)}=\frac{p(x)}{\int_{x \in A} p(x) dx}
\end{align}
\item Only makes sense if $P(A)>0$~!
\item Example: Uniform, Exponential [on board]
\end{itemize}

\begin{bbbox}{Moments and Variance of the continuous uniform distribution}
	Continuous uniform distribution: \\
	\begin{align*}
		X &\sim U(0,1) \\
		E(X) &= \int_0^1 x p(x) dx = \int_0^1 x dx = &= \left[ \frac{1}{2}x^2 \right]_0^1 = \frac{1}{2}\\
		E(X^2) &= \int_0^1 x^2 p(x) dx = \int_0^1 x^2 dx = &= \left[ \frac{1}{3}x^3 \right]_0^1 = \frac{1}{3}\\
		Var(X) &= E(X^2) - E(X)^2 = \frac{1}{3} - \frac{1}{4} = \frac{1}{12}\\
	\end{align*}
%	Exponential distribution: \\
%	\begin{align*}
%		E(X) &= \int_0^\infty x \lambda \mbox{e}^{-\lambda x} dx \\
%		     &= \left[ -\frac{1}{2}x^2 \mbox{e}^{-\lambda x} \right]_0^\infty \\
%	\end{align*}
\end{bbbox}

\subsubsection{Marginalizationa, Conditioning and Independence}
\begin{itemize}
\item $p_{X,Y}(x,y)$, joint probablity density function of $X$ and $Y$
\item $\int_x \int_y p(x,y)dx dy=1$
\item {Marginal distribution:} $p(x)= \int_{-\infty}^\infty p(x,y) dy$
\item {Conditional distribution:} $p(x|y)= \frac{p(x,y)}{p(y)}$
\item Note: $P(Y=y)=0$! Formally, conditional probability in the continuous case can be derived using infinitesimal events.
\item {Independence:} $X$ and $Y$ are independent if $p_{X,Y}(x,y)=p_X(x)p_Y(y)$
\end{itemize}