%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 4}

If we had an algorithm that finds the median of a sequence in linear time (worst case) - \textsc{findMedian($A$, $p$, $r$)} which returns the index of the median of the sequence $A[p..r]$, describe a worst-case linear time algorithm that finds any order statistics.
Provide a brief run-time analysis.

\subsection*{Solution}

To obtain any order statistics of a sequence \textsc{findMedian($A$, $p$, $r$)} can be called to check if order is less than median, as proposed by Algorithm \ref{alg2}.

Although in worst case \textsc{findMedian} is called $\log n$ times, because the length of array that is given to \textsc{findMedian} is different each time, the eventual runtime would be linear in $n$.

\begin{algorithm}[H]
\caption{\textsc{findOrder($A$, $p$, $r$, $s$)}}\label{alg2}
\begin{algorithmic}[1]
\State $q \leftarrow $ \textsc{findMedian($A$, $p$, $r$)}
\State \textsc{Partition($A$, $p$, $r$, $q$)}
\If {$s < q$}
\State \Return \textsc{findMedian($A$, $p$, $q$)}
\ElsIf {$s = q$}
\State \Return $A[q]$
\Else
\State \Return \textsc{findMedian($A$, $q$, $r$)}
\EndIf
\end{algorithmic}
\end{algorithm}

where \textsc{Partition($A$, $p$, $r$, $q$)} is a simple algorithm that partitions the array around pivot element with index $q$, as shown in Algorithm \ref{alg3}.

\begin{algorithm}[H]
\caption{\textsc{Partition($A$, $p$, $r$, $q$)}}\label{alg3}
\begin{algorithmic}[1]
\State $i \leftarrow p - 1 $
\For {$j \leftarrow p$ to $r - 1$}
\If {$A[j] \leq q$}
\State $i \leftarrow i + 1$
\State exchange A[i] with A[j]
\EndIf
\State exchange A[i+1] with A[r]
\EndFor
\end{algorithmic}
\end{algorithm}

A more rigorous run-time analysis can be obtained using recursion $T(n) = T(\frac{n}{2}) + \mathcal{O}(n)$.
As $T(\frac{n}{2}) \leq \mathcal{O}(n)$ for all $n$, runtime would be bound by $O(n)$.


%----------------------------------------------------------------------------------------
%   NEWTON-RAPHSON
%----------------------------------------------------------------------------------------

\section{Newton-Raphson Method}
\subsection{Simple Newton-Raphson Algorithm}

Let $f:\mathbb{R}^{n}\rightarrow \mathbb{R}^{m}$ be a function that is differentiable along both dimentions. $f$ and its derivatives are defined as following:
\[f:
\begin{pmatrix}
    x_1\\
    \vdots\\
    x_N
  \end{pmatrix}
  \rightarrow
  \begin{pmatrix}
    f_1(x_1,...,X_N)\\
    \vdots\\
    f_N(x_1,...,x_N)
  \end{pmatrix},\
  H: \begin{pmatrix}
    x_1\\
    \vdots\\
    x_N
  \end{pmatrix}
  \rightarrow
  \begin{pmatrix}
    \frac{\partial f_i(x_1,...,x_N)}{\partial x_j}
  \end{pmatrix}_{i \in [1,N], j \in[1,N]},
\]
where $H$ is the Jacobian matrix of $f$.

The Newton-Raphson method tries to solve the equation $f(U) = 0$. It assumes that given a current position $U$, the best direction leading to a root of the function is the direction given by the slope of $f$ at $U$.

Let $U$ be an initial position chosen arbitrary. The algorithm looks for a point $V$ such that:
\[
f(U+V) = 0
\]
And
\begin{equation}
f(U+V) = f(U) + H(U).V
\label{eq:equation1}
\end{equation}
It is easy to notice that:
\begin{equation}
f(U) + H(U).V = 0
\label{eq:equation2}
\end{equation}

Then, $U$ is replaced by $U+V$, and the process is repeated until either $U$ converges or a maximum number of iterations is reached. The Taylor approximation in Equation~\vref{eq:equation1} ensures that we are looking for the root in the direction of the slope. However, no guarantee is given that the point reached at the end of this direction is minimizing the function $f$.

This is why the Newton-Raphson method has an unfortunate tendency to diverge if the initial guess is not sufficiently close to the root.

\subsection{Backtracking}
A global method is a one that converges to a solution from almost any starting point. In this section we will develop an algorithm called backtracking that combines the rapid superlinear speed of convergence of Newton's method, with a globally convergent strategy that will guarantee some progress towards the solution at each iteration. In other words, at each step of the algorithm, the next point given by Equation~\vref{eq:equation2} is only chosen if it minimizes the function. Figure~\vref{fig:backtracking} shows the full algorithm.

The complexity of the algorithm is $\Theta(N x c)$ - $c$ is the complexity of the function solve.

\begin{figure}[ht]
  \centering
  \begin{algorithm}[H]
    \KwData{$f$: a function, $H$: the jacobian matrix of $f$, $U_0$: a starting position, $N$: maximal number of iterations, $\varepsilon$: precision.}
    \KwResult{$U$: a root of $f$}
    $U \leftarrow U_0$\\
    tmp $\leftarrow 1$\\
    \For{$i$ from $1$ to $N$}{
      $V \leftarrow \text{solve}(f(U) + H(U).V = 0)$\\
      \While{$\|f(U + \text{step}*V)\|> \|f(U)\|$}{$\text{step} \leftarrow \text{step}*2/3$}
        $U \leftarrow U + \text{step}*V$\\
        \If{$\|f(U)< \varepsilon\|$}
          {return $U$}
    }
    return $U$\\
  \end{algorithm}
  \caption{Backtracking algorithm}
  \label{fig:backtracking}
\end{figure}

\subsection{Tests}
One way to easily test this method is to check if the point returned $U$ is a root of $f$. This can be done by computing $f(U)$. The next figures show the speed of...
%Julien s'occupe de finaliser d'après ce qu'il m'a dit


\lab{Monte-Carlo Integration}{Monte-Carlo Integration}
\objective{Use Monte-Carlo integration to estimate areas.}

Some multivariable integrals which are critical in applications are impossible to evaulate symbolically.
For example, the integral of the joint normal distribution
\[
\int_{\Omega} \frac{1}{\sqrt{(2 \pi)^k}} e^{- \frac{\bold{x}^T\bold{x}}{2}}
\]
is ubiquitous in statistics.
However, the integrand does not have a symbolic antiderivative.
This means we must use numerical methods to evaluate this integral.

The standard technique for numerically evaluating multivariable integrals is \emph{Monte-Carlo Integration}.
Monte-Carlo (MC) integration is radically different from 1-dimensional techniques like Simpson's rule, relying on probability to calculate the integral.
Although it converges slowly, MC integration is frequently used to evaluate multivariable integrals because the higher-dimensional analogs of methods like Simpson's rule are inefficient.

\section*{A motivating example}
Suppose we want to numerically compute the area of a circle of radius 1.
From analytic methods, we know the answer is $\pi$.
Empirically, we can estimate this quantity by randomly choosing points in a $2 \times 2$ square.
The percent of points that land in the inscribed circle, times the area of the square, should approximately equal the area of the circle (see Figure \ref{fig:MCCircle}).

\begin{figure}
\includegraphics[width=.7\textwidth]{MC_Circle.pdf}
\caption{Finding the area of a circle using random points}
\label{fig:MCCircle}
\end{figure}

We do this in NumPy as follows. First generate 500 random points in the square $[0,1] \times [0,1]$.
\begin{lstlisting}
>>> numPoints = 500
>>> points = np.random.rand(2, numPoints)
\end{lstlisting}
We rescale and shift these points to be uniformly distributed in $[-1,1]\times[-1,1]$.
\begin{lstlisting}
>>> points = points*2-1
\end{lstlisting}
Next we compute the number of points in the unit circle.
The function \li{np.hypot(a, b)} returns the norm of the vector $(a, b)$.
\begin{lstlisting}
>>> # Create a mask of points in the circle
>>> circleMask = np.hypot(points[0,:], points[1,:]) <= 1
>>> # Count how many there are
>>> numInCircle = np.count_nonzero(circleMask)
\end{lstlisting}
Finally, we approximate the area.
\begin{lstlisting}
>>> # Area is approximately (area of the square)*(num points in circle)/(total num points)
>>> 4.*numInCircle/numPoints
3.024
\end{lstlisting}
This differs from $\pi$ by about 0.117.

We analyze the error of the MC method by repeating this experiment for many values of \li{numPoints} and plotting the errors.
The result is the blue line in Figure \ref{fig:mc_error}.
The error appears to be proportional to $1/\sqrt{N}$ where $N=$\li{numPoints} (the green line in Figure \ref{fig:mc_error}).
This means that to divide the error by10, we must sample \emph{100 times} more points.

\begin{figure}
\includegraphics[width=.7\textwidth]{mc_error.pdf}
\caption{The Monte-Carlo integration method was used to compute the area of a circle of radius 1.
The blue line plots the average error in 100 runs of the MC method on $N$ sample points, where $N$ appears on the horizontal axis.
The green line is a plot of $1/\sqrt{N}$. }
\label{fig:mc_error}
\end{figure}

