\documentclass[methods-collated.tex]{subfiles}

\begin{document}

\section{Statistics}

\subsection*{Probability}

\begin{align*}
  \Pr(A \cup B) &= \Pr(A) + \Pr(B) - \Pr(A \cap B) \\
  \Pr(A \cap B) &= \Pr(A|B) \times \Pr(B) \\
  \Pr(A|B) &= \frac{\Pr(A \cap B)}{\Pr(B)} \\
  \Pr(A) &= \Pr(A|B) \cdot \Pr(B) + \Pr(A|B^{\prime}) \cdot \Pr(B^{\prime})
\end{align*}

Mutually exclusive: \(\Pr(A \cap B) = 0\) \\

Independent events:
\begin{flalign*}
  \quad \Pr(A \cap B) &= \Pr(A) \times \Pr(B)& \\
  \Pr(A|B) &= \Pr(A) \\
  \Pr(B|A) &= \Pr(B)
\end{flalign*}

\subsection*{Combinatorics}

\begin{align*}
  \text{Arrangements} && {n \choose k} & = \frac{n!}{(n-k)} \\
  \text{Combinations} && {n \choose k} & = \frac{n!}{k!(n-k)!}
\end{align*}

Note \({n \choose k} = {n \choose k-1}\)

\begin{cas}
  Keyboard \(\rightarrow\) Advance \(\rightarrow\) \keystroke{nCr}/\keystroke{nPr} \\
  \-\hspace{1em} \texttt{nCr(n, r)} or \texttt{nPr(n, r)}
\end{cas}

\subsection*{Distributions}

\begin{tikzpicture}
  \begin{axis}[axis lines=left,
    ticks=none,
    xmin=0,
    ymax=0.5,
    enlargelimits=upper,
    ylabel={\(\Pr(X=x)\)},
    xlabel={\(x\)},
    every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
    every axis y label/.style={at={(axis description cs:-0.02,0.5)}, anchor=south west, rotate=90},
    ]
    \fill[pattern=north east lines, pattern color=orange] (0,0)  -- plot[domain=0:1.68, samples=50] function {abs(x)*exp(-x)} -- (1.68,0) -- cycle;
    \fill[pattern=north west lines, pattern color=red] (1.68,0)  -- plot[domain=1.68:5, samples=50] function {abs(x)*exp(-x)} -- (5,0) -- cycle;
    \draw[dashed, blue, very thick] (axis cs:1.68,0) -- (axis cs:1.68,0.31) node [above, anchor=south west, black] {Median};
    \draw[dashed, blue, very thick] (axis cs:2,0) -- (axis cs:2,0.27) node [above, anchor=west, black] {Mean};
    \draw[dashed, blue, very thick] (axis cs:1,0) -- (axis cs:1,0.365) node [above, black] {Mode};
    \node at (1,0.18) {\textbf{50\%}};
    \node at (3.1,0.08) {\textbf{50\%}};
    \addplot[thick, black, no markers, samples=200, domain=0:5] {abs(x)*exp(-x)};
  \end{axis}
\end{tikzpicture}

\subsubsection*{Mean \(\mu\)}

\begin{align*}
  E(X) &= \frac{\Sigma \left[ x \cdot f(x) \right]}{\Sigma f} \tag{\(f =\) absolute frequency} \\
  &= \sum_{i=1}^n \left[ x_i \cdot \Pr(X=x_i) \right] \tag{discrete}\\
  &= \int_\textbf{X} (x \cdot f(x)) \> dx
\end{align*}

\subsubsection*{Mode}

Value of \(X\) which has the highest probability

\begin{itemize} \tightlist
  \item Most popular value in discrete distributions
  \item Must exist in distribution
  \item Represented by local max in pdf
  \item Multiple modes exist when \(>1 \> X\) value have equal-highest probability
\end{itemize}

\subsubsection*{Median}

Value separating lower and upper half of distribution area

\textbf{Continuous:}
\[ m = X \> \text{such that} \> \int_{-\infty}^{m} f(x) \> dx = 0.5 \]

\textbf{Discrete:} (not in course)
\begin{itemize} \tightlist
  \item Does not have to exist in distribution
  \item Add values of \(X\) smallest to largest until sum is \(\ge 0.5\)
  \item If \(X_1 < 0.5 < X_2\), then median is the average of \(X_1\) and \(X_2\)
  \begin{itemize}\tightlist
    \item If \(m > 0.5\), then value of \(X\) that is reached is the median of \(X\)
  \end{itemize}
\end{itemize}

\subsubsection*{Variance \(\sigma^2\)}

\begin{align*}
  \operatorname{Var}(x) &= \sum_{i=1}^n p_i (x_i-\mu)^2 \\
  &= \sum (x-\mu)^2 \times \Pr(X=x) \\
  &= \sum x^2 \times p(x) - \mu^2 \\
  &= \operatorname{E}(X^2) - [\operatorname{E}(X)]^2 \\
  &= E\left[(X-\mu)^2\right]
