\documentclass{article}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}

\begin{document}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}

\tableofcontents
\newpage

\section{Probability Space}
There are often various approaches to probability each with its own advantages
and disadvantages.

Experiment is a procedure that can be infinitely repeated and has a
well-defined set of possible outcomes, known as the sample space.

The observation/result of the experiment are termed as outcomes.

\subsection{Classical Approach}
        Probability of an event $E$ is defined to be: $$P(E)=\frac{\text{Number of outcomes in }E}{\text{Total number of outcomes}}$$
         Some examples are tossing a coin or rolling a die.
         Disadvantages:
         \begin{itemize}
             \item Unable to model biases. It says nothing about cases where no
             physical symmetry exists.
             \item Doesn't deal with cases where total outcomes are infinite.
         \end{itemize}

         \subsection{Frequentist Approach}
         Also known as the relative frequency approach or frequentism. It defines
         an event's probability as the limit of its relative frequency in many
         trials.

         Probability is defined to be:
         $$ P(E)=\lim_{n \to \infty} \frac{n_E}{n}$$
         where an experiment is conducted $n$ times and event $E$ occurs $n_E$
         times.
         Disadvantages:
         \begin{itemize}
             \item It isn't efficient to conduct an experiment multiple times
             just to find the probability of an event occuring.
             \item It is unable to deal with subjective belief. Eg: Suppose a
             cricket expert says there is a $50\%$ of RCB winning the IPL this
             year. It doesn't mean that the RCB has won half the titles in the
             past.
         \end{itemize}
%
         \subsection{Axiomatic Approach}

         \subsubsection{Probability Space}

The triple ($S$, $F$, $P$) is referred to as a probability space where:
\begin{itemize}
    \item $S$ : Sample space, set of all possible outcomes of the experiment.
    \item $F$ : Event Space, subset of the sample space
    \item $P$ : Probability Measure
\end{itemize}
\subsubsection{Sample Space}

$S$ can either be finite or countably infinite or uncountably infinite.
%28/05

Examples for S:
\begin{itemize}
        \item Finite Sample Space: Single coin toss $ S=\{H,T\} $ and two coin tosses $ S=\{HH,HT,TT,TH\} $.
        \item Countably Infinite Sample Space: Keep tossing a coin till you get a head $ S=\{H,TH,TTH...\}$.
        \item Uncountably Infinite Sample Space: We have a circular dart board and we are measuring the angle at which a dart hits the board. $S=[0,2\pi]$
\end{itemize}

\subsection{Event Space}
Collection of events is called an event space, there are some properties to be satisified such as:
it has to be a ``Sigma Field''.

\subsubsection{Sigma Field}
A sigma field (or sigma algebra) F is a collection of subsets of S which satisfies the following properties:
\begin{itemize}
    \item $S \in F$
    \item If $E \in F$, then $E^C \in F$
    \item If $E_1,E_2,E_3 \cdots \in F $, then $\bigcup_{i=1}^{\infty} E_i \in F$
\end{itemize}

\subsubsection{Examples for Event Space}
\begin{itemize}
    \item Smallest possible event space:
    $$ F =\{ \phi,S\} $$
    \item Next non-trivial event space:
    $$ F=\{ \phi,E ,E^c ,S\}$$
    \item If $E_1 \in F$ and $E_2 \in F$, then $E_1 \cap E_2 \in F$ (Proof in 1.4.3)
    \item For S=$\{1,2,3,4,5,6\}$ , $E_1 =\{1,2\}$ and $E_2=\{3,4\}$. The smallest event space containing $E_1$ and $E_2$ is:
    $$ F=\{\phi , S, E_1 , E_{1}^{C},E_2 , E_{2}^{C},E_1 \cup E_2 , (E_{1} \cup E_{2})^{C}\}$$
\end{itemize}

\subsubsection{Proposition 1}
A$_1$, A$_2$, A$_3$,....A$_n$ $\in$ $F$, then $\bigcap\limits_{i=1}^n$A$_i$ $\in$ $F$.

\begin{proof}
    If A$_1$, A$_2$, A$_3$,....A$_n$ $\in$ $F$, then A$_1^c$, A$_2^c$, A$_3^c$,....A$_n^c$ $\in$ $F$ and $\bigcup\limits_{i=1}^n$A$_i^c$ $\in$ $F$. Then by property 2, $(\bigcup\limits_{i=1}^n$A$_i^c)^c$ = $\bigcap\limits_{i=1}^n$A$_i$ $\in$ $F$.
\end{proof}

\subsubsection{Proposition 2}
A, B $\in$ $F$, then A$\setminus$B = A - B $\in$ $F$.\medskip

\begin{proof}
    If B $\in$ $F$, then B$^c$ $\in$ $F$ by  property 2. So, A $\cap$ B$^c$ = A$\setminus$B $\in$ $F$ (As seen in proposition 1).
\end{proof}
\subsection{Probability Measure}
The probability measure P is a function returning an event's probability. A probability is a real number between zero and one.
$$ P:F\rightarrow [0,1]$$

P has to satisfy the following 3 axioms:
\begin{itemize}
    \item $P(E) \geq 0$
    \item $P(S)=1$
    \item If $E_1,E_2 \cdots \in F$ such that $E_i \cap E_j =\phi$ then:
    $$ P(\bigcup_{i=1}^{\infty} E_{i})= \sum_{i=1}^{\infty} P(E_i)$$
\end{itemize}

For two disjoint sets:
$P(E_1 \cup E_2)= P(E_1)+P(E_2)+ \sum P(\phi)$

We will later see that $P(\phi)$ is indeed 0.

\subsection{Derived Properties of Probability}
\begin{enumerate}
    \item $$P(E^C)=1-P(E)$$
        \begin{proof}
            $$E \cup E^C = S$$
                $$P(E)+P(E^C)=1$$

        \end{proof}
    \item For any two events $E_1$ and $E_2$,
    $$ P(E_1 \cup E_2)= P(E_1)+P(E_2)-P(E_1 \cap E_2)$$
    \begin{proof}
        $$ P(E_1 \cup E_2)=P(E_1)+P(E_2 \cap E_1^C)$$
        Now, $$ E_2=(E_2 \cap E_1)\cup (E_2 \cap E_1^C)$$
        $$ \Rightarrow P(E_2)=P(E_1 \cap E_2)+ P(E_1^C \cap E_2)$$
        Also,
        $$ P(E_1 \cup E_2)= P(E_1)+P(E_1^C \cap E_2)$$
        Substitute the required value in the final equation.
    \end{proof}
\end{enumerate}

Question:
$ S=\{1,2,3,4,5,6\}$. 1 and 5 are equally likely and probability of getting a 6 is one-third.

Find minimum and maximum probability that we get an even number.

Answer:$$ Minimum \;Prob. = \frac{1}{3} \;\; when \;\; P_2 = P_4 = 0$$
$$ Maximum \;Prob. = 1,  \;\; P_2+P_4 =\frac{2}{3}  $$

%31/05 notes

\section{Conditional Property}

Given that an event $A$ has occured.

$ (S,F,P) \rightarrow$ Original probability space

If additional info has been given that A has occured, probability space need to be suitably modified.

eg: $S=\{1,2,3,4,5,6\},\;E_1=\{1,2\},\;E_2=\{3,4\}$
$$ F=\{\phi , S, E_1 , E_{1}^{C},E_2 , E_{2}^{C},E_1 \cup E_2 , (E_{1} \cup E_{2})^{C}\}$$
and event $A=E_1^c = \{3,4,5,6\}$ has occured.

Then, $ F_A= \{ \phi, A, \{3,4\},\{5,6\} \} $.(shown later)

\subsection{Modified probability space}