This is a slow convergence rate, but it is independent of the number of dimensions of the problem.
This dimension independence is what makes the MC method useful for multivariable integrals.

\section*{Monte-Carlo Integration}

You can calculate the area of the unit circle with the following integration problem:
\[
\mbox{Area of unit circle } = \int_{[-1,1]\times[-1,1]} f(x,y) dA
\]
where
\begin{equation}\label{equ:mc_func}
f(x,y) = \begin{cases} 1 &\mbox{ if $x$,$y$ is in the unit circle} \\ 0 &\mbox{ otherwise.} \end{cases}
\end{equation}

We can use a random-points method as above to approximate any integral.
Suppose we with to evaluate
\[
\int_\Omega f(x) dV.
\]
We can approximate this integral using the formula
\begin{equation}\label{equ:mc_eq}
\int_\Omega f(x) dV \approx V(\Omega) \frac{1}{N} \sum_{i=1}^N f(x_i),
\end{equation}
where $x_i$ are uniformly distributed random vectors in $\Omega$ and $V(\Omega)$ is the volume of $\Omega$.
This is the formula for Monte-Carlo Integration.

In our example, $\Omega$ was the box $[-1,1] \times [-1,1]$ and $f$ was the function defined in \eqref{equ:mc_func}.
Then $\sum_{i=1}^N f(x_i)$ is the number of points in the unit circle, $N$ is the total number of points, and \eqref{equ:mc_eq} is the same as the formula we derived previously.

The intuition behind \eqref{equ:mc_eq} is that $\frac{1}{N} \sum_{i=1}^N f(x_i)$ approximates the average value of $f$ on $\Omega$.
We multiply this (approximate) average value by the volume of $\Omega$ to get the (approximate) integral of $f$ on $\Omega$.

As an 1-dimensional example consider the integral
\[
\int_0^1 x dx \approx (1-0)\frac{1}{N} \sum_{i=1}^N x_i=\frac{1}{N} \sum_{i=1}^N x_i.
\]

The integral on the left-hand-side is $1/2$.
In the approximation on the right-hand-side, $x_i$ is drawn from a uniform distribution on $[0,1]$.
The average of $N$ such draws will converge to $1/2$.

\begin{problem}
\label{prob:mc}
Implement Monte-Carlo integration with the following function.
Your implementation should run the Monte-Carlo algorithm several times and return the average of those runs.
\begin{lstlisting}
def mc_int(f, mins, maxs, numPoints=500, numIters=100):
    '''Use Monte-Carlo integration to approximate the integral of f
    on the box defined by mins and maxs.

    INPUTS:
    f         - A function handle. Should accept a 1-D NumPy array
    	 	as input.
    mins      - A 1-D NumPy array of the minimum bounds on integration.
    maxs      - A 1-D NumPy array of the maximum bounds on integration.
    numPoints - An integer specifying the number of points to sample in
    		the Monte-Carlo method. Defaults to 500.
    numIters - An integer specifying the number of times to run the
    		Monte Carlo algorithm. Defaults to 100.

    ALGORITHM:
    Run the Monte-Carlo algorithm `numIters' times and return the average
    of these runs.

    EXAMPLES:
    >>> f = lambda x: np.hypot(x[0], x[1]) <= 1
    >>> # Integral over the square [-1,1] x [-1,1] should be pi
    >>> mc_int(f, np.array([-1,-1]), np.array([1,1]))
    3.1290400000000007
    '''
\end{lstlisting}

Hints:
\begin{enumerate}
\item To create a random array of points on which to evaluate \li{f}, first create a random array of points in $[0,1] \times \ldots \times [0,1]$.
Then multiply this array by the appropriate vector to stretch it the right amount in each direction.
Finally, add the appropriate vector to shift the points to the right location.
\item You can evaluate \li{f} on an array of points in one line using \li{np.apply_along_axis()}.
\end{enumerate}
\end{problem}

\begin{comment}
\begin{problem}
\label{prob:mc_test}
The exact value of the integral of
\[
f(w,x,y,z) = sin(x) y^5 -y^3 + zw + yz^3
\]
on $[-1,1]x[-1,1]x[-1,1]x[-1,1]$ is 0.
\begin{enumerate}
\item Run the function \li{mc_int()} you wrote in Problem \ref{prob:mc} on $f$ 10 sample points.
Do this 100 times and take the average. What is the error?
\item Repeat part (1) of this problem with 100, 1000, and 10000 sample points.
Plot the errors of your estimates.
\end{enumerate}
\end{problem}
\end{comment}

One application of Monte Carlo integration is integrating probability density functions that do not have closed form solutions.

\begin{comment}
\begin{problem}
The standard normal distribution is an important object of study in probability and statistic.
It is defined by the density function $\frac{1}{\sqrt{2 \pi}} e^{- \frac{x^2}{2}}$.
(Here we are assuming a mean of $0$ and a variance of $1$).
This is a function that cannot be integrated symbolically.
We can use monte carlo integration to estimate the probability that a normally distributed random variable will take a value below a given point.
The probability that the random variable we are considering is less than (or equal to) a given value $x$ is
\[\int_{-\infty}^x \frac{1}{\sqrt{2 \pi}} e^{- \frac{t^2}{2}} dt\]
This function is essentially zero for values of $x$ that lie reasonably far from the mean, so we can estimate this probability by integrating from $-5$ to $x$ instead.

Compare your result at $x = 1$ with the output of the code
\begin{lstlisting}
from scipy.stats import norm
N = norm()
N.cdf(1)
\end{lstlisting}
\end{problem}
\end{comment}

\begin{problem}
The joint normal distribution of $N$ independent random variables with mean 0 and variance 1 is
\[
f(\x) = \frac{1}{\sqrt{(2 \pi)^N}} e^{- \frac{\x^T\x}{2}}.
\]
The integral of $f(\x)$ over a box is the probability that a draw from the distribution will be in the box.
However, $f(\x)$ does not have a symbolic antiderivative.
\begin{enumerate}
\item The integral of this function on $B = [-1,1]\times [-1,1]\times[-1,1] \subset \mathbb{R}^3$ can be computed in SciPy with the following code.
\begin{lstlisting}
import scipy.stats as stats

# Define the bounds of the box to integrate over
mins = np.array([-1, -1, -1])
maxs = np.array([1, 1, 1])

# Each variable has mean 0 and variance 1
means = np.zeros(3)
covs = np.ones(3)

# Compute the integral
value, inform = stats.mvn.mvnun(mins, maxs, means, covs)
\end{lstlisting}
Then \li{value} is the integral of $f(\x)$ on $B$.
Use SciPy to integrate $f(\x)$ on $\Omega=[-0.5, 0.75]\times[0,1]\times[0, 0.5]\times[0,1] \subset \mathbb{R}^4$.

\item Use the function \li{mc_int()} you wrote in Problem \ref{prob:mc} to integrate $f(\x)$ on $\Omega$ with 10, 100, 1000, and 10,000 sample points.
Plot the errors of your estimates.
\end{enumerate}
\end{problem}

\section*{A caution}
You can run into trouble if you try to use MC integration on an integral that does not converge.
For example, we may attempt to evaluate
\[
\int_0^1 \frac{1}{x}
\]
with MC integragtion using the following code.
\begin{lstlisting}
>>> k = 5000
>>> np.mean(1/np.random.rand(k,1))
21.237332864358656
\end{lstlisting}

Since this code returns a finite value, so we could assume that this integral has a finite value.
In fact, the integral is infinite.
We could discover this emprically by using larger and larger values of $k$, and noting that MC integration returns larger and larger values.

\begin{comment}
\begin{problem}
\label{prob:mc_flawed}
Create a new function (based upon the function from Problem \ref{prob:mc}) that uses a ``flawed'' random number generator that doesn't produce numbers between $-.95$ and $-1$. Test your method on the function from Problem \ref{prob:mc_test}. How bad is the error?
\end{problem}
\end{comment}


\subsection{Reinforcement Learning}

A \textbf{Markov decision process (MDP)} is defined by

a set of states  $s\in S$ a set of actions $a \in A$;

Action dependent transition probabilities $T(s,a,s')=P(s'|s, a)$ , so that for each state  s  and action  a ,  $\displaystyle \sum _{s'\in S} T(s,a,s')=1$.

