\documentclass[a4paper]{article}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage{array}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tcolorbox}
\usepackage{fancyhdr}
\usepackage{pgfplots}
\usepackage{tabularx}
\usepackage{keystroke}
\usepackage{listings}
\usepackage{xcolor} % used only to show the phantomed stuff
\definecolor{cas}{HTML}{e6f0fe}
\usepackage{mathtools}
\pgfplotsset{compat=1.16}

\pagestyle{fancy}
\fancyhead[LO,LE]{Unit 4 Specialist --- Statistics}
\fancyhead[CO,CE]{Andrew Lorimer}

\setlength\parindent{0pt}

\begin{document}

  \title{Statistics}
  \author{}
  \date{}
  \maketitle

  \section{Linear combinations of random variables}

  \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}

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

  \subsubsection*{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*{Linear combination of two random variables}

  \begin{align*}
    \textbf{Mean:} && \operatorname{E}(aX+bY) & = a\operatorname{E}(X)+b\operatorname{E}(Y) \\
    \textbf{Variance:} && \operatorname{Var}(aX+bY) &= a^2 \operatorname{Var}(X) + b^2 \operatorname{Var}(Y) \tag{if \(X\) and \(Y\) are independent}\\
  \end{align*}

  \section{Sample mean}

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

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

  where \(n\) is the size of the sample (number of sample points) and \(x\) is the value of a sample point

  \begin{tcolorbox}[colframe=cas!75!black, title=On CAS]

  \begin{enumerate}
    \item Spreadsheet
    \item In cell A1: \verb;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{tcolorbox}

  \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{tcolorbox}[colframe=cas!75!black, title=On 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{tcolorbox}
  
  \section{Normal distributions}

  mean = mode = median

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

  Normal distributions must have area (total prob.) of 1 \(\implies \int^\infty_{-\infty} f(x) \> dx = 1\)
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

{\begin{center} \begin{tikzpicture}
  \pgfplotsset{set layers}
\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-3:3,samples=50,smooth}, 
  axis x line=bottom, 
  axis y line=left,
  enlargelimits=upper,
  x=\textwidth/10,
  ytick={0.55},
  yticklabels={\(\frac{1}{\sigma \sqrt{2\pi}}\)}, 
  xtick={-2,-1,0,1,2},
  x tick label style = {font=\footnotesize},
  xticklabels={\((\mu-2\sigma)\), \((\mu-\sigma)\), \(\mu\), \((\mu+\sigma)\), \((\mu+2\sigma)\)},
  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.2)}, anchor=south west, rotate=90},
  ylabel={\(\Pr(X=x)\)}]
  \addplot {gauss(0,0.75)};
\end{axis}
\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-3:3,samples=50,smooth}, 
  axis x line=bottom, 
  enlargelimits=upper,
  x=\textwidth/10,
  xtick={-2,-1,0,1,2},
  axis x line shift=30pt,
  hide y axis,
  x tick label style = {font=\footnotesize},
  xlabel={\(Z\)},
  every axis x label/.style={at={(axis description cs:1,-0.25)},anchor=south west}]
  \addplot {gauss(0,0.75)};
\end{axis}
\end{tikzpicture}\end{center}}

  \section{Central limit theorem}

  If \(X\) is randomly distributed with mean \(\mu\) and sd \(\sigma\), then with an adequate sample size \(n\) the distribution of the sample mean \(\overline{X}\) is approximately normal with mean \(E(\overline{X})\) and \(\operatorname{sd}(\overline{X}) = \frac{\sigma}{\sqrt{n}}\).

  \section{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\)
  \end{itemize}

  \subsection*{95% confidence interval}

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

  where: \\
  \(\mu\) is the population mean (unknown) \\
  \(\overline{x}\) is the sample mean \\
  \(\sigma\) is the population sd \\
  \(n\) is the sample size from which \(\overline{x}\) was calculated

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

  \colorbox{cas}{\textbf{On CAS}}

  Menu \(\rightarrow\) Stats \(\rightarrow\) Calc \(\rightarrow\) Interval \\
  Set Type = One-Sample Z Int, Variable

  \subsection*{Interpretation of confidence intervals}

  95% confidence interval \(\implies\) 95% of samples will contain population mean \(\mu\).

  \subsection*{Margin of error}

  For 95% confidence interval for \(\mu\), margin of error \(M\) is:

  \begin{align*}
    M &= 1.96 \times \dfrac{\sigma}{\sqrt{n}} \\
    \implies n &= \left( \dfrac{1.96 \sigma}{M} \right)^2
  \end{align*}

  \subsection*{General case}

  A confidence interval of \(C\)% is given by

  A 95% confidence interval for \(\mu\) will have \(M\) when
  \[ \overline{x} \pm k \dfrac{\sigma}{\sqrt{n}} \]

  where \(k\) is such that \(\Pr(-k < Z < k) = \frac{C}{100}\)


\end{document}