\end{align*}

\subsubsection*{Standard deviation \(\sigma\)}

\begin{align*}
  \sigma &= \operatorname{sd}(X) \\
  &= \sqrt{\operatorname{Var}(X)}
\end{align*}

\subsection*{Binomial distributions}

Conditions for a \textit{binomial distribution}:
\begin{enumerate} \tightlist
  \item Two possible outcomes: \textbf{success} or \textbf{failure}
  \item \(\Pr(\text{success})\) (=\(p\)) is constant across trials
  \item Finite number \(n\) of independent trials
\end{enumerate}


\subsubsection*{Properties of \(X \sim \operatorname{Bi}(n,p)\)}

\begin{align*}
  \mu(X) &= np \\
  \operatorname{Var}(X) &= np(1-p) \\
  \sigma(X) &= \sqrt{np(1-p)} \\
  \Pr(X=x) &= {n \choose x} \cdot p^x \cdot (1-p)^{n-x}
\end{align*}

\begin{cas}
  Interactive \(\rightarrow\) Distribution \(\rightarrow\) \verb;binomialPdf;
  \begin{description}[nosep, style=multiline, labelindent=0.5cm, leftmargin=3cm, font=\normalfont]
    \item [x:] no. of successes
    \item [numtrial:] no. of trials
    \item [pos:] probability of success
  \end{description}
\end{cas}

\subsection*{Continuous random variables}

A continuous random variable \(X\) has a pdf \(f\) such that:

\begin{enumerate}
  \item \(f(x) \ge 0 \forall x \)
  \item \(\int^\infty_{-\infty} f(x) \> dx = 1\)
\end{enumerate}

\begin{align*}
  E(X) &= \int_\textbf{X} (x \cdot f(x)) \> dx \\
  \operatorname{Var}(X) &= E\left[(X-\mu)^2\right]
\end{align*}

\[ \Pr(X \le c) = \int^c_{-\infty} f(x) \> dx \]

