\section{Basic probability lemmas}
\label{sec:probability}

% ARUN: We don't need this any more.
% \begin{lemma}[Mean and variance of the sum of two random variables]
% \label{lem:combined-variance}
%   Let $x$ and $y$ be two random variables with mean $0$, variances $\sigma^2_x$ and $\sigma^2_y$ and a correlation coefficient of $\rho$.
%   Then, the estimator $z = \alpha x + (1-\alpha) y$, where $0 \le \alpha
%   \le 1$ also has mean $0$ and has minimum variance $\sigma^2_z$ when
%   \begin{align*}
%   \alpha &=
%   \begin{cases}
%     0 & \rho > \frac{\sigma_x}{\sigma_y} \\
%     1 & \rho > \frac{\sigma_x}{\sigma_y} \\
%     \frac{\sigma_y (\sigma_y - \rho \sigma_x)}{\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y} & \text{otherwise},
%   \end{cases}
%   &
%   \sigma^2_z &=
%   \begin{cases}
%     \sigma^2_y & \rho > \frac{\sigma_x}{\sigma_y} \\
%     \sigma^2_x & \rho > \frac{\sigma_x}{\sigma_y} \\
%     \frac{\sigma_x^2 \sigma_y^2 (1 - \rho^2)}{\sigma_x^2 + \sigma_y^2 - 2 \rho \sigma_x \sigma_y} & \text{otherwise}.
%   \end{cases}
%   \end{align*}
%
%   In general, if $x_i$ are uncorrelated random variables, $z = \sum_{i} \alpha_i x_i$ where $\sum_i \alpha_i = 1$ has mean and optimal variance,
%   $$\frac{1}{\sigma_z^2} = \sum_i \frac{1}{\sigma_i^2}.$$
% \end{lemma}
% \begin{proof}
%   \newcommand{\alphab}{\bar{\alpha}}
%   For notational convenience, let $\alphab \eqdef 1 - \alpha$.
%   That $z$ has mean $0$ follows directly from the linearity of expectations.
%   The variance of $z$ can be calculated as follows:
%   \begin{align*}
%     \sigma^2_z &\eqdef \var(z)
%     &= \E[z^2] - {\E[z]}^2 \\
%     &= \E[{(\alpha x+ \alphab y)}^2] - 0 \\
%             &= \E[\alpha^2 x^2 + \alphab^2 y^2 + 2 \alpha \alphab x y] \\
%             &= \alpha^2 \sigma_x^2 + \alphab^2 \sigma_y^2 + 2 \alpha \alphab \E[x y] \\
%             &= \alpha^2 \sigma_x^2 + \alphab^2 \sigma_y^2 + 2 \alpha \alphab \rho \sigma_x \sigma_y,
%   \end{align*}
%   using the fact that $\rho \eqdef \frac{\E[xy]}{\sigma_x \sigma_y}$.
%
%   We introduce Lagrange multipliers $\lambda_1, \lambda_2 \ge 0$ to handle the constraint that $0 \le \alpha \le 1$,
%   \begin{align*}
%     \sL &=
%     \alpha^2 \sigma_x^2 + \alphab^2 \sigma_y^2 + 2 \alpha \alphab \rho \sigma_x \sigma_y
%     + \lambda_1 \alpha + \lambda_2 \alphab \\
%     \frac{d}{d\alpha} \sL &=
%     2 \alpha \sigma_x^2 - 2(1 - \alpha) \sigma_y^2 + 2 (1 - 2\alpha) \rho \sigma_x \sigma_y + \lambda_1 - \lambda_2
% &=
%     2 (\alpha (\sigma_x^2 + \sigma_y^2 - 2 \rho \sigma_x\sigma_y) - \sigma_y^2 + \rho \sigma_x \sigma_y + \lambda'_1 - \lambda'_2),
%   \end{align*}
%   where $\lambda'_1$ and $\lambda'_2$ are suitably redefined to absorb the constant.
%
%   This quantity is minimized when the gradient with respect $\alpha$ is $0$,
%   \begin{align*}
%     \alpha (\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y) &= \sigma_y^2 - \rho \sigma_x \sigma_y + \lambda'_2 - \lambda'_1 \\
%     \alpha &= \frac{\sigma_y^2 - \rho \sigma_x \sigma_y + \lambda'_2 - \lambda'_1}{\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y} \\
%     \alphab &= \frac{\sigma_x^2 - \rho \sigma_x \sigma_y - \lambda'_2 + \lambda'_1}{\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y}.
%   \end{align*}
%
%   The KKT conditions give us that $\lambda'_1 \alpha = 0$ and $\lambda'_2 (1-\alpha) = 0$, which implies that only one of $\lambda'_1$ or $\lambda'_2$ are non-zero.
%   We can see that $\alpha = 0$ when $\sigma_y^2 - \rho \sigma_x \sigma_y < 0$, or when $\rho \ge \frac{\sigma_y}{\sigma_x}$.
%   Likewise, $\alpha = 1$ when $\sigma_x^2 - \rho \sigma_x \sigma_y < 0$, or when $\rho \ge \frac{\sigma_x}{\sigma_y}$.
%   This gives us the result on $\alpha$.
%
%   The value of $\sigma_z^2$ when $\alpha = 0$ or $\alpha=1$ is simply $\sigma_y^2$ or $\sigma_x^2$.
%   When $0 < \alpha < 1$, it is,
%   \begin{align*}
%     \sigma_z^2
%             &= \frac{%
%             \sigma_x^2 \sigma_y^2 {(\sigma_y - \rho \sigma_x)}^2
%             + \sigma_x^2 \sigma_y^2 {(\sigma_x - \rho \sigma_y)}^2
%             + 2 \rho \sigma_x^2 \sigma_y^2 (\sigma_y - \rho \sigma_x) (\sigma_x - \rho \sigma_y)}{%
%             {(\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y)}^2
%             }\\
%             &= \sigma_x^2 \sigma_y^2
%             \frac{%
%              \sigma_y^2 + \rho^2 \sigma_x^2 - 2 \rho \sigma_x \sigma_y
%             + \sigma_x^2 + \rho^2 \sigma_y^2 - 2 \rho \sigma_x \sigma_y
%             + 2 \rho (\sigma_x \sigma_y - \rho \sigma_y^2 - \rho \sigma_x^2 + \rho^2 \sigma_x \sigma_y)}{%
%             {(\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y)}^2
%             }\\
%             &= \sigma_x^2 \sigma_y^2
%             \frac{%
%              (1 - \rho^2) (\sigma_x^2 + \sigma_y^2 - 2 \rho \sigma_x \sigma_y)}{%
%              {(\sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y)}^2
%             }\\
%             &=
%             \frac{(1 - \rho^2) \sigma_x^2 \sigma_y^2}{%
%               \sigma_x^2 + \sigma_y^2 - 2\rho \sigma_x \sigma_y
%             }\\
%   \end{align*}
% \end{proof}

\begin{lemma}[Mean and variance of the product of two random variables]
\label{lem:variance-product}
  Let $x$ and $y$ be two independent random variables with means $\mu_x$ and $\mu_y$, and variances $\sigma^2_x$ and $\sigma^2_y$.
  Then, the estimator $z = x y$ has mean $\mu_x \mu_y$ and variance
  $$\sigma^2_z = \sigma_x^2 \sigma_y^2 + \mu_x^2 \sigma_y^2 + \sigma_x^2 \mu_y^2.$$
\end{lemma}
\begin{proof}
  If $x$ and $y$ are independent, $\E[xy] = \E[x]\E[y]$. Thus $\E[z] = \mu_x \mu_y$.

  The variance of $z$ can be calculated as follows:
  \begin{align*}
    \var(z) &= \E[z^2] - {\E[z]}^2 \\
    &= \E[{(xy)}^2] - {\E[xy]}^2 \\
            &= \E[x^2] \E[y^2] - {\E[x]}^2 {\E[y]}^2 \\
            &= (\sigma^2_x + \mu_x^2)(\sigma^2_y + \mu_y^2) - \mu_x^2 \mu_y^2 \\
            &= \sigma_x^2 \sigma_y^2 + \mu_x^2 \sigma_y^2 + \sigma_x^2 \mu_y^2 + \mu_x^2 \mu_y^2 - \mu_x^2 \mu_y^2 \\
            &= \sigma_x^2 \sigma_y^2 + \mu_x^2 \sigma_y^2 + \sigma_x^2 \mu_y^2.
  \end{align*}
\end{proof}

\begin{lemma}[Mean and variance of the ratio of two random variables]
\label{lem:variance-ratio}
  Let $x$ and $y$ be two random variables such that $y$ is strictly positive (i.e.\ $y > 0$) with means $\mu_x$ and $\mu_y$, variances $\sigma^2_x$ and $\sigma^2_y$. % and correlation $\rho$.
  %Furthermore, $y$ has positive support (i.e.\ $y > 0$).
  Then, the first-order Taylor approximation of $z = x / y$ has mean $\mu_x / \mu_y$.
  %and variance
  %$$\sigma^2_z \approx \frac{\mu^2_x}{\mu_y^2} \left(\frac{\sigma_x^2}{\mu_x^2}
  %  + 2\rho \frac{\sigma_x \sigma_y}{\mu_x \mu_y}
  %  + \frac{\sigma_y^2}{\mu_y^2} \right).$$
Furthermore, if $x$ and $y$ are the mean of a $n_x$ and $n_y$ independent random variables,
the approximation error of using the first-order approximation goes to 0 as $n_x, n_y \to \infty$.
\end{lemma}
\begin{proof}
  This is a standard result in statistics. For completeness, we provide a proof below.

  Let $f(x,y) = \frac{x}{y}$.
  Even if $x$ and $y$ are independent, $\E[f(x,y)]$ is not necessarily equal to $f(\E[x],\E[y])$.
  However, taking a first-order Taylor expansion around $(\mu_x, \mu_y)$, we get
  \begin{align*}
    \E[f(x,y)]
     &\approx f(\mu_x,\mu_y) + f_x'(\mu_x, \mu_y) \E[x - \mu_x] + f'_y(\mu_x, \mu_y) \E[y - \mu_y] \\
     &= \frac{\mu_x}{\mu_y}.
  \end{align*}

 % Taking a similar approach to calculate variance, we get,
 % \begin{align*}
 %   \var(f(x,y))
 %   &\approx \E[{(f(x,y) - \E[f(x,y)])}^2] \\
 %            &= \E[{(f(\mu_x,\mu_y) + f'_x(\mu_x, \mu_y) (x - \mu_x) + f'_y(\mu_x, \mu_y) (y - \mu_y) - f(\mu_x, \mu_y))}^2] \\
 %            &= {f'_x(\mu_x, \mu_y)}^2 \E[{(x - \mu_x)}^2] + {f'_y(\mu_x, \mu_y)}^2 \E[{(y - \mu_y)}^2]
 %             + 2 f'_x(\mu_x, \mu_y)f'_y(\mu_x, \mu_y) \E[(x - \mu_x)(y - \mu_y)]\\
 %             &= {f'_x(\mu_x, \mu_y)}^2 \sigma_x^2 + {f'_y(\mu_x, \mu_y)}^2 \sigma_y^2
 %             + 2 f'_x(\mu_x, \mu_y)f'_y(\mu_x, \mu_y) \rho \sigma_x \sigma_y.
 % \end{align*}
 % Noting that $f'_x(\mu_x, \mu_y) = \frac{1}{\mu_y}$ and that $f'_y(\mu_x, \mu_y) = -\frac{\mu_x}{\mu_y^2}$, we get,
 % \begin{align*}
 %   \var(f(x,y))
 %   &\approx \frac{\sigma_x^2}{\mu_y^2} + \frac{\sigma_y^2 \mu_x^2}{\mu_y^4}
 %   + 2 \frac{\mu_x}{\mu_y^3} \rho \sigma_x \sigma_y \\
 %   &= \frac{\mu^2_x}{\mu_y^2} \left(\frac{\sigma_x^2}{\mu_x^2}
 %   + 2\rho \frac{\sigma_x \sigma_y}{\mu_x \mu_y}
 %   + \frac{\sigma_y^2}{\mu_y^2} \right).
 %\end{align*}

  We note that if $x$ and $y$ are the sum of independent random variables, then by the central limit theorem all moments of $x$ and $y$ greater than $1$ go to $0$ as $n_x, n_y \to \infty$.

