[methods] fix typo in transformations fraction
[notes.git] / spec / statistics.tex
index ee414c6960e80ff085e954ebca1c475b9e59d6bc..bc64a9807a62a8c9c24a221623bb197cd7aca407 100644 (file)
@@ -1,33 +1,7 @@
-\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}
-
+\documentclass[spec-collated.tex]{subfiles}
 \begin{document}
 
-  \title{Statistics}
-  \author{}
-  \date{}
-  \maketitle
-
-  \section{Linear combinations of random variables}
+  \section{Statistics}
 
   \subsection*{Continuous random variables}
 
     \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 \]
+  
 
-  \subsubsection*{Linear functions \(X \rightarrow aX+b\)}
+  \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) \\
     \textbf{Variance:} && \operatorname{Var}(aX+b) &= a^2 \operatorname{Var}(X) \\
   \end{align*}
 
-  \subsection*{Linear combination of two random variables}
+  \subsection*{Expectation theorems}
+
+  For some non-linear function \(g\), the expected value \(E(g(X))\) is not equal to \(g(E(X))\).
 
   \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}\\
+    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*}
 
-  \section{Sample mean}
+  \subsection*{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]
+  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{enumerate}
+\begin{cas}
+  \begin{enumerate}[leftmargin=3mm]
     \item Spreadsheet
-    \item In cell A1: \verb;mean(randNorm(sd, mean, sample size));
+    \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{tcolorbox}
+  \end{cas}
 
   \subsubsection*{Sample size of \(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{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}
+    \hspace{1em} 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.
+
+    To calculate parameters of a dataset: \\
+    \-\hspace{1em}Calc \(\rightarrow\) One-variable
+
+  \end{cas}
   
-  \section{Normal distributions}
+  \subsection*{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}
+  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}
+
+  \begin{figure*}[hb]
+    \centering
+    \include{normal-dist-graph}
+  \end{figure*}
+
+  \subsection*{Central limit theorem}
+
+  \begin{theorembox}{}
+    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}}\).
+  \end{theorembox}
+
+  \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}
 
-  \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.
+  \subsubsection*{95\% confidence interval}
 
-  \colorbox{cas}{\textbf{On CAS}}
+  For 95\% c.i. of population mean \(\mu\):
 
-  Menu \(\rightarrow\) Stats \(\rightarrow\) Calc \(\rightarrow\) Interval \\
-  Set Type = One-Sample Z Int, Variable
+  \[ x \in \left(\overline{x} \pm 1.96 \dfrac{\sigma}{\sqrt{n}} \right)\]
 
-  \subsection*{Interpretation of confidence intervals}
+  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}
 
-  95% confidence interval \(\implies\) 95% of samples will contain population mean \(\mu\).
+  \begin{cas}
+    Menu \(\rightarrow\) Stats \(\rightarrow\) Calc \(\rightarrow\) Interval \\
+    Set \textit{Type = One-Sample Z Int} \\ \-\hspace{1em} and select \textit{Variable}
+  \end{cas}
 
   \subsection*{Margin of error}
 
-  For 95% confidence interval for \(\mu\), margin of error \(M\) is:
-
+  For 95\% confidence interval of \(\mu\):
   \begin{align*}
     M &= 1.96 \times \dfrac{\sigma}{\sqrt{n}} \\
     \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}
 
-  A confidence interval of \(C\)% is given by
+  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}\)
+
+  \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\).
+
+  \section{Hypothesis testing}
+
+  \begin{warning}
+    Note hypotheses are always expressed in terms of population parameters
+  \end{warning}
 
-  A 95% confidence interval for \(\mu\) will have \(M\) when
-  \[ \overline{x} \pm k \dfrac{\sigma}{\sqrt{n}} \]
+  \subsection*{Null hypothesis \(\textbf{H}_0\)}
 