Reward functions $R(s, a, s')$ representing the reward for starting in state  $s$ , taking action  $a$  and ending up in state  $s'$  after one step. (The reward function may also depend only on  $s$ , or only  $s$  and  $a$ .)

Therefore a Markov decision process is defined by $MDP = <S,A,T,R>$ MDPs satisfy the Markov property in that the transition probabilities and rewards depend only on the current state and action, and remain unchanged regardless of the history (i.e. past states and actions) that leads to the current state.

Rewards collected after the  $n$th  step do not depend on the previous states $s_1,s_2,\cdots,s_{n-1}$

\textbf{Markov properties:}\\

Rewards collected after the $n$th  step do not depend on the previous actions $a_1,a_2,\cdots,a_{n}$

\textbf{(Infinite horizon) discounted reward based utility}\\

\begin{align*}
U[s_0,s_1,\ldots ]= R(s_0) + \gamma R(s_1) + \gamma ^2 R(s_2) \ldots = \\
= \sum _{t=0}^\infty \gamma ^ t R(s_ t) \text {where }0\leq \gamma <1\\
\leq \frac{R_{max}}{1-\gamma}
\end{align*}

\textbf{Bellman Equations}\\

value fn $V^{*}(s)$ --  is the expected reward from starting at state $s$ and
acting optimally, i.e. following the optimal policy;\\
\begin{enumerate}
  \item \textbf{value fn} $V^{*}(s)$ – is the expected reward from starting at state $s$ and
acting optimally, i.e. following the optimal policy;
  \item \textbf{Q-function} $Q^{*}(s,a)$ –is the expected reward from starting at state $s$, then
acting with action $a$(not necessarily the optimal action), and acting optimally
afterwards.
\end{enumerate}

$V^{*}(s) = \underset {a}{max}Q^{*}(s,a)$\\

$Q^{*}(s,a)=\sum _{s'}T(s,a,s')[R(s,a,s')+\gamma V^{*}(s')]$\\

Q-value: $Q(s,a)$ in state $s$ take action $a$ and act optimally afterwards.

Policy $\pi^*: s \rightarrow a$ is set of actions to maximize the expected reward for every state $s$.

\begin{align*}
&\pi^*(s) = argmax_a(Q^*(s,a))\\
&Q^*(s,a)= \sum_{s'} T(s,a,s')[R(s,a,s') + \gamma max_{a}Q(s',a')]
\end{align*}

To find the policy two algos: Value iteration and Q-value iteration (look online).

\textbf{Bellman optimality equation}
$Q^*(s,a)=E[R_{t+1} + \gamma \underset{a}{\max{Q^*(s,a)}}]$

\textbf{Q-value Iteration Update Rule}\\
$Q_{k+1}^*(s, a) = \sum _{s'} T(s, a, s')(R(s, a, s') + \gamma \text {max}_{a'} Q_ k^*(s', a')).$

\subsection{Q value iteration by sampling}

Exponential running average is defined as\\
$\bar{x}_n = (1-\alpha)*\bar{x}_{n-1} + \alpha x_n$\\
\begin{align*}
&Q_{i+1}(s, a) = (1-\alpha)\cdot Q_{i}(s, a) + \alpha \cdot sample_i(s,a)\\
&Q_{i+1}(s, a) = Q_{i}(s, a) + \alpha (Q_{i}(s, a) - sample_i(s,a))\\
&Q_{i+1}(s, a) = Q_{i}(s, a) + \alpha (Q_{i}(s, a) - (R(s,a,s')
+ \gamma max_{a}Q(s',a')))
\end{align*}\\
$Q_{i}(s, a)$ is not the Q-value of the sample $i$, but the average Q-value up
to the sample $i$ including.  It already embed all the experience up to
sample $i$.\\
The above equation should recall those of the stochastic gradient descent,
it’s just a different form of this update.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 4}

Prove that if $x$ and $y$ are any two distinct vertices in a tree, there is a unique simple path from $x$ to $y$.

\subsection*{Solution}

Proof is given by contradiction.
Assume the statement is false.
The opposite of the claimed statement is either there is no simple path from $x$ to $y$ or there are more than two different simple paths.
The former can be rejected simply by going back to definition of the tree which requires all vertices to be connected, reserving a simple path from each node to another.
Let us assume there are more than one paths from $x$ to $y$, like $p_1$ and $p_2$.

\begin{equation}
p_1: x \rightarrow v_1 \rightarrow v_2 \rightarrow \cdots \rightarrow v_{k-1} \rightarrow y
\end{equation}

\begin{equation}
p_2: x \rightarrow v_{1}^{\prime} \rightarrow v_{2}^{\prime} \rightarrow \cdots \rightarrow v_{k-1}^{\prime} \rightarrow y
\end{equation}
As $p_1$ and $p_2$ are necessarily two different paths, they differ at least in one vertex.
Let $v_i$ ($1<i<k-1$) be the last vertex common between $p_1$ and $p_2$ ($v_i = v_{i}^{\prime}$).
In this case, there will be two simple paths from $v_i$ to $y$, one from vertices in $p_1$ and one from vertices in $p_2$.
Therefore, a simple loop of the form $v_i \rightarrow v_{i+1} \rightarrow \cdots v_{k-1} \rightarrow y \rightarrow v_{k-1}^{\prime} \rightarrow \cdots \rightarrow v_{i+1}^{\prime} \rightarrow v_i$ will exist.
This violates definition of the tree in which there should be no simple loops.
Therefore, the assumption is false and there are no more than one simple path from $x$ to $y$.


\subsection{k-means clustering}

\subsubsection{Introduction}

K-means clustering is the most widely used unsupervised model.

In k-means clustering we identify \(k\) centroids in the feature space. We then calculate the distance from each data point to each of the centroids, and allocate the data point to the nearest centroid.

This requires a method for calculating the location of the centroids.

\subsubsection{Identifying the centroids}

We apply an iterative approach to identifying the centroids.

We first initialise by assigning centroids randomly to existing data points.

We then iteratively perform the following:

\begin{itemize}
\item Calculate the distances between each data point and each centroid.
\item Assign each data point to the closed centroid.
\item Update each centroid location to the mean of the data points allocated to it.
\end{itemize}

\subsubsection{Calculating distances}

This method requires us to calculate the distance between two points in the feature space.

For k-means we use the Euclidian distance.

\subsubsection{Potential issues}

It is possibile for a centroid to have no data assigned to it. If this happens we can eliminate the cluster, or reassign some data points.

The algorithm may only arrive at a local minima. In order to maximise the chance of an effective clustering, we can do k-means under different initialisations of the centroids in order to minimise risk of bad local optima.

\subsubsection{Choosing \(k\)}

If the points in each cluster follow a normal distribution, that's a good sign. This can be tested with Anderson-Darling.

If it's not normal, we can split the cluster into 2.

\subsubsection{Using clusting as part of data analysis}

We can choose \(k\) if output is being used in later data analysis (eg type assignment, complaint level or something)


A {\bf singleton set} (or just a {\bf singleton}) is a set with {\it exactly one} element. There is "essentially just one" singleton set.
This a very different situation than that for the {\bf empty set}, where we know there's {\it exactly one} empty set.
Why does this happen? How does this happen? We try to explain this.

\section{Any two singleton sets are "essentially the same"}

$\tab$ {\bf Definition}. A {\bf set-isomorphism} is a
\href{https://en.wikipedia.org/wiki/Bijection,_injection_and_surjection}{{\bf bijection}}
between sets.

$\tab$ {\bf Theorem}. There exists a unique set-isomorphism between any two singleton sets.
There's "exactly one" singleton set, not necessarily because any two singleton sets have the same elements
(which is how {\bf equality} of sets is defined),
but because, given any two singleton sets, there's exactly one set-isomorphism (ie. a bijection) between the two.
This allows us to say there's "exactly one" singleton set even when there {\it really} isn't exactly one singleton set.
In other words, this allows us to say there's a {\it unique} singleton set,
provided we're willing to consider two singletons sets "the same" whenever there exists a unique set-isomorphism between the two.
The jargon for this is: there's a unique singleton set,
\href{https://en.wikipedia.org/wiki/Up_to}{up to}
unique set-isomorphism.

$\tab$ {\it Proof}. Why do we talk about "the" singleton set? Is there {\it only one} singleton set?
For instance, using the
\href{https://en.wikipedia.org/wiki/Axiom_of_extensionality}{{\bf axiom of extension}}
(aka. the axiom of extensionality), we can prove that there's only one empty set.
This is because the axiom of extension says that {\it equality of sets} is
\href{https://en.wikipedia.org/wiki/If_and_only_if}{{\bf materially equivalent}}
to {\it equality of their elements}. More precisely, two sets are equal
\href{https://en.wikipedia.org/wiki/Logical_biconditional}{{\bf IFF}}
they have exactly the same elements.
If there were two empty sets, then for them to be not equal they'd have to differ by (at least) one element:
one of the two empty sets would have to have an element that is not in the other.
But this can't happen because an empty set has, by definition, no elements.

So, what about singleton sets? There's certainly more than one,
because, for instance, the singleton set $\{0\}$ is not the same as the singleton set $\{1\}$:
the set $\{0\}$ has the one element $0$, and the set $\{1\}$ has the one element $1$;
and $0$ is {\it not} equal to $1$, under {\it most} reasonable interpretations of the symbol $0$ and the symbol $1$
(for example, in the
\href{https://ncatlab.org/nlab/show/trivial+ring}{zero ring},
$0$ {\it does} equal $1$).

But these two sets are {\bf set-isomorphic}: there exists a {\bf set-isomorphism} (ie. a {\bf bijection}) between the two.
What does a bijection between $\{0\}$ and $\{1\}$ look like?
Well, for starters, what does a {\bf function} between $\{0\}$ and $\{1\}$ look like?
Consider a function $f: \{0\} \to \{1\}$ from $\{0\}$ to $\{1\}$.
By definition, the function $f$ must map each element of $\{0\}$ to exactly one element of $\{1\}$.
But $\{0\}$ has exactly one element, namely $0$, so all we need to decide is: what is the image of $0$ under $f$?
In other words, what is $f[0]$? Well, how many choices do we have?
For each element of $\{1\}$ we have one choice, and each choice leads to a different function.
But $\{1\}$ only has one elements, namely $1$, so there's only one choice:
the only possible answer is that $f$ must map $0$ to $1$, meaning $f: 0 \mapsto 1$ or equivalently $f[0] = 1$.

So, there's only one function from $\{0\}$ to $\{1\}$. And this function $f$ is very special: it's a {\it bijection}.
Why is $f$ a bijection? Because $f$ is an {\bf injection} (distinct elements of $\{0\}$ go to distinct elements of $\{1\}$),
and because $f$ is a {\bf surjection} (every element of $\{1\}$ is the image of at least one element of $\{0\}$).
So, $f$ is an {\bf isomorphism of sets}.

And, since there's {\it only one} function from $\{0\}$ to $\{1\}$, this bijection/set-isomorphism is {\it unique}.
This is very important, so let's say it again: {\it there's a unique set-isomorphism from $\{0\}$ to $\{1\}$}.
But when arguing this way we never used any specific properties of $0$ or of $1$.
All we used was the fact that the sets $\{0\}$ and $\{1\}$ have {\it exactly one} element.
So, the same argument applies to any two sets with exactly one element (any two {\bf singleton} sets). How?

Well, let $A$ be an arbitrary (but fixed) singleton set, and let $B$ be an arbitrary (but fixed) singleton set. \\
Since $A$ is a singleton set, we can write $A$ as $\{a\}$, where the symbol $a$ is an arbitrary label for whatever element the singleton set $A$ happens to contain. \\
Since $B$ is a singleton set, we can write $B$ as $\{b\}$, where the symbol $b$ is an arbitrary label for whatever element the singleton set $B$ happens to contain. \\

If it happens that $a$ equals $b$, then $\{a\}$ and $\{b\}$ have exactly the same elements, and
it follows that $\{a\}$ equals $\{b\}$ (by the {\bf axiom of extension}).

If it happens that $a$ does {\it not} equal $b$, then $\{a\}$ and $\{b\}$ don't have exactly the same elements,
and it follows that $\{a\}$ does {\it not} equal $\{b\}$ (by the {\bf axiom of extension}).
Still, we claim there's a unique set-isomorphism between $\{a\}$ and $\{b\}$.
To see this, let $f: \{a\} \to \{b\}$ be a {\bf function} from $\{a\}$ to $\{b\}$.
By the definition of function, $f$ must map each element of $\{a\}$ to exactly one element of $\{b\}$.
But $\{a\}$ has exactly one element (namely, $a$) and $\{b\}$ has exactly one element (namely, $b$), so the only possible function is $f : a \mapsto b$,
which is the function that maps $a$ to $b$.
This function is both an {\bf injection} (distinct elements of $\{a\}$ go to distinct elements of $\{b\}$) and
a {\bf surjection} (every element of $\{b\}$ is the image of at least one element of $\{a\}$).
So, it turns out that $f: a \mapsto b$ is a {\bf bijection}.
Single $f$ is the only possible function from $\{a\}$ to $\{b\}$, this bijection is {\it unique}.
But a bijection is, by definition, a {\bf set-isomorphism} (aka. an isomorphism of sets).
So, {\it there's a unique set-isomorphism} from $\{a\}$ to $\{b\}$.
Since $\{a\}$ and $\{b\}$ are arbitrary singleton sets, it follows that there's a unique set-isomorphism between any two singleton sets. \\
$\tab$ $\square$


\chapter{Sufficient statistics}
In this chapter the theory of sufficient statistics are introduced.
\\
\\
% FIND REF. http://people.unica.it/musio/files/2008/10/Casella-Berger.pdf fra side 271 og ut.
First we begin with understanding of a statistic. Information about this subject can be found in \cite{casella2002statistical}. A statistic is a function that returns a summary of the data. Examples of this can be mean value and standard deviation of the sample. Lets assume our data is from a distrbution depending on some parameters $\Theta$. The statistics we are interested in are those who contain information about the parameters $\Theta$. This leads us to sufficient statistics. A sufficient statistic is a statistic that captures all information about the parameters $\Theta$ and discard the rest. From \cite{casella2002statistical} we have the following.
\begin{defn}
(Sufficient statistic definition) A statistic $T(\boldsymbol{X})$ is sufficient statistic for $\Theta$ if the conditional distribution of sample $\boldsymbol{X}$ gives the value for $T(\boldsymbol{X})$ does not depend on $\Theta$
\end{defn}
To find sufficient statistics can be difficult, however one can use the factorization theorem to find the statistics. The theorem is as follows.
\begin{theorem} % s276
(Factorization theorem) Let $f(\boldsymbol{x}| \Theta)$ denote the joint pdf or pmf of a sample $\boldsymbol{X}$. A statistic $T(\boldsymbol{X})$ is a sufficient for $\Theta$ if and only if there exist functions $g(t|\Theta)$ and $h(\boldsymbol{x})$ such that, for all sample points $\boldsymbol{x}$ and all parameter points $\Theta$,
\begin{equation}
f(\boldsymbol{x}|\Theta) = g(T(\boldsymbol{x})| \Theta)h(\boldsymbol{x}).
\end{equation}
\end{theorem}
$h(\boldsymbol{x})$ should not be dependent on $\Theta$. Then the remaining part will be $g(T(\boldsymbol{x})| \Theta)$ and from this we can see the sufficient statistic.

\section{Sufficient statistics}
To find the sufficient statistics for our NHPP one can use the likelihood function as shown in \ref{eq:like}. The likelihood function can be found by
\begin{equation}
P(x_1,..., x_n) = P(x_1,...,x_n|N(\tau)=n)P(N(\tau)=n)
\end{equation}
The probability of event $x_i$ given number of events $n$ is
\begin{equation}
P(x_i|n) = \frac{\lambda(x_i)}{\Lambda(\tau)}.
\end{equation}
Hence the joint probability given  $n$
\begin{equation}
P(x_1,...,x_n| N(\tau) = n) = \prod_{i = 1}^{n} \frac{\lambda(x_i)}{\Lambda(\tau)}.
\end{equation}
The probability of $N=n$ events is as shown in equation ?? with $t = \tau$. The joint probability of  $\boldsymbol{x}$ becomes
\begin{equation}
P(x_1,...,x_n) = e^{-\Lambda(\tau)} \frac{\Lambda(\tau)}{n!} \prod_{i=1}^n \frac{\lambda(x_i)}{\Lambda(\tau)}.
\end{equation}
From this the log likelihood function becomes
\begin{equation}
l = -\Lambda + n\log(a) + n\log(b) + (b-1)\sum_{i=1}^{n} \log(x_i) + c\sum_{i=1}^{n} x_i.
\label{eq:loglike}
\end{equation}
The $\log(n!)$ term has been neglected because it can be treated as constant regarding the parameters $a$, $b$ and $c$. From the factorization theorem we can set $u(x) = 1$. Then we get the sufficient statistics $(n,\displaystyle\sum_{i=1}^{n} x_i, \displaystyle\sum_{i=1}^{n} \log(x_i))$.
The last statistic can be rewritten to $\displaystyle\prod_{i=1}^{n} x_i$.


\section{Integrating Factor Method}
All 1st order linear differential equations have the form
\begin{equation*}
	a_1(x)\dd{y}{x} + a_0(x)y = b_1(x),
\end{equation*}
which can be rewritten as
\begin{equation*}
	\dd{y}{x} + a(x)y = b(x).
\end{equation*}

\noindent
This equation isn't always separable, and we can't just integrate both sides unless $a_1(x)y$ = 0.\\

\noindent
If $a_0(x) = a_1'(x)$, then we could rewrite the equation and solve by doing the product rule in reverse.
\begin{equation*}
	\left(a_1(x)y\right)' = b_1(x) \implies y = \frac{\int{b_1(x) \mathrm{d}x}}{a_1(x)}
\end{equation*}

\noindent
It's possible to rearrange into this form by multiplying the equation by some function. Specifically, what were looking for is a function $\mu(x)$ such that
\begin{equation*}
	\mu(x)\dd{y}{x} + \mu(x)a(x)y = \mu(x)b(x) \text{ and } \mu'(x) = \mu(x)a(x).
\end{equation*}
This equation involving $\mu(x)$ is one that we know how to solve because it's separable.\footnote{Although we are taking an indefinite integral to find $\mu(x)$, we do not have a $+ C$ term.}
\begin{equation*}
	\mu'(x) = \mu(x)a(x) \implies \mu(x) = e^{\int{a(x) \mathrm{d}x}}.
\end{equation*}
Substituting the solution for $\mu(x)$ back,
\begin{equation*}
	e^{\int{a(x) \mathrm{d}x}}\dd{y}{x} + e^{\int{a(x) \mathrm{d}x}}a(x)y = e^{\int{a(x) \mathrm{d}x}}b(x) \implies e^{\int{a(x) \mathrm{d}x}}\dd{y}{x} + \mu'(x)y = e^{\int{a(x) \mathrm{d}x}}b(x).
\end{equation*}
Applying the product rule in reverse,
\begin{equation*}
	y = \frac{\int{\mu(x)} b(x) \mathrm{d}x}{\mu(x)} \text{, } \mu(x) = e^{\int{a(x) \mathrm{d}x}}.
\end{equation*}

\begin{example}
	Solve the following 1st order linear ODE.
	\begin{equation*}
		y^\prime - y = 2e^{x}
	\end{equation*}
\end{example}
\noindent
$a(x) = -1$, so
\begin{equation*}
	\mu(x) = e^{\int{-1 \mathrm{d}x}} = e^{-x}.
\end{equation*}
Applying to our equation and solving,
\begin{equation*}
	y^\prime e^{-x} - ye^{-x} = 2 \implies \left(ye^{-x}\right)^\prime = 2 \implies ye^{-x} = 2x + C \implies y = \frac{2x + C}{e^{-x}}.
\end{equation*}


\subsection{Metric-preserving transformations and isometry groups}

If we have a bilinear form we can write the form as:

\(u^TMv\)

After a transformation \(P\) to the vectors it is:

\((Pu)^TM(Pv)\)

\(u^TP^TMPv\)

So the value of the metric will be unaffected if:

\(u^TP^TMPv=u^TMv\)

\(P^TMP=M\)

\subsubsection{Equivalent metrics}

Different metrics can produce the same group. For example multiplying the metric by a constant.

\(P^TMP=M\)


\section{Statistics}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Probability Distributions
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

\formdesc{Expected Value}

\begin{equation}
	E(X) = \mu = \sum_{i=1}^k x_i ~ P(X = x_i)
\end{equation}

for a discrete random variable with $k$ possible values.
\hformbar

\formdesc{General Variance Formula}

\begin{equation}
	Var(X) = \sigma^2 = \sum_{j=1}^k (x_j - \mu)^2 ~ P(X = x_j),
\end{equation}

or, the sum of the squared deviations $(x_j - \mu)^2$ weighted by the corresponding probabilities $P(X=x_1),  \ldots, P(X=x_k)$.
\hformbar

\formdesc{General Standard Deviation}

\begin{equation}
	\sigma = \sqrt{\sigma^2} = \sqrt{Var(X)}
\end{equation}
\hformbar

\formdesc{Linear Combinations of Variables}

\begin{equation}
	Z = aX + bY
\end{equation}

is a linear combination of the independent, random variables $X$ and $Y$ (often $a$ and $b$ are $1$ or $-1$).

\begin{eqnarray}
  E(Z) &=& a \times E(X) + b \times E(Y) \\
  Var(Z) &=& a^2 \times Var(X) + b^2 \times Var(Y)
\end{eqnarray}
\hformbar

\formdesc{Probability Density Function (PDF)}

\begin{equation}
	P(a \leq X \leq b) = \int_{a}^{b} f(x) ~ dx
\end{equation}

is a PDF of $X$, for any two numbers $a$ and $b$ where $a \leq b$. I.e., the probability that $X$ takes on a value in the interval $[a, b]$ is the area above this interval and below the graph of the density curve.

\begin{itemize}
	\item $P(X=c) = 0$ for any constant (bins are infinitesimally small)
	\item $\sum P(x_i) = 1$
\end{itemize}

\hformbar

\formdesc{Normal Distribution v. Standard Normal}

There is any entire family of distributions that can be called normal, but the
prototypical distribution with mean of $0$ and standard deviation of $1$ is
called the standard normal. Formally defined by its PDF as:

\begin{equation}
  f(x \mid \mu, \sigma^2) = \frac{1}{\sqrt{2 \pi \sigma^2}} \, e^{ \frac{-(x - \mu)^2}{2 \sigma^2}}
\end{equation}

\subsection*{Properties}

\begin{enumerate}
	\item Symmetric around mean
	\item Mean = mode = median
	\item Denser at center than in tails
\end{enumerate}

Consequently,

\begin{itemize}
	\item 68 percent of distribution is within one standard deviation of the mean
	\item 95 percent of distribution is within approximately two standard deviations of the mean
\end{itemize}
\hformbar

\formdesc{Evaluating Normality}

\begin{center}
    \includegraphics[width=1.5in]{normal_qq}
\end{center}

\begin{itemize}
	\item A normal probability plot using quartiles can be used to evaluate how closely a given distribution adheres to normality, where the straight line is a perfect normal curve
	\item As $N$ increases, the deviation from normality will decrease
\end{itemize}

\hformbar

\formdesc{Z Scores}

\begin{equation} Z = \frac{x - \mu}{\sigma}\end{equation}

converts any value from a normal distribution to its corresponding value on the standard normal distribution

\begin{itemize}
	\item Describes the number of standard deviations a point is from the mean $\mu$
	\item Z scores to the left of $\mu$ are negative, and positive to the right of $\mu$
\end{itemize}
\hformbar

\formdesc{Z Scores: Probabilities on Normal Distribution}

\subsection*{Ex. What is the probability $X > A$, given $X \sim N(\mu=1500, \sigma=300)$?}

\begin{equation}
	Z = \frac{x - \mu}{\sigma} = \frac{1630 - 1500}{300} = 0.43
\end{equation}

This is 0.6664 on Z table, so 66.64 percent of $X$ is to the left of A so:

\begin{equation}
	1 - 0.6664 = 0.3336
\end{equation}

The probability $X > A$ is 33.36 percent.

\subsection*{Ex. Given $A = 1400$ and $X \sim N(\mu=1500, \sigma=300)$, what is the percentile corresponding to A?}

\begin{equation}
	Z = \frac{x - \mu}{\sigma} = \frac{1400 - 1500}{300} = -0.33
\end{equation}

The corresponding value on the Z table is 0.3707, so $A$ is the 37th percentile.

\subsection*{Ex. Given $p = .40$ and $X \sim N(\mu=70, \sigma=3.3)$, what is the value corresponding to percentile $p$?}

Lookup $p$ on Z table, getting a $Z = -0.25$. Work backwards:

\begin{equation}
	-0.25 = Z = \frac{x - \mu}{\sigma} = \frac{x - 70}{3.3}
\end{equation}

and solve for $x = 69.18$.

\subsection*{Ex. What is the probability $X$ is between $A$ and $B$, given $X \sim N(\mu, \sigma)$?}

Using Z-scores method, find the area to the left of $A$ and to the right of $B$, then $A - B = 1 -$ area left of $A -$ are to right of $B$.
\hformbar

\formdesc{Bernoulli Distribution}

\begin{equation}
	P(X = x) = \left\{\begin{matrix}
					  p ~$for$~ x = 1\\
                      1 - p ~$for$~ x =0
                \end{matrix}\right.
\end{equation}

describes the distribution of individual trials with two possible outcomes, success or failure, described by proportion of successes $0 \leq p \leq 1$:

\begin{eqnarray}
  \hat{p}   &=& \frac{\mid successes \mid}{\mid failures \mid} \\
  \mu       &=& p \\
  \sigma^2  &=& p(1 - p)
\end{eqnarray}

\begin{itemize}
	\item The probability of success after $n$ trials is $(1 - p)^{n - 1} \times p$
\end{itemize}

\hformbar

\formdesc{Bernoulli: Geometric Distribution}

\begin{center}
    \includegraphics[width=2in]{geometric_dist}
\end{center}

describes the wait time until a success for \textit{independent} Bernoulli random variables; or, the probability of observing the $k$-th success by the $n$-th trial

\begin{eqnarray}
  \mu       &=& \frac{1}{p} \\
  \sigma^2  &=& \frac{1 - p}{p^2}
\end{eqnarray}

\begin{itemize}
	\item Higher $p$ means fewer trials until success
	\item Can never be approximated by a normal distribution
\end{itemize}

\hformbar

\formdesc{Binomial Distribution}

\begin{center}
    \includegraphics[width=2in]{binomial}
\end{center}

describes the probability of having exactly $k$ successes in $n$ independent Bernoulli trials (with probability of success $p$):

\begin{eqnarray}
	P(x = k \mid n, \mu, \sigma) &=& \begin{pmatrix}
		n\\
		k
	\end{pmatrix}
	p^k (1 - p)^{n -k} \\
	&=& \frac{n!}{k!(n - k)!} ~p^k (1 - p)^{n - k}
\end{eqnarray}

Parameters, can be used to approximate to normal when $n$ is sufficiently large and $np$ and $n(1-p)$ are both greater than or equal to 10:

\begin{eqnarray}
  \mu       &=& np \\
  \sigma^2  &=& np(1 - p)
\end{eqnarray}

\hformbar

\formdesc{Poisson Distribution}

\begin{center}
    \includegraphics[width=2in]{poisson}
\end{center}

\begin{equation}
	P(X = x \mid \lambda) = \frac{\lambda^x e^{-\lambda}}{x!}
\end{equation}

Describes the number of events in a larger population over a unit of time with rate $\lambda$:

\begin{eqnarray}
  \mu       &=& \lambda \\
  \sigma^2  &=& \lambda
\end{eqnarray}

\hformbar

%%%%%%%%%%%%%%%%%%%%%%%%
%
% Inferential Statistics
%
% See Statistical Inference by Casella p. 373 for alternative and better
% notation for this
%
%%%%%%%%%%%%%%%%%%%%%%%%

\formdesc{Inferential Statistics}

The body of thought governing the inferences of populations from samples,
and how these sample statistics can vary.
\hformbar

\formdesc{Standard Error}

\begin{equation}
	SE = \frac{\sigma}{\sqrt{n}}
\end{equation}

is the standard deviation of distributions of sample statistics, when population $\sigma$ is known. If it is unknown, and if $n > 30$, substitute sample standard deviation $s$

\begin{itemize}
	\item $SE$ decreases as $n$ increases
	\item $SE$ decreases as $\sigma$ (or $s$) decreases
\end{itemize}
\hformbar

\formdesc{Confidence Intervals}

\begin{equation}
	\bar{x} \pm z \times SE
\end{equation}

\begin{itemize}
	\item $\bar{x}$ is the sample statistic, such as sample mean
	\item $z \times SE$ is the \textit{margin of error}
	\item $z$ is the desired confidence level, e.g., $z = 1.96$ for a 95 percent confidence interval
\end{itemize}

\textit{Interpretation.} ``We are $Z$ percent confident the true population \textit{statistic} is between $A$ and $B$''; or, ``$Z$ percent of samples will have a \textit{sample statistic} between $A$ and $B$.''

\hformbar

\formdesc{Central Limit Theorem}

Given a population with a finite mean $\mu$ and a finite non-zero variance
$\sigma^2$, the sampling distribution of the mean approaches a normal
distribution with a mean of $\mu$ and a variance of $\frac{\sigma^2}{N}$, as
$N$, the sample size, increases---regardless of the shape of the parent population.
\hformbar

\formdesc{Hypothesis Testing}

\begin{table}[htp]
  \begin{center}
    \begin{tabular}{c|c}
      One-Sided      & Two-Sided       \\
      \hline
      $H_0: x = A$   & $H_0: x = A$    \\
      $H_A: x >/< A$ & $H_A: x \neq A$
    \end{tabular}
  \end{center}
\end{table}

\vspace{-2em}

The process of comparing two point estimates, to determine if any difference between them is ``real'' or the result of natural variance in samples.

\begin{itemize}
	\item \textit{Type I} errors, or false positives, occur when $H_0$  is true, but rejected
	\item \textit{Type II} errors, or false negatives, occur when $H_A$ is true, and $H_0$ is not rejected
\end{itemize}

\subsection*{Quantifying Risk}

\begin{itemize}
	\item The risk of Type I errors is quantified by $\alpha$, i.e., the probability the point estimate is more than $z^*$ standard deviations away from the true population parameter
	\item The $p$-value is the probability of observing data at least as favorable to the alternative hypothesis, i.e., as ``extreme,'' as the present data set, if $H_0$ is actually true
	\item If the $p$-value is less than the chosen $\alpha$, data is sufficient to reject $H_0$
\end{itemize}
\hformbar

\formdesc{P-Value Calculations}

\subsection*{One-Sided}

%\begin{enumerate}
%	\item Look up test statistic, e.g., $\bar{x}$, on $z$-table
%	\item Determine the probability that $Z$ is more extreme than $\bar{x}$:
%	    \begin{equation}
%	    	Z = \frac{\bar{x} - \mu}{SE}}
%	    \end{equation}
%	\item Use
%\end{enumerate}

\subsection*{Two-Sided}

\hformbar

\formdesc{Sample Proportions}

Population parameter $\pi$ is sampled:

\begin{eqnarray}
  \mu       &=& \hat{p} = \frac{\sum_n^{i=1} x_{i}}{n} \\
  SE_{\hat{p}}  &=& \sqrt{\frac{p(1-p)}{n}}
\end{eqnarray}

where $0 \leq \hat{p} \leq 1$ and $x_i = \{0, 1\}$
\hformbar

\formdesc{Sample Proportions: Confidence Intervals}

\begin{enumerate}
	\item Assess normality:
		\begin{itemize}
			\item At least 10 observations for each $\{0, 1\}$
			\item Sample is less than 10 percent of population and observations are independent
		\end{itemize}
	\item Calculate standard error $SE_{\hat{p}} = \sqrt{\frac{p(1-p)}{n}}$
	\item Determine $z^*$, e.g., 1.96
	\item Put together point estimate and margin of error:
	\begin{equation}
		\hat{p} \pm z^* \times SE_{\hat{p}}
	\end{equation}
\end{enumerate}

\hformbar

\formdesc{Sample Proportions: Hypothesis Tests}

\textbf{FIX}

\begin{eqnarray}
  H_0: \hat{p} = 0.5 \\
  H_A: \hat{p} >/\neq 0.5
\end{eqnarray}

\begin{enumerate}
	\item Evaluate normality
	\item Compute $SE_{\hat{p}}$ \textit{using null hypothesis}:
		\begin{equation}
			SE_{\hat{p}} = \sqrt{\frac{p(1-p)}{n}},
		\end{equation}
		often $ \sqrt{\frac{0.5(1-0.5)}{n}}$
	\item Calculate Z-score using hypotheses:
		\begin{equation}
			\frac{\hat{p} - \hat{p}_0}{SE_{\hat{p}}}
		\end{equation}
	\item Convert $Z$ to $p$-value and decide whether to reject the null or fail to
\end{enumerate}

\hformbar

\formdesc{Sample Proportions: Sample Size}
\hformbar

\formdesc{Difference of Proportions}
\hformbar

\formdesc{Difference of Proportions: Confidence Intervals}
\hformbar

\formdesc{Difference of Proportions: Hypothesis Tests}
\hformbar

\formdesc{Difference of Proportions: Pooled Proportion}
\hformbar

\formdesc{$\chi^2$ goodness of fit}

\begin{equation}
	\chi^2 = \sum_{k=1}^N \frac{ (observed_k - expected_k)^2 }{ expected_k }
\end{equation}

\begin{itemize}
	\item $k$ mutually exclusive classes
	\item $n$ observations of $x_i$
	\item one parameter, degrees of freedom $df$
	\item follows the chi-square distribution if null hypothesis is true
\end{itemize}

Summarizes how strongly observed count data deviates from the expected, or null, counts---larger values of $\chi^2$ indicate stronger deviation

\subsection*{Does a statistical model fit this sample?}

\begin{enumerate}
	\item Develop hypotheses:
		\begin{enumerate}
			\item $H_0$: Sample follows distribution $D$
			\item $H_A$: Sample does not follow distribution $D$
		\end{enumerate}
	\item Check assumptions
		\begin{enumerate}
			\item Each expected count must be at least 5
			\item Can use binning to get around this
		\end{enumerate}
	\item Establish expected counts (expected proportion of total count in each bin):
		\begin{equation}
			E_k = expected_k \times n
		\end{equation}
	\item Compute $\chi^2$ statistic
	\item Validate assumptions hold to apply $\chi^2$ to $\chi^2$ distribution
	\item Using $k-1$ degrees of freedom, use $\chi^2$ table to compute a $p$-value
	\item Decide to reject or fail to reject $H_0$
\end{enumerate}

\hformbar

\formdesc{$\chi^2$: $p$-value}
\hformbar

\formdesc{Two-Way Tables: Independence}
\hformbar

%%%%%%%%
%
% Trash?
%
%%%%%%%%

\formdesc{Sample Statistics: Mean and Variance}

\begin{itemize}
  \item $\mu_M = \mu$ is the mean of the sampling distribution of means
  \item $\sigma_M^2 = \frac{\sigma^2}{N}$ is the variance of the sampling
	distribution of the mean
  \item $\sigma_M = \frac{\sigma}{\sqrt{N}}$ is the standard error of the sampling
	distribution of the mean
\end{itemize}

As $N$ increases, variance of sample mean decreases
\hformbar

\formdesc{Sample Statistics: Difference in Mean}

Two samples from a population the size $n_1$ and $n_2$, calculate the means
$M_1$ and $M_2$, and the difference is $M_1 - M_2$

\begin{eqnarray}
  \mu_{M_1 - M_2} &=& M_1 - M_2 \\
  \sigma_{M_1 - M_2}^2 &=& \sigma_{M_1}^2 + \sigma_{M_2}^2 \\
  \sigma_{M_1 - M_2} &=& \sqrt{\frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}}
\end{eqnarray}

When variance and sample size are the same, standard error becomes:

\begin{equation}
  \sigma_{M_1 - M_2} = \sqrt{\frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}}i
  = \sqrt{ \frac{\sigma^2}{n} + \frac{\sigma^2}{n}}
  = \sqrt{ \frac{2 \sigma^2}{n} }
\end{equation}

If $n_1 \neq n_2$ then variance becomes:

\begin{equation}
  \sigma_{M_1 - M2}^2 = \frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}
\end{equation}

\subsection*{What is the probability that the mean of sample 1 will exceed that of
sample 2 by $N$ or more?}

\begin{enumerate}
  \item Find mean: $\mu_{M_1 - M_2} = M_1 - M_2$
  \item Find standard error: $\sigma_{M_1 - M_2}$
  \item Find area underneath distribution of sample 1 to the right of the mean
	of sample 2 plus $N$
\end{enumerate}

\hformbar

\formdesc{Sample Statistics: $r$ and $\rho$}

\begin{itemize}
  \item Not normally distributed---right-skewed---because correlation cannot
	exceed 1
  \item As $\rho$ increases, the more right-skewed the distribution
\end{itemize}

\hformbar

\formdesc{Sample Statistics: Proportion $\pi$}

Sampling proportion is closely related to the binomial distribution---the
total number of successes---where $p$ is the distribution of the mean number of
successes

\begin{eqnarray}
  \mu_p &=& \pi \\
  \sigma_p &=& \frac{\sqrt{N \pi (1 - \pi)}}{N}
           = \sqrt{\frac{\pi (1 - \pi)}{N}}
\end{eqnarray}

\subsection*{Find probability $p$ is greater than $A$}

Given $N$ and population proportion $\pi$:

\begin{enumerate}
  \item Find mean of $p = \pi$
  \item Calculate standard error as above
  \item Conduct as normal distribution given $N$ is sufficiently large and $\pi$
	is not too close to 0 or 1
\end{enumerate}

\hformbar

\formdesc{Estimation}

The process of estimating population parameters from sample statistics. Usually
results in a point estimate as well as interval estimates called confidence
intervals.

\hformbar

\formdesc{Degrees of Freedom}

\hformbar

\newpage


\section{Introduction}\label{introduction}

We start by discussing fundamental combinatorial constructions such as words of a fixed alphabet, permutations of finite
sets and the number of subsets with a given number of elements. While we give some examples of how some combinatorial problems
naturally arise, our main goal in this first section is to provide an intuitive introduction to the topic of
Enumerative Combinatorics.

\subsection{Words}

The first definition of this lecture is the definition of a word over a finite alphabet of symbols.

\begin{defn}[word]
Let $A$ be a finite set, called \textit{alphabet}. A \textit{word} is a finite sequence of elements of $A$. The \textit{size} of $A$ is its cardinality $|A|$.
The \textit{size} or \textit{length} of a word is its number of elements from $A$.
\end{defn}
\noindent
We might abuse language to a certain extent and call the elements of $A$ \textit{letters}.

\begin{exmp}
Let $A=\{1, 2, 3, 4\}$, then $112$, $132$, $1234$, $111$, $2345$, and so on, are all words over $A$.
Notice that the length of the words is not fixed.
\end{exmp}

In the study of combinatorics a very classical question is as follows. Given an alphabet $A$ of length $|A|=n$ , how many words over $A$ of length $k$ exist? This question leads us to the following theorem.

\begin{theorem}
The number of words of length $k$ in an alphabet consisting of $n$ letters is equal to $n^k$.
\label{thmNumWords}
\end{theorem}
\begin{proof}
There are $n$ possibilities for the first letter. The same holds for all other letters.
Thus, the total number of words equals
$$
\underbrace{n \cdot n \cdot n \cdots n}_\text{$k$-times} = n^k
$$
\end{proof}

The above proof is fairly simple, but there is a different possible interpretation of this problem. Let $X$ and $Y$ be sets. We might ask: what is the total
number of maps of the form $X \to Y$ without restrictions (an element in $Y$ can be hit multiple times)? It appears that this question can be solved using
Theorem \ref{thmNumWords}. For seeing this, we number the elements of $X$ and $Y$ using natural numbers.
Given a map $f:X \to Y$, the sequence $f(1),f(2), \cdots, f(k)$ forms a word of length $k$. The number of words is therefore equivalent to the number of such maps.

\subsection{Permutations}

A notion that is closely related to the definition of a word, is the definition of a \textit{permutation}. First of all, the basic idea is to count maps, but
usually with certain properties. Let $X$ and $Y$ be sets with $|X|=k$ and $|Y|=n$. We might ask: how many bijections $f:X \to Y$ are there? Recall that
a map is bijective if it is surjective and injective. In other words, given two elements $x,y \in X$ such that $f(x)=f(y)$, then it is implied that $x=y$ (the injective property), and
every element of $Y$ appears in the preimage of a certain element of $X$ (the surjective property). It follows that the cardinalities of $X$ and $Y$ must be equal.
These observations lead us to the definition of a permutation.

\begin{defn}[permutation]
A \textit{permutation} is a bijection from $\{1, 2, 3, \cdots, n\}$ to itself.
\end{defn}
\noindent
In that sense the number of permutations is the number of bijections from $\{1, 2, \cdots, n\}$ to itself.
Equivalently, a permutation might be interpreted as a total linear ordering of the set $\{1, 2, 3, \cdots, n\}$.
If so, we need to arrange them in a certain way. How many ways to do this are there?
The following theorem provides one possible solution.

\begin{theorem}
The number of permutations of $\{1, 2, \cdots, n\}$ is equal to $n!=1\cdot 2 \cdot 3 \cdots n$.\footnote{Recall that $0!=1$.}
\end{theorem}

\begin{proof}
There are $n$ choices for the first element. For the second element there are $n-1$ choices since
one element is already occupied. For the $k$-th element there are $n-k+1$ choices. For the last element there is one choice.
Thus, the total number of permutations is given by $n(n-1)(n-2)\cdots 2 \cdot 1=n!$. This completes the proof.
\end{proof}

In the following, we will generalize the above result to injective maps. Let $k \leq n$. In how many ways can we produce a collection
of $k$ elements out of $n$? Such a collection is called a $k$-permutation.

\begin{defn}[$k$-permutation]
A $k$-permutation is a total linear ordering on a $k$-element subset of $\{1, 2, \cdots, n\}$.
\end{defn}

\begin{exmp}
Let $\{1,2 ,3\}$. A $1$-permutation is just a $1$-element subset, thus there is nothing order. The set of
$2$-permutations is given by $\{\{1,2\}, \{1,3\}, \{2,1\}, \{2,3\}, \{3,1\}, \{3,2\}\}$.
Thus , there are six $2$-permutations in total.
\end{exmp}

The general case of the number of $k$-permutations is examined in the following theorem.

\begin{theorem}
The number of $k$-permutations of $\{1,2, \cdots, n\}$ is equal to $$n(n-1)\cdots (n-k+1).$$
\end{theorem}

\begin{proof}
There are $n$ possible choices for the first entry of the subset. There are $(n-1)$ possible choices for the second element because the second element can occupy any entry except the one of the first element.
For the $k$-th entry there are $n-k+1$ possible choices. All these possibilities are independent, hence
the total number of $k$-permutations is equal to their product $n(n-1)\cdots (n-k+1)$.
\end{proof}

The number $n(n-1)\cdots (n-k+1)$ is sometimes called the $k$ \textit{decreasing power} and is denoted by $n^{\downarrow k}$.

\subsection{Merry-go-rounds and Fermat's little theorem}
The problem which we are going to discuss next will illustrate a connection of combinatorics to algebra and possibly number theory.
\\
Consider a train $Q$ consisting of $p$ carriages. Suppose that we have $n$ colours. In how many ways can we paint $Q$, if we can paint
each carriage with one of the $n$ colours? Consider figure \ref{fig:marrygoroundtrain} as an example. There is a train with $5$ carriages which
are coloured in a certain way using $3$ different colours.

\begin{figure}[ht]
    \centering
    \incfig{marrygoroundtrain}
    \caption{Initial train colouring.}
    \label{fig:marrygoroundtrain}
\end{figure}

Now, this is exactly the word problem, thus the solution is given by $n^p$.
\\
\noindent
\textbf{Question:} Suppose that instead of a train we have a merry-go-round and suppose further, for some reason, that $p$ is prime.
In how many ways can we paint each carriage of a merry-go-round using $n$ colours?

\begin{figure}[ht!]
    \centering
    \scalebox{.3}{\incfig{marrygoroundinitialcase}}
    \caption{Marry-go-round initial case.}
    \label{fig:marrygoroundinitialcase}
\end{figure}

In figure \ref{fig:marrygoroundinitialcase} a marry-go-round with $5$ carriages is visualized. They are again coloured using $3$ colours.\\
What is the difference to the problem we have considered before? In this case we can rotate the carriages without changing its colours, but these colourings are considered to be identical.
\\
\\
\textbf{Idea:} We cut the merry-go-round (consider the two lines in figure \ref{fig:marrygoroundinitialcase} as the "cutting point") and make a train out of it and there are exactly $p$ places on which we could make a cut.
\\
\\
The first naive guess would be that the solution to our problem is equal to $\frac{n^p}{p}$, since we can get $p$ trains from each merry-go-round. This is wrong for an obvious reason. The number $\frac{n^p}{p}$
is by no means always integer. So let us trying to understand what is wrong with this reasoning. First, if we cut at two different places in our merry-go-round, then we could get the same train. An easy way for seeing this
is to consider a merry-go-round in which each carriage is coloured using the same colour. There are still $p$ ways to make a train out of it but each of them is identical. Compare this reasoning with figure \ref{fig:marrygoroundonecolour}.

\begin{figure}[ht]
    \centering
    \incfig{marrygoroundonecolour2}
    \caption{Marry-go-round with only one colour.}
    \label{fig:marrygoroundonecolour}
\end{figure}

To say it in different words, if we rotate a merry-go-round by an arbitrary number of times, it will eventually coincide with itself, thus if a merry-go-round goes to itself after some nontrivial rotation, then all carriages have the same colour.
Now, notice that this is not the case if not all carriages have the same colour, since then we get exactly $p$ different trains, but observe that this is only true provided that $p$ is prime. In order to see this, consider a merry-go-round with $4$ carriages,
and assume that we have two colours. black and white. Then, there are only two different trains which we can get from this merry-go-round. Colour the merry-go-round as follows: black - white - black - white. Then after two rotations, we get the same train. Thus,
instead of four trains we get two. However, if $p$ is prime, then our claim is indeed true.
\newpage
If our merry-go-round has carriages of different colours, then we can compute the number of such merry-go-rounds.
It is the number of trains divided by $p$. What is the number of trains which have different colors?
This is $n$ to the power of $p$, which is the total number of trains, minus the number of trains of the same color, which is just $n$ and for each color, there is only one train for which all carriages have this given colour.
Hence, our final result is given by the following equation.
$$
\frac{n^p-n}{p} + p
$$

It is again surprising that the above number is always an integer. This is not obvious at all and proving this using number theory is quite more involved. In general, we have obtained a very nice result given by the following theorem.

\begin{theorem}
Given $n \in \mathbb{N}$ and a prime $p$, then $n^p -n\ |\ p$ ($n^p - n$ is divisible by $p$).
\end{theorem}
\noindent
The above is Fermat's little Theorem and we have proven it in a completely combinatorial way.

\subsection{Binomial coefficients}

Next, we consider the following important definition.

\begin{defn}[bionmial coefficient]
Let $k \leq n$. The number of $k$-element subsets of $\{1, 2, \cdots, n\}$ is called a \textit{binomial coefficient} ${n \choose k}$ ("$n$ choose $k$").
\end{defn}

How do we interpret this number? Suppose that we are hiring people to certain positions and suppose that we have $n$ candidates and we have $k$ identical positions.
The number of selecting $k$ people out of $n$ is called ${n \choose k}$. Notice that this is different than the problem from problem about $k$-permutations.
In our current case, the order of the $k$ elements that we choose does not matter. Thus, choosing all $k$ element subsets of a set and then ordering them in all possible ways
is equal to the $k$-permutations that we have seen above. How do we count the number of $k$-element subsets of a set?

\begin{theorem}
The number of $k$-element subsets of a set with cardinality $n$ is equal to
$$
{n \choose k}=\frac{n!}{k!(n-k)!}=\frac{n(n-1)\cdots (n-k+1)}{k!}.
$$
\label{thmBinomial}
\end{theorem}

\begin{proof}
We consider ordered collections of distinct elements of length $k$. There are $n$ ways of choosing the first element, and $n-k+1$ ways of choosing the $k$-th element.
Hence there are
$$
n(n-1)\cdots (n-k+1)=\frac{n!}{(n-k)!}
$$
ways such ordered collections. Since we are concerned with the number of unordered subsets and each subset has cardinality $k$, each subset can be rearranged in $k!$ ways.
Therefore, the number of ways to choose a $k$-element subset of a set with cardinality $n$ is given by
$$
\frac{\frac{n!}{(n-k)!}}{k!}=\frac{n!}{k!(n-k)!}.
$$
This completes the proof.
\end{proof}

With the above theorem, we have shown something additionally that is not obvious at all and not necessarily easy to prove either.
We have also shown that the number $\frac{n(n-1)\cdots (n-k+1)}{k!}$ is integer.

At this point we provide some properties of binomial coefficients. We leave the proof as an easy exercise to the reader.

\begin{enumerate}[(i)]
    \item $\displaystyle {n \choose 0}={n \choose n}=1$.
    \item $\displaystyle {n \choose 1}={n \choose n-1}=n$
    \item $\displaystyle {n \choose 1}={n \choose n-k}$.
    \item $\displaystyle {n \choose k}={n - 1 \choose k -1} + {n - 1 \choose k}$.
\end{enumerate}

Let us examine property $(iv)$ in more detail. Despite its obvious algebraic proof, there is an intuitive combinatorial version as well.
We provide a proof sketch.

\begin{theorem}
$\displaystyle {n \choose k}={n - 1 \choose k -1} + {n - 1 \choose k}$
\label{recurrencePascal}
\end{theorem}

\begin{proof}[Proof-sketch]
Given a set $A=\{1, \cdots, n\}$ with $n$ elements. The number of $k$-element subsets of $A$ is ${n \choose k}$ according to theorem \ref{thmBinomial}.
Suppose that we pick the element $1$ of $A$, then all $k$-element subsets of $A$ are divided into two parts, some of them contain the element $1$, and the others do not.
Since we have already picked one element, we need to pick $k-1$ elements out of the remaining $n-1$ elements. There are ${n-1 \choose k-1}$ subsets containing $1$.
Thus, the subsets of cardinality $k$ that do not contain $1$ is equal to ${n - 1 \choose k}$, because we want to pick a $k$-element subset out of $n-1$ elements since $1$ element is forbidden.
\\
Adding up the two parts are equal to ${n \choose k}$. This completes the proof.
\end{proof}

\subsection{The Pascal triangle}

Binomial coefficients can be arranged in a triangle which is called the Pascal triangle.
Its rows are indexed by integers from $0$ up to infinity and in each row we write down all binomial coefficients.
For example, for ${n \choose k}$, $n=4$ represents the fifth row of the Pascal triangle and since $k \leq n$ there are $5$ binomial coefficients, namely ${4 \choose 0}=1, {4 \choose 1}=4, {4 \choose 2}=6, {4 \choose 3}=4$ and ${4 \choose 4}=1$.
\\
The relation given In Theorem \ref{recurrencePascal} provides a way of constructing the Pascal triangle. It says that the sum of two neighbors, namely ${n-1 \choose k-1}$ and ${n-1 \choose k}$ is equal to ${n \choose k}$. Thus, taking two neighbors in one row and
adding them up gives as the element below them (e.g. $3+3$ in row $n=3$ provides the element $6$ in the row $n=4$).
\\
\\
\begin{tabular}{>{$n=}l<{$\hspace{12pt}}*{13}{c}}
    0 &&&&&&&1&&&&&&\\
    1 &&&&&&1&&1&&&&&\\
    2 &&&&&1&&2&&1&&&&\\
    3 &&&&1&&3&&3&&1&&&\\
    4 &&&1&&4&&6&&4&&1&&\\
    5 &&1&&5&&10&&10&&5&&1&\\
    6 &1&&6&&15&&20&&15&&6&&1
\end{tabular}

\begin{proposition}
$\displaystyle {n \choose 0} + {n \choose k} + \cdots + {n \choose n}=2^n.$
\label{propositionPascalTriangle}
\end{proposition}

\begin{proof}
The binomial coefficient ${n \choose k}$ is the number of $k$-element subsets of a $n$-element set $A=\{1, \cdots, n\}$. Hence,
$$
\sum_{k=0}^n {n \choose k}
$$
is equal to the number of all possible subsets of $A$. We can associate a subset of $A$ with a binary number of length $n$
whereby each digit in this binary number represents a specific element in $A$. Now, setting a digit to $1$ means that we pick the element from $A$ at this position
for our subset. Setting it to $0$ means that we do not pick it. This is equal to the number of maps $\{1, \cdots, n\} \to \{0,1\}$ and the total number of such maps is equal to $2^n$ or in other words, the number of binary numbers that
can be represented with $n$ digits is equal to $2^n$. This completes the proof.
\end{proof}

We call the above binomial coefficients, because they arise as coefficients in the binomial formulas $(a+b)^n$. Let us prove this.

\begin{theorem}(Newton's Binomial Theorem)
$$
(a+b)^n = \sum_{k=0}^n {n \choose k} a^{n-k} \cdot b^k=a^n+n \cdot a^{n-1} \cdot b+ \frac{n(n-1)}{2} a^{n-2} \cdot b^2+ \cdots + n \cdot a \cdot b^{n-1}+b^n
$$
\label{newtonBinomialThm}
\end{theorem}

\begin{proof}
We expand the left-hand-side.
$$
(a+b)^n=\underbrace{(a+b)(a+b)(a+b)\cdots (a+b)}_{n-times}
$$
Every term in the above sum is the result of choosing either $a$ or $b$ from each of the $n$ factors. We want to calculate the coefficient for $a^{n-k}b^k$.
Since $b^k$, we must choose $b$ from exactly $k$ of the factors and we choose $a$ from the remaining $n-k$ factors.
Thus, the coefficients for $a^{n-k}b^k$ is equal to ${n \choose k}$. This completes the proof.
\end{proof}

\begin{corolarry}
$\displaystyle {n \choose 0}- {n \choose 1} + {n \choose 2} - {n \choose 3} + \cdots + (-1)^n {n \choose n}=0$
\end{corolarry}

\begin{proof}
Consider the binomial formula as provided in Theorem \ref{newtonBinomialThm}. Now, let $a=1$ and $b$ (or vice versa), it follows that
$$
(1-1)^n= \sum (-1)^k {n \choose k}.
$$
\end{proof}

Notice, that Newton's binomial Theorem provides an easy proof for proposition \ref{propositionPascalTriangle} as well by considering $(1+1)^n=\sum {n \choose k}$.

\subsection{Exercises}

\begin{enumerate}
    \item Find the number of subsets of a $10$-element set with an odd number of elements.
    \item Compute the sum $\displaystyle \sum_{k=0}^n 2^k {n \choose k}$.
    \item Find the number of ways to rearrange the letters of the word \textit{EULER}.
    \item Find the number of ways to rearrange the letters of the word \textit{CATALAN}.
    \item How many necklaces can be formed with $7$ distinct beads? Necklaces can be rotated and/or flipped over. All beads must be used.
\end{enumerate}