\end{proof}

%\begin{lemma}[Mean and variance of a importance-weighted estimate.]
%\label{lem:variance-ratio-average}
%  Let $p_i$ and $q_i$ be two sets of independent random variables with means $\mu$ and $\xi$ and variances $\sigma^2$ and $\pi^2$ respectively.
%  Then, $z = \frac{\sum_{i=1}^n p^2_i q_i}{\sum_{i=1}^n p_i}$ has mean $\xi$ and variance,
%  $$\sigma^2_z \approx
%  \frac{\mu^2 \xi^2}{n} {\left( 1 + \frac{\sigma^2}{\mu^2} \right)}^2
%             \left(
%                9 \frac{\sigma^2}{\mu^2} + 4 \frac{\pi^2}{\xi^2}
%                \right).$$
%\end{lemma}
%\begin{proof}
%  From \reflem{variance-ratio} we have that
%  $$\sigma^2_z \approx \frac{\mu^2_x}{\mu_y^2} \left(\frac{\sigma_x^2}{\mu_x^2}
%    + 2\rho \frac{\sigma_x \sigma_y}{\mu_x \mu_y}
%    + \frac{\sigma_y^2}{\mu_y^2} \right),$$
%    where $x = \frac{1}{n}\sum_{i=1}^n p_i^2 q_i$ and $y = \frac{1}{n}\sum_{i=1}^n p_i$.
%
%  In the following, we will make Gaussian assumptions on any moments $>3$ and ignore variance squared terms, e.g. $\sigma^4 \approx 0$.
%  Thus,
%  \begin{align*}
%    \E[x^3] &\approx 3\sigma_x^2 \mu_x + \mu_x^3 \\
%    \E[x^4] &\approx 6\sigma_x^2 \mu_x^2 + \mu_x^4 \\
%    \var[x^2] &= \E[x^4] - {\E[x^2]}^2 \\
%              &\approx 6\sigma_x^2 \mu_x^2 + \mu_x^4 - {(\mu_x^2 + \sigma_x^2)}^2 \\
%              &= 4\sigma_x^2 \mu_x^2
%  \end{align*}
%
%  Let us solve for each term independently,
%  \begin{align*}
%    \mu_x &= (\sigma^2 + \mu^2) \xi \\
%          &= \mu^2 \xi (1 + \frac{\sigma^2}{\mu^2}) \\
%    \mu_y &= \mu \\
%    \sigma_x^2 &\approx \frac{1}{n} (\var[{p_i^2}] \xi^2 + {(\sigma^2 + \mu^2)}^2 \pi^2) \\
%               &<  \frac{1}{n} (4 \mu^2 \sigma^2 \xi^2 + 4 \mu^4 \pi^2) \\
%               &= \frac{4}{n} \mu^4 \xi^2 \left( \frac{\sigma^2}{\mu^2}  + \frac{\pi^2}{\xi^2} \right) \\
%    \sigma_y^2 &= \frac{1}{n} \sigma^2.
%  \end{align*}
%
%  Finally, for $\rho \sigma_x \sigma_y = \E[x y] - \mu_x \mu_y$, we get,
%  \begin{align*}
%      \rho \sigma_x \sigma_y &\eqdef \E[(\sum_{i=1}^n p^2_i q_i)(\sum_{j=1}^n p_j)] -  \E[(\sum_{i=1}^n p^2_i q_i)]\E[(\sum_{j=1}^n p_j)]\\
%           &= \sum_{i=1}^n \E[p^3_i] \E[q_i] - \E[p^2_i] \E[q_i] \E[p_i] \\
%           &= \sum_{i=1}^n (\E[p^3_i] - \E[p^2_i] \E[p_i]) \E[q_i] \\
%           &= \frac{1}{n} (3\sigma^2 \mu + \mu^3 - (\sigma^2 + \mu^2) \mu) \xi \\
%           &= \frac{2}{n} \sigma^2 \mu \xi.
%  \end{align*}
%  noting that all other terms are 0.
%
%  Putting all of these together, we get,
%  \begin{align*}
%    \sigma_z^2 &<
%    \mu^2 \xi^2 {(1 + \frac{\sigma^2}{\mu^2})}^2
%           \left(
%           \frac{4}{n} (\frac{\sigma^2}{\mu^2} + \frac{\pi^2}{\xi^2})
%              +
%              \frac{4}{n} \frac{\sigma^2 \mu \xi}{\mu^2 \xi \mu}
%              +
%              \frac{1}{n} \frac{\sigma^2}{\mu^2}
%              \right) \\
%           &=
%           \frac{\mu^2 \xi^2}{n} {\left( 1 + \frac{\sigma^2}{\mu^2} \right)}^2
%             \left(
%                9 \frac{\sigma^2}{\mu^2} + 4 \frac{\pi^2}{\xi^2}
%                \right).
%  \end{align*}
%
%
%
%\end{proof}


% !TEX root = index.tex

\section{Gluing it Back Together}
\epigraph{The only way to learn mathematics is to do mathematics.}{Paul Halmos}
\begin{align*}
 \xymatrix@R-2pc{
\U \ar@{~>}[r] & \L^\bullet(\U) \ar@{~>}[r] & \check H^*(X)\\
 \mbox{Good cover of } X & \mbox{Cech Complex of } \U & \mbox{Cech Cohomology of } X
 }
\end{align*}
We'll now associate to any cover $\U$ a cochain complex called the \textbf{Cech complex} $\L^{\bullet}(\U)$. To define the complex we need two things:
\begin{alignat*}{4}
	&\mbox{the spaces: } &&& \L^i(\U)\\
	&\mbox{the maps: } &&& d^i: \L^i(\U) \rightarrow \L^{i+1}(\U)
\end{alignat*}

\begin{definition}
	The \textbf{direct sum} $V \oplus V'$ of vector spaces $V$ and $V'$ with basis $\B$ and $\B'$ is a vector with basis the disjoint union $\B \sqcup \B'$.
\end{definition}
\noindent \textbf{Caution:} The disjoint union $\{1,2 \} \sqcup \{1,3\}$ equals $\{1, 2, 1', 3 \}$ and is different from the union $\{1, 2, 3 \}$. We keep both the copies of 1. Hence, ${\dim (V \oplus V') = \dim V  + \dim V'}$.

\begin{ques} $ $
	\begin{enumerate}
		\item What is $V \oplus 0$? Is $V \oplus V \cong V$?
		\item What is the relationship between $\dim V$, $\dim V'$ and $\dim (V \oplus V')$?
		\item Does the set of all vector spaces form a group under $\oplus$?
		\item Construct $\R^n$ using direct sums.
	\end{enumerate}
\end{ques}

\subsection{The Spaces}
Recall that to every subset $ I \subseteq [n]$ we can associate an open set $U_I $ defined as
\begin{align*}
	U_I := \bigcap_{i \in I} U_i
\end{align*}
For good covers, the connected components of $U_I$ are contractible.

\begin{definition}
	For $ 0 \le k < n$, let $(U_{I_1}, U_{I_2}, \dots, U_{I_m})$ denote the non-empty sets $U_I$ with $|I| = k+1$. Define
	\begin{align*}
		\L^k(\U) := \L(U_{I_1}) \oplus \L(U_{I_2}) \oplus \dots \oplus \L(U_{I_m})
	\end{align*}
\end{definition}
\begin{example}
  \label{ex:triangle_2}
	For the triangle with cover the three sides $U_1, U_2, U_3$
  \begin{align*}
		|I| = 0 &  & U_1, U_2, U_3                         \\
		|I| = 1 &  & U_{\{1,2\}}, U_{\{2,3\}}, U_{\{1,3\}}
	\end{align*}
	\begin{center}
		\begin{tabular}{l c l c l }
			$\L^0(\U)$ & : & $\L(U_1) \oplus \L(U_2) \oplus \L(U_3)$ & = & $\F^3$\\
			$\L^1(\U)$ & : & $\L(U_{\{1,2\}}) \oplus \L(U_{\{1,3\}}) \oplus \L(U_{\{2,3\}})$ & = & $\F^3$\\
			$\L^2(\U)$ & : & $\L(U_{\{1,2,3\}})$ & = & 0\\
		\end{tabular}
	\end{center}
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}[scale=0.6]
			%% vertices
			\draw[fill=black] (0,0) circle (3pt);
			\draw[fill=black] (4,0) circle (3pt);
			\draw[fill=black] (2,3) circle (3pt);
			%% vertex labels
			\node at (2,-0.5) {$U_1$};
			\node at (0.5,1.75) {$U_2$};
			\node at (3.5,1.75) {$U_3$};
			\node at (2,-0.5) {$U_1$};
			\node at (-1,0) {$U_{\{1,2\}}$};
			\node at (5,0) {$U_{\{1,3\}}$};
			\node at (2,3.5) {$U_{\{2,3\}}$};
			%%% edges
			\draw[thick] (0,0) -- (4,0) -- (2,3) -- (0,0);
		\end{tikzpicture}
		\caption{$\U = \{U_1, U_2, U_3\}$ is a good cover of the triangle, $U_{\{1,2\}}, U_{\{2,3\}}, U_{\{1,3\}}$ are the vertices, and $U_{\{1,2,3\}}$ is empty.}
		\label{fig:triangle_cech}
	\end{figure}
	So that the cochain complex (without the differentials) looks like
	\begin{align*}
		\L^\bullet(\U) = \xymatrix{ 0 \ar[r] & \F^3 \ar[r] & \F^3 \ar[r] & 0 }
	\end{align*}
\end{example}