\begin{cas}
  Define piecewise functions: \\
  \-\hspace{1em}Math3 \(\rightarrow\)
  \begin{tikzpicture}%
    \draw rectangle (0.5,0.5); 
    \node at (0.08,0.25) {\(\{\)};
    \filldraw [black] (0.15, 0.4) rectangle(0.25, 0.3);
    \draw (0.35, 0.4) rectangle(0.45, 0.3);
    \node [font=\footnotesize] at (0.3,0.3) {\verb;,;};
    \draw (0.15, 0.2) rectangle(0.25, 0.1);
    \node [font=\footnotesize] at (0.3,0.1) {\verb;,;};
    \draw (0.35, 0.2) rectangle(0.45, 0.1);
  \end{tikzpicture}
  % TODO: finish this section
\end{cas}

\subsection*{Two random variables \(X, Y\)}

If \(X\) and \(Y\) are independent:
\begin{align*}
  \operatorname{E}(aX+bY) & = a\operatorname{E}(X)+b\operatorname{E}(Y) \\
  \operatorname{Var}(aX \pm bY \pm c) &= a^2 \operatorname{Var}(X) + b^2 \operatorname{Var}(Y)
\end{align*}

\subsection*{Linear functions \(X \rightarrow aX+b\)}

\begin{align*}
  \Pr(Y \le y) &= \Pr(aX+b \le y) \\
  &= \Pr\left(X \le \dfrac{y-b}{a}\right) \\
  &= \int^{\frac{y-b}{a}}_{-\infty} f(x) \> dx
\end{align*}

\begin{align*}
  \textbf{Mean:} && \operatorname{E}(aX+b) & = a\operatorname{E}(X)+b \\
  \textbf{Variance:} && \operatorname{Var}(aX+b) &= a^2 \operatorname{Var}(X) \\
\end{align*}

\subsection*{Expectation theorems}

For some non-linear function \(g\), the expected value \(E(g(X))\) is not equal to \(g(E(X))\).

\begin{align*}
  E(X^2) &= \operatorname{Var}(X) - \left[E(X)\right]^2 \\
  E(X^n) &= \Sigma x^n \cdot p(x) \tag{non-linear} \\
  &\ne [E(X)]^n \\
  E(aX \pm b) &= aE(X) \pm b \tag{linear} \\
  E(b) &= b \tag{\(\forall b \in \mathbb{R}\)}\\
  E(X+Y) &= E(X) + E(Y) \tag{two variables}
\end{align*}

\begin{figure*}[hb]
  \centering
  \include{../spec/normal-dist-graph}
\end{figure*}

\subsection*{Sample mean}

Approximation of the \textbf{population mean} determined experimentally.

\[ \overline{x} = \dfrac{\Sigma x}{n} \]

where
\begin{description}[nosep, labelindent=0.5cm]
  \item \(n\) is the size of the sample (number of sample points)
  \item \(x\) is the value of a sample point
\end{description}

\begin{cas}
  \begin{enumerate}[leftmargin=3mm]
    \item Spreadsheet
    \item In cell A1:\\ \path{mean(randNorm(sd, mean, sample size))}
    \item Edit \(\rightarrow\) Fill \(\rightarrow\) Fill Range
    \item Input range as A1:An where \(n\) is the number of samples
    \item Graph \(\rightarrow\) Histogram
  \end{enumerate}
\end{cas}

\subsubsection*{Sample size of \(n\)}

\[ \overline{X} = \sum_{i=1}^n \frac{x_i}{n} = \dfrac{\sum x}{n} \]

Sample mean is distributed with mean \(\mu\) and sd \(\frac{\sigma}{\sqrt{n}}\) (approaches these values for increasing sample size \(n\)).

For a new distribution with mean of \(n\) trials, \(\operatorname{E}(X^\prime) = \operatorname{E}(X), \quad \operatorname{sd}(X^\prime) = \dfrac{\operatorname{sd}(X)}{\sqrt{n}}\)

\begin{cas}

  \begin{itemize}
    \item Spreadsheet \(\rightarrow\) Catalog \(\rightarrow\) \verb;randNorm(sd, mean, n); where \verb;n; is the number of samples. Show histogram with Histogram key in top left
    \item To calculate parameters of a dataset: Calc \(\rightarrow\) One-variable
  \end{itemize}

\end{cas}

\subsection*{Population sampling}

\subsubsection*{Population proportion}

\[ p = \dfrac{n \text{ with attribute in population}}{\text{population size}} \]

Constant for a given population.

\subsection*{Sample proportion}

\[ \hat{p} = \dfrac{n \text{ with attribute in sample}}{\text{sample size}} \]

Varies with each sample.

\subsection*{Normal distributions}


\[ Z = \frac{X - \mu}{\sigma} \]

Normal distributions must have area (total prob.) of 1 \(\implies \int^\infty_{-\infty} f(x) \> dx = 1\) \\
\(\text{mean} = \text{mode} = \text{median}\)

\begin{warning}
  Always express \(z\) as +ve. Express confidence \textit{interval} as ordered pair.
\end{warning}

\subsection*{Confidence intervals}

\begin{itemize}
  \item \textbf{Point estimate:} single-valued estimate of the population mean from the value of the sample mean \(\overline{x}\)
  \item \textbf{Interval estimate:} confidence interval for population mean \(\mu\)
  \item \(C\)\% confidence interval \(\implies\) \(C\)\% of samples will contain population mean \(\mu\)
\end{itemize}

\begin{cas}
  Menu \(\rightarrow\) Stats \(\rightarrow\) Calc \(\rightarrow\) Interval \\
  Set \textit{Type = One-Sample Z Int} \\ \-\hspace{1em} and select \textit{Variable}
\end{cas}

\subsubsection*{95\% confidence interval}

For 95\% c.i. of population mean \(\mu\):

\[ x \in \left(\overline{x} \pm 1.96 \dfrac{\sigma}{\sqrt{n}} \right)\]

where:
\begin{description}[nosep, labelindent=0.5cm]
  \item \(\overline{x}\) is the sample mean
  \item \(\sigma\) is the population sd
  \item \(n\) is the sample size from which \(\overline{x}\) was calculated
\end{description}

\subsubsection*{Confidence interval of \(p\) from \(\hat{p}\)}

\[ x \in \left( \hat{p} \pm Z \sqrt{\dfrac{\hat{p}(1-\hat{p})}{n}} \right) \]

\subsection*{Margin of error}

For 95\% confidence interval of \(\mu\):
\begin{align*}
  M &= 1.96 \times \dfrac{\sigma}{\sqrt{n}} \\
  &= \dfrac{1}{2} \times \text{width of c.i.} \\
  \implies n &= \left( \dfrac{1.96 \sigma}{M} \right)^2
\end{align*}

Always round \(n\) up to a whole number of samples.

\subsection*{General case}

For \(C\)\% c.i. of population mean \(\mu\):

\[ x \in \left( \overline{x} \pm k \dfrac{\sigma}{\sqrt{n}} \right) \]
\hfill where \(k\) is such that \(\Pr(-k < Z < k) = \frac{C}{100}\)

\begin{cas}
  Menu \(\rightarrow\) Stats \(\rightarrow\) Calc \(\rightarrow\) Interval \\
  Set \textit{Type = One-\colorbox{important}{Prop} Z Int} \\
  Input  x \(= \hat{p} * n\)
\end{cas}

\subsection*{Confidence interval for multiple trials}

For a set of \(n\) confidence intervals (samples), there is \(0.95^n\) chance that all \(n\) intervals contain the population mean \(\mu\).

\end{document}