Then,
\begin{itemize}
    \item $S_A = A$. (Modified Sample Space)

    \item $F_A = \{ (E \cap A) | E \in F \} \rightarrow E \cap A \in F$. (Modified Event Space)

    (Also if some event $C \cap A = \phi $, then $C$ won't occur)

    To prove: $F_A$ also satisfies event space axioms.(see sec. 1.4.1)

    \begin{enumerate}
        \item $A \in F_A \rightarrow S\cap A = A$(S was original sample space)
        $\Rightarrow A \in F_A$

        \item $D \in F_A \Rightarrow D = E \cap A, D^c \in F_A$

        As,

        $D^c = A \setminus D= E^c \cap A \in F \quad(Because\;  E^c \in F)$

        \item $$D_1,D_2,\cdots \in F_A$$
        $$ (E_1 \cap A), \cdots \in F_A$$
        $$ E_1, E_2 \cdots \in F$$
        $$  \Rightarrow \bigcup_{i=1}^{\infty} E_i \in F_A$$
        $$ \Rightarrow  (\bigcup_{i=1}^{\infty} E_i) \cap A \in F_A$$
        $$\Rightarrow  (\bigcup_{i=1}^{\infty} E_i \cap A) \in F_A \Rightarrow  \bigcup_{i=1}^{\infty} D_i \in F_A$$

    \end{enumerate}
    Hence, $F_A$ is an event space.

    \item Modified probability measure

    $$ P(E/A)= \frac{P(E \cap A)}{P(A)}$$

    This definition is called conditional probability measure for any $E \in F$.

    eg:
    $ F_A = \{ \phi, \{3,4,5,6\}, \{ 3,4\}, \{5,6\} \}$, then $P(\{ 3,4\} / \{3,4,5,6\})=1/2$

    Now, we need to prove $ P(E/A)$  satisfies the 3 axioms of probability measure.(see sec. 1.5)
    \begin{itemize}
        \item $ P(E/A) \geq 0$ (as ratio of two nos. which are positive)
        \item $ P(S/A)=1$
        \item $ B_1, B_2 \cdots$ are all mutually disjoint.
        $$ P(\bigcap_{i=1}^{\infty} B_i /A) = \frac{ P(\bigcup_{i=1}^{\infty} B_i \cap A)}{P(A)}$$
        $$ =\frac{\sum_{i=1}^{\infty} P(B_i \cap A)}{P(A)} = \sum_{i=1}^{\infty} P(B_i /A)$$
        $$  \Rightarrow P(\bigcap_{i=1}^{\infty} B_i /A) = \sum_{i=1}^{\infty} P(B_i /A)$$
    \end{itemize}
\end{itemize}

\section{Total probability theorem}

Events $A_1,\cdots, A_n \in F$ which are all mutually exclusive/disjoint and exhaustive. Then,
$$ A_i \cap A_j = \phi \quad \forall\; i,j$$
$$ \bigcup_{i=1}^{n} A_i =S$$

$$ P(B)= \sum_{i=1}^{n} P(B/A_i)P(A_i)$$

the total probability theorem expresses $ P(B)$  in terms of conditional probability $ P(B/A_i) $ \& prior probability $P(A_i)$
\begin{proof}

    $$ B= \bigcup_{i=1}^{n} (B \cap A_i)$$

    $A_i$'s are disjoint, so $(B \cap A_i)$ are also disjoint.

    $$ P(B)= \sum_{i=1}^{n} P(B \cap A_i)=\sum_{i=1}^{n} P(B / A_i)P(A_i)$$

\end{proof}

\subsection{Question}
Two factories manufacture zoggles. 20\% of $F_1$ are defective. 5\% of $F_2$ are defective.

In any week, $F_1$ produces twice the number of zoggles as $F_2$. What is the probability that a zoggle chosen randomly in a week is defective?

$$ P(D)=P(F_1)P(D/F_1)+P(F_2)P(D/F_2)$$
$$ = \frac{2}{3} \times \frac{1}{5} + \frac{1}{3} \times \frac{1}{20} = \frac{3}{20}$$

%2/6

\section{Bayes Theorem}
$A_1, \cdots, A_n $ are events which are mutually exclusive and exhaustive.
$B$ be an arbitrary event. Then,
$$ P(A_i/B)= \frac{P(B/A_i)P(A_i)}{\sum_{i=1}^n P(B/A_i)P(A_i)}$$

\begin{itemize}
    \item $P(A_i)$- Prior probability
    \item $P(B/A_i)$- Likelihood
    \item $P(A_i/B)$- Posterior probability
\end{itemize}

Bayes theorem expresses posterior probabilities $P(A_i/B)$ in terms of prior probabilities  $P(A_i)$ and likehoods  $P(B/A_i)$.

In some experiments $P(A_i)$ are all same, $P(A_i)=1/n$. Then posterior probabilities are proportional to likelyhoods:
$$ P(A_i/B)= \frac{P(B/A_i)}{\sum_{i=1}^n P(B/A_i)}$$
$$ \Rightarrow P(A_i/B) \propto P(B/A_i)$$

\begin{proof}

    $$ P(A_i/B) = \frac{P(A_i \cap B)}{P(B)}$$
    $$ P(A_i/B)= \frac{P(B/A_i)P(A_i)}{\sum_{i=1}^{n} P(B/A_i)P(A_i)}$$

\end{proof}

\subsubsection{Example}
In answering a question in a multiple choice test, a student knows the answer with probability $p$ and guesses the answer otherwise. If he/she guesses from $m$ choices, the probability of being correct is $\frac{1}{m}$. Find the conditional probability that the student knew the answer if he/ she answered correctly.

Ans: $A_1=$ knowing the answer. $A_2=$ Guessing the answer.

$B= $Answer is correct. $A_1 \subseteq B$

$P(A_1)= p, P(A_2)= 1-p, P(B/A_1)= 1, P(B/A_2)= \frac{1}{m}$

$$ P(A_1/B)= \frac{P(B/A_1)P(A_1)}{P(B/A_1)P(A_1)+ P(B/A_2)P(A_2)}$$
$$ = \frac{p}{p + (1-p)\frac{1}{m}}$$

\section{Indepedent Events}
$A \;\&\; B$ are said to be independent. If
$$ P(A \cap B) = P(A)P(B)$$

In terms of conditional probability,
$$ P(B/A)= \frac{P(A \cap B)}{P(A)} = \frac{P(A)P(B)}{P(B)} = P(B)$$

$$ \Rightarrow P(B/A) = P(B)$$

Probability of event $B$ remains same with or without conditioning on $A$. Hence, $B$ is said to be independent of $A$.
Knowledge of occurence of event $A$ does not give any information about $B$.

If $A\; \&\; B $ are independent, then
$ A \;\&\; B^c$ are also independent.

\begin{proof}
    $$ P(B^c / A)= \frac{P(B^c \cap A)}{P(A)}= \frac{P(A)- P(A \cap B)}{P(A)}= \frac{P(A)- P(A)P(B)}{P(A)} = P(B^c)$$
\end{proof}

\subsection{Important Results}
\begin{enumerate}
    \item $P(A/B)$ may be greater than, less than or equal to $P(A)$.
    \item Independent events and mututally exclusive events are different.

    Indepedence: $P(A \cap B)= P(A)P(B)$

    Mututally exclusive: $A \cap B = \phi$

    \begin{enumerate}
        \item (Indepedent but not mutually exclusive). Coin toss followed by throwing dice experiment.

        $$ S=\{ (H,1), (H,2), \cdots, (H,6), (T,1), \cdots, (T,6)\}$$
        $F=$ Power set of S (Always an event space)

        $A=\{(H,1), \cdots, (H,6) \}$, $B= \{ (H,2), (H,4), (H,6), (T,2),(T,4),(T,6)$

        $P(A)= 1/2 \; \& \; P(B)= 1/2$
        $$ P(A \cap B)= P(\{ (H,2), (H,4), (H,6)\})= \frac{3}{12}= \frac{1}{4}$$
        $$ P(A\cap B)= P(A)P(B)$$
        \item (Not independent but mutually exclusive)
        If the events are mutually exclusive $\Rightarrow$ they are not independent.

        Single Coin Toss: $A= \{H \}$, $B= \{T \}$ $\rightarrow$ Mututally exclusive.

        $P(A \cap B)= 0$, $P(A)P(B)= \frac{1}{4}$

        %Eg 3: (Not independent and not mutually exclusive) hw

    \end{enumerate}
    \end{enumerate}

\subsection{Conditionally indepedent events}

$A \; \& \: B$ are said to be conditionally indepedent given $C$ if
$$ P((A \cap B)/ C)= P(A/C)P(B/C)$$
In terms of conditional probabilities,
$$ P(B/C)= \frac{P((A\cap B )/ C)}{P(A/C)}= \frac{\frac{P(A \cap B \cap C)}{P(C)}}{\frac{P(A \cap C)}{P(C)}} = \frac{P(A\cap B\cap C)}{P(A \cap C)} = P(B/(A \cap C))$$

Indepedent Events: $P(A \cap B)= P(A)P(B)$ \& $P(B/A)= P(B)$.

Conditionally indepedent events: $P(A\cap B / C)= P(A/C)P(B/C)$ \& $P(B/(A\cap ))$

\subsubsection{Example}
Two fair coins are tossed, $S=\{HH, HT, TH, TT \}$, $A=\{HH, HT \} $, $B=\{HH, TH\} $, $C=\{HH \}$ \& $D=\{HT, TH\}$

\begin{enumerate}
    \item Are $A$ \& $B$ independent?
    $$ P(A \cap B)= P(\{HH\}) = \frac{1}{4}= P(A)P(B)$$
    \item  Are $A$ \& $B$ conditionally indepedent given $C$?
    $$ P((A \cap B) /C)= P(A/C)=P(B/C)= 1$$
    $$ P(A/C)= \frac{P(A \cap C)}{P(C)}= \frac{\frac{1}{4}}{\frac{1}{4}}= 1$$
    \item Are $A$ \& $B$ conditionally indepedent given $D$?

    $$P(A/D)= \frac{P(A\cap D)}{P(D)}= \frac{\frac{1}{4}}{\frac{1}{2}}=\frac{1}{2} $$
    $$P(B/D)= \frac{P(B\cap D)}{P(D)}= \frac{\frac{1}{4}}{\frac{1}{2}}=\frac{1}{2} $$
    $$ P(A\cap B /D)= 0$$
    Hence, they are not conditionally indepedent given $D$.
\end{enumerate}

If $A$ and $B$ are independent, it doesn't imply $A$ and $B$ will be conditionally indepedent given $C$ and vice-versa.
%4/6 notes
\subsection{Indepedence of collection of events}

Three events $A_1, A_2$ \& $A_3$ are said to be indepedent if:

$$ P(A_1 \cap A_2 \cap A_3)=P(A_1)P(A_2)P(A_3)$$
$$ P(A_1 \cap A_2)=P(A_1)P(A_2)\quad P(A_2 \cap A_3)=P(A_2)P(A_3)\quad P(A_3 \cap A_1)=P(A_3)P(A_1)$$

\subsubsection{Chain rule of Probability}
$$ P(A_1 \cap A_2 \cap A_3)=P(A_1)P(A_2/A_1)P(A_3/A_1\cap A_2)$$
$$ P(\bigcap_{i=1}^n A_i)=P(A_1) \prod_{i=2}^n P(A_i/A_1\cdots A_{i-1})$$
For independent events $A_1, A_2, A_3$.
$$P(A_1)P(A_2/A_1)P(A_3/(A_1 \cap A_2))= P(A_1)P(A_2)P(A_3)$$
$$\Rightarrow P(A_2/A_1)P(A_3/(A_1 \cap A_2))= P(A_2)P(A_3)$$

Hence, if $A_1$, $A_2$, $A_3$ are independent events, the first condition doesn't imply the other conditions.

Eg: Pair-wise independence does not imply that three events are independent.
Consider example of two coin tosses:
$S= \{HH,HT,TH,TT\}, A_1=\{HH,HT\},A_2=\{HH,TH\},A_3=\{HT,TH\}$.

$A_1$ \& $A_2$ are indepedent $P(A_1 \cap A_2)=P(A_1)P(A_2)$

$A_2$ \& $A_3$ are indepedent $P(A_3 \cap A_2)=P(A_3)P(A_2)$

$A_1$ \& $A_3$ are indepedent $P(A_1 \cap A_3)=P(A_1)P(A_3)$

$A_1$, $A_2$  \& $A_3$ are not still not independent as $P(A_1 \cap A_2 \cap A_3) \neq P(A_1)P(A_2)P(A_3)$.

\section{Continuity of Probability}
$\Omega$ is an equivalent notation for sample space.

Consider the probability space $(S,F,P)$.

$E_i \in F$. Let $E_1 \subseteq E_2 \subseteq \cdots$ be countably infinite sequence of events (increasing sequence of events).
Then $$P(\bigcup_{i=1}^{\infty} E_i)= \lim_{i \to \infty} P(E_i)$$.
$$ \bigcup_{i=1}^{\infty} E_i \in F$$
Pushing the limit from inside the probability expression to outside.

$$P(\bigcup_{i=1}^{\infty} E_i)=\lim_{i \to \infty }P(E_i)$$
$$P(\lim_{n \to \infty} \bigcup_{i=1}^{n}E_i)=\lim_{i \to \infty }P(E_i)$$

Exchanging limits with probability $\rightarrow$ non trivial operation.
Exchanging limits with differentiation, integration etc. $\rightarrow$ require a proof.

\begin{itemize}
    \item $$E_1 \subseteq E_2 \subseteq \cdots$$
    $$P(\bigcup_{i=1}^{\infty} E_i)= \lim_{i \to \infty} P(E_i)$$

    \begin{proof}
        We shall use the third axiom.
        $D_1, D_2 \cdots \rightarrow$ mutually exclusive events.

        $$ P(\bigcup_{i=1}^{\infty} D_i)= \sum_{i=1}^{\infty}P(D_i)$$

        $E_1 \subseteq E_2 \subseteq \rightarrow $ not mutually exclusive.

        Hence we construct a sequence which are mututally exclusive and unions of both sequences have to be the same.

        $A_1=E_1$, $A_2=E_2\setminus E_1$, $ A_3=E_3 \setminus E_2$ , $\cdots$.
        $$ P(\bigcup_{i=1}^{\infty} E_i)= P(\bigcup_{i=1}^{\infty} A_i)= \sum_{i=1}^{\infty}P(A_i)= \lim_{n \to \infty} \sum_{i=1}^{\infty}P(A_i)$$

        $$\Rightarrow \lim_{n \to \infty} \sum_{i=1}^{n} P(A_i)= \lim_{n \to \infty} P(\bigcup_{i=1}^{n}A_i)= \lim_{n \to \infty}P(E_n)$$
        If there exists a subsequence of events, which is increasing.

    \end{proof}

    \item Let $E_1 \supseteq E_2 \supseteq \cdots$ be a countably infinte sequence of events(decreasing sequence of events)

    $$P(\bigcap_{i=1}^{\infty}E_i)=\lim_{i \to \infty} P(E_i)$$

    \begin{proof}
        We can use the result of the previous proof.
        $$ E_1^c \subseteq E_2^c \subseteq \cdots$$
        which is an increasing set of events
        $$P(\bigcup_{i=1}^{\infty} E_i^c)= \lim_{i \to \infty}P(E_i^c)$$
        as,
        $$ \left(\bigcap_{i=1}^{\infty} E_i \right)= \left( \bigcup_{i=1}^{\infty} E_i^c \right)^c $$

        $$\Rightarrow 1- P(\bigcup_{i=1}^{\infty} E_i^c)= \lim_{i \to \infty}1- P(E_i^c)$$

        $$P \left(\bigcap_{i=1}^{\infty} E_i \right) = \lim_{i \to \infty }P(E_i)$$

    \end{proof}

\end{itemize}

%4/6  \subsection{Example of continuity of probability}

%7/6 lecture 6

\section{Random Variables}

Consider a probability space $(S,F,P)$ and an experiment is conducted.

Probability spaces vary a lot based on the experiment. We use random variables to be able to develop a theory of probability which is indepedent of the actual experiment which is performed.

$$S \xrightarrow[]{\text{R.V.}} \mathbb{R}$$
$$F \xrightarrow[]{\text{R.V.}} B$$

Random Variable is a function which maps the sample space to the real line.
It also maps the event space to the Borel $\sigma$-algebra, $B$.

\subsection{Borel $\sigma$-algebra}
It is the smallest $\sigma$- algebra which contains sets of the form $(-\infty ,x]\; \forall x \in \; \mathbb{R}$.

$$ B=\{ \mathbb{R} , (-\infty ,x] ,(x,\infty ), \phi, (-\infty,x) ,(x,y),[x,y], \{x\},(x,y],[x,y)\}$$

$$ E_i =(-\infty ,x_i] \quad x_i = x-\frac{1}{i}$$
$$ \bigcup_{i=1}^{\infty}(-\infty ,x_i] = \bigcup_{i=1}^{\infty} E_i =(-\infty ,x)$$

Random Variable $X:S \rightarrow R$

$X$ has to be a measurable function.

A function is said to be measurable if  pre-image of $(-\infty ,x ] \; \forall x \in \mathbb{R}$ is in the event space.
$$X:S \rightarrow R$$
$$\text{inverse image} \leftarrow(-\infty ,x ] $$
$$ X^{-1} ((-\infty ,x ]) \subseteq S \qquad  X^{-1} ((-\infty ,x ]) \in F$$
eg:
$ X: \{1,2,3,4,5,6\} \rightarrow \{0,1,0,1,0,1 \}$

$$X^-1\left( (-\infty, 0.5]\right)=\{1,3,5\}$$

In the above example, $X$ is not an invertible mapping. We are using $X^{-1}$ as a notation.

$X$ is a map from $S$ to $\mathbb{R}$ such that
$$X:S \rightarrow \mathbb{R}$$
$$ X^{-1}\left( (-\infty, x ]\right) \leftarrow (-\infty, x ] \qquad X^{-1}\left( (-\infty, x ]\right) \in F $$

\subsection{Examples}
\begin{itemize}
    \item X which is a random variable.

    $S= \{a,b,c\}\; ,\; F=\{ \phi, \{ a\}, \{b,c \}, S\}\; , \; X:S \rightarrow \mathbb{R}$

    $X(\omega) =0\; ,\; \omega =a $

    $X(\omega)= 1\;,\; \omega = b,c$
    \begin{enumerate}
        \item $( -\infty,x]\;,\; x < 0\;,\; X^{-1}\left( (-\infty , x]\right) = \phi \in F$
        \item $( -\infty,x]\;,\; 0 \leq x <1 \;,\; X^{-1}\left( (-\infty , x]\right) = \phi \in F$
        \item $( -\infty,x]\;,\; x \geq 1\;,\; X^{-1}\left( (-\infty , x]\right) = S \in F$
    \end{enumerate}

    \item X which is not a random variable
    $S= \{a,b,c\}\; ,\; F=\{ \phi, \{ a\}, \{b,c \}, S\}\; , \; X:S \rightarrow \mathbb{R}$

    $X(\omega) =0\; ,\; \omega =b $

    $X(\omega)= 1\;,\; \omega = a,c$

    But $X^{-1}((-\infty,x])\; , \;0\leq x<1 = \{b\} \; , \; \{ b\} \notin F$

    $X$ is not a random variable.
\end{itemize}

If $S$ is finite \& $F$ is a power set of $S$, then every function is a random variable.
$$ X^{-1}((-\infty,x]) \subseteq S$$

$F$ is set of all subsets of $S$.

%9/6
\subsection{Conditions on random variable}
\begin{theorem}
    Let a probability space be $(S,F,P)$ and $X$ is a random variable $X: S \rightarrow \mathbb{R}$.
    The following conditions hold:
    \begin{itemize}
        \item $X^{-1}((-\infty,x)) \in F$
        \item $X^{-1} (\{x\}) \in F$
        \item $X^{-1}((x_1 , x_2]) \in F$
        \item $X^{-1} ((x_1,x_2)) \in F$
    \end{itemize}
\end{theorem}

\begin{proof}

    Applying the axioms of the event space we want to show the above conditions are true.

    $A_i=X^{-1}((-\infty,x]) \qquad x_i = x - \frac{1}{i}$. Now,
    $$ \bigcup_{i=1}^{\infty} (-\infty,x_i] = (-\infty,x)$$

    $$ \bigcup_{i=1}^{\infty} A_i = \bigcup_{i=1}^{\infty} X^{-1}((-\infty,x_i]) = X^{-1}((-\infty,x)) \in F$$

    $$ X^{-1}((-\infty ,x] \cap {(-\infty,x)}^c) = X^{-1} (\{x\}) \in F$$

    Similarly the other conditions can also be proved.
\end{proof}

\section{Cumulative distribution function}
It is denoted by $F_X(x)$.

$$ F_X(x)= P(X \leq x) = P((-\infty, x])= P(X^{-1}((-\infty, x]))$$

The above definitions are different notations used, the last one is well defined from the probability space.

\subsection{Properties of CDF}

\begin{enumerate}
    \item $F_X(x)$ is a monotonically non-decreasing function of x.
    \begin{proof}
        If $x_2 \geq x_1$, then $F_X(x_2) \geq F_X(x_1)$.

        $$F_X(x_2) = P(X \leq x_2)= P(X \leq x_1)+P(x_1 < X \leq x_2)$$
        $$F_X(x_2) = P(X \leq x_2) \geq P(X \leq x_1) -F_X(x_1) $$

    \end{proof}

    \item $\lim_{x \to \infty} F_X(x)= 1$

    \begin{proof}
        Construct a decreasing set of events. $A_i= (-\infty, i]$, $B_i = X^{-1}(A_i)$.
        $$ \bigcap_{i=1}^{\infty}A_i= \mathbb{R}$$
        $$ \bigcap_{i=1}^{\infty}B_i= S $$

        Applying continuity of probability,
        $$ \lim_{i \to \infty}P(B_i)= P(\bigcup_{i=1}^{\infty}B_i) =P(S) = 0$$
        $$ \lim_{i \to \infty}P(X \leq i)= \lim_{x \to \infty}P(X \leq x) = \lim_{x \to \infty} F_X(x)$$

        The change from integers to real numbers $x$ in the last step is valid.

    \end{proof}
    \item $\lim_{x \to -\infty} F_X(x)= 0$
    \begin{proof}
        Construct a decreasing set of events. $A_i= (-\infty, -i]$, $B_i = X^{-1}(A_i)$.
       $$ B_1 \supseteq B_2 \supseteq \cdots$$

       $$ \bigcap_{i=1}^{\infty}B_i= \phi $$
       $$ P(\bigcap_{i=1}^{\infty}B_i) = \lim_{i \to \infty}P(B_i)= 0$$
       $$  \lim_{i \to \infty}P(B_i) =  \lim_{i \to \infty}P((-\infty,i])= \lim_{x \to \infty}P((-\infty,x]) \rightarrow \text{(x = all real nos.)}$$
       $$ \lim_{x \to \infty}P((-\infty,x]) = \lim_{x \to \infty}F_X(x)= 0$$
    \end{proof}
%11/6
    \item $F_X(x)$ is a right continuous function.
    $$ \lim_{x \to x_0^+} F_X(x)= F_X(x_0)$$

    Right continuous function: $\lim_{x \to x_0^+} f(x)= f(x_0)$

    Left continuous function: $\lim_{x \to x_0^-} f(x)= f(x_0)$

    Continuous function(Approaching from either left or right): $\lim_{x \to x_0} f(x)= f(x_0)$

    \begin{proof}
        Decreasing sequence of events $B_i= (X \leq x+\frac{1}{i})$

        $$ B_1 \supseteq B_2 \supseteq \cdots$$
        $$ \bigcap_{i=1}^{\infty}B_i= \{ X \leq x\}$$
        $$ P(\bigcap_{i=1}^{\infty}B_i) = P(X \leq x) = F_X(x)$$
        $$ \Rightarrow \lim_{i \to \infty}P(B_i)= \lim_{i \to \infty}P(X \leq x + \frac{1}{i})$$
        We are changing the variable from an integer to real.
        $$= \lim_{\epsilon \to 0}P(X \leq x + \epsilon)=\lim_{\epsilon \to 0^+}F_X(x + \epsilon) $$
        $$ = \lim_{x_0 \to x^+}F_X(x_0) \quad \text{Change of variable: }x_0= x+ \epsilon $$

    \end{proof}
\end{enumerate}

\subsection{Indicator Random Variable}
\begin{equation*}
    I_A (x)=
    \begin{cases}
      1, & \text{if}\ x \in A \\
      0, & \text{if}\ x \notin A
    \end{cases}
\end{equation*}

$$I_A:S \to \mathbb{R}$$

$$ F_{I_{A}} (x)= P(I_A \leq x)$$

If $x < 0 $, $P(I_A \leq x)= 0$.

If $0 \leq x <1 $, $P(I_A \leq x)= P(A^c)=1-P(A)$.

If $x \geq 1 $, $P(I_A \leq x)= 1$.

$$ \lim_{x \to 0^+} F_{I_A}(x)= P(A^c) \qquad F_{I_A}(0)= P(A^c)$$

$$ \lim_{x \to 1^+} F_{I_A}(x)= 1 \qquad F_{I_A}(1)= 1$$

\subsection{Examples of CDF's and non-CDF's}
\begin{itemize}
    \item Is $F_1(x)$ a valid CDF?
    \begin{equation*}
        F_1 (x)=
        \begin{cases}
          1, & \text{if}\ x \leq 0 \\
          0.5, & \text{if}\ 0<x\leq 1 \\
          0.25+0.25x, & \text{if}\ 1<x\leq 3 \\
          1, & \text{if}\ 3<x
        \end{cases}
    \end{equation*}

    No, as it is not right continuous at $x=0$.

    $$ \lim_{x=0^+}F_1(x)=0.5, \quad F_1(0)=0$$

    \item Is $F_2(x)$ a valid CDF?
    \begin{equation*}
        F_2 (x)=
        \begin{cases}
          0, & \text{if}\ x < 0 \\
          0.5, & \text{if}\ 0\leq x < 1 \\
          0.75, & \text{if}\ 1\leq x < 3 \\
          1, & \text{if}\ 3\leq x
        \end{cases}
    \end{equation*}
    It is valid as it satisfies all 4 properties.

    \item Is $F_3(x)$ a valid CDF?
    \begin{equation*}
        F_3 (x)=
        \begin{cases}
          1, & \text{if}\ x < 0 \\
          0.5, & \text{if}\ 0 \leq x < 1 \\
          0.25, & \text{if}\ 1\leq x < 3 \\
          1, & \text{if}\ 3 \leq x
        \end{cases}
    \end{equation*}
    $F_3(x)$ is not a valid CDF as it is not non-decreasing.

\end{itemize}

%14/6
\begin{lemma}
    For any $x \in \mathbb{R}$,
    $$ P(X=x)= P(X \leq x)- P(X <x)$$
    $$ P(X=x)= F_X(x)- \lim_{\epsilon \to 0}F_X(x- \epsilon)$$

\end{lemma}

Upon applying continuity of probability.

\begin{proof}
    $$ B_1 \subseteq B_2 \subseteq \cdots$$
    $$B_i=\{ X \leq x - \frac{1}{i}\} \qquad \bigcup_{i=1}^{\infty}B_i=\{ X<x\}$$

    $$ P(X<x)= P(\bigcup_{i=1}^{\infty}B_i)= \lim_{i \to \infty} B_i = \lim_{\epsilon \to \infty}P(X \leq x- \epsilon)= \lim_{\epsilon \to 0}F_X(x-\epsilon)$$
\end{proof}
\begin{corollary}
    $F_X(.)$ is left continuous if and only if $P(X=x)=0 \; \forall \; x \in \mathbb{R} $. From lemma,

    $$ P(X=x)= F_X(x)- \lim_{\epsilon \to 0^+ }F_X(x-\epsilon)$$

\end{corollary}

A function is left continuous if

$$  \lim_{\epsilon \to 0^+}F_X(x - \epsilon)= F_X(x) \quad \forall x \in \mathbb{R}$$
$$ \Rightarrow P(X=x)= 0 \quad \forall x \in \mathbb{R}$$

$F_X(.)$ is continuous, means that it is both right continuous, and in particular, left continuous.

\section{Types of Random Variables}
\begin{itemize}
    \item Continuous random variable
    \item Discrete random variable
    \item Mixed random variable
\end{itemize}

\subsection{Continuous random variables}
A random variable $X$ with cummulative distribution function $F_X(.)$ is said to be a continuous random variable if $F_X(.)$ is continuous.

$P(X=x)=0 \quad \forall \; x \in \mathbb{R}$, probability of every point is zero.

An example for a continuous random variable is:
\begin{equation*}
    F_X (x)=
    \begin{cases}
      0, & \text{if}\ x < 0 \\
      x, & \text{if}\ 0 \leq x < 1 \\
      1, & \text{if}\ x\leq 1  \\
    \end{cases}
\end{equation*}

In the context of a continuous random variable, we need to ask what is the probability of intervals or unions of intervals: $ P(a \leq X \leq b)$

If $F_X(.)$ is differentiable, given that it is continuous then we can find another function for the continuous random variable.

$$ f_X(x)= \frac{dF_X(x)}{dx}$$

$f_X(x)$ is called the probability density function (pdf).
\begin{equation*}
    f_X (x)=
    \begin{cases}
      1, & \text{if}\ 0 < x < 1 \\
      0, & \text{otherwise}\
    \end{cases}
\end{equation*}

$$ F_X(a)= P(X \leq a)= \int_{-\infty}^{a}f_X(x)dx$$

$$  P(a \leq X \leq b)=F_X(b)-F_X(a) = \int_{a}^{b}f_X(x)dx$$

$$ P(x \leq X \leq x + \Delta x) \to \Delta x\text{ is very small(infintesimal)}$$
$$ P(x \leq X \leq x + \Delta x) \approx \text{area of rectangle with b=} \Delta x \;\& \text{ h= }f_X(x)$$
$$ P(x \leq X \leq x + \Delta x) \approx f_X(x) \Delta x$$

\subsubsection{Probability density function}
When is a function a valid probability density function?

Two properties of the probability density function are:
\begin{itemize}
    \item $f_X(x) \geq 0$

    As CDF is monotonically non decreasing, it's derivative is always non-negative.
    \item $ \int_{-\infty}^{\infty}f_X(x)dx= 1 \to \quad P(X \leq \infty )= F_X(\infty)=1$
\end{itemize}

Note: $f_X(.) \nleq 1$

The probability density function itself doesn't indicate any probability.
Only the area under the curve indicates probability.

\subsection{Discrete random variable}

$X$ is said to be a discrete random variable if the range of $X$ is either finite or countably infinite in $\mathbb{R}$.
$$ X:S \to \mathbb{R}$$

CDF of a discrete random variable will be constant everywhere and jumps at some points(finite or countably infinite number of points).

Range of $X={x_1,x_2,\cdots}$

Discrete random variables can take non-zero values unlike the continuous r.v's.

$$ F_X(a)= \sum_{x_i \leq a}P(X= x_i)$$

$$ P(X=x_i)= P_X(x_i)\qquad x_i \text{ is in the range of X.}$$
$P_X$ is said to be the probability mass function.

\subsubsection{Probability mass function}
When is a function a valid probability mass function?
$$ S= \text{ Range of X }= \{ x_1, x_2, \cdots\}$$
Two properties of the probability mass function are:
\begin{itemize}
    \item $P_X(x_i) \geq 0$

    \item $ \sum_{x_i \in S}P_X(x_i)=1$
\end{itemize}
%17/6

\subsection{Mixed random variable}

$F_X(.)$ is continuous in some intervals and also jumps at some points. The probability density function has some impulses included in it. Let, $X_1$ be a continuous random variable, $X_2$ be a discrete random variable, then $Y$, a linear combination, is a mixed variable.
$$ Y= \alpha X_1 +\beta X_2$$

\subsection{Functions of random variables}
$X$ is a random variable. $X: S \to \mathbb{R}$.

$$ Y=g(X) \qquad g:\mathbb{R} \to \mathbb{R} \qquad Y= goX:S\to \mathbb{R}$$

We require a condition on $g$ to find out when $Y$ is a random variable.
$$ Y^{-1}((-\infty ,y]) \in F \Rightarrow X^{-1} \left( g^{-1} ((-\infty,y])\right) \in F$$
Let $\mathfrak{B}$ be an arbitrary set in the Borel $\sigma$-algebra generated by intervals of the form $(-\infty,x] = \mathfrak{B}$.

Then, since $X$ is a random variable, $X^{-1}(\mathfrak{B}) \in F$.

$ g^{-1} ((-\infty,y]) \in \mathfrak{B}$, (Borel $\sigma$-algebra generated by intervals of the form $(-\infty,x] $).
$$X^{-1} \left( g^{-1} ((-\infty,y])\right) \in F \Rightarrow \text{Y is a random variable} $$

Any common functions satisfy the condition needed for $g$.

\begin{itemize}
    \item
    Suppose $X$ is a discrete random variable, $X: S \to \mathbb{R}$, range of $X$ is discrete.

    $g$ is a function, such that $Y=g(X)$. Then $Y$ is a discrete random variable.

    $X$ has a probability mass function $P_X$, $Y \to P_Y$. Then,
    \begin{align*}
        P_Y(y)&= P(Y=y)= P(g(X)=y) \\
        &= P(\{ x_i | g(x_i)=y\}) \\
        &= \sum_{x_i : g(x_i)=y} P_X(x_i)
    \end{align*}

    Eg: Tossing a coin till first head.

    $$ S= \{ H,TH,TTH, \cdots\}$$

    $$X:S \to \mathbb{R}\qquad H \to 1 \qquad TH \to 2 \qquad TTH \to 3 \cdots$$

    $Y=g(X)= X \text{ mod } 4 \Rightarrow \text{Range of Y}= \{ 0,1,2,3\}$

    $$P_Y(0)= P(Y=0)= P(X \text{ mod } 4 =0)= P(\{ 4,8,\cdots\})= \sum_{x \text{ mod } 4=0}P_X(x)$$

    \item $X$ is a continuous random variable, $Y= g(X)$.

    Based on $g$, it can either be continuous or discrete.

    \begin{enumerate}
        \item When $X$ is continuous \& $Y$ is also continuous.

        Let $F_X(.)$ be the CDF of $X$ and $f_X(.)$ be the PDF of $Y$.
        $$ Y= aX+b \qquad a,b \in \mathbb{R}$$
        For the CDF's:
            $$F_Y(y)= P(Y \leq y) $$

        $$ \{ Y \leq y\}= \{ aX+b \leq Y\}$$
        \begin{enumerate}
            \item If $a>0$,
            $$ \{ Y \leq y\}  = \{ X \leq \frac{y-b}{a}\}$$
            $$ F_Y(y)= P(Y \leq y)= P\left( X \leq \frac{y-b}{a}\right)= F_X\left( \frac{y-b}{a}\right)$$
            \item If $a<0$,
            $$ \{ Y \leq y\}  = \{ X \geq \frac{y-b}{a}\}$$
            $$ F_Y(y)= P(Y \leq y)= P\left( X \geq \frac{y-b}{a}\right)= 1-F_X\left( \frac{y-b}{a}\right)$$
        \end{enumerate}

        For the PDF's:
        \begin{enumerate}
            \item If $a>0$

            $$ F_Y(y)= F_X\left( \frac{y-b}{a}\right)$$
            \begin{align*}
                f_Y(y)= \frac{dF_Y(y)}{dy} &= \frac{d}{dy}F_X\left( \frac{y-b}{a} \right)\\
                &= f_X \left( \frac{y-b}{a} \right)\frac{d}{dy}\left(\frac{y-b}{a} \right) \\
                &= \frac{1}{a}f_X\left(\frac{y-b}{a} \right)
            \end{align*}

            \item If $a>0$

            $$ F_Y(y)= 1- F_X\left( \frac{y-b}{a} \right)$$
            \begin{align*}
                f_Y(y)= \frac{-1}{a}f_X\left(\frac{y-b}{a} \right)
            \end{align*}

        \end{enumerate}

        Hence,
        \fbox{$ f_Y(y)= \frac{1}{|a|}f_X \left(\frac{y-b}{a} \right)$}
%18/6
        \item $X$ is continuous and $Y$ is continuous, but a many to one function.
        $$ Y= X^2$$
        Now let us find the CDF \& PDF of $Y$ in therms of CDF \& PDF of $X$.
        \begin{enumerate}
            \item $y<0$, $F_Y(y)=0$
            \item $y \geq 0$,
            \begin{align*}
                F_Y(y)= &= P(Y \leq y) \\
                    &= P(X^2 \leq y) = P(-\sqrt{y} \leq x \leq \sqrt{y}) \\
                    &= F_X(\sqrt{y})- F_X(-\sqrt{y}) + P(x= -\sqrt{y}) \\
                    &= F_X(\sqrt{y})- F_X(-\sqrt{y})
            \end{align*}
        \end{enumerate}

        $$ f_Y(y)= \frac{dF_Y(y)}{dy}= \frac{d}{dy}(F_X(\sqrt{y})- F_X(-\sqrt{y}))$$
        $$ = \frac{1}{2\sqrt{y}}[f_X(\sqrt{y})- f_X(-\sqrt{y})]$$
        \item $X$ is continuous, $Y$ is continuous and a many to one function.
        \begin{equation*}
            f_X (x)=
            \begin{cases}
              \frac{1}{2\pi}, & \text{if}\ 0 \leq x \leq 2\pi \\
              0, & \text{otherwise}\
            \end{cases}
        \end{equation*}
        $$ F_X(x)= \int_{-\infty}^{x} f_x(x)dx$$
        $$ Y=\sin X$$
        Let us find the PDF of $Y$.

        \begin{enumerate}
            \item Consider $0 \leq y \leq 1$
            \begin{align*}
                F_Y(y)&= P(Y \leq y) \\
                    &= P(\sin{X} \leq Y) \\
                    &= P(\{ 0 \leq X \leq \sin^{-1} y\} \cup \{ \pi - \sin^{-1}y \leq X \leq 2\pi \})\\
                    &=P( 0 \leq X \leq \sin^{-1}y)+(\pi - \sin^{-1}y \leq X \leq 2 \pi )\\
                    &= F_X(\sin^{-1}y)- F_X(0)+F_X(2\pi)- F_X(\pi - \sin^{-1}y)\\
                    &= \frac{\sin^{-1}y}{2\pi}-0+1- \left( \frac{\pi - \sin^{-1}y}{2\pi}\right) \\
                    &= \frac{1}{2}+ \frac{\sin^{-1}y}{\pi}
            \end{align*}
            \item $-1 \leq y <0$
            \begin{align*}
                F_y(y)&= P(Y \leq y) = P(\sin X \leq y)\\
                &= P(\pi - \sin^{-1}y \leq X \leq 2\pi + \sin^{-1}y )
            \end{align*}
        \end{enumerate}

    %    \begin{equation*}
    %        f_Y (y)=
    %        \begin{cases}
    %         \frac{1}{\pi \sqrt{1-y^2}}, & \text{if}\ -1 \leq y < 1 \\
    %         \frac{1}{\pi \sqrt{1-y^2}}, & \text{if}\ 0 \leq y \leq 1
    %
    %        \end{cases}
    %    \end{equation*}

    \item $X$ is continuous and $Y$ is discrete.

    $X$ has CDF $F_X(.)$ \& PDF $f_X(.)$

    $Y$ has CDF $F_Y(.)$ \& PMF $P_Y(.)$

    $$Y=g(x)=k \; if \; k \leq x < k+1 \qquad k \in \mathbb{Z} $$
    The above operation is also called quantization. Range of $g$ is countably infinite \& thus $Y$ is a discrete random variable.

    We would like to experss the PMF of $Y$ in terms of the PDF of $X$.

    \begin{align*}
        P_Y(y)&= P(Y=y)= P(y \leq X < y+1) \\
        &= \int_{y}^{y+1}f_X(x)dx
    \end{align*}

    In general,
    $$ P_Y(y)= \int f_X(x)dx \qquad S= \{ x: g(x)=y\}$$

    \end{enumerate}
\end{itemize}

    \subsection{General formula for determining PDF of $Y= g(X)$}
    Where $g$ is differentiable,

    $$ P(y \leq Y \leq y + \Delta y) \approx f_Y(y)\Delta y$$
    The left hand side can be written in terms of PDF of $X$.
    $$ g(x_i)= y$$
    $$ g^{-1}[y,y + \Delta y]= [x_1, x_1+ \Delta x_1]\cup [x_2, x_2+ \Delta x_2] \cdots [x_n, x_n+ \Delta x_n]$$
What is the sign of slope of $g(x)$ at $x_i$?
\begin{itemize}
    \item If $g^{'} (x_i) \geq 0 \qquad [x_i,x_i+ \delta x_i]$
    \item If $g^{'} (x_i) < 0 \qquad [x_i- \delta x_i,x_i]$
\end{itemize}

\begin{align*}
    f_Y(y)\Delta y &= \sum_{i=1}^{n}P(x_i \leq X \leq x_i + \Delta x_i)\\
                    &= \sum_{i=1}^{n} f_x(x_i)\Delta x_i
\end{align*}
$$ f_Y(y)= \sum_{i=1}^{n} f_X(x_i)\frac{\Delta x_i}{\Delta y}= \sum_{i=1}^{n} f_X(x_i)\frac{1}{(\Delta y / \Delta x_i)}$$

For infintesimally small $\Delta y$ \& corresponding $\Delta x_i$:
$$ \frac{\Delta y}{\Delta x_i}= |g^{'}(x_i)|$$

We take modulus as LHS is positive, i.e. magnitude of the slope of tangent at $x_i$.

$$ f_Y(y) = \sum_{i=1}^{n} \frac{f_X(x_i)}{|g^{'}(x_i)|}$$

Examples:
\begin{itemize}
    \item $Y= X^2$
    $$ f_Y(y)= \frac{f_X(\sqrt{y})}{2\sqrt{y}}+ \frac{f_X(-\sqrt{y})}{2\sqrt{y}}$$
    \item $Y= \sin X$, we take $\sin^{-1} y(2n\pi + \sin^{-1} y)$ \& $\pi - \sin^{-1} y(2n\pi + \pi - \sin^{-1} y)$

    $$ f_Y(y)= \sum_{n}\left[ \frac{f_X(2n\pi + \sin^{-1} y)}{\sqrt{1- y^2}} + \frac{f_X(2n\pi + \pi - \sin^{-1} y)}{\sqrt{1- y^2}} \right]$$
\end{itemize}

%21/6

\begin{theorem}
    If $Y= F_X(X)$, where $X$ itself is a random variable with CDF $F_X(.)$, the CDF of Y:
    $$ F_Y(y)= P(Y \leq y)= P(F_X(x) \leq y)$$
\end{theorem}
    \begin{itemize}
        \item If $y<0$,
        $$ \{ X \leq F_{X}^{-1}((-\infty,y])\}= \phi \qquad P(F_{X}(x)\leq y)=0 $$
        \item If $y \geq 1$,
        \begin{align*}
            F_Y(y)&= P(Y \leq y) = P(F_X(x) \leq y) \\
            &= 1 \qquad \text{(max value of }F_X \text{ is 1, } F_{X}^{-1}((-\infty,y])= \mathbb{R})
        \end{align*}
        \item If $0 \leq y <1 $,
        \begin{align*}
            F_Y(y)= P(Y \leq y)&= P(F_X(x)\leq y) \\
            &= P(X \leq F_{X}^{-1}(y)) \\
            &= F_X(F_{X}^{-1}(y)) \\
            &= y
        \end{align*}
    \end{itemize}
    \begin{equation*}
           f_Y (y)=
            \begin{cases}
             0, & \text{if}\  y < 0 \\
             y, & \text{if}\  0 \leq y < 1 \\
             1, & \text{if}\  y \geq 1
            \end{cases}
        \end{equation*}
If we take any random variable $X$ \& apply $F_X(.)$ as a function of $X$, then resulting random variable is a uniform random variable.

Suppose we start with an uniform random variable $Y$ and consider a function of $Y$ which is $X= F_{X}^{-1}(Y)$, then X has a CDF $F_X(.)$.

%\fbox{In, MATLAB, there is an inbuilt function }

\section{Expectation of a random variable}
It is also called mean (or average).

For a discrete random variable $X$, let $S$ be the range of $X$, which is either finite or countably infinte.

$$ E(X)= \sum_{x_i \in S}x_i P_X(x_i)$$

where $P_X$ is the PMF of random variable $X$.

The expectation, is based on the weighted average, where weights are the probabilities of the random variable taking a particular value unlike the general notion of average.

For a continuous random variable $X$,
$$ E(X)= \int_{-\infty}^{\infty}x f_X(x)dx$$
The integral can be thought of as the limit of a summation. Hence,
$$ E(X)= \lim_{\Delta x_i \to 0} \sum_{i= -\infty}^{\infty}x_i f_X(x_i) \Delta x_i$$

\subsection{Expectation of a function of a random variable}

$ Y= g(X)$, $X$ \& $Y$ are discrete.
\begin{align*}
    E(Y)= \sum_{y_i}y_i P_Y(y_i)
\end{align*}
We can express $E(Y)$ in terms of PMF of $X$, $P_X$,
\begin{align*}
    E(Y)&= \sum_{y_i}y_i P_Y(y_i) \\
    &= \sum_{y_i}y_i \sum_{x_{i,j}:g(x_{i,j})= y_i} P_X(x_{i,j}) \\
    &= \sum_{y_i} \sum_{x_{i,j}:g(x_{i,j})= y_i} y_i P_X(x_{i,j}) \\
    &= \sum_{y_i} \sum_{x_{i,j}:g(x_{i,j})= y_i} g(x_{i,j}) P_X(x_{i,j}) \\
    &= \sum_{x_j} g(x_j) P_X(x_j)\\
    \Rightarrow E(Y) &= \sum_{y_i}y_i P_Y(y_i) \\
    &= \sum_{x_i} g(x_i) P_X(x_i)
\end{align*}
For continuous random variable,
\begin{align*}
    E(Y) &= \int_{-\infty}^{\infty}y f_Y(y)dy \\
    &= \int_{-\infty}^{\infty} g(x)f_X(x)dx
\end{align*}

\subsection{Variance of a random variable}
It is defined as,
$$ E[g(X)] \qquad g(X)= (X - E(X))^2$$

$E(X)$ indicates the average value of a random variable. So there is a variation in the values the random variable takes around mean, now we would like to measure this variation and hence the variance comes into play.

$(X- E(X))$ measures variation from the mean, and we square it so that the positive and negative values don't cancel each other.
%23/6

If $X$ is discrete:

$E(X)= \mu$

$E(X)= E(X-E(X)^2) = \sum_{x_i} (x_i - \mu )^2 P_X (x_i)$

If $X$ is continuous:

$E(X)= \mu$

$E(X)= E(X-E(X)^2)= \int_{-\infty}^{\infty}(x- \mu)^2 f_X(x)dx$

\begin{itemize}
    \item $ Var(X) \geq 0$

    $Var(X)=0$ iff $X=E(X)$ with probability 1.
\end{itemize}

\subsection{Examples of discrete random variables}
\begin{enumerate}
    \item Bernoulli random variable:

    Experiment: Coin toss experiment.
    $$X:S = \{ H,T\} \qquad H \to 1 \qquad T to 0$$
    PMF of $X$, $P_X(1)= p,\; P_X(0)=1-p$

    $E(X)= 0(1-p)+1(p)=p$.

    $Var(X)= (0-p)^2(1-p)+ (1-p)^2 p = p(1-p)$

    \item Binomial random variable:

    Experiment: Toss a biased coin $n$ times. $X$ is defined as the number of heads in that string.
    $$ S= \{ HH\cdots H, TH\cdots H, \cdots \}$$
    $X:S \to R$

    Range of $X$: $\{ 0,1, cdots, n\}$

    PMF of $X$: $P_X (k)= {n \choose k} p^k (1-p)^{n-k}$
    \begin{align*}
        E(X)&= \sum_{k=0}^{n}k {n \choose k} p^k (1-p)^{n-k}\\
        &= \sum_{k=1}^{n} \frac{n!}{(k-1)!(n-k)!} p^k (1-p)^{n-k} \\
        &= np \sum_{k=0}^{n-1} \frac{(n-1)!}{k!(n-1-k)!} p^k (1-p)^{n-1-k} \\
        &= np
    \end{align*}
    $Var(X)= np(1-p)$

    \item Geometric random variable:

    Experiment: Keep tossing a biased coin till we get a head.
    $$ S= \{ H, TH, TTH, \cdots \} \qquad X:S \to R$$
    $P(H)= p, \; P(T)= 1-p$

    $P_X(k)= p(1-p)^{k-1}$

    $$E(X)= \sum_{k=1}^{\infty} k p (1-p)^{k-1}= \frac{1}{p}$$

    \begin{align*}
        Var(X)&= E((X-E(X)^2)\\
        &= E(X^2 -2X E(X) + (E(X))^2)\\
        &= E(X^2)+E(-2XE(X))+E((E(X))^2\\
        &= E(X^2)-2E(X)E(X)+(E(X))^2 \\
        &= E(X^2)-(E(X))^2
    \end{align*}
    We know, $Var(X) \geq 0 \Rightarrow E(X^2) \geq (E(X))^2$

    Now, $Var(x)$ for a geometric random variable:
    \begin{align*}
        Var(X)&= E(X^2)-E(X)^2 \\
        &=  \sum_{k=1}^{\infty} k^2 p (1-p)^{k-1} - \left( \frac{1}{p}\right) ^2 \\
        &= \frac{2-p}{p^2} - \frac{1}{p^2} \\
        &= \frac{1-p}{p^2}
    \end{align*}
    \item Poisson random variable:

    Experiment: When we have to count rare events within a time frame.
    $$ X= \{ 0,1,2, \cdots\}$$
    PMF of Poisson RV:
    $$ P_X(k)= \frac{\lambda ^k}{k!} e^{-\lambda}$$
    where $\lambda$ is a parameter and is the average value.

    $Var(X)= \lambda$

    \begin{align*}
        \sum_{k=0}^{\infty} P_X(k) &= e^{-\lambda } \sum_{k=0}^{\infty} \frac{\lambda ^k}{k!} \\
        &= e^{-\lambda} e^{\lambda} \\
        &= 1
    \end{align*}
\end{enumerate}

\subsection{Examples of continuous random variables}

\begin{enumerate}
%25/6
    \item Uniform random variable:

    Experiment: Measuring voltages, quantization error (original signal - quantized signal) is modelled as a uniform random variable.
    \begin{equation*}
            f_X (x)=
            \begin{cases}
            \frac{1}{b-a}, & \text{if}\ a \leq x \leq b \\
             0, & \text{otherwise}
            \end{cases}
       \end{equation*}

    $E(X)= \frac{a+b}{2}$

    $ Var(X)= \frac{(b-a)^2}{12}$
    \item Exponential random variable:

    Experiment: Used to model completion time of a process.

    \begin{equation*}
            f_X (x)=
            \begin{cases}
            \lambda e^{-\lambda x}, & \text{if}\ x \geq 0\\
             0, & \text{otherwise}
            \end{cases}
       \end{equation*}

       $$E(X)=\int_{0}^{\infty} x \lambda e^{-\lambda x}dx = \frac{1}{x}$$
       $$ Var(X)= E(X^2)- E(X)^2=\frac{1}{\lambda ^2}$$

       \item Gaussian random variable:

       Experiment: Noise is generally modelled as a Gaussian random variable. We shall discuss this in detail while doing the central limit theorem.

       $$ f_X(x)= \frac{1}{\sqrt{2\pi \sigma^2}} e^{-\frac{(x- \mu)^2}{2 \sigma^2}}$$

       Mean of $X$: $\mu$

       $Var(x)= {\sigma}^2$
   \end{enumerate}

%26/6

\section{Random vectors and joint CDF}
Consider 2 individual random variables $X_1$ $X_2$, with $(S,F,P)$ as the probability space.
$X_1:S \to \mathbb{R} \qquad X_2:S \to \mathbb{R}$

$$ (X_1,X_2) :S \to {\mathbb{R}}^2$$

Then $(X_1,X_2) $ form a random vector and the joint CDF of $X_1$ and $X_2$:
$$ F_{X_1,X_2}(x_1,x_2)= P(X_1 \leq x_1, X_2 \leq x_2)$$

$$ X_1 \leq x_1 = \{ w: X_1(w) \leq x_1\}= A_1 \in F$$

$$ X_2 \leq x_2 = \{ w: X_2(w) \leq x_2\}= A_2 \in F$$

$$(X_1 \leq x_1, X_2 \leq x_2)= \{ \omega : X_1(\omega) \leq x_1 \, \& \, X_2(\omega) \leq x_2\} = A_1 \cap A_2 \in F$$

$$ P(X_1 \leq x_1 , X_2 \leq x_2) \; \forall \; (x_1 , x_2) \in {\mathbb{R}}^2$$

By definition, $ F_{X_1 , X_2}(x_1 , x_2) =  P(X_1 \leq x_1 , X_2 \leq x_2)$.

Then we can expand this definition till $n$, i.e.

$X_1,X_2, \cdots , X_n \qquad X_iS \to \mathbb{R}$

We can say that the joint CDF of $n$ random variables:
$$ F_{X_1,\cdots ,X_n}(x_1,\cdots ,x_n)= P(X_1 \leq x_1, X_2 \leq x_2, \cdots X_n \leq x_n)$$

\subsection{Properties of joint CDF}

\begin{enumerate}
    \item $F_{X,Y}(.,.) $ is monotonically non decreasing in x,y.

    We can compare them for $\{ \{ x_1 = x_2, y_1 \leq y_2 \}, \{x_1 \leq x_2, y_1 = y_2 \} , \{ x_1 < x_2 , y_1 < y_2\}\}$ but not for $\{ x_1 > x_2 , y_1 < y_2\}$ etc.
    \item $$ \lim_{x \to -\infty, y \to -\infty} F_{X,Y} (x,y)=0$$
    $$ \lim_{x \to -\infty} F_{X,Y} (x,y)=0\qquad \lim_{y \to -\infty} F_{X,Y} (x,y)=0$$

    \item $$ \lim_{x \to \infty, y \to \infty} F_{X,Y} (x,y)=1$$
    $$ \lim_{x \to \infty} F_{X,Y} (x,y)= F_Y (y)\qquad \lim_{y \to \infty} F_{X,Y} (x,y)= F_X(x)$$

    \item $F_{X,Y}(.,.)$ is right continuous individually in x \& y.
    $$ \lim_{x \to x_0^{+}} F_{X,Y} (x,y)=F_X(x_0,y)$$
    $$ \lim_{(x,y) \to (x_0^{+},y_0^{+})} F_{X,Y} (x,y)=F_X(x_0,y_0)$$

\end{enumerate}

Different cases possible for $X$ \& $Y$:
\begin{itemize}
    \item $X$ \& $Y$ both discrete.
    \item $X$ \& $Y$ both continuous.
    \item $X$ is discrete \& $Y$ is continuous.
\end{itemize}

\begin{enumerate}
    \item $X$ \& $Y$ both discrete.

    Joint PMF:
    $$ P_{X,Y}(x_i,y_i)= P(X= x_i,Y= y_i)$$
    Expressing joint CDF in terms of the joint PMF,
    $$ F_{X,Y}(x,y) = \sum_{x_i \leq x} \sum_{y_i \leq y} P_{X,Y}(x_i,y_i)$$

    $X\to P_X \qquad Y \to P_Y \qquad (X,Y) \to P_{X,Y}$

    We can form a relation between $P_{X,Y}$ and $P_X$,
    $$ P_X(x_i) \sum_{y_i} P_{X,Y}(x_i,y_i)$$
    $$ P(X= x_i) \sum_{y_i} P_{X,Y}(X=x_i,Y=y_i)$$

    In the context of the joint PMF, $P_X$ \& $P_Y$ are also called as marginal PMF's.
    And the process of summing over the range of the other random variable to get the marginal PMF, is known as marginalization.
\end{enumerate}

\subsection{Conditional PMF}

Let us recall conditional probabilities (in the probability space $(S,F,P)$),
$$ P(A/B)= \frac{P(A \cap B)}{P(B)} \qquad P(B)>0$$
In the context of random variables, how do we define conditional probabilities?

Given an event $B \in F$, $P(B)>0$. Conditional CDF conditioned on the event $B$ would be:
$$ F_{X/B}(x/B)= \frac{P( (X \leq x)\cap B)}{P(B)}$$
$X \leq x= \{ w: X(w) \leq x\}= A \in F$

If $B$ is of the form $\{ Y \leq y \}$
$$ F_{X/B}(x/B)= \frac{P((X \leq x) \cap B)}{P(B)}$$
$$ F_{X/\{ Y \leq y \}}(x / \{ Y \leq y \})= \frac{P(\{ X \leq x \} \cap \{ Y \leq y \})}{P(\{ Y \leq y \})}= \frac{F_{X,Y}(x,y)}{F_Y (y)}$$
We shall denote this, conditional PMF, by:
$$ F_{X/Y}(x/y)= \frac{F_{X,Y}(x,y)}{F_Y (y)}$$

If both $X$ \& $Y$ are discrete, for defining conditional PMF, we will not condition on $\{ Y \leq y_i \} $ but instead condition on $\{ Y = y_i \}$

$$ P_{X/\{ Y = y_i \}}(x_i/\{ Y = y_i \})= \frac{P((X= x_i) \cap (Y=y_i))}{P(Y=y_i)}$$
Conditional PMF:
$$ P_{X/Y} (x_i /y_i)= \frac{P_{X,Y}(x_i,y_i)}{P_Y(y_i)}= \frac{\text{Joint PMF}}{\text{Marginal PMF}}$$
\\

% Example:

%28/6

If $X$ \& $Y$ are both continuous random variables, we can define joint PDF of $X$ \& $Y$ as
$$ f_{X,Y}(x,y)= \frac{\partial^2 F_{X,Y}(x,y)}{\partial x \partial y}$$

$$ F_{X,Y}(x,y) =\int_{-\infty}^{x} \int_{-\infty}^{y} f_{X,Y}(x,y) dx dy$$

Marginal PDF's of $X$ \& $Y$ can be expressed in terms of joint PDF's as:
$$ f_X (x)= \int_{-\infty}^{\infty} f_{X,Y}(x,y) dy$$
$$ f_Y (y)= \int_{-\infty}^{\infty} f_{X,Y}(x,y) dx$$

A joint PDF is said to be valid if,
$$ f_{X,Y}(x,y) \geq 0 \quad \& \quad \int_{-\infty}^{x} \int_{-\infty}^{y} f_{X,Y}(x,y)\partial x \partial y =1$$
\\

When both $X$ \& $Y$ are continuous,
$P(X=x )=0,\; P(Y=y)=0 $ i.e. probability that the random variable takes a single value, finite or countably infinite set of values is zero.

Conditional PDF can be expressed in terms of joint PDF's and marginal PDF's,
$$ f_{X/Y}(x/y)=\frac{f_{X,Y}(x,y)}{f_Y (y)}$$ \\

$$ f_{X/Y}(x/y)= \lim_{\Delta x \to 0, \Delta y \to 0} \frac{P(x<X \leq x+ \Delta x / y<Y \leq y+ \Delta y)}{\Delta x}$$
$$ f_{X/B}(x/B)= \lim_{\Delta x \to 0} \frac{P(x<X \leq x+ \Delta x /B)}{\Delta x}$$

If $B= \{ y<Y \leq y+ \Delta y \}$, and let $\Delta y \to 0$, $P(B) > 0$.

$$ \Rightarrow f_{X/Y}(x/y)= \lim_{\Delta x \to 0, \Delta y \to 0} \frac{P(x<X \leq x+ \Delta x , y<Y \leq y+ \Delta y)}{P(y<Y \leq y+ \Delta y)\Delta x} $$
$$= \lim_{\Delta x \to 0, \Delta y \to 0} \frac{}{} = $$

\subsection{Bayes rule for discrete random variable}
$$ P_{Y/X}(y_i /x_i) = \frac{P_{X/Y} (x_i/y_i) P_Y (y_i)}{P_X (x_i)}$$
$$ =\frac{P_{X/Y} (x_i/y_i) P_Y (y_i)}{\sum_{y_i}P_{X/Y}(x_i/y_i) P_Y (y_i)}$$

\subsection{Bayes rule for continuous random variable}
$$ P(x < X \leq x+ \Delta x / y<Y \leq y+ \Delta y)= \frac{P(x < X \leq x+ \Delta x , y<Y \leq y+ \Delta y)P(x < X \leq x+ \Delta x)}{P(y<Y \leq y+ \Delta y)}$$
$$ f_{X/Y}(x/y) \Delta x =\frac{f_{Y/X} (y/x) \Delta y f_X (x) \Delta x}{f_Y (y) \Delta y} $$

$$ \Rightarrow f_{X/Y}(x/y)  =\frac{f_{Y/X} (y/x)  f_X (x) }{f_Y (y)}$$

%Eg: $X$ \& $Y$ being continuous random variables
%Consider a joint PDF of random variable $X$ \& $Y$ which is constant on set $S$ as shown in figure %unable to draw figure yet

\subsection{Bayes rule when X is discrete and Y is continuous}
If $X$ is discrete and $Y$ is continuous, then we can't talk about either joint PMF or PDF. We can talk about the joint CDF,
$$ P_{X/Y} (x/y) = \frac{f_{Y/X} (y/x) P_X (x)}{f_Y(y)}$$

\begin{align*}
    P_{X/Y}(x/y) &= \lim_{\Delta y \to 0} P(X=x/y<Y \leq y + \Delta y) \\
    &= \lim_{\Delta y \to 0} \frac{P(X=x,y<Y \leq y + \Delta y)}{P(y<Y \leq y + \Delta y)} \\
    &= \lim_{\Delta y \to 0} \frac{P(y< Y \leq y + \Delta y / X= x)P(X=x)}{P(y<Y \leq y+ \Delta y)} \\
    &= \lim_{\Delta y \to 0}\frac{f_{Y/X} (y/x) \Delta y P(X=x)}{f_Y(y) \Delta y} \\
    &=\frac{f_{Y/X} (y/x) P_X (x)}{f_Y(y)}
\end{align*}

\section{Independent random variables}

\begin{itemize}
    \item Discrete case: Two random variables are said to be indepedent if
    $$ P_{X,Y}(x,y)= P_X (x) P_Y(y)$$
    i.e. joint PMF is the product of the marginal PMF's.

    OR,

    $$ P_{X/Y}(x/y) = P_X (x)$$
    \item Continuous case: Two random variables are sid to be indepedent if
    $$ f_{X,Y}(x,y)= f_X (x) f_Y (y)$$
    i.e. joint PDF is the product of the marginal PDF's.

    OR,

    $$ f_{X/Y}(x/y) = f_X (x)$$
\end{itemize}

If both $X$ \& $Y$ are discrete and indepedent, show that $F_{X,Y}(x,y)= F_X (x) F_Y (y)$.
\begin{proof}
    For the discrete case,
    \begin{align*}
        F_{X,Y}(x,y)&= P(X \leq x, Y \leq y) \\
        &= \sum_{x_i \leq x} \sum_{y_i \leq y} P_{X,Y}(x_i,y_i) \\
        &= \sum_{x_i \leq x} \sum_{y_i \leq y} P_{X}(x_i) P_Y(y_i) \\
        &= \left[ \sum_{x_i \leq x} P_{X}(x_i)\right] \left[ \sum_{y_i \leq y} P_{y}(y_i)\right] \\
        &= F_X (x) F_Y (y)
    \end{align*}
    Similarly, using the same steps it can be proved for the continuous case too.
\end{proof}
%30/6

$P(X \in A , Y \in B)= P(X \in A) P(Y \in B) \Rightarrow F_{X,Y}(x,y)= F_X (x) F_Y(y)$ by choosing $A= (-\infty,x]$ and $B=(-\infty,y]$

\begin{lemma}
    Let $X$ and $Y$ be indepedent random variables and g and h be measurable functions. Then $g(X)$ \& $h(Y)$ are also indepedent random variables.
\end{lemma}

\end{document}


\section{Methods}

In this section we first build up intuition and theory for vanilla (standard) VAEs before extending the math to $\beta$-VAE.

\subsection{Variational auto-encoders}

We have data $\{\vx_i\}_{i=1}^N$ and we wish to find some low-dimensional (latent) representation for each $i$: $\{\vz_i\}_{i=1}^N$. We assume that the data are in fact generated stochastically conditioned on the latent representations, such that:
\begin{align*}
    \vx|\vz \sim p(\vx|\vz)
\end{align*}

$p$ is commonly either a Gaussian distribution (in the case of normally distribuetd data) or a Bernoulli distribution (in the case of pixels). The goal of a VAE is to learn a good approximation to this distribution, $\ptheta$, with parameters $\theta$, to model our data $\vx$, as well as the distribution $\ptheta(\vz|\vx)$ to model the hypothesized latent variables $\vz$ conditioned on the observed data. We can arrange related $\vx$ and $\vz$ using Bayes' rule:
\begin{equation*}
    \ptheta(\vx|\vz) = \frac{\ptheta(\vz|\vx)\ptheta(\vx)}{\ptheta(\vz)}
\end{equation*}
Under this framework, we want to maximize the log-probability of data generated according to the process: (1) sample a point $\vz\sim\ptheta(\vz)$, then sample $\vx\sim\ptheta(\vx|\vz)$:
\begin{align*}
    \ell &=\sum_{i=1}^N\log\ptheta(\vx_i) \\
    \intertext{Marginalizing over all values of $\vx$,}
    \ell&=\sum_{i=1}^N\log\int\ptheta(\vx_i|\vz)\ptheta(\vz)d\vz
\end{align*}

We now introduce an auxiliary distribution, $\qphi$. This will allow us to massage this expression into one involving a KL-divergence and will ultimately be the distribution that we learn using the model.
\begin{align*}
    \ell &= \sum_{i=1}^N\log\int\ptheta(\vx_i|\vz_i)\ptheta(\vz_i)\frac{\qphi(\vz_i)}{\qphi(\vz_i)}d\vz_i \\
    &= \sum_{i=1}^N\log\int\frac{\ptheta(\vx_i,\vz_i)}{\qphi(\vz_i)}\qphi(\vz_i)d\vz_i \\
    &= \sum_{i=1}^N\log\E\left[\frac{\ptheta(\vx_i,\vz_i)}{\qphi(\vz_i)}\right] \\
    \intertext{Using Jensen's inequality, we get the evidence lower bound (ELBO)}
    &\geq \sum_{i=1}^N\E_{\vz\sim\qphi}\log\left[\frac{\ptheta(\vx_i,\vz_i)}{\qphi(\vz_i)}\right] \\
    \ell &= \sum_{i=1}^N(\E_{\vz\sim\qphi}[\log\ptheta(\vx_i, \vz_i)] - \E_{\vz\sim\qphi}[\log\qphi(\vz)])
\end{align*}
Recall that the definition of the KL divergence between $\qphi$ and $\ptheta$ is:
\begin{align*}
    \kl(\qphi(\vz)||\ptheta(\vz|\vx)) &= \Eq\left[ \log\frac{\qphi(\vz)}{\ptheta(\vz|\vx)} \right] \\
    \intertext{Since $\ptheta(\vz|\vx) = \frac{\ptheta(\vz,\vx)}{\ptheta(\vx)}$ (by definition of conditional probability),}
    \kl(\qphi(\vz)||\ptheta(\vz|\vx)) &= -\left( \Eq[\log\ptheta(\vz,\vx)] - \Eq[\log\qphi(\vz)]\right) + \log\ptheta(\vx)
\end{align*}
Plugging this back into our log-likelihood,
\begin{align*}
    \ell &= \sum_{i=1}^N\left( \log(\ptheta(\vx_i)) - \kl(\qphi(\vz_i)||\ptheta(\vz_i|\vx_i)) \right)
\end{align*}
The first term is maximized when the reconstruction $\vx$ is faithful to the original data, whereas the second term is minimized when $\ptheta$ is close to $\qphi$.

\subsection{Choice of $\qphi$}

In theory, we could choose an arbitrarily expressive family of distributions $\qphi$ to model the latent variable $\vz$. In practice, we make the simplifying {\bf mean-field assumption}, which states that the dimensions of $\qphi$ are independent:

\begin{align*}
    \qphi(\vz|\vx) = \prod_{j=1}^d\qphi(\vz_j|\vx_j)
\end{align*}

In our case, we operationalize this by saying that $\qphi$ is an isotropic Gaussian (diagonal covariance) \cite{blei2011}. Although this assumption reduces the expressiveness of the variational family $\qphi$, we gain significantly in terms of computational efficiency when it comes time to learn the parameters of the distribution.

At this point, we are in effect trying to learn a unique distribution $\qphi(\vx|\vz)$ for each $\vx$. If we have $N$ observations and a latent space of dimension $d$, then if the latent distribution is Gaussian, we would need to learn a mean and variance for each dimension of each latent variable, or $2\times N \times d$. In other words, $N\gg p$ (where $p$ is the number of parameters in the model), which both dramatically increases the variance of the parameter estimates (they are highly dependent on the particular data sample at hand) and increases the cost of fitting the model. To get around this, we perform {\it amortised} variational inference: instead of directly estimating $\mu_i$ and $\Sigma_i$ for each $i = 1, \dots, N$, we assume that each $\mu_i, \Sigma_i$ can be approximated by a function $g_\phi(\vx)$, typically a neural network \cite{jaanTutorial}. We refer to this function interchangeably as the {\bf encoder} or {\bf inference network}. This is reresented by the left rectangle in figure \ref{fig:encoder-decoder} and is denoted $g_\phi$ here. Because the weights $\phi$ are shared across all observations, we say that the cost of learning $\phi$ is amortised across the entire dataset (hence, ``amortised variational inference''). Symmetrically, the conditional posterior distribution $\ptheta(\vx|\vz)$ is implemented as a neural network that is typically the mirror image of the encoder, which is called the {\bf decoder} or {\bf generative network} and is denoted $f_\theta$ here.

\begin{figure}[h]
    \includegraphics[width=\linewidth]{encoder-decoder.png}
    \caption{Simplified schematic of the encoder and decoder, where the left and right rectangles labelled $q(z|x)$ and $p(x|z)$ refer to the inferential and generative networks, respectively. First, $\vz$ is generated from $\vx$ using the encoder and then $\vx$ is reconstructed using $\vz$ \cite{jaanTutorial}. Note: the notation in this figure is opposite to what we use in this paper: $\theta$ and $\phi$ have been flipped such that $\theta$ parameterizes $q$ and $\phi$ parameterizes $p$. Otherwise, the setup is the same to what we have been discussing.}
    \label{fig:encoder-decoder}
\end{figure}

\subsection{The reparameterization trick}

A further advantage of variational inference is that it allows us to convert this problem from one of sampling from a posterior distribution -- which can require the use of computationally intensive or mathematically complex methods such as expectation-maximization -- to a simple non-convex optimization problem. That is, all the terms in the ELBO loss function can be expressed in terms of $\phi$ and $\theta$, meaning that if we can differentiate through all the operations involved, then this problem can be solved with gradient descent. To revisit, the encoder and decoder form a single algorithm:
\begin{enumerate}
    \item Given $\vx$, compute $\vmu, \Sigma = g_\phi(\vx)$
    \item Sample $\vz\sim\N(\vmu,\Sigma)$
    \item Compute reconstruction $\hat{x} = f_\theta(\vz)$
\end{enumerate}

The one operation that does not allow for easy differentiation is the sampling step (2). In order to get around this, we use the ``reparameterization trick''. Recall that a $d$-dimensional isotropic Gaussian random variable can be converted into a sample from the $d$-dimensional multivariate normal distribution:
\[
    \vec{\epsilon} = (\vz - \vmu) \odot \frac{1}{\vsig} + \vmu
\]
Where $\vec{\epsilon}$ is a standard normal random variable (not to be confused with $\vz$, which is our latent variable with mean $\mu$ and covariance $\Sigma$) and $\vsig$ is the $d$-dimensional vector of standard deviations for the components of $\vz$. $\odot$ represents element-wise multiplication. Using simple algebra, we can rearrange this:
\[
    \vz = (\vec{\epsilon} \odot \vsig) + \vmu
\]
This means that sampling $\vz\sim\N(\vmu,\Sigma)$ is equivalent to sampling $\vec{\epsilon}\sim\N(0,I)$ and then transforming it into $\vz$ as above. The gradient operation is well-defined for sampling from the standard normal, so we can make the entire VAE algorithm differentiable by adding this reparameterization step:

\begin{enumerate}
    \item Given $\vx$, compute $\vmu, \Sigma = g_\phi(\vx)$. Let $\vsig = \sqrt{\operatorname{diag}(\Sigma)}$
    \item Sample $\vec{\epsilon}\sim\N(0, I)$
    \item Let $\vz = \vec{\epsilon}\odot\vsig + \vmu$
    \item Compute reconstruction $\hat{x} = f_\theta(\vz)$
\end{enumerate}

\subsection{$\beta$-VAE}

In the original paper, the authors provide an independent derivation of the $\beta$-VAE loss using constrained optimization \cite{higgins2016beta}. Given data $\vx$ distributed according to the data distribution $\D$, the goal is to jointly learn generative parameters $\theta$ and latent parameters $\phi$ for the distributions $\ptheta$ and $\qphi$, respectively. We want to find $\theta$ and $\phi$ such that the posterior log-probability $\ptheta(\vx|\vz)$ where $\vz\sim\qphi$ is maximized:
\begin{align*}
    \theta^*,\phi^* = \underset{\theta,\phi}{\argmax}\E_{\vx\sim\D}\left[ \Eq\left[ \log\ptheta\left( \vx|\vz \right) \right] \right]
\end{align*}
Subject to constraint
\begin{align*}
    \kl\left( \qphi(\vz|\vx)||p(\vz) \right) < \delta
\end{align*}
Where $\delta$ is a small positive constant (since KL divergence is nonnegative). The constraint is imposed to narrow the "information bottleneck" by encouraging the latent posterior $\qphi(\vz|\vx)$ to be close to a simple prior $p(\vz)$, which in this case is a standard gaussian, $\N(0, I)$. The idea is that we want to make $\qphi$ less expressive, which in turn forces it to be as efficient as possible in which latent factors are represented. Hopefully, the efficiency is maximized when the latent factors line up with the original factors that generated the data.

We can rewrite this constrained optimization problem as a Lagrangian subject to the KKT condition, with Lagrange multiplier $\beta$ and Lagrangian objective $\F$:

\begin{align*}
    \F(\theta, \phi, \beta; \vx, \vz) &= \Eq\left[ \log\ptheta(\vx|\vz) \right] - \beta\left( \kl(\qphi(\vz|\vx)||p(\vz)) - \delta \right) \\
    &= \Eq\left[ \log\ptheta(\vx|\vz) \right] - \beta\left( \kl(\qphi(\vz|\vx)||p(\vz))\right) + \beta\delta \\
    &\geq \Eq\left[ \log\ptheta(\vx|\vz) \right] - \beta\left( \kl(\qphi(\vz|\vx)||p(\vz))\right)
\end{align*}
Immediately, we can recognize that this is nearly identical to the vanilla VAE loss, except for the hyperparameter $\beta$. When $\beta=1$, this is equivalent to the vanilla VAE loss. When $\beta = 0$, the model becomes similar to a non-variational auto-encoder, since the only objective is to faithfully reconstruct the data. As $\beta$ increases, the bottleneck in the latent space becomes narrower, promoting a sparser latent representation (i.e. a latent space that more closely resembles a standard Gaussian).

\subsubsection{Measuring disentanglement}

The authors of the original $\beta$-VAE paper go a step further and propose a ``disentanglement metric'' to quantify how well the model's latent representations align with the hypothesized generative factors for the data \cite{higgins2016beta}.

Formally, we assume the data $\vx$ are drawn from a dataset $\D=\{X, V\}$, where $X$ is the data matrix, and $V$ is a matrix of hypothesized {\it generative factors} for each data point. For instance, a factor for a dataset of faces might be {\it skin tone} or {\it glasses}. An image is created using a simulator: $\vx\sim\Sim(\vv)$. Then,
\begin{enumerate}
    \item Uniformly sample factor $y$ from the set of $K$ possible generative factors
    \item Repeat $L$ times:
    \begin{enumerate}
        \item Sample two sets of generative latent representations, $\vv_{1,l}$ and $\vv_{2,l}$ such that both $\vv_{1,l}$ and $\vv_{2,l}$ have the same value for factor $y$ and are free to differ in all other generative factors
        \item Simulate an image $x_{1,l\sim\Sim(v_{1,l})}$ and compute the latent representation $\vz_{1,l}\sim q(\vz|\vx_{1,l})$ using the encoder $g_\phi(\vx_{1,l})$. Repeat for $v_{1,l}$
        \item Compute a difference vector $z^l_{\diff}=|z_{1,l}-z_{2,l}|_1$
    \end{enumerate}
    \item Given all $z^l_{\diff}$ computed as above, calculate the average difference vector $\bar{z}_{\diff} = \frac{1}{L}\sum_{l=1}^Lz^l_{\diff}$
    \item Use $\bar{z}_{\diff}$ as the input for a linear classifier (i.e. logistic regression), where the goal is to predict the factor $y$ that was kept constant between all pairs of latent factors
\end{enumerate}

This algorithm can be seen in figure \ref{fig:disentanglement-metric}. The intuition behind this metric is that when a pair of data points $(x_{1,l}, x_{2,l})$ has the same value for some generative factor, then a well-disentangled latent representation will be such that a small subset of the latent dimensions will correspond to that factor and will therefore have small difference, averaged over many such pairs. Thus, a linear classifier trained on supervised pairs of $(\bar{z}_{\diff}, y)$ will be able to map the averaged difference vectors to the corresponding generative factor that was held constant across the pairs.

\begin{figure}
    \includegraphics[width=\linewidth]{disentanglement-metric.png}
    \caption{A schematic of the proposed disentanglement metric from \cite{higgins2016beta}. Each vector $\vz^l_{\diff}$ in the process is generated from pairs of latent representations $z_{1,l}, z_{2,l}$ with the exact same value for a chose generative factor $y$. All of the $L$ pairs in batch $b$ may differ in terms of the value of the generative factor, but the choice of $y$ is constant between pairs. The linear classifier, $p(y|\vz^b_{\diff})$ is a multivariate classification algorithm whose goal is to predict which generative factor was used to generate the average difference vector $\vz_{\diff}^b$ (in the text, we refer to this average difference vector as $\bar{z}_{\diff}$).}
    \label{fig:disentanglement-metric}
\end{figure}

\subsection{Implementation}

Depending on the type and complexity of the data, various architectures for the inference and generative networks are used. In the simplest case, each is a feed-forward neural network, which is common for either numeric data or small-sized image data (e.g. MNIST, which is $28\times28\times1$). For larger image data, such as the CelebA dataset ($64\times64\times3$), convolutional neural networks (CNNs) are often used, with the structure of the encoder neural network mirroring the structure of the decoder neural network. The weights of the encoder and decoder are found using well-known gradient descent algorithms (e.g. the authors use Adam, RMSProp, and Adagrad for different experiments).


\section{Numerical Methods}

\begin{frame}{Approximation for stochastic differential equations}
	\begin{equation*}
	dX=f(X)dt+{\color{red}g(X)dW}
	\end{equation*}
	\begin{center}
	$f,g$ are functions of X.
	\end{center}
	\begin{itemize}
		\item It's difficult to find an explicit solution.
		\item We can approximate the solution.
		\item Simulations!
	\end{itemize}
\end{frame}

\begin{frame}{Numerical methods}
Integral form of a SDE:
$$X(t)=X_0+\int_{0}^{t}f(X(s))ds+{\color{red}\int_{0}^{t}g(X(s))dW(s)}$$

X(t) is a random variable for each value of t. To apply numerical methods:
	\begin{itemize}
		\item Discretized time steps
		%\item X(t) will be the limit as our stepsize goes to zero.
	\end{itemize}
\end{frame}

%\begin{frame}{It\^{o}'s Formulas}
\begin{frame}{Ito's Formulas}
  	\begin{itemize}
   		\item First version: $$f(B(b))-f(B(a))=\int_{a}^{b}{\frac{\partial f}{\partial B} 				dB}+\int_{a}^{b}{\frac{1}{2} \frac{\partial^2 f}{\partial B^2} dt} $$\\
    		\item Second version: $$f(b,B(b))-f(a,B(a))=\int_{a}^{b}{\frac{\partial f}{\partial B} 			dB}+\int_{a}^{b}{\left(\frac{\partial f}{\partial s}+\frac{1}{2}\frac{\partial^2 f}
    		{\partial B^2}\right) ds}$$\\
    		\item Third version: The Stochastic Chain Rule
    		$$\theta (t,x_t)=\theta(a,x_a)+\int_{a}^{t}\frac{\partial \theta}{\partial s}ds+
    		\int_{a}^{t}f\frac{\partial \theta}{\partial x}dW+
  		\int_{a}^{t}g\frac{\partial \theta}{\partial x}+f^2 \frac{1}{2} \frac{\partial ^2\theta}
  		{\partial x^2}ds$$\\
  	\end{itemize}
\end{frame}

\begin{frame}{Euler-Maruyama method}
Let $\tau_j=j\Delta t$. In the interval [0,L] we have the EM method:\bigskip\\
$$X_j=X_{j-1}+f(X_{j-1})\Delta t+g(X_{j-1})(W(\tau_j)-W(\tau_{j-1}))$$
 which is an approximation for the equation
$$X(\tau_j)=X(\tau_{j-1})+\int_{\tau_{j-1}}^{\tau_{j}}f(X(s))ds+\int_
{\tau_{j-1}}^{\tau_{j}}g(X(s))dW(s)$$
%We have Euler's method in the deterministic case ($g\equiv 0$)
\end{frame}

\begin{frame}
\begin{block}{Strong Convergence}
We say that a method has strong order of convergence equal to $\gamma$ if there exists a constant C such that:
$$E|X_n-X(\tau)|\leq C \Delta t^\gamma$$
for any fixed $\tau=n \Delta t \in [0,T]$ and $\Delta t$ sufficiently small.
\end{block}
\bigskip
The EM method has strong order of convergence of $\gamma=\frac{1}{2}$\cite{doi:10.1137/S0036144500378302}.
\end{frame}

\begin{frame}{Milstein's method}
The Milstein method raises the strong order of convergence to 1\cite{doi:10.1137/S0036144500378302}:
	\begin{equation*}
	\begin{split}
	X_{j} & =X_{j-1}+ f(X_{j-1}) \; \Delta t + g(X_{j-1})(W(\tau_j)-W(\tau_{j-1}))\\
	  &\quad +\frac{1}{2}g(X_{j-1})g'(X_{j-1})((W(\tau_j)-W(\tau_{j-1}))^2-\Delta t)
	\end{split}
	\end{equation*}
\end{frame}

\begin{frame}{Example}
If we consider the differential equation:
\begin{equation*}
  dX=\alpha X dt+ \beta X dW,
\end{equation*}
Where $\alpha, \beta$ are constants. The theoretical solution is:
\begin{equation*}
  X(t)=X_{0}\exp{\left((\alpha -\frac{1}{2}\beta ^2)t +\beta W(t)\right)}
\end{equation*}
\end{frame}

\begin{frame}
	\begin{center}
	\includegraphics[scale=0.55]{alpham15_beta05.png}
	\end{center}
\end{frame}

\begin{frame}
A system of equations can be expressed in differential form \cite{klodenNumerics}:
	\begin{eqnarray*}
		dx&=&f_1(x,y)dt+{\color{red}g_1(x,y)dW}\\
		dy&=&f_2(x,y)dt+{\color{red}g_2(x,y)dW}\\
	\end{eqnarray*}
The equivalent integral form is the following:
	\begin{eqnarray*}
		x_t=x_a+\int_{a}^{t}f_1 ds+{\color{red}\int_{a}^{t}g_1dW_1}\\
		y_t=y_a+\int_{a}^{t}f_2 ds+{\color{red}\int_{a}^{t}g_2dW_2}\\
	\end{eqnarray*}
\end{frame}

\begin{frame}
Applying the chain rule for $f_1$:
	\begin{equation*}
	\begin{split}
	f_1(x_j,y_j) & \approx f_1(x_{j-1},y_{j-1})+f_{1x}(x_{j-1},y_{j-1})(x-x_{j-1})\\
	& \quad +f_{1y}(x_{j-1},y_{j-1})(y-y_{j-1})+\frac{1}{2} f_{1xx}(x_{j-1},y_{j-1})(x-
	x_{j-1})^2\\
	& \quad +f_{1xy}(x_{j-1},y_{j-1})(x-x_{j-1})(y-y_{j-1})\\
	& \quad +\frac{1}{2}f_{1yy}(x_{j-1},y_{j-1})(y-y_{j-1})^2
	\end{split}
	\end{equation*}
Substitute into the integral form:
	\begin{eqnarray*}
	x_j-x_{j-1} & \approx & \int_{\tau_{j-1}}^{\tau_{j}}f_1 ds+\int_{\tau_{j-1}}^{\tau_{j}}g_1dW_1
	\end{eqnarray*}
\end{frame}

\begin{frame}
Similarly for $g_1$ and if we ignore the high order terms:
	\begin{equation*}
	\begin{split}
	x_{j} & \approx x_{j-1}+f_1(x_{j-1},y_{j-1})\Delta t+g_1(x_{j-1},y_{j-1})\Delta W_1\\
	& \quad +g_{1x}(x_{j-1},y_{j-1})\int_{\tau_{j-1}}^{\tau_{j}}
	\int_{\tau_{j-1}}^{s}g_1dW_1dW_1\\
	& \quad	+g_{1y}(x_{j-1},y_{j-1})\int_{\tau_{j-1}}^{\tau_{j}}\int_{\tau_{j-1}}^{s}
	g_2dW_2dW_1\\
	\end{split}
	\end{equation*}
\end{frame}

\begin{frame}
Applying the chain rule again:
	\begin{equation*}
	\begin{split}
	x_{j} &= x_{j-1}+f_1(x_{j-1},y_{j-1})\Delta t+g_1(x_{j-1},y_{j-1})\Delta W_1\\
	&\quad +\frac{1}{2}g_{1x}(x_{j-1},y_{j-1})g_{1}(x_{j-1},y_{j-1})(\Delta W_1^2-\Delta t)\\
	&\quad +g_{1y}(x_{j-1},y_{j-1})g_2(x_{j-1},y_{j-1})\Delta W_1 \Delta W_2\\
	\end{split}
	\end{equation*}
\end{frame}

\begin{frame}
Similarly:
\begin{equation*}
	\begin{split}
	y_{j}&=y_{j-1}+f_2(x_{j-1},y_{j-1})\Delta t+g_2(x_{j-1},y_{j-1})\Delta W_2\\
	&\quad +g_{2x}(x_{j-1},y_{j-1})g_{1}(x_{j-1},y_{j-1})\Delta W_1 \Delta W_2\\
	&\quad +\frac{1}{2}g_{2y}(x_{j-1},y_{j-1})g_2(x_{j-1},y_{j-1})(\Delta W_2^2-\Delta t)\\
	\end{split}
	\end{equation*}
\end{frame}


\chapter{Excision and relative homology}
We have already seen how to use the Mayer-Vietoris sequence:
we started with a sequence
\[ \dots \to H_n(U \cap V) \to H_n(U) \oplus H_n(V) \to H_n(U+V) \to H_{n-1}(U \cap V) \to \dots \]
and its reduced version,
then appealed to the geometric fact that $H_n(U+V) \cong H_n(X)$.
This allowed us to algebraically make computations on $H_n(X)$.

In this chapter, we turn our attention to the long exact
sequence associated to the chain complex
\[ 0 \to C_n(A) \injto C_n(X) \surjto C_n(X,A) \to 0. \]
The setup will look a lot like the previous two chapters,
except in addition to $H_n : \catname{hTop} \to \catname{Grp}$
we will have a functor $H_n : \catname{hPairTop} \to \catname{Grp}$
which takes a pair $(X,A)$ to $H_n(X,A)$.
Then, we state (again without proof) the key geometric result,
and use this to make deductions.

\section{The long exact sequences}
Recall \Cref{thm:long_exact_rel}, which says that the sequences
\[ \dots \to H_n(A) \to H_n(X) \to H_n(X,A) \to H_{n-1}(A) \to \dots. \]
and
\[ \dots \to \wt H_n(A) \to \wt H_n(X) \to H_n(X,A) \to \wt H_{n-1}(A) \to \dots \]
are long exact.
By \Cref{prob:triple_long_exact} we even have a long exact sequence
\[
	\dots
	\to H_n(B,A)
	\to H_n(X,A)
	\to H_n(X,B)
	\to H_{n-1}(B,A)
	\to \dots.
\]
for $A \subseteq B \subseteq X$.
An application of the second long exact sequence above gives:
\begin{lemma}
	[Homology relative to contractible spaces]
	\label{lem:rel_contractible}
	Let $X$ be a topological space,
	and let $A \subseteq X$ be contractible.
	For all $n$, \[ H_n(X, A) \cong \wt H_n(X). \]
\end{lemma}
\begin{proof}
	Since $A$ is contractible, we have $\wt H_n(A) = 0$ for every $n$.
	For each $n$ there's a segment of the long exact sequence given by
	\[ \dots \to \underbrace{\wt H_n(A)}_{=0} \to \wt H_n(X) \to H_n(X,A)
	\to \underbrace{\wt H_{n-1}(A)}_{=0} \to \dots. \]
	So since $0 \to \wt H_n(X) \to H_n(X,A) \to 0$ is exact,
	this means $H_n(X,A) \cong \wt H_n(X)$.
\end{proof}

In particular, the theorem applies if $A$ is a single point.
The case $A = \varnothing$ is also worth noting.
We compile these results into a lemma:
\begin{lemma}
	[Relative homology generalizes absolute homology]
	Let $X$ be any space, and $\ast \in X$ a point. Then for all $n$,
	\[
		H_n(X, \{\ast\}) \cong \wt H_n(X)
		\qquad\text{and}\qquad
		H_n(X, \varnothing) = H_n(X).
	\]
\end{lemma}

\section{The category of pairs}
Since we now have an $H_n(X,A)$ instead of just $H_n(X)$,
a natural next step is to create a suitable category of \emph{pairs}
and give ourselves the same functorial setup as before.

\begin{definition}
	Let $\varnothing \neq A \subseteq X$ and $\varnothing \neq B \subseteq X$
	be subspaces, and consider a map $f : X \to Y$.
	If $f\im(A) \subseteq B$ we write
	\[ f : (X,A) \to (Y,B). \]
	We say $f$ is a \vocab{map of pairs},
	between the pairs $(X,A)$ and $(Y,B)$.
\end{definition}
\begin{definition}
	We say that $f,g : (X,A) \to (Y,B)$ are \vocab{pair-homotopic} if they
	are ``homotopic through maps of pairs''.

	More formally, a \vocab{pair-homotopy}
	$f, g : (X,A) \to (Y,B)$ is a map $F : [0,1] \times X \to Y$,
	which we'll write as $F_t(X)$, such that
	$F$ is a homotopy of the maps $f,g : X \to Y$
	and each $F_t$ is itself a map of pairs.
\end{definition}
Thus, we naturally arrive at two categories:
\begin{itemize}
	\ii $\catname{PairTop}$, the category of \emph{pairs} of
	toplogical spaces, and
	\ii $\catname{hPairTop}$, the same category except
	with maps only equivalent up to homotopy.
\end{itemize}
\begin{definition}
	As before, we say pairs $(X,A)$ and $(Y,B)$ are
	\vocab{pair-homotopy equivalent}
	if they are isomorphic in $\catname{hPairTop}$.
	An isomorphism of $\catname{hPairTop}$ is a
	\vocab{pair-homotopy equivalence}.
\end{definition}

We can do the same song and dance as before with the prism operator to obtain:
\begin{lemma}[Induced maps of relative homology]
	We have a functor
	\[ H_n : \catname{hPairTop} \to \catname{Grp}. \]
\end{lemma}
That is, if $f : (X,A) \to (Y,B)$ then we obtain an induced map
\[ f_\ast : H_n(X,A) \to H_n(Y,B). \]
and if two such $f$ and $g$ are pair-homotopic
then $f_\ast = g_\ast$.

Now, we want an analog of contractible spaces for our pairs:
i.e.\ pairs of spaces $(X,A)$ such that $H_n(X,A) = 0$.
The correct definition is:
\begin{definition}
	Let $A \subseteq X$.
	We say that $A$ is a \vocab{deformation retract} of $X$
	if there is a map of pairs $r : (X, A) \to (A, A)$
	which is a pair homotopy equivalence.
\end{definition}
\begin{example}
	[Examples of deformation retracts]
	\listhack
	\begin{enumerate}[(a)]
		\ii If a single point $p$ is a deformation retract of a space $X$,
		then $X$ is contractible, since the retraction $r : X \to \{\ast\}$
		(when viewed as a map $X \to X$)
		is homotopic to the identity map $\id_X : X \to X$.
		\ii The punctured disk $D^2 \setminus \{0\}$
		deformation retracts onto its boundary $S^1$.
		\ii More generally, $D^{n} \setminus \{0\}$
		deformation retracts onto its boundary $S^{n-1}$.
		\ii Similarly, $\RR^n \setminus \{0\}$
		deformation retracts onto a sphere $S^{n-1}$.
	\end{enumerate}
\end{example}
Of course in this situation we have that
\[ H_n(X,A) \cong H_n(A,A) = 0. \]

\begin{exercise}
	Show that if $A \subseteq V \subseteq X$,
	and $A$ is a deformation retract of $V$,
	then $H_n(X,A) \cong H_n(X,V)$ for all $n$.
	(Use \Cref{prob:triple_long_exact}. Solution in next section.)
\end{exercise}

\section{Excision}
Now for the key geometric result, which is the analog of
\Cref{thm:open_cover_homology} for our relative homology groups.
\begin{theorem}
	[Excision]
	Let $Z \subseteq A \subseteq X$ be subspaces such that
	the closure of $Z$ is contained in the interior of $A$.
	Then the inclusion $\iota (X \setminus Z, A \setminus Z) \injto (X,A)$
	(viewed as a map of pairs) induces an isomorphism of
	relative homology groups
	\[ H_n(X \setminus Z, A \setminus Z) \cong H_n(X,A). \]
\end{theorem}
This means we can \emph{excise} (delete) a subset $Z$ of $A$ in computing
the relative homology groups $H_n(X,A)$.
This should intuitively make sense:
since we are ``modding out by points in $A$'',
the internals of the point $A$ should not matter so much.

The main application of excision is to decide
when $H_n(X,A) \cong \wt H_n(X/A)$.
Answer:

\begin{theorem}
	[Relative homology $\implies$ quotient space]
	\label{thm:good_pair}
	Let $X$ be a space and $A$ be a subspace such that
	$A$ is a deformation retract of some open set $V \subseteq X$.
	Then the quotient map $q : X \to X/A$ induces an isomorphism
	\[ H_n(X,A) \cong H_n(X/A, A/A) \cong \wt H_n(X/A). \]
\end{theorem}
\begin{proof}
	By hypothesis, we can consider the following maps of pairs:
	\begin{align*}
		r & : (V,A) \to (A,A)  \\
		q & : (X,A) \to (X/A, A/A) \\
		\widehat q &: (X-A, V-A) \to (X/A-A/A, V/A-A/A).
	\end{align*}
	Moreover, $r$ is a pair-homotopy equivalence.
	Considering the long exact sequence of a triple
	(which was \Cref{prob:triple_long_exact})
	we have a diagram
	\begin{diagram}
		H_n(V,A) & \rTo & H_n(X,A) & \rTo^f & H_n(X, V) & \rTo & H_{n-1}(V,A) \\
		\dTo_r^\cong && && && \dTo_r^\cong \\
		\underbrace{H_n(A,A)}_{=0} && && && \underbrace{H_{n-1}(A,A)}_{=0}
	\end{diagram}
	where the isomorphisms arise since $r$ is a pair-homotopy equivalence.
	So $f$ is an isomorphism.
	Similarly the map
	\[ g : H_n(X/A, A/A) \to H_n(X/A, V/A) \]
	is an isomorphism.

	Now, consider the commutative diagram
	\begin{diagram}
		H_n(X,A) & \rTo^f & H_n(X,V) & \lTo^{\text{Excise}} & H_n(X-A, V-A) \\
		\dTo^{q_\ast} && && \dTo^{\widehat{q}_\ast}_{\cong} \\
		H_n(X/A,A/A) & \rTo^g & H_n(X/A,V/A)
		& \lTo^{\text{Excise}} & H_n(X/A-A/A, V/A-A/A) \\
	\end{diagram}
	and observe that the rightmost arrow $\widehat{q}_\ast$ is an isomorphism,
	because outside of $A$ the map $\widehat q$ is the identity.
	We know $f$ and $g$ are isomorphisms,
	as are the two arrows marked with ``Excise'' (by excision).
	From this we conclude that $q_\ast$ is an isomorphism.
	Of course we already know that homology relative to a point
	is just the relative homology groups
	(this is the important case of \Cref{lem:rel_contractible}).
\end{proof}

\section{Some applications}
One nice application of excision is to compute $\wt H_n(X \vee Y)$.
\begin{theorem}[Homology of wedge sums]
	Let $X$ and $Y$ be spaces with basepoints $x_0 \in X$ and $y_0 \in Y$,
	and assuming each point is a deformation retract of some open neighborhood.
	Then for every $n$ we have
	\[
		\wt H_n(X \vee Y)
		= \wt H_n(X) \oplus \wt H_n(Y).
	\]
\end{theorem}
\begin{proof}
	Apply \Cref{thm:good_pair} with the subset $\{x_0, y_0\}$ of $X \amalg Y$,
	\begin{align*}
		\wt H_n (X \vee Y)
		\cong \wt H_n( (X \amalg Y) / \{x_0, y_0\} )
		&\cong H_n(X \amalg Y, \{x_0,y_0\}) \\
		&\cong H_n(X, \{x_0\}) \oplus H_n(Y, \{y_0\}) \\
		&\cong\wt H_n(X) \oplus \wt H_n(Y). \qedhere
	\end{align*}
\end{proof}

Another application is to give a second method
of computing $H_n(S^m)$.
To do this, we will prove that
\[ \wt H_n(S^m) \cong \wt H_{n-1}(S^{m-1}) \]
for any $n,m > 1$.
However,
\begin{itemize}
	\ii $\wt H_0(S^n)$ is $\ZZ$ for $n=0$ and $0$ otherwise.
	\ii $\wt H_n(S^0)$ is $\ZZ$ for $m=0$ and $0$ otherwise.
\end{itemize}
So by induction on $\min \{m,n\}$ we directly obtain that
\[
	\wt H_n(S^m) \cong
	\begin{cases}
		\ZZ & m=n \\
		0 & \text{otherwise}
	\end{cases}
\]
which is what we wanted.

To prove the claim, let's consider the exact sequence
formed by the pair $X = D^2$ and $A = S^1$.
\begin{example}[The long exact sequence for $(X,A) = (D^2, S^1)$]
	Consider $D^2$ (which is contractible) with boundary $S^1$.
	Clearly $S^1$ is a deformation retraction of $D^2 \setminus \{0\}$,
	and if we fuse all points on the boundary together we get $D^2 / S^1 \cong S^2$.
	So we have a long exact sequence
	\begin{diagram}
		\wt H_2(S^1) & \rTo & \underbrace{\wt H_2(D^2)}_{=0} & \rTo & \wt H_2(S^2) \\
		&&& \ldTo(4,2) & \\
		\wt H_1(S^1) & \rTo & \underbrace{\wt H_1(D^2)}_{=0} & \rTo & \wt H_1(S^2) \\
		&&& \ldTo(4,2) & \\
		\wt H_0(S^1) & \rTo & \underbrace{\wt H_0(D^2)}_{=0} & \rTo & \underbrace{\wt H_0(S^2)}_{=0}
	\end{diagram}
	From this diagram we read that
	\[
		\dots, \quad
		\wt H_3(S^2) = \wt H_2(S^1), \quad
		\wt H_2(S^2) = \wt H_1(S^1), \quad
		\wt H_1(S^2) = \wt H_0(S^1).
	\]
\end{example}
More generally, the exact sequence for the pair $(X,A) = (D^m, S^{m-1})$
shows that $\wt H_n(S^m) \cong \wt H_{n-1}(S^{m-1})$,
which is the desired conclusion.

\section{Invariance of dimension}
Here is one last example of an application of excision.
\begin{definition}
	Let $X$ be a space and $p \in X$ a point.
	The $k$th \vocab{local homology group} of $p$ at $X$ is defined as
	\[ H_k(X, X \setminus \{p\}). \]
\end{definition}
Note that for any open neighborhood $U$ of $p$, we have by excision that
\[ H_k(X, X \setminus \{p\}) \cong H_k(U, U \setminus \{p\}). \]
Thus this local homology group only depends on the space near $p$.

\begin{theorem}
	[Invariance of dimension, Brouwer 1910]
	Let $U \subseteq \RR^n$ and $V \subseteq \RR^m$ be nonempty open sets.
	If $U$ and $V$ are homeomorphic, then $m = n$.
\end{theorem}
\begin{proof}
	Consider a point $x \in U$ and its local homology groups. By excision,
	\[ H_k(\RR^n, \RR^n \setminus \{x\}) \cong
		H_k(U, U \setminus \{x\}). \]
	But since $\RR^n \setminus \{x\}$ is homotopic to $S^{n-1}$,
	the long exact sequence of \Cref{thm:long_exact_rel} tells us
	that
	\[
		H_k(\RR^n, \RR^n \setminus \{x\})
		\cong
		\begin{cases}
			\ZZ & k = n \\
			0 & \text{otherwise}.
		\end{cases}
	\]
	Analogously, given $y \in V$ we have
	\[ H_k(\RR^m, \RR^m \setminus\{y\}) \cong H_k(V, V\setminus\{y\}). \]
	If $U \cong V$, we thus
	deduce that
	\[ H_k(\RR^n, \RR^n\setminus\{x\}) \cong H_k(\RR^m, \RR^m\setminus\{y\}) \]
	for all $k$.  This of course can only happen if $m=n$.
\end{proof}

\section\problemhead
\begin{problem}
	Let $X = S^1 \times S^1$ and $Y = S^1 \vee S^1 \vee S^2$.
	Show that \[ H_n(X) \cong H_n(Y) \] for every integer $n$.
\end{problem}

\begin{problem}[Hatcher \S2.1 exercise 18]
	Consider $\QQ \subset \RR$.
	Compute $\wt H_1(\RR, \QQ)$.
	\begin{hint}
		Use \Cref{thm:long_exact_rel}.
	\end{hint}
	\begin{sol}
		We have an exact sequence
		\[
			\underbrace{\wt H_1(\RR)}_{=0}
			\to \wt H_1(\RR, \QQ) \to \wt H_0(\QQ) \to
			\underbrace{\wt H_0(\RR)}_{=0}.
		\]
		Now, since $\QQ$ is path-disconnected
		(i.e.\ no two of its points are path-connected)
		it follows that $\wt H_0(\QQ)$ consists of
		countably infinitely many copies of $\ZZ$.
	\end{sol}
\end{problem}

\begin{sproblem}
	What are the local homology groups of a topological $n$-manifold?
\end{sproblem}

\begin{problem}
	Let \[ X = \{(x,y) \mid x \ge 0\} \subseteq \RR^2 \]
	denote the half-plane.
	What are the local homology groups of points in $X$?
	% http://math.stackexchange.com/questions/350667/local-homology-group-a-homeomorphism-takes-the-boundary-to-the-boundary
\end{problem}

\begin{problem}
	[Brouwer-Jordan separation theorem,
	generalizing Jordan curve theorem]
	\yod
	Let $X \subseteq \RR^n$ be a subset
	which is homeomorphic to $S^{n-1}$.
	Prove that $\RR^n \setminus X$
	has exactly two path-connected components.
	\begin{hint}
		For any $n$, prove by induction for $k=1,\dots,n-1$ that
		(a) if $X$ is a subset of $S^n$ homeomorphic to $D^k$
		then $\wt H_i(S^n \setminus X) = 0$;
		(b) if $X$ is a subset of $S^n$ homeomorphic to $S^k$
		then $\wt H_i(S^n \setminus X) = \ZZ$ for $i=n-k-1$
		and $0$ otherwise.
	\end{hint}
	\begin{sol}
		This is shown in detail in Section 2.B of Hatcher.
	\end{sol}
\end{problem}