\begin{ques}
	Find the Cech complex $\L^\bullet(\U)$ (without the differentials) for each of the following spaces using your favorite good cover $\U$. Compute the Euler characteristic $\chi$ of each of the spaces. (Note that you don't need to know the differentials $d^i$ to compute $\chi$.)
	\begin{multicols}{2}
		\begin{enumerate}
			\item $ \R^n$
			\item $ S^1$ (= the circle)
			\item A Tree
			\item The bipartite graph $ K_{2,3}$
			\item $ \R^2 \setminus \{(0,0) \}$
			\item $ \R^2 \setminus \{(0,0), (1,0), \dots, (k,0) \}$
			\item $S^1 \sqcup S^1$ (disjoint union of 2 circles)
			\item $S^1 \vee S^1$
			\item $ S^2 $ minus a point
			\item $ S^2 $ minus 2 points
		\end{enumerate}
	\end{multicols}
\end{ques}

\subsection{The Maps $d^i$}
The maps $d^i$ are simply the restriction maps $\res$ as defined in Question \ref{q:restrictions}.
\begin{ques}**
  Describe $d^i$ as a matrix in the canonical bases without looking at the next page (which has the answer). Find the differential $d^i$ in Example \ref{ex:triangle_2}.
\end{ques}
\newpage

\noindent In the canonical bases $d^i: \L^i(\U) \rightarrow \L^{i+1}(\U)$ is a matrix whose
\begin{enumerate}
  \item Rows correspond to the number of connected components of $U_I$ for all $|I|=i+2$
  \item Columns correspond to the number of connected components of $U_J$ for all $|J| = i+1$
  \item The $(i,j)^{th}$ entry is 1 if and only if the $i^{th}$ connected component of $U_I$ contains the $j^{th}$ connected component of $U_J$.
\end{enumerate}
\begin{example}
  \label{ex:triangle_cech}
	For a triangle in Figure \ref{fig:triangle_cech} we have inclusions $U_{\{1,2\}} \subseteq U_1$ etc., hence the differential $d^0$ looks like
	\begin{center}
		\begin{tabular}{ l | c c c  }
			& $U_1$ & $U_2$ & $U_3$ \\\hline
			$U_{\{1,2\}}$ & 1 & 1 & 0 \\
			$U_{\{2,3\}}$ & 0 & 1 & 1 \\
			$U_{\{1,3\}}$ & 1 & 0 & 1
		\end{tabular} $ = d^0$
	\end{center}
  So that
    \begin{align*}
  		\L^\bullet(\U)
      &=
      0 \rightarrow \F^3 \xrightarrow{\begin{bmatrix}1 & 1 & 0 \\0 & 1 & 1 \\ 1 & 0 & 1 \end{bmatrix}} \F^3 \rightarrow 0
  	\end{align*}
  \end{example}

  \begin{example}
    For the circle $S^1$ we can find a good cover consisting of two (slightly overlapping) semicircles.
    \label{ex:circle_cech}
    \begin{figure}[H]
  		\centering
  		\begin{tikzpicture}[scale=0.6]
  			%% vertices
        \draw (2,2) circle (2cm);
  			\draw[fill=black] (0,2) circle (3pt);
        \draw[fill=black] (4,2) circle (3pt);
        \node at (2,4.5) {$U_1$};
        \node at (2,-0.5) {$U_2$};
        \node at (-0.5,1.5) {$a$};
        \node at (4.5,1.5) {$b$};
  		\end{tikzpicture}
  		\caption{$\{U_1, U_2\}$ is a good cover of the circle with $U_{\{1,2\}} = \{ a, b \}$}
  		\label{fig:triangle_cech}
  	\end{figure}
    \begin{align*}
  		|I| = 0 &  & U_1, U_2                         \\
  		|I| = 1 &  & U_{\{1,2\}}
  	\end{align*}
  	\begin{center}
  		\begin{tabular}{l c l c l }
  			$\L^0(\U)$ & : & $\L(U_1) \oplus \L(U_2)$ & = & $\F^2$\\
  			$\L^1(\U)$ & : & $\L(U_{\{1,2\}})$ & = & $\F^2$
  		\end{tabular}
  	\end{center}

    \end{example}

\iffalse
\begin{example}
  \label{ex:triangle_cech}
	For a triangle in Figure \ref{fig:triangle_cech} we have the following non-empty sets
	\begin{align*}
		|I| = 0 &  & U_1, U_2, U_3                         \\
		|I| = 1 &  & U_{\{1,2\}}, U_{\{2,3\}}, U_{\{1,3\}}
	\end{align*}
	Hence the spaces in the chain complex are
	\begin{align*}
		0 \rightarrow \F^3 \rightarrow \F^3 \rightarrow 0
	\end{align*}
	Because of the non-empty inclusions $U_{\{1,2\}} \subseteq U_1$ etc. the differential looks like
	\begin{center}
		\begin{tabular}{ l | c c c  }
			& $U_1$ & $U_2$ & $U_3$ \\\hline
			$U_{\{1,2\}}$ & 1 & 1 & 0 \\
			$U_{\{2,3\}}$ & 0 & 1 & 1 \\
			$U_{\{1,3\}}$ & 1 & 0 & 1
		\end{tabular}
	\end{center}
  So that
    \begin{align*}
  		\L^\bullet(\U)
      &=
      0 \rightarrow \F^3 \xrightarrow{\begin{bmatrix}1 & 1 & 0 \\0 & 1 & 1 \\ 1 & 0 & 1 \end{bmatrix}} \F^3 \rightarrow 0
  	\end{align*}
  \end{example}

\begin{ques}
Compute the cohomology of the Cech Complex found in Example \ref{ex:triangle_cech}.
\end{ques}

\begin{ques}
  Find $\check H(-)$ for the following spaces.
  \begin{multicols}{2}
    \begin{enumerate}
      \item $ \R^m$
      \item $ S^1 = $ the circle
      \item A Tree
      % \item The bipartite graph $ K_{2,3}$
      \item $ \R^2 \setminus \{(0,0) \}$
      % \item $ \R^2 \setminus \{(0,0), (1,0), \dots, (k,0) \}$ for some positive integer $ k$
      \item $ S^2 $ minus a point
      \item $ S^2 $ minus 2 points
    \end{enumerate}
  \end{multicols}
\end{ques}

\begin{ques}
  Find the cohomology of a finite planar graph (thought of as a subset of $\R^2$).
\end{ques}
\fi


\subsection{Interaction Objective}
\label{text:approach/objective/interactive}
The interaction between robot and pedestrian itself is an abstract quantity and can thus hardly be used for optimization itself. In the following, we consider a scene with the robot facing only one pedestrian to approach this problem, then to generalize the developed concept to interactions with multiple pedestrians.
\newline
While it is hard to find a measure for the interaction between the robot and the pedestrian in the scene when regarding the problem in general, it can be simplified when a) focussing on one of the interacting agents and b) define the measure to vanish its value. Under these conditions, the problem can be re-formulated as decreasing the impact the robot induces on the pedestrian, which is a lot simpler to quantify than the abstract concept of interaction. To do so the un-conditioned\footnote{For brevity the term "un-conditioned" means, not depending on the robot's state. However, the pedestrians trajectories predictions are of course still conditioned on its state history as well as the states and state history of the other pedestrians in the scene, as described in Section \ref{text:approach/formulation}.} pedestrian's trajectory distribution $\distwo[]$ is computed, i.e., the distribution that would occur if no robot would be in the scene, and then compared to its actual conditioned trajectory distribution $\dist[]$.\footnote{When the prediction model requires the input of a robot trajectory, e.g. as an "input format" requirement of a neural network-based model, predicting the un-conditioned trajectory distribution might not be straight-forward. Using a re-trained un-conditioned model is not an option since factors causing a different distribution independent from the robot's trajectory might come into play. Thus, within the project a "pseudo"-robot is used which is located very far away from any pedestrian, hence minimizing its effect on the pedestrian's behavior.} With some general distance measure $\Delta(\cdot, \cdot)$ the interactive objective function given the robot's trajectory $\x_{0:T}$ is defined as:

\begin{equation}
J_{int}^k(\x_{0:T}) = \sum_{t=0}^T \Delta(\dist[k]_t, \distwo[k]_t)
\label{eq:objective_interaction}
\end{equation}

\begin{figure}[!ht]
\begin{center}
\begin{tikzpicture}

    \node[inner sep=0pt] (pedwo) at (-3,1)
    {\includegraphics[width=.05\textwidth]{images/walking.png}};
    \draw [dotted, ultra thick, name path=A] (pedwo) to[out=180, in=0] node[above] {$\xpedwo[k]$} (-8, 1);

    \node[inner sep=0pt] (pedw) at (-3,-1)
    {\includegraphics[width=.05\textwidth]{images/walking.png}};
    \node[inner sep=0pt] (robot) at (-7,-1)
    {\includegraphics[width=.05\textwidth]{images/robot.png}};
    \draw [ultra thick, name path=B] (pedw) to[out=180, in=10] node[below, sloped] {$\xped[k]$}(-8,-3);

    \draw[thick, decorate, decoration={brace, amplitude=20pt}] (-1.5,2) -- (-1.5,-2);

    \node[inner sep=0pt] (ped) at (5,0.5)
    {\includegraphics[width=.05\textwidth]{images/walking.png}};

    \draw [dotted, ultra thick, name path=A] (ped) to[out=180, in=0] node[above] {$\xpedwo[k]$} (0, 0.5);
    \draw [ultra thick, name path=B] (ped) to[out=180, in=10] node[below, sloped] {$\xped[k]$}(0,-1.5);

    \tikzfillbetween[of=A and B]{blue, opacity=0.2};
    \node[] (D) at (1, -0.3){$D_{int}$};

\end{tikzpicture}
\end{center}
\caption{Interactive measure in case of deterministic and uni-modal pedestrian trajectory predictions, which trivially is the area enclosed by the un-conditioned (upper) and the conditioned (middle) trajectory.}
\label{img:ado_w_wo_distance_trajectory}
\end{figure}

If the pedestrian's trajectory prediction would be deterministic and unimodal, this measure simply breaks down to the distance between both trajectories, which is the area enclosed by both trajectories in continuous time, as shown in Figure \ref{img:ado_w_wo_distance_trajectory}, or a sum of distance per time-step in discrete time. For probabilistic and multimodal predictions, computing the distance between both distributions is more difficult, especially when demands such as computational cost and differentiability (to be used in an optimization) have to be factored in.

%\begin{figure}[!ht]
%\begin{center}
%\begin{tikzpicture}
%
%    \node[inner sep=0pt] (ped) at (5,0)
%    {\includegraphics[width=.05\textwidth]{images/walking.png}};
%    \node[inner sep=0pt] (robot) at (0,0)
%    {\includegraphics[width=.05\textwidth]{images/robot.png}};
%
%    \draw [dotted, ultra thick, name path=A] (ped) to[out=180, in=0] (0, 2) node[above, sloped] {$\xpedwo[k]$}  to[out=180, in=0] (-4, 0);
%
%    \draw [ultra thick, name path=B] (ped) to[out=180, in=10] (0,-2) node[below, sloped] {$\xped[k]$} to[out=180, in=0] (-4, 0);
%
%\end{tikzpicture}
%\end{center}
%\caption{Interactive measure in case of deterministic and uni-modal pedestrian trajectory predictions, which trivially is the area enclosed by the un-conditioned (upper left) and the conditioned (upper right) trajectory.}
%\label{img:int_acceleration_reason}
%\end{figure}

The correlation between the acceleration carried out on a passenger and its comfort is widely known, e.g., for the driving use case \cite{Hoberock1976}. Therefore, assuming that the same measure applies for correlation between the acceleration the pedestrian itself has to exert (e.g., to evade a dynamic obstacle) and its comfort is likely.

% Therefore, instead of contrasting the position distributions, it might be valuable to use the velocity or acceleration distributions. Figure \ref{img:int_acceleration_reason} shows a possible scenario in which the robot's presence affects the pedestrian such that the trajectory prediction is mirrored. When the robot is static, and when no other pedestrian is close, both trajectories are equally safe  and "comfortable" for the pedestrian. Also they are equal in length, so that there is no reason to chose one above the other. However, due to the large enclosed area, a purely position-based distance metric would be non-zero by far, while an acceleration-based distance metric would be zero since the velocities of the pedestrian-only change their sign, not their absolute value.

\subsubsection{Kullback-Leibler Divergence}
A commonly used metric for expressing the distance between two distributions is the Kullback-Leibler Divergence $D_{KL}$, which determines the distance between  some distribution $q$ and another distribution $p$ as:

\begin{equation}
D_{KL} = \int_x q(x) log \frac{q(x)}{p(x)} dx
\end{equation}

$D_{KL}$ is a well-defined loss term and commonly used in many applications, such as generative deep learning models \cite{Goodfellow2014}\cite{Salzmann2020} (similarly the Jenson-Shannon Divergence). However, it is not analytically defined for some "complex" distributions such as \ac{GMM}s, the output distribution of Trajectron \cite{Ivanovic2018}. Methods to approximate the KL-Divergence for \ac{GMM}s have been discussed in \cite{Cui2015} and embrace Monte Carlo sampling, signature quadratic form distance \cite{Beecks2011}, and several more. All of these methods are not computationally feasible for an online application, especially when gradients have to be computed. Other methods simplify the real \ac{GMM} to a single Gaussian, by a weighted average over its parameters, which is not guaranteed to be a meaningful distribution and loses the advantages of predicting multimodal distributions in the first place.
\newline
Regarding the Trajectron \cite{Ivanovic2018} as a prediction model, some intermediate distribution might be used for comparison instead of the output distribution, such as the categorical distribution in its latent space\footnote{In fact, the Trajectron's latent space could not be used as a basis for the interactive objective function anyway, since it does not depend on the robot's trajectory. However, it might be used to assess the similarity between scenarios, which will be discussed in Section \ref{text:approach/runtime/warm_starting}.}. Although this approach might give rise to using standard distance measures such as $D_{KL}$, it would impede interpretability and generality of the overall framework, as it would have to be redefined for every other prediction model.

\subsubsection{Trajectory Projection}
Combining computational efficiency and the capability of representing a measure based on the full distribution is hard, as demonstrated in the examples above. However, by exploiting that the predicted distribution $\xped[]_t \sim \dist[]_t$ is a continuous, well-defined distribution with infinite support \footnote{Although not all distributions have infinite support, these properties surely hold for the most commonly used ones in the area of pedestrian prediction such as Gaussians, \ac{GMM}s \cite{Salzmann2020} or non-closed form distribution such as SGAN \cite{Gupta2018}.}, \ref{eq:objective_interaction} can be redefined as the probability of the unconditioned distribution $\distwo[]_t$ with respect to the conditioned distribution $\dist[]_t$ which is equivalent to the integral over the product distribution $\int \int \distwo[]_t \cdot \dist[]_t \, dxdy$.
\newline
For two \ac{GMM}s, the distribution product is not analytically defined though and would involve numerically solving an \ac{ODE} \cite{Schrempf2005} or multi-scale sampling \cite{Ihler2003}. Therefore as simplification, not the full un-conditioned distribution is taken into account but only its mean value $\mathbb{E}[\distwo[]_t]$ and weighted by the conditioned mode importance vector:\footnote{A derivation of the exact objective formulation for a \ac{GMM} as underlying distribution can be found in the appendix.}

\begin{equation}
J_{int}^k = - \sum_{t=0}^T \mathbb{E}_{\dxped[k] \sim \dist[k]} \log p(\, \mathbb{E}[\distwo[k]_t] \, | \, \dxped[k]_t, \dx_t)
\label{eq:objective_interact_prob}
\end{equation}

To deal with reasonable large values, compared to the other objective functions, and for the independence of gradients (sum not product), instead of the \ac{PDF} $p = pdf(x, y)$, its logarithmic value $\log p$ probability is used. Since the product probability should be maximized while the optimization stated in Problem \ref{problem:general} seeks the minimum, the expectation value's negative value is used. Note, that $\dist[]$ and $\distwo[]$ denote the distribution over velocities, not positions. This makes the objective independent from translational deviations along the trajectories.
\newline
Equation \ref{eq:objective_interact_prob} is efficient to compute as it can be batched over the full length of the trajectory and all modes. Also, it uses the full distribution and has a unique global minimum when the means of both distributions are identical (at least for Gaussian-like distributions such as \ac{GMM}s). In fact, \ref{eq:objective_interact_prob} is similar to the \ac{ELBO} loss of the Trajectron loss function (compare Equation \ref{eq:trajectron_loss}), which shows that the term is suitable for general optimization, especially concerning the Trajectron model itself. However, $J_{int}$ is generally applicable to all prediction models, that output a probabilistic distribution, independent from multi-modality.


\documentclass[letterpaper,10pt]{article}

\usepackage{fullpage}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{braket}

%We would like cardinality of a set to require one (not four) commands
\newcommand{\card}[1]{\left\vert{#1}\right\vert}

\title{Set Notation Help}
\author{Craig Kelly}

\begin{document}

% Force pdflatex to properly use letter as page size (instead
% of defaulting to A4)
\special{papersize=8.5in,11in}
\setlength{\pdfpageheight}{\paperheight}
\setlength{\pdfpagewidth}{\paperwidth}

\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Some set notation}

Let $ Z $ be the set of integer $ \geq 0 $.
Show $ Z \times Z \times Z $ is countable by constructing the bijection
$ f:  Z \times Z \times Z \rightarrow \mathbb{N} $

I am assuming that $0 \notin \mathbb{N}$,
so $\mathbb{N} = (1,2,3, \cdots)$

\begin{proof}

First we show that there is a bijection
$ g: Z \times Z \rightarrow \mathbb{N} $.

blah, blah, blah

$ g: Z \times Z \rightarrow \mathbb{N} $
is a bijection and therefore $Z \times Z$ is countable.

We may now construct $ f:  Z \times Z \times Z \rightarrow \mathbb{N} $
via composition:

$ f(Z_{1}, Z_{2}, Z_{3}) = h(g(Z_{1}, Z_{2}), Z_{3}) $

\end{proof}

\begin{proof}

$ L1 = \set{M | \text{M accepts w if w contains the substring 10} } $
is undecidable

Blah, blah, blah

If M(w) accepts, then L(HWAMw) = $\set{w | \text{w contains the substring 10} }$

If M(w) rejects or fails to halt, then L(HWAMw) = $\emptyset$

Thus, we know
$(M,w) \in A_{TM} \Leftrightarrow HWAMw \in L1$

\end{proof}

\pagebreak

\begin{proof}
$ L2 = \set{M | \text{M accepts an odd number of strings} } $ is undecidable

$P(M)$ if $\card{L(M)}$ is odd.

$x \in \set{000, 111, 101}$

Blah, blah, blah

If M(w) accepts, then L(HWBMw) = $\set{000, 111, 101}$.  Note that
$\card{L(HWBMw)} = 3$, which is odd.

If M(w) rejects or fails to halt, then L(HWBMw) = $\emptyset$, so
$\card{L(HWBMw)} = 0$

Thus, we know
$(M,w) \in A_{TM} \Leftrightarrow HWBMw \in L2$

\end{proof}

\end{document}


%% State Space Modelling of Dynamic Systems
%% Lecture 21: State Observers
\def\FileDate{10/04/02}
\def\FileVersion{1.0}
% ----------------------------------------------------------------
% Notes pages *********************************************************
% ----------------------------------------------------------------

In many practical cases it is not possible to measure all the states of a system.
That is  we cannot form  $u=r-\mathbf{Kx}$  for the purposes of feedback control because we do not have access to $\mathbf{x}$\footnote{Or some states in $\mathbf{x}$, either because the states are not \emph{physical states} and hence cannot be measured, or because they \emph{are} physical states but we do not have suitable sensors for the physical quantity that is represented by the state.}

If the structure of the system is known  (i.e. $\mathbf{A}$, $\mathbf{B}$, $\mathbf{C}$, and $\mathbf{D}$) then it may be possible to reconstruct the states from one or more of the system outputs by means of an observer. It is necessary that the system states be observable from the output(s).

\ifslidesonly
\begin{slide}
   \heading{State Observers}
   \begin{itemize}
   	\item In many practical cases it is not possible to measure all the states of a system.
   	\item That is  we cannot form  $u=r-\mathbf{Kx}$  for the purposes of feedback control because we do not have access to $\mathbf{x}$.
   	\item If we know $\mathbf{A}$, $\mathbf{B}$, $\mathbf{C}$, and $\mathbf{D}$ then it may be possible to reconstruct the states from one or more of the system outputs by means of an \textbf{observer}.
   	\item It is necessary that the system states be observable from the output(s).
   \end{itemize}
\end{slide}
\fi

\begin{slide}
   \heading{State Observer}
The main idea is to construct a model of the system and subject it to the same input:
\begin{center}
	\resizebox{200pt}{!}{\includegraphics{pictures/observer1.pdf}}
\end{center}
\end{slide}

Severe differences occur between $\mathbf{x}$  and  $\hat{\mathbf{x}}$ due to disturbances and parameter errors. Solution:-  Employ feedback !

\begin{slide}
   \heading{State Observer with Feedback}
\begin{center}
	\resizebox{280pt}{!}{\includegraphics{pictures/observer2.pdf}}
\end{center}
\end{slide}
\textbf{NB} To simplify treatment, we take $\mathbf{D}=0$.

\input{frag1}
\ifslidesonly
\begin{slide}
	\heading{Observer State Equations}
   \input{frag1}
\end{slide}
\fi

\input{frag2}
\ifslidesonly
\begin{slide}
	\heading{Observed State Errors}
   \input{frag2}
\end{slide}
\fi

\input{frag3}
\ifslidesonly
\begin{slide}
	\heading{Properties of the Observed Error State Equations}
   \input{frag3}
\end{slide}
\fi

\section*{Design of the $\mathbf{L}$ Matrix} % (fold)
\label{sec:design_of_the_l_matrix}

\input{frag4}
\ifslidesonly
\begin{slide}
	\heading{Design of the $\mathbf{L}$ Matrix (1)}
   \input{frag4}
\end{slide}
\fi

\input{frag5}
\ifslidesonly
\begin{slide}
	\heading{Design of the $\mathbf{L}$ Matrix (2)}
   \input{frag5}
\end{slide}
\fi

\input{frag6}
\ifslidesonly
\begin{slide}
	\heading{Design of the $\mathbf{L}$ Matrix (3)}
   \input{frag6}
\end{slide}
\fi

\subsection*{Example 1} % (fold)
\label{sub:example_1}

\textbf{Problem}: Design an observer with poles or eigen values $\lambda_{1,2} = -20,\ -20$ using the observer canonical form for the system with a TF:
\[
\frac{Y(s)}{U(s)}=\frac{7}{s^2+15s+44}.
\]

\textbf{SOLUTION}:
The observer canonical form gives:
% MathType!MTEF!2!1!+-
% faaagaart1ev2aaaKnaaaaWenf2ys9wBH5garuavP1wzZbqedmvETj
% 2BSbqefm0B1jxALjharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0x
% bbL8FesqqrFfpeea0xe9Lq-Jc9vqaqpepm0xbba9pwe9Q8fs0-yqaq
% pepae9pg0FirpepeKkFr0xfr-xfr-xb9Gqpi0dc9adbaqaaeGaciGa
% aiaabeqaamaabaabaaGcbaGaaCyqaiabg2da9maadmaabaqbamqabi
% GaaaqaaiabgkHiTiaaigdacaaI1aaabaGaaGymaaqaaiabgkHiTiaa
% isdacaaI0aaabaGaaGimaaaaaiaawUfacaGLDbaacaGG7aGaaGjbVl
% aahkeacqGH9aqpdaWadaqaauaadeqaceaaaeaacaaIWaaabaGaaG4n
% aaaaaiaawUfacaGLDbaacaGG7aGaaGjbVlaahoeacqGH9aqpdaWada
% qaauaadeqabiaaaeaacaaIXaaabaGaaGimaaaaaiaawUfacaGLDbaa
% caGG7aGaaGjbVlaahseacqGH9aqpcaaIWaaaaa!4C31!
\[
{\bf{A}} = \left[ {\begin{array}{*{20}c}
   { - 15} & 1  \\
   { - 44} & 0  \\
\end{array}} \right];\;{\bf{B}} = \left[ {\begin{array}{*{20}c}
   0  \\
   7  \\
\end{array}} \right];\;{\bf{C}} = \left[ {\begin{array}{*{20}c}
   1 & 0  \\
\end{array}} \right];\;{\bf{D}} = 0
\]
with $\mathbf{L}=[l_1,\ l_2]^T$  we have observer poles at the roots of:
\[
s^2+(15+l_1)s+(44+l_2)=0.
\]

The desired CE is:
\[
\alpha_e(s)=(s + 20)(s + 20)=s^2+40s+400=0
\]

Comparing coefficients gives:
\begin{eqnarray*}
	s^1:\ 15 + l_1 & = & 40 \to l_1=25 \\
	s^0:\ 44 + l_2 & = & 400 \to l_2=356 \\
\end{eqnarray*}

So the observer states are given by:
\[
\frac{d\hat{\mathbf{x}}}{dt}=\mathbf{A}\hat{\mathbf{x}}+\mathbf{B}u-\mathbf{L}(\mathbf{C}\hat{\mathbf{x}}-y)
\]
where $\mathbf{L}=[25\ 356]^T.$

\begin{slide}
   \heading{Block Diagram of the Observer}
\begin{center}
	\resizebox{250pt}{!}{\includegraphics{pictures/observer3.pdf}}
\end{center}
\end{slide}

% subsection example_1 (end)

% section design_of_the_l_matrix (end)

\section*{Design of $\mathbf{L}$ Matrix for Other Forms of State Equation} % (fold)
\label{sec:design_of_l_matrix_for_other_forms_of_state_equation}

\input{frag7}
\ifslidesonly
\begin{slide}
	\heading{Design of $\mathbf{L}$ Matrix for Other Forms of State Equation}
   \input{frag7}
\end{slide}
\fi

In MATLAB we could evaluate the $\mathbf{L}$ matrix using:
\begin{verbatim}
	L=(acker(A',C',p))'
\end{verbatim}  where  \verb|p|  is a vector of desired observer poles.

% section design_of_l_matrix_for_other_forms_of_state_equation (end)

\section*{Choice of Observer Poles} % (fold)
\label{sec:choice_of_observer_poles}

\input{frag8}
\ifslidesonly
\begin{slide}
	\heading{Choice of Observer Poles (1)}
   \input{frag8}
\end{slide}
\fi

\input{frag9}
\ifslidesonly
\begin{slide}
	\heading{Choice of Observer Poles (2)}
   \input{frag9}
\end{slide}
\fi

% section choice_of_observer_poles (end)

%----------------------------------------------------------------
% The end of notes
% ----------------------------------------------------------------
\endinput

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass[twoside]{MATH77}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\begin{document}
\begmath 2.15  Procedures to Avoid Loss of Precision: $\ln (1+x)$, etc.

\silentfootnote{$^\copyright$1997 Calif. Inst. of Technology, \thisyear \ Math \`a la Carte, Inc.}

\subsection{Purpose}

These procedures compute expressions involving logarithm, exponential, sine,
cosine, hyperbolic sine, hyperbolic cosine, and the gamma function, some
having arguments that are expressions, more accurately than is possible
using the Fortran intrinsic functions, or the gamma function described in
Chapter~2.3. See also discussion of the function SRCVAL in Section C
of Chapter~2.9.

\subsection{Usage}

\subsubsection{Program Prototype, Single Precision,\newline  $\ln (1+x)$}

\begin{description}

\item[REAL] \ {\bf  X, U, SLNREL}

\item[EXTERNAL] \ {\bf  SLNREL}

\end{description}

Assign a value to X and obtain U $= \ln (1+\text{X})$ by using
$$
\fbox{\bf U = SLNREL(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline $x - \ln (1+x)$}

\begin{description}

\item[REAL] \ {\bf  X, U, SRLOG1}

\item[EXTERNAL] \ {\bf  SRLOG1}

\end{description}

Assign a value to X and obtain U $= \text{X} - \ln (1+\text{X})$ by using
$$
\fbox{\bf U = SRLOG1(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline $x - 1 - \ln (x)$}

{\bf REAL \ X, U, SRLOG}

{\bf EXTERNAL \ SRLOG}

Assign a value to X and obtain U $= \text{X} - 1 - \ln (\text{X})$ by using
$$
\fbox{\bf U = SRLOG(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $\exp (x) - 1$}

\begin{description}

\item[REAL] \ {\bf X, U, SREXP}

\item[EXTERNAL] \ {\bf SREXP}

\end{description}

Assign a value to X and obtain U $= \exp (\text{X}) - 1$ by using
$$
\fbox{\bf U = SREXP(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $\sin (\pi x)$}

\begin{description}

\item[REAL] \ {\bf  X, U, SSINPX}

\item[EXTERNAL] \ {\bf  SSINPX}

\end{description}

Assign a value to X and obtain U $= \sin (\pi \text{X})$ by using
$$
\fbox{\bf U = SSINPX(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $\cos (\pi x)$}

\begin{description}

\item[REAL] \ {\bf  X, U, SCOSPX}

\item[EXTERNAL] \ {\bf  SCOSPX}

\end{description}

Assign a value to X and obtain U $= \cos (\pi \text{X})$ by using
$$
\fbox{\bf U = SCOSPX(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $(x-\sin (x))/x^3$}

\begin{description}

\item[REAL] \ {\bf  X, U, SSIN1}

\item[EXTERNAL] \ {\bf  SSIN1}

\end{description}

Assign a value to X and obtain U $= (\text{X}-\sin (\text{X}))/\text{X}^3$ by using
$$
\fbox{\bf U = SSIN1(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $(1-\cos (x))/x^2$}

\begin{description}

\item[REAL] \ {\bf  X, U, SCOS1}

\item[EXTERNAL] \ {\bf  SCOS1}

\end{description}

Assign a value to X and obtain U $= (1-\cos (\text{X}))/\text{X}^2$ by using
$$
\fbox{\bf U = SCOS1(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $\sinh(x) - x$}

\begin{description}

\item[REAL] \ {\bf  X, U, SSINHM}

\item[EXTERNAL] \ {\bf  SSINHM}

\end{description}

Assign a value to X and obtain U $= \sinh(\text{X}) - \text{X}$ by using
$$
\fbox{\bf U = SSINHM(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $\cosh(x) - 1$}

\begin{description}

\item[REAL] \ {\bf  X, U, SCOSHM}

\item[EXTERNAL] \ {\bf  SCOSHM}

\end{description}

Assign a value to X and obtain U $= \cosh(\text{X}) - 1$ by using
$$
\fbox{\bf U = SCOSHM(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $\cosh(x) -
 1 - \frac 12 x^2$}

{\bf REAL X, U, SCSHMM}

{\bf EXTERNAL SCSHMM}

Assign a value to X and obtain U $= \cosh(\text{X}) - 1 - \frac{1}{2} \text{X}^2$ by using
$$
\fbox{\bf U = SCSHMM(X)}
$$
\subsubsection{Program Prototype, Single Precision,\newline  $1/\Gamma (x+1) - 1$}

\begin{description}

\item[REAL] \ {\bf  X, U, SGAM1}

\item[EXTERNAL] \ {\bf  SGAM1}

\end{description}

Assign a value to X and obtain U $= 1/\Gamma (1+\text{X}) - 1$ by
using
$$
\fbox{\bf U = SGAM1(X)}
$$
\subsubsection{Argument Definitions}

\begin{description}

\item[X] \ [in] Argument of functions. Argument values may be constrained as
described in Section E below.
\end{description}

\subsubsection{Modifications for Double Precision}

For double precision computation, change the REAL type statement to DOUBLE
PRECISION and change the initial letter of the function name to D. Since
these functions are not generic intrinsic functions, it is important to
declare them explicitly to be DOUBLE PRECISION, because the default implicit
type would be REAL. The approximations used in SSINHM, SCOSHM and SCSHMM are
accurate to~14 decimal digits, so the REAL versions of these procedures
could be used to advantage on some platforms by changing the function type
and all internal variable types, constants, and arithmetic to double
precision.

\subsection{Example and Remarks}

See DRDLNREL and ODDLNREL for an example of the usage of these subprograms.

MATH77 does not include a special procedure to compute $\sin (x)/x$. On all
machines tested, $\sin (x)/x$ could be computed with good relative accuracy,
even for very small $x$, by using the Fortran intrinsic SIN function. The
only difficulty is that the user must test whether $x$ is exactly zero, and
use 1.0 for the result. The procedure SSIN1, described above, can be used to
compute $1-\sin (x)/x$ without loss of precision.

\subsection{Functional Description}

\subsubsection{Method}
\begin{description}
\item[For xLNREL,] when $1/\sqrt 2 < 1+x < \sqrt 2$, use approximation~2707 from
Hart, et. al., \cite{Hart:1968:CA}. Otherwise, use the Fortran intrinsic logarithm
function.

\item[For xRLOG1,] when $-0.39 \leq  x \leq  0.57$, use
rational approximations due to Alfred H. Morris, Jr., \cite{ahm:lib}. Otherwise,
use the Fortran intrinsic logarithm function.

\item[For xRLOG,] when $0.61 \leq  x \leq  1.57$, use rational
approximations due to Alfred H. Morris, Jr., \cite{ahm:lib}. Otherwise, use the
Fortran intrinsic logarithm function.

\item[For xREXP,] when $|x| \leq  0.15$, use rational approximations due
to Alfred H. Morris, Jr., \cite{ahm:lib}. Otherwise, use the Fortran intrinsic
exponential function.

\item[For xSINPX and xCOSPX,] for all values of $x$, use rational approximations
due to Alfred H. Morris, Jr., \cite{ahm:lib}.

\item[For xSIN1,] use Taylor's series for $|x| < 0.25$. Otherwise, use the Fortran
intrinsic sine function.

\item[For xCOS1,] when $\cos (x) \geq 0$ use $1-\cos (x) = \sin ^2(x) / (1+\cos (x))$%
. Otherwise, use $1-\cos (x)$. In both cases, use the Fortran intrinsic
functions.

\item[For SSINHM,] when $|x| < 1.65$, use rational approximations due to A. K.
Cline and R. J. Renka, \cite{ahm:lib}. Otherwise, use the Fortran intrinsic SINH
function.

\item[For SCOSHM,] when $|x| < 1.2$, use rational approximations due to A. K. Cline
and R. J. Renka, \cite{ahm:lib}. Otherwise, use the Fortran intrinsic COSH function.

\item[For SCSHMM,] when $|x| < 2.7$, use rational approximations due to A. K. Cline
and R. J. Renka, \cite{ahm:lib}. Otherwise, use the Fortran intrinsic COSH function.

\item[For xGAM1,] when $-0.5 \leq  x \leq  1.5$,
use rational approximations due to Alfred H. Morris, Jr., \cite{ahm:lib}.
Otherwise, use the xGAMMA routine from Chapter~2.3.
\end{description}
\subsubsection{Accuracy Tests}

The single precision subprograms were tested on an IBM PC/AT using IEEE
arithmetic, by comparison with the double precision Fortran intrinsic
functions (except the reference value for SGAM1 used DGAMMA from Chapter
2.3) at 2000~points. For each function except SSINPX and SCOSPX, the points
were chosen to cover the entire domain over which a special approximation is
used. For SSINPX and SCOSPX, 2000~points were chosen randomly in the region $%
0<x<2$, and SSINPX($10^x$), $\sin (10^x\pi )$, SCOSPX($10^x$) and $\cos
(10^x\pi )$ were then computed. For SSIN1 and SCOS1, 2000~points were chosen
randomly in the region $-8<x<-1$, then $y=10^x$,
SSIN1($y)$, $(y-\sin (y))/y^3$, SCOS1($y)$ and $(1-\cos (y))/y^2$ were
computed. The results of these tests may be summarized as follows:
\begin{table*}
\begin{center}
\begin{tabular}{l*{11}{r}}
 & \multicolumn{11}{c}{\bf Percentage of samples with specified number of
bits wrong\rule[-8pt]{0pt}{8pt}}\\
{\bf Function} & {\bf 0} & {\bf 1} & {\bf 2} & {\bf 3} & {\bf 4} &
{\bf 5} & {\bf 6} & {\bf 7} & {\bf 8} & {\bf 9} & {\bf $\geq 10$}\\
SLNREL & 100.0\\
$\ln (x+1)$ & 61.7 & 24.6 & 13.8\\
SRLOG1 & 57.3 & 23.7 & 10.9 & 6.7 & 1.5\\
$x-\ln (x+1)$ & 3.9 & 4.6 & 9.0 & 11.9 & 17.9 & 16.9 & 11.0 & 7.0 & 5.3 &
4.1 & 8.5\\
SRLOG & 56.7 & 23.9 & 10.9 & 6.9 & 1.6\\
$x-1-\ln (x)$ & 4.2 & 3.9 & 7.3 & 12.1 & 19.4 & 15.9 & 11.4 & 8.6 & 5.6 &
3.4 & 8.5\\
SREXP & 71.7 & 26.5 & 1.9\\
$\exp (x)-1$ & 8.2 & 8.8 & 16.9 & 26.0 & 18.8 & 10.8 & 5.2 & 2.8 & 1.6 &
0.4 & 0.6\\
SSINPX & 10.6 & 7.6 & 9.9 & 11.9 & 13.0 & 12.1 & 12.1 & 8.7 & 5.8 & 3.3 & 5.2\\
$\sin (\pi x)$ & 4.9 & 4.8 & 8.8 & 11.8 & 12.9 & 13.2 & 13.6 & 11.4 &
8.2 & 5.2 & 5.4\\
SCOSPX & 11.4 & 7.1 & 11.4 & 11.8 & 11.4 & 12.2 & 11.1 & 10.1 & 6.0 &
3.9 & 3.9\\
$\cos (\pi x)$ & 5.1 & 4.3 & 9.4 & 10.9 & 12.7 & 13.7 & 13.4 & 11.9 &
7.8 & 5.3 & 5.6\\
SSIN1 & 91.1 & 8.9\\
$(x-\sin (x))/x^3$ & 54.2 & 20.1 & 2.0 & 2.3 & 2.0 & 2.2 & 2.1 & 1.5 &
1.6 & 1.8 & 9.8\\
SCOS1 & 76.5 & 15.8 & 7.7 & 0.1\\
$(1-\cos (x))/x^2$ & 57.5 & 18.6 & 4.3 & 2.0 & 1.7 & 2.4 & 1.6 & 2.2 &
2.1 & 1.8 & 6.1\\
SSINHM & 66.3 & 29.4 & 4.3\\
$\sinh(x)-x$ & 13.4 & 12.3 & 23.6 & 17.0 & 8.5 & 7.7 & 5.0 & 3.7 & 2.5 &
2.0 & 4.3\\
SCOSHM & 68.7 & 28.3 & 3.0\\
$\cosh(x)-1$ & 18.8 & 19.1 & 17.5 & 13.7 & 8.5 & 6.0 & 5.0 & 3.5 & 2.8 &
1.6 & 3.7\\
SCSHMM & 46.5 & 32.9 & 19.8 & 0.8\\
$\cosh(x)-1-x^2/2$ & 12.0 & 11.4 & 16.9 & 13.7 & 10.4 & 5.7 & 5.0 & 4.0 &
3.0 & 3.3 & 14.7\\
SGAM1 & 63.3 & 27.5 & 8.9\\
$1/\Gamma (1+x)-1$ & 8.2 & 8.7 & 13.9 & 20.7 & 22.1 & 14.7 & 5.4 & 3.7 &
1.5 & 0.6 & 0.7\\
\end{tabular}
\end{center}\vspace{-10pt}
\end{table*}

\begin{tabular}{@{}l@{}rrr}
{\bf Function} & {\bf avg $|\text{E}|$} &
{\bf $\max |\text{E}|$} & {\bf std. dev.}\\
SLNREL & 0.25 & 0.50 & 0.14\\
$\ln (x+1)$ & 0.50 & 1.49 & 1.49\\
SRLOG1 & 0.46 & 3.28 & 0.43\\
$x-\ln (x+1)$ & 8075.58 & $\approx 7.9\times 10^6$ & $\approx 1.9\times 10^5$\\
SRLOG & 0.47 & 3.10 & 0.43\\
$x-1-\ln (x)$ & 3341.24 & $\approx 2.9\times 10^6$ & $\approx 7.3\times 10^4$\\
SREXP & 0.37 & 1.39 & 0.26\\
$\exp (x)-1$ & 16.51 & 7402.74 & 190.13\\
SSINPX & 58.32 & 5795.79 & 253.07\\
$\sin (\pi x)$ & 119.20 & 48061.18 & 1193.09\\
SCOSPX & 84.64 & 35894.27 & 965.42\\
$\cos (\pi x)$ & 94.27 & 19712.38 & 588.76\\
SSIN1 & 0.32 & 0.97 & 0.13\\
$(x-\sin (x))/x^3$ & 407.81 & 28944.33 & 2091.66\\
SCOS1 & 0.25 & 2.02 & 0.38\\
$(x-\cos (x))/x^2$ & 102.36 & 7931.0 & 525.75\\
SSINHM & 0.35 & 1.36 & 0.35\\
$\sinh(x)-x$ & 2584.93 & $\approx 2.6\times 10^6$ & $\approx 6.7\times 10^4$\\
SCOSHM & 0.39 & 1.35 & 0.27\\
$\cosh(x)-1$ & 2302.36 & $\approx 3.7\times 10^6$ & $\approx 8.3\times 10^4$\\
SCSHMM & 0.65 & 23.00 & 0.68\\
\multicolumn{2}{@{}l}{$\cosh(x)-1-x^2/2$}\\
 & $\approx 1.9\times 10^5$ & $>10^7$ & $\approx 1.6\times
10^6$\\
SGAM1 & 0.46 & 2.44 & 0.37\\
$1/\Gamma (1+x)-1$ & 32.94 & 28016.70 & 686.03\\
\end{tabular}

For the functions SLNREL, SRLOG1, SRLOG, SREXP, SSINHM, SCOSHM and SGAM1 the
error in units of the least significant digit was approximately constant over
the entire region. For SSINPX and SCOSPX the error in units of the least
significant digit and the absolute error both increased as the argument
increased. For SCSHMM the error in units of the least significant digit was
nearly constant over the entire region, except that one sample had 5 wrong
bits near the zero of the function.

When $\ln (1+x)$, $x-\ln (1+x)$, $x-1-\ln (x)$, $(x-\sin (x))/x^3$, $%
(1-\cos (x))/x^2$, $\sinh (x)-x$, $\cosh(x)-1$, $\cosh (x)-1-x^2/2$
and $1/\Gamma (1+x)-1$ were computed
using single precision intrinsic functions and single precision
arithmetic, there was substantial increase in the error in units of the least
significant digit near the zeros of the functions, even though the absolute
error approached zero. For $\sin (\pi x)$ and $\cos (\pi x)$ the error
in units of
the least significant digit and the absolute error increased as the argument
increased. The average, maximum and standard deviation of the errors in
units of the
least significant digit are summarized in the table at left, in units of $%
\rho = 2^{-23} \approx 1.192 \times 10^{-7}$, the relative precision of IEEE
single precision arithmetic.

For arguments in the range $0<x<1$, SSINPX and SCOSPX commit approximately
the same error as equivalent expressions constructed from the intrinsic SIN
and COS functions.

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}

If the argument of xLNREL or xRLOG1 $\leq  -1.0$ or the argument of
xRLOG $\leq $ 0.0, the Fortran intrinsic logarithm function will
produce an error message. If the argument of xGAM1 $\leq -1.0$ the
xGAMMA function will produce an error message. If the magnitude of
the argument of SSINPX, DSINPX, SCOSPX or DCOSPX $> 1 / \rho $, where
$\rho $ is the smallest number that can be added to one and give a
result different from one, these routines will issue an error message
using the error message processor, described in Chapter~19.2, at
level~2. If error termination is suppressed by using ERMSET, SSINPX
and DSINPX return 0.0, while SCOSPX and DCOSPX return~1.0.

\subsection{Supporting Information}

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in}\bf Required Files\vspace{2pt}\\
DCOS1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DCOS1\rule[-5pt]{0pt}{8pt}}\\
DCOSHM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DCOSHM\rule[-5pt]{0pt}{8pt}}\\
DCOSPX & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, DCOSPX, DERM1, DERV1\rule[-5pt]{0pt}{8pt}}\\
\end{tabular}

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in}\bf Required Files\vspace{2pt}\\
DCSHMM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DCSHMM\rule[-5pt]{0pt}{8pt}}\\
DGAM1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DERM1, DERV1, DGAM1, DGAMMA, ERFIN, ERMSG\rule[-5pt]{0pt}{8pt}}\\
DLNREL & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DLNREL\rule[-5pt]{0pt}{8pt}}\\
DREXP & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DREXP\rule[-5pt]{0pt}{8pt}}\\
DRLOG & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DRLOG\rule[-5pt]{0pt}{8pt}}\\
DRLOG1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DRLOG\rule[-5pt]{0pt}{8pt}}\\
DSIN1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DSIN1\rule[-5pt]{0pt}{8pt}}\\
DSINHM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, DSINHM\rule[-5pt]{0pt}{8pt}}\\
DSINPX & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, DERM1, DERV1, DSINPX\rule[-5pt]{0pt}{8pt}}\\
SCOS1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SCOS1\rule[-5pt]{0pt}{8pt}}\\
SCOSHM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SCOSHM\rule[-5pt]{0pt}{8pt}}\\
SCOSPX & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, SCOSPX, SERM1, SERV1\rule[-5pt]{0pt}{8pt}}\\
SCSHMM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SCSHMM\rule[-5pt]{0pt}{8pt}}\\
SGAM1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, SERM1, SERV1, SGAM1, SGAMMA\rule[-5pt]{0pt}{8pt}}\\
SLNREL & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SLNREL\rule[-5pt]{0pt}{8pt}}\\
SREXP & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SREXP\rule[-5pt]{0pt}{8pt}}\\
SRLOG & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SRLOG\rule[-5pt]{0pt}{8pt}}\\
SRLOG1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SRLOG\rule[-5pt]{0pt}{8pt}}\\
SSIN1 & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, SSIN1\rule[-5pt]{0pt}{8pt}}\\
SSINHM & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
SSINHM\rule[-5pt]{0pt}{8pt}}\\
SSINPX & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
AMACH, ERFIN, ERMSG, SERM1, SERV1, SSINPX\rule[-5pt]{0pt}{8pt}}\\
\end{tabular}

Designed and programmed by W. V. Snyder, JPL 1991 and 1993.

\begcode

\medskip\

\lstset{language=[77]Fortran,showstringspaces=false}
\lstset{xleftmargin=.8in}

\centerline{\bf \large DRDLNREL}\vspace{10pt}
\lstinputlisting{\codeloc{dlnrel}}
\newpage

\vspace{30pt}\centerline{\bf \large ODDLNREL}\vspace{10pt}
\lstset{language={}}
\lstinputlisting{\outputloc{dlnrel}}
\end{document}


\section{xODE: Ordinary Differential Equations}

[Simon Hood prepared the code.  We do not yet have extensive
documentation.  See modules for discussion.  This matrixerial come from
the ode interface.

This code is not yet folded into the general m3na library architecture.
See the ``hood'' subdirectory.]

This is a Library of numerical integrators to
ordinary differential equations of arbitrary order, linear or nonlinear.
Both initial value problems and
  boundary value problems are catered for:

\subsection*{IVPs}
At present only one routine is supplied;  this is based on a
      Runge-Kutta Order Four scheme.  This is usually good enough.  I will
      be adding a Runge-Kutta Order Six scheme, simply because I have an
      implementation I did years ago knocking around, though I don't expect
      it to be significantly better very often.  More significant might be
      the addition of a predictor-corrector scheme or something specifically
      for ``stiff'' differential equations.

\subsection*{BVPs}
Two routines are/will be supplied:
      a ``relaxation/finite-difference''
      routine and a second based on a ``shooting'' algorithm.  The latter
      is not yet implemented --- though it solves, in principle at least,
      the same problems as the former.  In practice, one routine may converge
      when the other does not;  furthermore, one can provide an
      independent check of the other (assuming both converge!);  finally,
      rest assured that the shooting algorithm {\it will\/} be implemented
      in the not too distant future as I need it for my research!


\chapter{Introduction to Hypothesis Testing \label{chapter:hypothesistesting}}

Hypothesis testing is a central idea underpinning much of the analysis in the clinical and biomedical research literature\footnote{I should state that there is still a lot of controversy around the whole idea of hypothesis testing and whether $p$-values should be used at all, etc.}. There are multiple approaches to hypothesis testing, but the most common is \textbf{null hypothesis testing}, which was developed by the statistician R.A. Fisher. In null hypothesis testing, one creates a model of how the data should look under default conditions and then quantifies the observed data's deviation from that model using a \textbf{test statistic}. If the test statistic is large enough, it means there is evidence that the default position is incorrect.

The statisticians Jerzy Neyman and Karl Pearson developed a different approach to hypothesis testing based on the idea of \textbf{model comparison}. In their approach, one sets up different models and then quantifies each model's fit to the data; the hypothesis test is used to see whether one model's fit to the data is significantly better than another's. We see the Neyman-Pearson philosophy reflected in techniques such as power calculations and likelihood ratio tests.

Most of the basic hypothesis tests we learn in introductory biostatistics courses (T-tests, chi-squared tests, etc.) follow Fisher's approach. We will focus on null hypothesis testing in this chapter and explore other ideas in subsequent chapters.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Basic Steps of a Hypothesis Test}

\begin{enumerate}
\item \textit{State the \textbf{null hypothesis}}. The null hypothesis corresponds to the default, or baseline, position; for our example, the null hypothesis might be, ``The events `has mutation' and `has cancer' are statistically independent.'' The \textbf{alternative hypothesis} is the hypothesis that is contrary to the null; for our example, it might be, ``The events `has mutation' and `has cancer' are not statistically independent.''
\item \textit{List statistical {assumptions}}. All hypothesis tests make one or more assumptions about the data, and it's important to state them clearly. For example, \textbf{parametric} hypothesis tests assume the data follow a particular probability distribution under the null, while \textbf{nonparametric} tests do not make this assumption.
\item \textit{Decide on an appropriate test and test statistic}. The \textbf{test statistic} quantifies the degree of deviation of the observed data from what one would expect under the null hypothesis\footnote{Some definitions: A \textbf{statistic} is just some quantity that summarizes a set of data, or gives some information about the value of a parameter. A \textbf{sufficient statistic} is a statistic that gives the maximum amount of information about a parameter that can possibly be obtained from the sample data.}.
\item \textit{Derive the distribution of the test statistic under the null}. This is called the \textbf{null distribution}.
\item \textit{Select a {significance level} under which you'll reject the null}. The \textbf{significance level}, usually written as $\alpha$, is the probability of a type I error. A type I error is committed when one rejects the null even though it is true (false positive result).
\item \textit{Compute the observed value of the test statistic from the data.}
\item \textit{Decide whether or not to reject the null hypothesis.}
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{The Z-Test}

A \textbf{Z-test} is a hypothesis test for which the null distribution is normal with known mean and standard deviation (i.e. known parameters $\mu$ and $\sigma$). It is most commonly used to compare the mean of a set of samples, $\overline{x}$, with a known population mean. It also appears in other contexts, such as significance tests of regression coefficients in generalized linear models (Chapter~\ref{chapter:glms}).

\paragraph{Example: SBP in an Appalachian Town} The distribution of systolic blood pressure (SBP) among Caucasian males ages 55-64 in the United States is roughly normal with mean 139.75 mmHg and standard deviation 21.40 mmHg (Source: Int. J. Epidemiol. 2: 294-301, 1973). The following graph shows a normal distribution with those parameters.

\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-z-test-example-00.png}
\end{center}

Here is a histogram of 10,000 data samples drawn independently from that distribution (i.e., what we would expect if we sampled the SBPs of 10,000 men from the United States at large):

\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-z-test-example-0.png}
\end{center}

Now, assume some researchers find a small community in rural Appalachia and measure the SBP of 20 Caucasian males ages 55-64 there. Their mean SBP is 125.45 mmHg, illustrated by the red dashed line in the graph below.

\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-z-test-example-1.png}
\end{center}

At first glance, this may not appear that unusual. After all, the red line is sort of near the center of the gray distribution, right? This analysis is flawed, however, because our 125.45 mmHg value isn't for one man - it's an average over 20 men. The distribution of the \textbf{sample mean}, $\overline{x}$, is different from that of each individual sample.

To see this, imagine taking 20 samples from the gray distribution, taking their mean, and recording that value. Now repeat that process 10,000 times. If you do that, you get the \textbf{distribution of the sample mean},  which is skinnier than the gray distribution:
\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-z-test-example-2.png}
\end{center}
It turns out that the distribution of the sample mean will have the same mean, $\mu_0$, as the population distribution, but its standard deviation will be $\sigma/\sqrt{n}$, where $n$ is the number of samples over which the mean is taken.