-  where \(k\) is such that \(\Pr(-k < Z < k) = \frac{C}{100}\)
+  Sample drawn from population has same mean as control population, and any difference can be explained by sample variations.
+
+  \subsection*{Alternative hypothesis \(\textbf{H}_1\)}
+
+  Amount of variation from control is significant, despite standard sample variations.
+
+  \subsection*{\(p\)-value}
+
+  Probability of observing a value of the sample statistic as significant as the one observed, assuming null hypothesis is true.
+
+  For one-tail tests:
+  \begin{align*}
+    p\text{-value} &= \Pr\left( \> \overline{X} \lessgtr \mu(\textbf{H}_1) \> \given \> \mu = \mu(\textbf{H}_0)\> \right) \\
+    &= \Pr\left( Z \lessgtr \dfrac{\left( \mu(\textbf{H}_1) - \mu(\textbf{H}_0) \right) \cdot \sqrt{n} }{\operatorname{sd}(X)} \right) \\
+    &\text{then use \texttt{normCdf} with std. norm.}
+  \end{align*}
+
+  \vspace{0.5em}
+  \begin{tabularx}{23em}{|l|X|}
+    \hline
+    \rowcolor{cas}
+    \(\boldsymbol{p}\) & \textbf{Conclusion} \\
+    \hline
+    \(> 0.05\) & insufficient evidence against \(\textbf{H}_0\) \\
+    \(< 0.05\) (5\%) & good evidence against \(\textbf{H}_0\) \\
+    \(< 0.01\) (1\%) & strong evidence against \(\textbf{H}_0\) \\
+    \(< 0.001\) (0.1\%) & very strong evidence against \(\textbf{H}_0\) \\
+    \hline
+  \end{tabularx}
+
+  \subsubsection*{Finding \(n\) for a given \(p\)-value}
+
+  Find \(c\) such that \(\Pr(Z \lessgtr c)\) such that \(c = \alpha\) (use \texttt{invNormCdf} on CAS).
+
+  \subsection*{Significance level \(\alpha\)}
+
+  The condition for rejecting the null hypothesis.
+
+  \-\hspace{1em} If \(p<\alpha\), null hypothesis is \textbf{rejected} \\
+  \-\hspace{1em} If \(p>\alpha\), null hypothesis is \textbf{accepted}
+
+  \subsection*{\(z\)-test}
+
+  Hypothesis test for a mean of a sample drawn from a normally distributed population with a known standard deviation.
+
+  \begin{cas}
+  Menu \(\rightarrow\) Statistics \(\rightarrow\) Calc \(\rightarrow\) Test. \\
+  Select \textit{One-Sample Z-Test} and \textit{Variable}, then input:
+    \begin{description}[nosep, style=multiline, labelindent=0.5cm, leftmargin=2cm, font=\normalfont]
+    \item[\(\mu\) cond:] same operator as \(\textbf{H}_1\)
+    \item[\(\mu_0\):] expected sample mean (null hypothesis)
+    \item[\(\sigma\):] standard deviation (null hypothesis)
+    \item[\(\overline{x}\):] sample mean
+    \item[\(n\):] sample size
+  \end{description}
+  \end{cas}
+
+  \subsection*{One-tail and two-tail tests}
+  
+  \[ p\text{-value (two-tail)} = 2 \times p\text{-value (one-tail)} \]
+
+  \subsubsection*{One tail}
+
+  \begin{itemize}
+    \item \(\mu\) has changed in one direction
+    \item State ``\(\textbf{H}_1: \mu \lessgtr \) known population mean''
+  \end{itemize}
+
+  \subsubsection*{Two tail}
+
+  \begin{itemize}
+    \item Direction of \(\Delta \mu\) is ambiguous
+    \item State ``\(\textbf{H}_1: \mu \ne\) known population mean''
+  \end{itemize}
+
+  \begin{align*}
+    p\text{-value} &= \Pr(|\overline{X} - \mu| \ge |\overline{x}_0 - \mu|) \\
+    &= \left( |Z| \ge \left|\dfrac{\overline{x}_0 - \mu}{\sigma \div \sqrt{n}} \right| \right) \\
+  \end{align*}
 
+  where
+  \begin{description}[nosep, labelindent=0.5cm]
+    \item [\(\mu\)] is the population mean under \(\textbf{H}_0\)
+    \item [\(\overline{x}_0\)] is the observed sample mean
+    \item [\(\sigma\)] is the population s.d.
+    \item [\(n\)] is the sample size
+  \end{description}
+
+  \subsection*{Modulus notation for two tail}
+
+  \(\Pr(|\overline{X} - \mu| \ge a) \implies\) ``the probability that the distance between \(\overline{\mu}\) and \(\mu\) is \(\ge a\)''
+
+  \subsection*{Inverse normal}
+
+  \begin{cas}
+    \verb;invNormCdf("L", ;\(\alpha\)\verb;, ;\(\dfrac{\sigma}{n^\alpha}\)\verb;, ;\(\mu\)\verb;);
+  \end{cas}
+
+  \subsection*{Errors}
+
+  \begin{description}[labelwidth=2.5cm, labelindent=0.5cm]
+    \item [Type I error] \(\textbf{H}_0\) is rejected when it is \textbf{true}
+    \item [Type II error] \(\textbf{H}_0\) is \textbf{not} rejected when it is \textbf{false}
+  \end{description}
+
+  \begin{tabularx}{\columnwidth}{|X|l|l|}
+    \rowcolor{cas}\hline
+    \cellcolor{white}&\multicolumn{2}{c|}{\textbf{Actual result}} \\
+    \hline
+    \cellcolor{cas}\(\boldsymbol{z}\)\textbf{-test} & \cellcolor{light-gray}\(\textbf{H}_0\) true & \cellcolor{light-gray}\(\textbf{H}_0\) false \\
+    \hline
+    \cellcolor{light-gray}Reject \(\textbf{H}_0\) & Type I error & Correct \\
+    \hline
+    \cellcolor{light-gray}Do not reject \(\textbf{H}_0\) & Correct& Type II error \\
+    \hline
+  \end{tabularx}
+
+% \subsection*{Using c.i. to find \(p\)}
+% need more here
 
 \end{document}