\begin{question}{}
If $n=1$, what is the standard deviation of the sample mean? If $n=\infty$, what is the standard deviation of the sample mean?
\end{question}

\begin{question}{}
The sample mean for our $20$ sampled Appalachian men is shown as a vertical red dashed line in the figure above. Now that you know what the distribution of the sample mean looks like, do you think the observation from your Appalachian town is ``weird''?
\end{question}

\noindent Let's conduct a hypothesis test to evaluate whether we have evidence that the mean SBP among men in this town is different from that of the general U.S. population.

\begin{enumerate}
\item \textit{State the \textbf{null hypothesis}}. Here the null hypothesis is going to be our default position: that there is no difference. Let $\mu_c$ be the true mean SBP for men in the community and $\mu_0$ be the mean for the general population.
\begin{align*}
H_0: &~\mu_c = \mu_0 \\
H_a: &~\mu_c \neq \mu_0
\end{align*}
\item \textit{List statistical {assumptions}}. We make two assumptions. First, we assume that the SBPs of the different men in the sample are statistically independent. Second, we assume that under the null, SBP will follow a normal distribution with mean 139.75 and standard deviation 21.40, the same as the general population of men aged 55-64.
\item \textit{Decide on an appropriate test and test statistic}. Our test statistic in this case is going to be the \textbf{Z-statistic}, which measures the deviation of the sample mean from the population mean in units of the standard deviation of the sample mean, $\sigma/\sqrt{n}$:
$$ Z = \frac{\overline{x} - \mu_0}{\sigma / \sqrt{n}} \qquad \text{where} \qquad \overline{x} = \frac{1}{n} \sum_{i=1}^n x^{(i)}$$
In our case, $n = 20$ because $\overline{x}$, our sample mean, is an average of 20 samples.
\item \textit{Derive the distribution of the test statistic under the null}. The Z-statistic follows a \textbf{standard normal} distribution under the null, which is a normal distribution with $\mu=0$ and $\sigma=1$. To see this, remember that the distribution of $\overline{x}$ under the null is $\mathcal{N}(\mu_0, \sigma/\sqrt{n})$. When you calculate the Z-statistic, you shift that distribution by a distance $\mu_0$ so it is centered at zero, then adjust its width (standard deviation) to 1.0 by dividing by $\sigma/\sqrt{n}$.
\item \textit{Select a {significance level} under which you'll reject the null}. For the purposes of this example, we will choose $\alpha = 0.05$ (5\% chance of a type I error). The null distribution of the Z-statistic is shown below. The vertical dotted black lines are situated at the \textbf{critical values} that produce $\alpha = 0.05$ (the area under the null distribution that is outside those lines is 0.05).

\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-z-test-example-3-z.png}
\end{center}

\item \textit{Compute the observed value of the test statistic from the data.} The observed value of the test statistic is:
$$ Z = \frac{\overline{x} - \mu_0}{\sigma / \sqrt{n}} = \frac{125.45 - 139.75}{21.40/\sqrt{20}} = -2.99. $$
\item \textit{Decide whether or not to reject the null hypothesis.} The value of our test statistic falls outside the region contained by the critical values (the \textbf{acceptance region}), so we reject the null at this value of $\alpha$.
\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-z-test-example-4-z.png}
\end{center}
\end{enumerate}

\begin{question}{}
As $\alpha$ gets smaller, are you more or less likely to reject the null for the same value of the test statistic? Hint: What does making $\alpha$ smaller do to the positions of the two black dotted lines in the figure, above?
\end{question}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Definitions}

\begin{itemize}
\item \textbf{Type I Error:} When a hypothesis test rejects the null even though the null is true (also called a \textbf{false positive}). The type I error rate is usually denoted by $\alpha$.
\item \textbf{Type II Error:} When a hypothesis test fails to reject the null even though it is false (also called a \textbf{false negative}). The type II error rate is usually denoted by $\beta$.
\item \textbf{P-value:} The probability of obtaining a test statistic at least as extreme as the one that was actually obtained, assuming the null is true. A $p$-value can be \textbf{one-sided} or \textbf{two-sided}. The difference lies in the definition of ``extreme''. In a one-sided test, we find the probability that the test statistic is at least as extreme \emph{in the same direction} as the one we observed. In a two-sided test, we find the probability that the test statistic is at least as extreme \emph{in either direction} (positive or negative deviation). In most cases, this has the practical effect of doubling the $p$-value.
\item \textbf{Power:} The probability that a hypothesis test will reject the null when the null is false (that the test will detect a true effect if the effect is there). Usually denoted $1 - \beta$.
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Pearson's Chi-Squared Test}

Imagine you have data on two discrete variables for $n$ different subjects. You want to test whether the value of one covariate is independent of the value of the other. To do this, you can arrange your data in a \textbf{contingency table} where the rows and columns correspond to the values of the two variables. \textbf{Pearson's chi-squared test} can then be used to assess the independence of row and column values.

\paragraph{Example: Association of Genotype and Disease} Imagine you want to test whether a person's genotype at a particular locus is associated with whether or not he/she has Disease X. You find 100 people with the disease and 100 healthy controls ($n=200$) and genotype them:

\begin{center}
\includegraphics[width=0.55\textwidth]{img/pearson-chisq-fig-1.png}
\end{center}

\noindent \noindent Let's conduct a hypothesis test to examine this result.

\newcommand{\indep}{\perp \!\!\! \perp}
\newcommand{\independent}{\perp\mkern-9.5mu\perp}
\newcommand{\notindependent}{\centernot{\independent}}

\begin{enumerate}
\item \textit{State the \textbf{null hypothesis}}. We consider the genotype at this locus, $G$, to be a random variable (see Chapter~\ref{chapter:probabilitydistributions}) with three possible outcomes: \emph{AA}, \emph{Aa}, and \emph{aa}. We likewise consider the patient's disease status, $D$, to be a random variable with two possible outcomes: disease or no disease. We state our null hypothesis mathematically as:
\begin{align*}
H_0: &~ G \independent D \\
H_a: &~ G \notindependent D
\end{align*}
where the symbol $\independent$ refers to statistical independence of $G$ and $D$. We encountered statistical independence in our discussion of maximum likelihood in Chapter~\ref{chapter:mlebasics}. Mathematically, statistical independence means that the joint probability of observing a particular value for $G$ and a particular value for $D$ is simply equal to the product of their individual probabilities:
$$ P(G=g, D=d) = P(G=g) P(D=d) $$
Under these conditions, the expected values of the cells of our table are:
\begin{center}
\includegraphics[width=0.55\textwidth]{img/pearson-chisq-fig-2.png}
\end{center}
For example, consider the cell $G = AA, D = X$. Assuming the total number of patients is fixed at $n=200$ and $G$ and $D$ are independent, the expected number of people in that cell is:
\begin{align*} P(G=AA, D=X) \cdot n &= \left(\frac{119}{200}\right) \left(\frac{100}{200}\right) \cdot 200 \\
&= \mathbf{59.5} \end{align*}
Our task now is to decide whether our observed table counts are different enough from what we expect under the null to cause us to reject the null.

\item \textit{List statistical {assumptions}}. We assume that the data are sampled randomly and independently from a fixed population where each member of the population has an equal probability of selection\footnote{A further assumption of the chi-squared test is that expected counts for each cell must be sufficiently high. A common rule is 5 or more in all cells of a $2\times 2$ table, and 5 or more in 80\% of cells in larger tables, but no cells with zero counts.}.

\item \textit{Decide on an appropriate test and test statistic}. The chi-squared test works by calculating expected counts in all $r \times c$ cells of the table ($r$ = number of rows, $c$ = number of columns) and then measuring the data's deviation from those expected counts. The \textbf{chi-squared test statistic} has the form
$$ X^2 = \sum_{i=1}^r \sum_{j=1}^c \frac{(O_{ij} - E_{ij})^2}{E_{ij}} $$
where $O$ refers to ``observed count'' and $E$ to ``expected count''. The expected counts are those that assume statistical independence of rows and columns (blue table, above).

\item \textit{Derive the distribution of the test statistic under the null}. Under the null, the $X^2$ test statistic follows a chi-squared distribution (Section~\ref{sect:chisqdist}) with $(r-1)(c-1)$ degrees of freedom. In the case of our genotype example, there are $r=2$ rows and $c=3$ columns, thus $2$ degrees of freedom.

\item \textit{Select a {significance level} under which you'll reject the null}. The $\chi^2$ distribution with $2$ degrees of freedom is shown below. Two vertical lines are shown at different significance levels: $\alpha = 0.05$ and $\alpha = 0.1$.

\begin{center}
\includegraphics[width=0.6\textwidth]{img/hyp-chisq-test-example-2.png}
\end{center}

\item \textit{Compute the observed value of the test statistic from the data.}

\begin{question}{}
Using the formula in step~$4$, above, compute the actual value of the chi-squared test statistic for this example. Hint: You should end up with a value that corresponds to the position of the red dashed line in the figure below.
\begin{center}
\includegraphics[width=0.7\textwidth]{img/hyp-chisq-test-example-3.png}
\end{center}
\end{question}

\item \textit{Decide whether or not to reject the null hypothesis.} Based on our calculated value of the test statistic, we will reject the null at $\alpha = 0.1$ and fail to reject the null at $\alpha = 0.05$.
\end{enumerate}

Although it looks much different from the Z-test, the chi-squared test follows the same formalism: defining a null hypothesis, figuring out what the data should look like under the null, quantifying the deviation of the observed data from what's expected using a test statistic, and deciding if that test statistic presents strong enough evidence to cause us to reject the null.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Student's T-tests}

The final example we will look at today is the \textbf{T-test}. Like the $Z$-test, the $T$-test (actually a family of tests) deals with situations where you have data that are assumed to be normally distributed under the null hypothesis. However, in this scenario, the population standard deviation, $\sigma$ is not known and must be estimated from the data itself.

\subsection{One Sample T-test}

Assume you have a dataset $x^{(1)}, \dots, x^{(n)}$, of real numbers that you can plausibly assume are normally distributed. You want to test whether the mean of your data is equal to a fixed value, $\mu_0$. Under the null hypothesis that the means are the same, the test statistic
$$ T = \frac{\overline{x} - \mu_0}{s/\sqrt{n}} $$
which we call a ``T statistic'', follows a T-distribution (Section~\ref{sect:tdist}) with $n-1$ degrees of freedom\footnote{A one-sample T-test looks a lot like a Z-test. However, because we use $s$ to estimate the population standard deviation from data, we must account for variation in our estimate. It turns out that the sample variance, $s^2$, follows a chi-squared distribution with $n-1$ degrees of freedom, where $n$ is the sample size. In this case, by the definition of the $T$-distribution (Section~\ref{sect:tdist}), the T statistic follows a Student's T-distribution with $n-1$ degrees of freedom. As the number of samples, $n$, grows, the sample standard deviation approaches the population standard deviation and the T-test becomes a Z-test. But when $n$ is small, the T-test is quite a bit more conservative.}. Here $\overline{x}$ refers to the sample mean, and $s$ refers to the \textbf{sample standard deviation}:
$$ s = \sqrt{\frac{1}{n-1} \sum_{i=1}^n (x^{(i)} - \overline{x})^2} $$

\begin{question}{}
Compare the formula for the sample standard deviation to the maximum likelihood estimate of the parameter, $\sigma$, of a normal distribution (Section~\ref{sect:mlenormal}). What is the same/different? Note in particular the use of $n-1$ in the denominator, rather than $n$. This arises because the MLE for $\sigma$, $\hat{\sigma}$, is a \textbf{biased} estimate of the population standard deviation (more on this later). For large $n$, however, the two are nearly identical.
\end{question}

\subsection{Two Independent Samples, Equal Variance}

Assume you have a dataset $x^{(1)}, \dots, x^{(n)}$ and another dataset $y^{(1)}, \dots, y^{(m)}$. You assume that both are drawn from normal distributions with equal variance but potentially different means. You want to test whether the means are equal.

The same basic machinery for the one-sample T-test can be deployed in this context with a slightly different test statistic. The test statistic
$$ T = \frac{\overline{x} - \overline{y}}{s_p \sqrt{\cfrac{1}{n} + \cfrac{1}{m}}} $$
where
\begin{align*} s_p^2 &= \frac{(n-1) s_x^2 + (m-1) s_y^2}{m + n - 2} \\
s_x^2 &= \frac{1}{n-1} \sum_{i=1}^n (x^{(i)} - \overline{x})^2 \\
s_y^2 &= \frac{1}{m-1} \sum_{i=1}^m (y^{(i)} - \overline{y})^2 \end{align*}
follows a $t$-distribution with $m + n - 2$ degrees of freedom.

\subsection{Two Independent Samples, Unequal Variance}

Sometimes you have two independent samples but cannot assume the variances are equal. Again, similar machinery can be deployed. In this case, you can use \textbf{Welch's T-test}, which uses the test statistic
$$ T = \frac{\overline{x} - \overline{y}}{s_{xy}} $$
where
$$ s_{xy} = \sqrt{\frac{s_x^2}{n} + \frac{s_y^2}{m}}. $$
This test statistic approximately follows a $t$-distribution with degrees of freedom given by the {Welch-Sattherwaite Equation}
$$ \text{d.f.} = \frac{\left(\cfrac{s_x^2}{n} + \cfrac{s_y^2}{m} \right)^2}{\cfrac{(s_x^2/n)^2}{n-1} + \cfrac{(s_y^2/m)^2}{m-1}} $$

\subsection{Matched Pairs}

Assume you have a data set of matched pairs. This could be a set of measurements of the same individuals taken at two different points in time, for example, or paired measurements taken from individuals with similar characteristics. You want to test whether the second set of values have changed relative to the first set of values.

To do this, you can use a one-sample T-test on the \emph{differences} of the individual pairs. If no change has occurred, you would expect the mean of those differences to be zero. If we define $x^{(i)}$ as the difference of the paired observations for sample $i$ and $\overline{x}$ as $\frac{1}{n}\sum_{i=1}^n x^{(i)}$, the sample mean of those differences, then

$$ T = \frac{\overline{x}}{s/\sqrt{n}}$$

\noindent follows a T-distribution with $n-1$ degrees of freedom.

\begin{question}{}
Here are some sample data. They come from a study that looked at the effect of ozone, a component of smog, on the weight gain of rats. (Original source: Biometrika 63: 421-434, 1976, reproduced in Rice's \emph{Mathematical Statistics and Data Analysis}, p. 465.) A group of 22 seventy-day-old rats were kept in an environment containing ozone for $7$ days, and their weight gains were recorded. Another group of 23 rats of a similar age were kept in an ozone-free environment for a similar time and their weight gains were also recorded. Here are the data for the control group:

{\footnotesize \tt
\begin{center}
\begin{tabular}{rlrr}
  \toprule
  & group & original\_weight & weight\_gain \\
  \midrule
  1 & control & 340.8 & 41.0 \\
  2 & control & 389.1 & 25.9 \\
  3 & control & 355.2 & 13.1 \\
  4 & control & 421.8 & -16.9 \\
  5 & control & 377.1 & 15.4 \\
  6 & control & 404.3 & 22.4 \\
  7 & control & 321.2 & 29.4 \\
  8 & control & 447.5 & 26.0 \\
  9 & control & 305.9 & 38.4 \\
  10 & control & 335.9 & 21.9 \\
  11 & control & 386.3 & 27.3 \\
  12 & control & 377.0 & 17.4 \\
  13 & control & 357.2 & 27.4 \\
  14 & control & 441.7 & 17.7 \\
  15 & control & 383.7 & 21.4 \\
  16 & control & 373.7 & 26.6 \\
  17 & control & 336.0 & 24.9 \\
  18 & control & 419.4 & 18.3 \\
  19 & control & 287.1 & 28.5 \\
  20 & control & 602.8 & 21.8 \\
  21 & control & 325.4 & 19.2 \\
  22 & control & 452.4 & 26.0 \\
  23 & control & 398.9 & 22.7 \\
  \midrule
  Mean & control & 384.4 & 22.4 \\
  St.Dev. & control & 65.5 & 10.8 \\
  \bottomrule
\end{tabular}
\end{center}
}

\noindent And here are the data for the ozone group:

{\footnotesize \tt
\begin{center}
\begin{tabular}{rlrr}
  \toprule
  & group & original\_weight & weight\_gain \\
  \midrule
  1 & ozone & 437.4 & 10.1 \\
  2 & ozone & 275.9 & 7.3 \\
  3 & ozone & 296.3 & -9.9 \\
  4 & ozone & 295.9 & 17.9 \\
  5 & ozone & 379.7 & 6.6 \\
  6 & ozone & 274.1 & 39.9 \\
  7 & ozone & 360.0 & -14.7 \\
  8 & ozone & 331.9 & -9.0 \\
  9 & ozone & 531.8 & 6.1 \\
  10 & ozone & 350.5 & 14.3 \\
  11 & ozone & 345.7 & 6.8 \\
  12 & ozone & 268.1 & -12.9 \\
  13 & ozone & 339.9 & 12.1 \\
  14 & ozone & 352.4 & -15.9 \\
  15 & ozone & 435.8 & 44.1 \\
  16 & ozone & 476.9 & 20.4 \\
  17 & ozone & 462.5 & 15.5 \\
  18 & ozone & 368.0 & 28.2 \\
  19 & ozone & 504.3 & 14.0 \\
  20 & ozone & 188.0 & 15.7 \\
  21 & ozone & 466.9 & 54.6 \\
  22 & ozone & 288.8 & -9.0 \\
  \midrule
  Mean & ozone & 365.0 & 11.0 \\
  St.Dev. & ozone & 88.6 & 19.0 \\
  \bottomrule
\end{tabular}
\end{center}
}

\begin{enumerate}
\item[(a)] Imagine that the population weight distribution of rats is known to be normal with $\mu = 350$ (grams) and unknown $\sigma$. How would you test the hypothesis that the mean of the control group is equal to the population mean? How would you test the hypothesis that the mean of the ozone group is equal to the population mean?
\item[(b)] How would you test the hypothesis that the mean original weights of the ozone and control groups are equal? Do not assume equal variance.
\item[(c)] How would you test the hypothesis that the mean weight gain in the ozone group is equal to the mean weight gain in the control group? Do not assume equal variance.
\item[(d)] How would your approach in part (c) change if you assumed the weight gains in the two groups had equal variance?
\end{enumerate}

\noindent Plug in the relevant numbers from the tables above to perform each hypothesis test with $\alpha = 0.05$. The following table of critical values for the $T$-distribution\footnote{Borrowed with gratitude from https://www.stat.purdue.edu/~lfindsen/stat503/t-Dist.pdf} may help you:

\begin{center}
\includegraphics[width=0.9\textwidth]{img/t-distribution-critical-values.png}
\end{center}

\noindent \textbf{Answers:} (a) One-sample $T$-test of control group original weights vs. null of $\mu_0 = 350$; $T$-statistic is 2.5165, 22 d.f., two-sided $p$-value is 0.01964, reject null at $\alpha=0.05$. One-sample $T$-test of ozone group original weights vs. null of $\mu_0 = 350$; $T$-statistic is 0.7961, 21 d.f., two-sided $p$-value is 0.4349, fail to reject null at $\alpha=0.05$. (b) Welch's two-sample $T$-test of control vs. ozone group original weights; $T$-statistic is 0.8293, d.f is estimated using the Welch-Sattherwaite equation at 38.619, two-sided $p$-value is 0.4120, fail to reject null at $\alpha=0.05$. (c) Welch's two-sample $T$-test of control vs. ozone group weight gains; $T$-statistic is 2.4629, d.f. is estimated using the Welch-Sattherwaite equation at 32.918, two-sided $p$-value is 0.01918, reject null at $\alpha=0.05$. (d) You would use Pearson's two-sample $T$-test, which assumes equal variances; $T$-statistic is 2.4919, d.f. is 43, two-sided $p$-value is 0.01664, reject null at $\alpha=0.05$.

\end{question}