\documentclass[10pt]{beamer}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

%\input{slides_header.tex}
\input{/home/sahir/git_repositories/EPIB607/resources/assets/slides/slides_header2.tex}
\graphicspath{{/home/sahir/git_repositories/EPIB607/resources/assets/slides/figure/}}

%\let\oldShaded\Shaded
%\let\endoldShaded\endShaded
%\renewenvironment{Shaded}{\footnotesize\oldShaded}{\endoldShaded}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

	%\title{Introduction to Regression Trees}
	%\author{Sahir Bhatnagar \inst{1}}
	%\author[shortname]{Sahir Rai Bhatnagar, PhD Candidate (Biostatistics) }
	%\institute[shortinst]{Department of Epidemiology, Biostatistics and Occupational Health}

	\title{004 - Exploring Data - Part II}
	\author{EPIB 607 - FALL 2020}
	\institute{
		Sahir Rai Bhatnagar\\
		Department of Epidemiology, Biostatistics, and Occupational Health\\
		McGill University\\

		\vspace{0.1 in}

		\texttt{sahir.bhatnagar@mcgill.ca}\\
		%\texttt{\url{https://sahirbhatnagar.com/EPIB607/}}
	}

	\date{slides compiled on \today}

	\maketitle

\begin{frame}{Summarizing relationships between two variables}
							\protect\hypertarget{summarizing-relationships-between-two-variables}{}

							Approaches for summarizing relationships between two variables vary
							depending on variable types:

							\begin{itemize}
								\item
								Two numerical variables
								\item
								Two categorical variables
								\item
								One numerical variable and one categorical variable
							\end{itemize}

\end{frame}

\section{Two numerical variables and the correlation coefficient}

\begin{frame}[fragile]{Scatterplots}
	\protect\hypertarget{two-numerical-variables-1}{}

	\small

	\scriptsize

\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(ggplot2);} \hlkwd{library}\hlstd{(oibiostat);}
\hlkwd{data}\hlstd{(famuss)}

\hlkwd{plot}\hlstd{(famuss}\hlopt{$}\hlstd{height, famuss}\hlopt{$}\hlstd{weight,} \hlkwc{xlab} \hlstd{=} \hlstr{"Height (in)"}\hlstd{,} \hlkwc{ylab} \hlstd{=} \hlstr{"Weight (lb)"}\hlstd{)}

\hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= famuss,} \hlkwc{mapping} \hlstd{=} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= height,} \hlkwc{y} \hlstd{= weight))} \hlopt{+}
  \hlkwd{geom_point}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{0.8}\hlstd{,} \hlkwc{pch} \hlstd{=} \hlnum{21}\hlstd{)}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=0.45\linewidth]{figure/numerical-1}
\includegraphics[width=0.45\linewidth]{figure/numerical-2}

}

\end{knitrout}

	\normalsize

\end{frame}

						\begin{frame}{Pearson's correlation coefficient}
							\protect\hypertarget{two-numerical-variables}{}

								\begin{itemize}

							\item The \textbf{\textcolor{red}{sample}} correlation ($r$) between two variables $X$ and $Y$ is given by:
							\begin{align}
							r &= \frac{1}{n-1} \sum_{i=1}^n z_X \cdot z_Y \\
							&=\frac{1}{n-1} \sum_{i=1}^{n}\left(\frac{x_{i}-\bar{x}}{s_{X}}\right)\left(\frac{y_{i}-\bar{y}}{s_{Y}}\right)
							\end{align}

							\item $\left(x_{1}, y_{1}\right),\left(x_{2}, y_{2}\right), \ldots,\left(x_{n}, y_{n}\right)$ the $n$ paired sample values of $X$ and $Y$

							\item $z_{X}$ and $z_{Y}$ are the sample Z-scores of the $X$ and $Y$ variables, respectively

							\item $s_{X}$ and $s_{Y}$ are the sample standard deviations of the $X$ and $Y$ variables, respectively

							\item $\bar{x}$ and $\bar{y}$ are the sample means of the X and Y variables, respectively

							\pause

							\item The correlation coefficient quantifies the strength of a \textbf{\textcolor{red}{linear}} trend.

													\end{itemize}

						\end{frame}

\begin{frame}[fragile]{Plot of weight vs. height in \texttt{famuss} dataset}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=\maxwidth]{figure/unnamed-chunk-1-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]{Plot of Z-scores weight vs. Z-scores height in \texttt{famuss} dataset}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=\maxwidth]{figure/unnamed-chunk-2-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]{Partition the graph into four quadrants $(x,y)$}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=\maxwidth]{figure/unnamed-chunk-3-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]{Correlation depends on which quadrants the points are on}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=\maxwidth]{figure/unnamed-chunk-4-1}

}

\end{knitrout}

\end{frame}

\begin{frame}{Pearson's correlation coefficient}
	\protect\hypertarget{two-numerical-variables}{}

	\begin{itemize}

		\item
		The correlation coefficient \(r\) takes on values between -1 and 1.
		\pause
		\item
		The closer \(r\) is to \(\pm 1\), the stronger the linear association.
		\pause

		\item 	Two variables \(X\) and \(Y\) are

		\begin{itemize}
			\item
			\emph{positively associated} if \(Y\) increases as \(X\) increases ($r>0$)
			\item
			\emph{negatively associated} if \(Y\) decreases as \(X\) increases ($r<0$)
		\end{itemize}

	\pause

	\item Since the formula for calculating the correlation coefficient standardizes the variables, changes in scale or units of measurement will not affect its value

	\end{itemize}

\end{frame}

\begin{frame}{Exercise: Show mathematically that the correlation ($r$) is bounded by -1 and 1}
	\pause
	Consider that we can't have higher correlation than when we compare a list to itself (perfect correlation).
	\vspace{4in}
\end{frame}

\begin{frame}{Correlation and Simple linear Regression}
	\begin{itemize}
		\item
If we are predicting a random variable $Y$ knowing the value of another
variable $X=x$ using a regression line, then the formula for the regression can be given by:
\begin{equation}
\left(\frac{Y-\bar{y}}{s_{Y}}\right)=r\left(\frac{x-\bar{x}}{s_{X}}\right)
\end{equation}

\item This can be rewritten as:
\begin{align}
Y &= \bar{y}+r\left(\frac{x-\bar{x}}{s_{X}}\right) s_{Y}
\end{align}

	\end{itemize}
\end{frame}

\begin{frame}[fragile]{Correlation in \texttt{R}}
							\protect\hypertarget{two-numerical-variables-2}{}

							\begin{itemize}
		\item Correlation between weight and height in the \texttt{famuss} dataset:

\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{cor}\hlstd{(famuss}\hlopt{$}\hlstd{height, famuss}\hlopt{$}\hlstd{weight)}
\end{alltt}
\begin{verbatim}
## [1] 0.53
\end{verbatim}
\end{kframe}
\end{knitrout}

							\pause

							\item We can also obtain the correlation between \texttt{weight} and \texttt{height} from a simple linear regression:

\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(}\hlkwd{lm}\hlstd{(height} \hlopt{~} \hlstd{weight,} \hlkwc{data} \hlstd{= famuss))}
\end{alltt}
\begin{verbatim}
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  58.2952     0.5732   101.7   <2e-16 ***
## weight        0.0548     0.0036    15.2   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3 on 593 degrees of freedom
## Multiple R-squared: 0.282,	Adjusted R-squared: 0.281
## F-statistic:  233 on 1 and 593 DF,  p-value: <2e-16
\end{verbatim}
\end{kframe}
\end{knitrout}

						\pause

						\item Exercise: calculate the correlation coefficient from the regression coefficient for weight.

							\normalsize

					\end{itemize}

\end{frame}

\begin{frame}{Let's remind ourselves about random variability}
\begin{itemize}
	\item In many cases, we do not observe data for the entire population of interest but rather for a random sample.
	\item As with the mean and standard deviation, the sample correlation is the most commonly used estimator of the population correlation.
	\item This implies that the correlation we compute and use as a summary is a random variable.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Let's remind ourselves about random variability}
\small
Lets create a pseudo population from the 595 observations by sampling \textbf{with replacement}, and calculate the correlation. Lets repeat this process 1000 times:
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{B} \hlkwb{<-} \hlnum{1000}\hlstd{; N} \hlkwb{<-} \hlnum{595}
\hlstd{R} \hlkwb{<-} \hlkwd{replicate}\hlstd{(B, \{}
        \hlstd{dplyr}\hlopt{::}\hlkwd{sample_n}\hlstd{(famuss,} \hlkwc{size} \hlstd{= N,} \hlkwc{replace} \hlstd{=} \hlnum{TRUE}\hlstd{)} \hlopt{%>%}
        \hlstd{dplyr}\hlopt{::}\hlkwd{summarize}\hlstd{(}\hlkwc{r} \hlstd{=} \hlkwd{cor}\hlstd{(height, weight))} \hlopt{%>%}
        \hlstd{dplyr}\hlopt{::}\hlkwd{pull}\hlstd{(r)}
\hlstd{\})}
\end{alltt}
\end{kframe}
\end{knitrout}

\begin{figure}
	\begin{minipage}[h]{0.30\linewidth}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{mean}\hlstd{(R)}
\end{alltt}
\begin{verbatim}
## [1] 0.53
\end{verbatim}
\begin{alltt}
\hlkwd{quantile}\hlstd{(R,} \hlkwc{probs} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{0.025}\hlstd{,} \hlnum{0.975}\hlstd{))}
\end{alltt}
\begin{verbatim}
## 2.5%  98%
## 0.47 0.59
\end{verbatim}
\end{kframe}
\end{knitrout}
	\end{minipage}
	\hspace{0.4cm}
	\begin{minipage}[h]{0.59\linewidth}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(R,} \hlkwc{breaks} \hlstd{=} \hlnum{20}\hlstd{,} \hlkwc{col} \hlstd{=} \hlstr{"lightblue"}\hlstd{,} \hlkwc{xlab} \hlstd{=} \hlstr{"correlation"}\hlstd{,}
     \hlkwc{main} \hlstd{=} \hlstr{"Distribution of samples of size 595"}\hlstd{)}
\hlkwd{abline}\hlstd{(}\hlkwc{v} \hlstd{=} \hlkwd{mean}\hlstd{(R),} \hlkwc{col} \hlstd{=} \hlstr{"red"}\hlstd{,} \hlkwc{lwd} \hlstd{=} \hlnum{2}\hlstd{)}
\hlkwd{abline}\hlstd{(}\hlkwc{v} \hlstd{=} \hlkwd{quantile}\hlstd{(R,} \hlkwc{probs} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{0.025}\hlstd{,} \hlnum{0.975}\hlstd{)),} \hlkwc{col} \hlstd{=} \hlstr{"blue"}\hlstd{,}
       \hlkwc{lty} \hlstd{=} \hlnum{2}\hlstd{,} \hlkwc{lwd} \hlstd{=} \hlnum{2}\hlstd{)}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/histboot-1}

}

\end{knitrout}
	\end{minipage}
\end{figure}

\end{frame}

\begin{frame}{Another example: NHANES\footnote{\tiny{\url{http://www.cdc.gov/nchs/nhanes.htm}}}}
	\begin{itemize}
		\item The National Health and Nutrition Examination Survey (NHANES) consists of a set of surveys and measurements conducted by the US CDC to assess the health and nutritional status of adults	and children in the United States.
		\item The following example uses data from a sample of 500 adults (individuals ages 21 and older) from the NHANES dataset\footnote{\tiny{The sample is available as \texttt{nhanes.samp.adult.500} in the \texttt{R} \code{oibiostat} package}}.
	\end{itemize}
\end{frame}

\begin{frame}[fragile,plain]

\vspace{-0.5in}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{figure}

{\centering \subfloat[\label{fig:unnamed-chunk-6-1}]{\includegraphics[width=0.55\linewidth]{figure/unnamed-chunk-6-1} }
\subfloat[\label{fig:unnamed-chunk-6-2}]{\includegraphics[width=0.55\linewidth]{figure/unnamed-chunk-6-2} }

}

\caption[(a) A scatterplot showing height versus weight from the 500 individuals in the sample from NHANES]{(a) A scatterplot showing height versus weight from the 500 individuals in the sample from NHANES. One participant 163.9 cm tall (about 5 ft, 4 in) and weighing 144.6 kg (about 319 lb) is highlighted. (b) A scatterplot showing height versus BMI from the 500 individuals in the sample from NHANES. The same individual highlighted in (a) is marked here, with BMI 53.83. Fitted regression lines are shown in red with correlation coefficient $r$. BMI = weight/height$^2$ $\times 703$.}\label{fig:unnamed-chunk-6}
\end{figure}

\end{knitrout}

\end{frame}

\frame{\frametitle{Cautionary notes}
	\begin{itemize}

		\item The formulas above are for a \underline{particular sample}, hence the lower case letters $r,x,y$. In statistical terms, $r$ is the \textbf{estimator} for the population-level correlation $\rho$ (the \textbf{estimand}) of the random variables $X$ and $Y$. The actual value of the sample correlation is denoted by $\widehat{r}$ and is called the \textbf{estimate}

		\pause

		\item This implies that we are not 100\% confident in our estimate and therefore should provide a confidence interval  as well.

		\pause

		\item A strong linear relationship is not necessarily a
		\textbf{causal} relationship, that is, just because $r \approx 1$ (or $r \approx -1$)
		does not mean that $x$ \textbf{causes} changes in $y$ (we may have a \textit{spurious}
		correlation).  \pause

		\item Just because $r \approx 0$ does not mean that that $x$ and $y$
		are unrelated, merely that they are \textbf{uncorrelated}. That
		is, it is possible to construct examples where $x$ and $y$ have
		a strong functional relationship, but where $r=0$.

		\item $X,Y$ independent $\Rightarrow$ $r_{XY} = 0$
		\item $r_{XY} = 0$ $\not \Rightarrow$ $X,Y$ are independent

	\end{itemize}

}

\begin{frame}[fragile]{Anscombe's quartet\footnote{\tiny{Anscombe, Francis J. (1973). Graphs in statistical analysis. The American Statistician, 27, 17–21. doi: 10.2307/2682899.}}}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(datasets);}\hlkwd{data}\hlstd{(}\hlstr{"anscombe"}\hlstd{)}
\end{alltt}
\end{kframe}\begin{figure}

{\centering \includegraphics[width=0.55\linewidth]{figure/unnamed-chunk-7-1}

}

\caption[All four panels have the exact same linear correlation coefficient]{All four panels have the exact same linear correlation coefficient}\label{fig:unnamed-chunk-7}
\end{figure}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]{Zero linear correlation does not imply independence}
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{set.seed}\hlstd{(}\hlnum{12}\hlstd{)}
\hlstd{x} \hlkwb{<-} \hlkwd{runif}\hlstd{(}\hlnum{100}\hlstd{,}\hlopt{-}\hlnum{1}\hlstd{,}\hlnum{1}\hlstd{)}
\hlstd{y} \hlkwb{<-} \hlstd{x}\hlopt{^}\hlnum{2}
\hlkwd{plot}\hlstd{(x,y,} \hlkwc{pch} \hlstd{=} \hlnum{19}\hlstd{)}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/unnamed-chunk-8-1}

}

\begin{kframe}\begin{alltt}
\hlkwd{cor}\hlstd{(x,y)}
\end{alltt}
\begin{verbatim}
## [1] -0.023
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}{Another example of same summary statistics but very different relationships}
\url{https://www.autodeskresearch.com/publications/samestats}
\end{frame}

\begin{frame}[fragile,plain]{Transformations to improve linear fit}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{figure}

{\centering \subfloat[\label{fig:004-nyt-1}]{\includegraphics[width=0.53\linewidth]{figure/004-nyt-1} }
\subfloat[\label{fig:004-nyt-2}]{\includegraphics[width=0.53\linewidth]{figure/004-nyt-2} }

}

\caption{(a) per capita income vs. life expectancy (b) log per capita income vs. life expectancy. Fitted regression line in red with correlation coefficient $r$.\footnote{\tiny{The World Development Indicators (WDI) is a database of country-level variables (i.e., indicators) recording outcomes for a variety of topics, including economics, health, mortality, fertility, and education}}}\label{fig:004-nyt}
\end{figure}

\end{knitrout}

\end{frame}

\frame{\frametitle{Rank correlation} The Pearson correlation,
	recall, is a measure of \textit{linear} association. This may
	be undesirable for a number of reasons:
	\begin{itemize}
		\item $y$ and $x$ may be related, but not linearly (e.g., shape
		may be quadratic) \pause
		\item one or both of $y$ and $x$ may be an ordered categorical
		variable (e.g., highest level of education attained, income category, age group, etc.)
		and the investigator may not wish to impose a particular numerical scale \pause
		\item A nonparametric approach may be preferred if $y$ or $x$
		are thought not to be Normally distributed
	\end{itemize}
	We can overcome these concerns using a correlation that is
	based on the ranks of the data, called \textbf{Spearman's rank
		correlation}. } \frame{\frametitle{Spearman's rank correlation}
	This is most easily understood through the use of an example. \\ \ \\

	Suppose we want to examine the correlation between gestational
	age (GA) and birthweight (BW). {\footnotesize
		\begin{center}
			\begin{tabular}{|l|cccccccc|} \hline
				Infant & 1&2&3&4&5&6&7&8 \\ \hline
				BW (g) &2621&2863&3322&3508&3518&3770&3784&3801 \\
				GA (days) &270&271&267&268&276&282&288&278 \\
				\hline
				BW rank &1&2&3&4&5&6&7&8 \\
				GA rank &3&4&1&2&5&7&8&6 \\ \hline
			\end{tabular}
	\end{center} }
} \frame{\frametitle{Spearman's rank correlation} Spearman's
	rank correlation is based on the squared differences in rank
	for each individual:
	\begin{center}
		\begin{tabular}{|l|cccccccc|} \hline
			Infant & 1&2&3&4&5&6&7&8 \\ \hline
			Rank by BW &1&2&3&4&5&6&7&8 \\
			Rank by GA &3&4&1&2&5&7&8&6 \\ \hline
			Difference &-2&-2&2&2&0&-1&-1&2 \\
			Difference$^2$ &4&4&4&4&0&1&1&4 \\ \hline
		\end{tabular}
	\end{center}

	Then Spearman's rank correlation coefficient is computed to be
	\[ r_s = 1- \frac{6\sum d^2}{n^3-n}\]

	In our example, this gives $r_s = 0.738$. }

\frame{\frametitle{Spearman's rank correlation} Spearman's rank
	correlation is equivalent to calculating a Pearson's
	correlation on the ranks:
	\[r = \hbox{Corr}(\hbox{Rank}_{\hbox{GA}},\hbox{Rank}_{\hbox{BW}}) = 0.738 \]
}

\frame{\frametitle{Rank correlation: Kendall's $\tau$} There is
	another rank correlation, Kendall's $\tau$, which we will again
	learn by example. \\ \ \\

	We study the correlation between gestational age and
	birthweight. {\footnotesize
		\begin{center}
			\begin{tabular}{|l|cccccccc|} \hline
				Infant & 1&2&3&4&5&6&7&8 \\ \hline
				BW (g) &2621&2863&3322&3508&3518&3770&3784&3801 \\
				GA (days) &270&271&267&268&276&282&288&278 \\
				\hline
				BW rank&1&2&3&4&5&6&7&8 \\
				GA rank &3&4&1&2&5&7&8&6 \\ \hline
			\end{tabular}
	\end{center} }

} \frame{\frametitle{Rank correlation: Kendall's $\tau$}
	\begin{center}
		\begin{tabular}{|l|cccccccc|} \hline
			Infant & 1&2&3&4&5&6&7&8 \\ \hline
			Rank by b.weight &1&2&3&4&5&6&7&8 \\
			Rank by gest.age &3&4&1&2&5&7&8&6 \\ \hline
		\end{tabular}
	\end{center}
	First, we order the data according to one of the rankings (we
	chose to do so with birthweight).\\ \ \\

	Next, we sum the number of infants to the right of each cell
	with a \textbf{higher} ranking for the \textit{other} variable
	(gestational age), and call this $P$:
	\[P = 5+4+5+4+3+1+0=22\]
} \frame{\frametitle{Rank correlation: Kendall's $\tau$}

	Then Kendall's rank correlation coefficient is computed to be
	\[ \tau = \frac{2\times P}{\frac{1}{2}n(n-1)}-1\]

	In our example, this gives $\tau = 0.57$. \\ \ \\ \ \\ \pause

	We can perform hypothesis testing on Kendall's $\tau$; the
	approximately Normal test statistic is \[z = \frac{2\times
		P}{\sqrt{n(n-1)(2n+5)/18}}\] } \frame{\frametitle{Rank
		correlation: Kendall's $\tau$} In our example, if we wish to
	test $H_0: \tau=0$ vs.~$H_A: \tau \ne 0$, this gives
	\begin{eqnarray*}
		z & = & \frac{2\times P}{\sqrt{n(n-1)(2n+5)/18}}\\
		& = & \frac{2\times 22}{\sqrt{8\times7\times21/18}}\\
		& = & 5.444
	\end{eqnarray*}
	which yields a p-value of $P(|Z|>5.444) < 0.001$, indicating
	that there is a statistically significant association as
	measured by Kendall's rank correlation between gestational age
	and birthweight. }

\frame{\frametitle{Rank correlation} \textbf{Notes:}\\
	\begin{itemize}
		\item Both Spearman's and Kendall's correlations lie between
		$-1$ and 1; positive values correspond to a positive association, negative values to a
		negative association. \pause
		\item Both Spearman's and Kendall's correlations are
		nonparametric statistics. \pause
		\item Corrections for ties are required (beyond the scope of this course). \texttt{R} handles it for you.
	\end{itemize}

}

\section{Two categorical variables and contingency tables}

\begin{frame}[fragile]{Two categorical variables}
A contingency table summarizes data for two categorical variables:
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{tab1} \hlkwb{<-} \hlkwd{table}\hlstd{(famuss}\hlopt{$}\hlstd{race,}
              \hlstd{famuss}\hlopt{$}\hlstd{actn3.r577x)}
\hlstd{tab1}
\end{alltt}
\begin{verbatim}
##
##               CC  CT  TT
##   African Am  16   6   5
##   Asian       21  18  16
##   Caucasian  125 216 126
##   Hispanic     4  10   9
##   Other        7  11   5
\end{verbatim}
\begin{alltt}
\hlkwd{addmargins}\hlstd{(tab1)}
\end{alltt}
\begin{verbatim}
##
##               CC  CT  TT Sum
##   African Am  16   6   5  27
##   Asian       21  18  16  55
##   Caucasian  125 216 126 467
##   Hispanic     4  10   9  23
##   Other        7  11   5  23
##   Sum        173 261 161 595
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]{Conditional distribution of genotype \textit{given} race}
\begin{figure}
\begin{minipage}[h]{0.30\linewidth}
\small
			\begin{center}
			The distributions we create this way are called \textbf{\textcolor{blue}{conditional distributions}}, because they show the distribution of one variable for just those cases that satisfy a condition on another variable
			\end{center}

\normalsize
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{addmargins}\hlstd{(}
  \hlkwd{prop.table}\hlstd{(tab1,} \hlkwc{margin} \hlstd{=} \hlnum{1}\hlstd{)}
\hlstd{)}
\end{alltt}
\begin{verbatim}
##
##                CC   CT   TT  Sum
##   African Am 0.59 0.22 0.19 1.00
##   Asian      0.38 0.33 0.29 1.00
##   Caucasian  0.27 0.46 0.27 1.00
##   Hispanic   0.17 0.43 0.39 1.00
##   Other      0.30 0.48 0.22 1.00
##   Sum        1.72 1.93 1.35 5.00
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{minipage}
\hspace{0.4cm}
\begin{minipage}[h]{0.59\linewidth}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sjPlot}\hlopt{::}\hlkwd{plot_xtab}\hlstd{(famuss}\hlopt{$}\hlstd{race,}
                  \hlstd{famuss}\hlopt{$}\hlstd{actn3.r577x,}
                  \hlkwc{margin} \hlstd{=} \hlstr{"row"}\hlstd{)}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/genotype-marginals-1}

}

\end{knitrout}
\end{minipage}
\end{figure}
\end{frame}

\begin{frame}[fragile]{Conditional distribution of race \textit{given} genotype}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{addmargins}\hlstd{(}\hlkwd{prop.table}\hlstd{(tab1,} \hlkwc{margin} \hlstd{=} \hlnum{2}\hlstd{))}
\end{alltt}
\begin{verbatim}
##
##                 CC    CT    TT   Sum
##   African Am 0.092 0.023 0.031 0.147
##   Asian      0.121 0.069 0.099 0.290
##   Caucasian  0.723 0.828 0.783 2.333
##   Hispanic   0.023 0.038 0.056 0.117
##   Other      0.040 0.042 0.031 0.114
##   Sum        1.000 1.000 1.000 3.000
\end{verbatim}
\end{kframe}
\end{knitrout}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sjPlot}\hlopt{::}\hlkwd{plot_xtab}\hlstd{(famuss}\hlopt{$}\hlstd{race, famuss}\hlopt{$}\hlstd{actn3.r577x,} \hlkwc{margin} \hlstd{=} \hlstr{"col"}\hlstd{,} \hlkwc{show.total} \hlstd{= F,} \hlkwc{show.n} \hlstd{= F)}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/race-marginals-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]{Marginal distributions of race and genotype}
Given a contingency table, the frequency distribution of one of the variables is called its \textcolor{blue}{\textbf{marginal distribution}}.

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{table}\hlstd{(famuss}\hlopt{$}\hlstd{race)} \hlopt{/} \hlkwd{nrow}\hlstd{(famuss)}
\end{alltt}
\begin{verbatim}
##
## African Am      Asian  Caucasian   Hispanic      Other
##      0.045      0.092      0.785      0.039      0.039
\end{verbatim}
\begin{alltt}
\hlstd{sjPlot}\hlopt{::}\hlkwd{plot_frq}\hlstd{(famuss}\hlopt{$}\hlstd{race)}
\hlstd{sjPlot}\hlopt{::}\hlkwd{plot_frq}\hlstd{(famuss}\hlopt{$}\hlstd{actn3.r577x)}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=0.45\linewidth]{figure/unnamed-chunk-10-1}
\includegraphics[width=0.45\linewidth]{figure/unnamed-chunk-10-2}

}

\end{knitrout}

\end{frame}

\begin{frame}{Mosaic plots}
%	\small
\begin{itemize}
	\item A mosaic plot is a graphical display that allows you to examine the relationship among two or more categorical variables.
	\item The mosaic plot starts as a square with length one. The square is divided first into horizontal bars whose widths are proportional to the probabilities associated with the first categorical variable.
	\item Then each bar is split vertically into bars that are proportional to the conditional probabilities of the second categorical
	variable. Additional splits can be made if wanted using a third, fourth variable, etc.
\end{itemize}

\end{frame}

\begin{frame}[fragile]{Mosaic plots - race and genotype}

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{# devtools::install_github("haleyjeppson/ggmosaic")}
\hlstd{pacman}\hlopt{::}\hlkwd{p_load}\hlstd{(ggmosaic)}
\hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= famuss)} \hlopt{+}
  \hlkwd{geom_mosaic}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{=} \hlkwd{product}\hlstd{(race, actn3.r577x),}
                  \hlkwc{fill} \hlstd{= race))}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/mosaic-1-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]{Mosaic plots - race, genotype and sex}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
    \hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= famuss)} \hlopt{+}
      \hlkwd{geom_mosaic}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{=} \hlkwd{product}\hlstd{(race, actn3.r577x),}
                      \hlkwc{fill} \hlstd{= race,} \hlkwc{conds} \hlstd{=} \hlkwd{product}\hlstd{(sex)),}
                      \hlkwc{divider} \hlstd{=} \hlkwd{mosaic}\hlstd{(}\hlstr{"v"}\hlstd{))}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/mosaic-2-1}

}

\end{knitrout}

\end{frame}

\section{A numerical variable and a categorical variable}

\begin{frame}{A numerical variable and a categorical variable}
	\protect\hypertarget{a-numerical-variable-and-a-categorical-variable}{}

	\begin{itemize}
		\item \emph{FAMuSS} was designed to study the relationship between genotype at
	the location \emph{r577x} in the gene \emph{ACTN3} and muscle strength.

	\item Muscle strength was assessed by the percent change in non-dominant arm
	strength after resistance training (\texttt{ndrm.ch}).

	\item What visualization would be a good choice to make this comparison?
	\end{itemize}

\end{frame}

\begin{frame}[fragile]{A numerical variable and a categorical variable}
	\protect\hypertarget{a-numerical-variable-and-a-categorical-variable-1}{}

	\scriptsize

	\scriptsize

\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= famuss,} \hlkwc{mapping} \hlstd{=} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= actn3.r577x,} \hlkwc{y} \hlstd{= ndrm.ch,} \hlkwc{fill} \hlstd{= actn3.r577x))} \hlopt{+}
  \hlkwd{geom_boxplot}\hlstd{()}
\end{alltt}
\end{kframe}

{\centering \includegraphics[width=\maxwidth]{figure/box-1-1}

}

\end{knitrout}

	\normalsize

\end{frame}

\begin{frame}[fragile]{Correlations}
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{cor}\hlstd{(famuss}\hlopt{$}\hlstd{actn3.r577x, famuss}\hlopt{$}\hlstd{ndrm.ch)}
\end{alltt}

{\ttfamily\noindent\bfseries\color{errorcolor}{\#\# Error in cor(famuss\$actn3.r577x, famuss\$ndrm.ch): 'x' must be numeric}}\begin{alltt}
\hlkwd{cor}\hlstd{(}\hlkwd{as.numeric}\hlstd{(famuss}\hlopt{$}\hlstd{actn3.r577x), famuss}\hlopt{$}\hlstd{ndrm.ch,} \hlkwc{method} \hlstd{=} \hlstr{"pearson"}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 0.1
\end{verbatim}
\begin{alltt}
\hlkwd{cor}\hlstd{(}\hlkwd{as.numeric}\hlstd{(famuss}\hlopt{$}\hlstd{actn3.r577x), famuss}\hlopt{$}\hlstd{ndrm.ch,} \hlkwc{method} \hlstd{=} \hlstr{"kendall"}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 0.077
\end{verbatim}
\begin{alltt}
\hlkwd{cor}\hlstd{(}\hlkwd{as.numeric}\hlstd{(famuss}\hlopt{$}\hlstd{actn3.r577x), famuss}\hlopt{$}\hlstd{ndrm.ch,} \hlkwc{method} \hlstd{=} \hlstr{"spearman"}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 0.098
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\section{Summary}

\begin{frame}{Summary of exploring data slides}

\begin{itemize}
	\item Two types of variables:
	\begin{itemize}
		\item \textbf{Numeric}: Discrete, Continuous
		\item \textbf{Categorical}: Ordinal, Nominal
	\end{itemize}
\pause
\item The collection of values for a numerical or categorical is called the distribution of that variable
\pause
\item Measures of center include mean and median.
\item Measures of spread include standard deviation, interquartile range
\item Median and IQR are robust to outliers
\pause
\item Histograms, boxplots, violin plots, and scatterplots are useful graphical summaries of numerical data, which can also be grouped by a categorical variable
\item Bar plots, contingency tables, mosaic plots are useful summaries of categorical data
\end{itemize}
\end{frame}

\begin{frame}{Summary of exploring data slides \textit{continued}}

	\begin{itemize}
		\item Correlation coefficient ($r$) quantifies the strength of a linear trend.
		\item The \texttt{multiple R-squared} in a simple linear regression output is equal to $r^2$.
		\item Transformation (e.g. log) can produce better linear associations for highly skewed data. But be careful about the interpretation!
		\pause
		\item Given a contingency table, the frequency distribution of one of the variables is called its marginal distribution
		\item Conditional distributions show the distribution of one variable for just those cases that satisfy a condition on another variable
		\item See \url{https://www.r-graph-gallery.com/} and \url{https://www.data-to-viz.com/} for a collection of graphical displays
	\end{itemize}
\end{frame}

\begin{frame}[fragile]{Session Info}
	\tiny

\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Pop!_OS 19.10

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.7.so

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
 [1] ggmosaic_0.3.0      cowplot_1.0.0       openintro_2.0.0
 [4] usdata_0.1.0        cherryblossom_0.1.0 airports_0.1.0
 [7] oibiostat_0.2.0     NCStats_0.4.7       FSA_0.8.30
[10] forcats_0.5.0       stringr_1.4.0       dplyr_1.0.2
[13] purrr_0.3.4         readr_1.3.1         tidyr_1.1.2
[16] tibble_3.0.3        ggplot2_3.3.2.9000  tidyverse_1.3.0
[19] knitr_1.29

loaded via a namespace (and not attached):
 [1] nlme_3.1-143       fs_1.3.2           lubridate_1.7.4    RColorBrewer_1.1-2
 [5] insight_0.8.1      httr_1.4.1         backports_1.1.9    R6_2.4.1
 [9] sjlabelled_1.1.3   lazyeval_0.2.2     DBI_1.1.0          colorspace_1.4-1
[13] withr_2.2.0        tidyselect_1.1.0   emmeans_1.4.5      compiler_3.6.2
[17] performance_0.4.4  cli_2.0.2          rvest_0.3.5        pacman_0.5.1
[21] xml2_1.3.0         plotly_4.9.2       sandwich_2.5-1     labeling_0.3
[25] bayestestR_0.5.2   scales_1.1.1       mvtnorm_1.0-12     digest_0.6.25
[29] minqa_1.2.4        htmltools_0.5.0    pkgconfig_2.0.3    lme4_1.1-21
[33] dbplyr_1.4.2       highr_0.8          htmlwidgets_1.5.1  rlang_0.4.7
[37] readxl_1.3.1       rstudioapi_0.11    farver_2.0.3       generics_0.0.2
[41] zoo_1.8-7          jsonlite_1.7.0     sjPlot_2.8.3       magrittr_1.5
[45] parameters_0.5.0   Matrix_1.2-18      Rcpp_1.0.4.6       munsell_0.5.0
[49] fansi_0.4.1        lifecycle_0.2.0    stringi_1.4.6      multcomp_1.4-12
[53] snakecase_0.11.0   MASS_7.3-51.5      plyr_1.8.6         grid_3.6.2
[57] sjmisc_2.8.3       crayon_1.3.4       lattice_0.20-38    ggeffects_0.14.1
[61] haven_2.3.1        splines_3.6.2      sjstats_0.17.9     hms_0.5.3
[65] pillar_1.4.6       boot_1.3-24        estimability_1.3   effectsize_0.2.0
[69] codetools_0.2-16   reprex_0.3.0       glue_1.4.2         evaluate_0.14
[73] data.table_1.12.8  modelr_0.1.5       vctrs_0.3.4        nloptr_1.2.2.1
[77] cellranger_1.1.0   gtable_0.3.0       productplots_0.1.1 assertthat_0.2.1
[81] TeachingDemos_2.12 xfun_0.16          xtable_1.8-4       broom_0.7.0
[85] coda_0.19-3        viridisLite_0.3.0  survival_3.1-8     TH.data_1.0-10
[89] ellipsis_0.3.1
\end{verbatim}
\end{kframe}
\end{knitrout}

\end{frame}

\end{document}

\hypertarget{case-study-molecular-cancer-classification}{%
\section{Case study: molecular cancer classification}\label{case-study-molecular-cancer-classification}}

\begin{frame}{The potential value of genomic data in cancer}
							\protect\hypertarget{the-potential-value-of-genomic-data-in-cancer}{}

							The majority of cancers are diagnosed by an expert pathologist examining
							slides of malignant cells.

							Can that be done more accurately by characterizing the genetic makeup of
							the malignancy?

							\begin{itemize}
								\tightlist
								\item
								This is perhaps the major potential of genomic characterizations of
								tumors.
							\end{itemize}

							There are many forms of childhood leukemia.

							\begin{itemize}
								\item
								Acute myeloblastic leukemia (AML) and acute lymphoblastic leukemia
								(ALL) are the most common.
								\item
								AML is a cancer of the bone marrow, where white blood cells
								(lymphocytes) are produced.
								\item
								ALL is a cancer of the lymphocytes and is designated as B-cell (ALLB)
								or T-cell (ALLT).
							\end{itemize}

\end{frame}

\begin{frame}{Prognosis of the two cancers}
							\protect\hypertarget{prognosis-of-the-two-cancers}{}

							The probability that a child diagnosed with ALL is survives at least 5
							years after the diagnosis is approximately 90\%.

							Approximately 65\% of children diagnosed with AML survive at least 5
							years.

							The diagnosis of leukemia type determines the therapy that will be given
							to the child, and the successful treatments for ALL and AML are
							different.

							In 1999, Todd Golub from the Dana-Farber and the Broad Institute
							examined the possibility of classifying leukemia through using a genetic
							analysis of a blood sample.

						\end{frame}

						\begin{frame}{Analyzing the Golub data}
							\protect\hypertarget{analyzing-the-golub-data}{}

							We can re-analyze the Golub data using tools from graphical and
							numerical summaries.

							Our analysis will not be identical to the Golub analysis, but will be
							similar in spirit.

							The tools are straighforward\ldots

							\begin{itemize}
								\item
								Thinking through the problem and assembling the tools is the hard
								part.
								\item
								The process is more important than the final recipe.
							\end{itemize}

						\end{frame}

						\begin{frame}{Gene expression (details in \emph{OI Biostat})}
							\protect\hypertarget{gene-expression-details-in-oi-biostat}{}

							\small

							\begin{itemize}
								\item
								The genetic code stored in DNA contains the information for producing
								the proteins that determine an organism's phenotype.
								\item
								Genes that are transcriptionally active (i.e.~turned ``on'') are
								transcribed into messenger RNA (mRNA) that gets translated into
								proteins.
								\item
								Genes can be switched on or off, and expressed at varying levels.
								Variations in gene expression produce the range of physical,
								biochemical, and developmental differences in cells and tissues.
								\item
								Quantifying the amount of RNA produced in a cell allows for a measure
								of gene expression.
								\item
								The transcriptome, or expression profile, is the complete set of RNA
								transcripts produced by the genome in a cell or set of cells.
							\end{itemize}

						\end{frame}

						\begin{frame}{Microarrays (details in \emph{OI Biostat})}
							\protect\hypertarget{microarrays-details-in-oi-biostat}{}

							\small

							\begin{itemize}
								\item
								Microarray technology is based on hybridization between two DNA
								strands, in which complementary nucleotide sequences specifically pair
								together.
								\item
								The mRNA from a sample is converted into complementary-DNA (cDNA),
								labeled with a fluorescent dye, and added to the microarray.
								\item
								When cDNA from the sample encounters complementary DNA probes, the two
								strands will hybridize, allowing the cDNA to adhere to specific spots
								on the slide.
								\item
								When the chip is illuminated and scanned, the intensity of
								fluorescence detected at each spot corresponds to the amount of bound
								cDNA.
								\item
								DNA microarrays do not directly quantify gene expression levels or
								quantity of mRNA present in a sample.
								\item
								The fluorescence intensity data only provide a relative measure of
								gene expression, showing which genes on the chip seem to be more or
								less active in relation to each other.
							\end{itemize}

						\end{frame}

						\begin{frame}{Microarrays}
							\protect\hypertarget{microarrays}{}

							\begin{figure}
								\centering
								\includegraphics[scale=0.5]{figures/microarray_schematic.jpg}
								\caption{fluorescence detection}
							\end{figure}

						\end{frame}

						\begin{frame}{The Golub clinical data}
							\protect\hypertarget{the-golub-clinical-data}{}

							Demographic variables described in \emph{OI Biostat} Table 1.54:

							\scriptsize

							\begin{longtable}[]{@{}ll@{}}
								\toprule
								\begin{minipage}[b]{0.12\columnwidth}\raggedright
									Variable\strut
								\end{minipage} & \begin{minipage}[b]{0.82\columnwidth}\raggedright
									Description\strut
								\end{minipage}\tabularnewline
								\midrule
								\endhead
								\begin{minipage}[t]{0.12\columnwidth}\raggedright
									Samples\strut
								\end{minipage} & \begin{minipage}[t]{0.82\columnwidth}\raggedright
									Sample or chip number. The material from each patient was examined on a
									separate chip and experimental run.\strut
								\end{minipage}\tabularnewline
								\begin{minipage}[t]{0.12\columnwidth}\raggedright
									BM.PB\strut
								\end{minipage} & \begin{minipage}[t]{0.82\columnwidth}\raggedright
									Type of patient material. BM denotes bone marrow; PB denotes a
									peripheral blood sample.\strut
								\end{minipage}\tabularnewline
								\begin{minipage}[t]{0.12\columnwidth}\raggedright
									Gender\strut
								\end{minipage} & \begin{minipage}[t]{0.82\columnwidth}\raggedright
									F for female, M for male.\strut
								\end{minipage}\tabularnewline
								\begin{minipage}[t]{0.12\columnwidth}\raggedright
									Source\strut
								\end{minipage} & \begin{minipage}[t]{0.82\columnwidth}\raggedright
									Hospital where the patient was treated.\strut
								\end{minipage}\tabularnewline
								\begin{minipage}[t]{0.12\columnwidth}\raggedright
									tissue.mf\strut
								\end{minipage} & \begin{minipage}[t]{0.82\columnwidth}\raggedright
									A variable showing the combination of type of patient material and sex
									of the patient. BM:f denotes bone marrow from a female patient,
									etc.\strut
								\end{minipage}\tabularnewline
								\begin{minipage}[t]{0.12\columnwidth}\raggedright
									cancer\strut
								\end{minipage} & \begin{minipage}[t]{0.82\columnwidth}\raggedright
									The type of leukemia; aml is acute myeloblastic leukemia, allB is acute
									lymphoblastic leukemia which started in B-cells (cells that mature into
									plasma cells) origin, and allT is acute lymphoblastic leukemia with
									T-cell origin (T-cells are a type of white blood cell).\strut
								\end{minipage}\tabularnewline
								\bottomrule
							\end{longtable}

						\end{frame}

						\begin{frame}{The Golub expression data}
							\protect\hypertarget{the-golub-expression-data}{}

							The expression data is contained in the last 7,129 columns.

							Each column is a variable with a name corresponding to the name of the
							probe on the microarray.

							The expression levels record fluorescence intensity for each gene.

							\begin{itemize}
								\item
								The intensity levels have no inherent biological meaning.
								\item
								Data have been normalized to adjust for variability between the
								separate arrays used for each patient.
							\end{itemize}

						\end{frame}

						\begin{frame}{Selected variables and columns from Golub data}
							\protect\hypertarget{selected-variables-and-columns-from-golub-data}{}

							\captionsetup[table]{labelformat=empty}
							\scriptsize

							\scriptsize

							\begin{longtable}[]{@{}rllrrr@{}}
								\caption{\emph{OI Biostat} Table 1.40}\tabularnewline
								\toprule
								Samples & Gender & cancer & AFFX-BioB-5\_at & AFFX-BioB-M\_at &
								AFFX-BioB-3\_at\tabularnewline
								\midrule
								\endfirsthead
								\toprule
								Samples & Gender & cancer & AFFX-BioB-5\_at & AFFX-BioB-M\_at &
								AFFX-BioB-3\_at\tabularnewline
								\midrule
								\endhead
								39 & F & allB & -1363.28 & -1058.59 & -541.47\tabularnewline
								40 & F & allB & -796.29 & -1167.10 & 7.54\tabularnewline
								42 & F & allB & -679.14 & -1069.83 & -690.30\tabularnewline
								47 & M & allB & -1164.40 & -1109.94 & -990.13\tabularnewline
								48 & F & allB & -1299.65 & -1402.00 & -1077.54\tabularnewline
								\bottomrule
							\end{longtable}

							\normalsize

						\end{frame}

						\begin{frame}{Analyzing the Golub leukemia data}
							\protect\hypertarget{analyzing-the-golub-leukemia-data}{}

							We will do an analysis in class using some of the simple but
							surprisingly powerful ideas behind numerical and graphical summaries.

							The goal of the Golub study was to develop a procedure for
							distinguishing between AML and ALL based only on the gene expression
							levels of a patient. There are two major issues to be addressed:

							\begin{enumerate}
								\item
								Which genes are the most informative for making a prediction?
								\item
								What is a workable strategy for predicting leukemia type from
								expression data for a specific set of genes?
							\end{enumerate}

						\end{frame}

						\begin{frame}[fragile]{Starting small\ldots{}}
							\protect\hypertarget{starting-small}{}

							\footnotesize

							\scriptsize

\begin{verbatim}
##    cancer        A         B        C        D
## 69   allB 39307.96 35232.401 41170.76 35792.79
## 67   allT 32281.88 41432.024 59328.51 49608.14
## 55   allB 47429.94 35568.928 56074.96 42857.78
## 56   allB 25533.87 16983.749 28056.75 32693.92
## 59   allB 35960.55 24191.746 27637.90 22240.75
## 52    aml 46177.95  6189.465 12557.24 34485.41
## 53    aml 43790.70 33661.825 38380.30 29758.25
## 51    aml 53420.05 26109.245 31427.20 23809.70
## 50    aml 41241.59 37589.773 47325.77 30099.36
## 54    aml 41300.57 49198.412 66026.10 56248.62
\end{verbatim}
\end{frame}

%\begin{frame}[allowframebreaks]
%\nocite{breiman1984classification}
%	\nocite{friedman2001elements}
%	\nocite{james2013introduction}
%	\nocite{lopez2015arbres}
%	\frametitle{References}
%\printbibliography
%\end{frame}

\end{document}


\documentclass[10pt]{report}

\usepackage{geometry}
\geometry{
	letterpaper,
	hmargin=0.5in,
	vmargin=1in,
	footskip=0.25in
}

\usepackage{enumerate} % for enumerate counter
\usepackage{subcaption} % for subfigures
\usepackage{amsthm} % for QED
\usepackage{mathtools} % for delimiter

\usepackage{listings} % for code
\lstset{
	language=R,
	basicstyle=\footnotesize\ttfamily,
	numbers=none,
	stepnumber=1,
	numbersep=8pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	frame=single,
	tabsize=2,
	captionpos=t,
	breaklines=true,
	breakatwhitespace=false
}

\usepackage{float} % for figure [H]
\usepackage{booktabs} % for tabular
\usepackage{caption} % for \caption*
\usepackage[export]{adjustbox} % for valign=t
\usepackage{array} % for column type m
\usepackage{verbatim}
\usepackage{graphicx}
%\graphicspath{ {imgs/} }

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[L]{\hwAuther}
\fancyhead[C]{\courseNo}
\fancyhead[R]{\hwNo}

\usepackage{amssymb}
\usepackage{amsmath}

%Cover
\newcommand{\courseTitle}{Introduction to Mathematical Modeling}
\newcommand{\courseNo}{Math 380}
\newcommand{\hwAuther}{Zhihao Ai}

\newcommand{\hwNo}{HW \#4}
\newcommand{\hwDate}{Due on 02/20}

\title{
	\courseTitle\\
	\hwNo\\
	\hwDate
}
\author{\hwAuther}
\date{}
%

%Custom
%\everymath{\displaystyle}
\setlength\parindent{0pt}

%Custom commands
\newcommand{\ds}{\displaystyle}
\newcommand{\ts}{\textstyle}

\newcolumntype{N}{>$ c <$}
\newcolumntype{M}[1]{>{\centering\arraybackslash $}m{#1}<{$}}

\newcommand{\abs}[1] {\left| #1 \right|}

\DeclarePairedDelimiter\autoparen{(}{)}
\newcommand{\pa}[1]{\autoparen*{#1}}

\newcommand{\var} {\text{var}}

\newcommand{\m}[1] {\mathbf{#1}}

\begin{document}

\maketitle

\section*{Section 3.3}
\begin{enumerate}
	\item [4.]
	Make an appropriate transformation to fit the model $P=ae^{bt}$ using Equation (3.4). Estimate $a$ and $b$.
	\begin{table}[H]
		\centering
		\begin{tabular}{*{7}{c}}
			\toprule
			$t$ & 7 & 14 & 21 & 28 & 35 & 42 \\ \midrule
			$P$ & 8 & 41 & 133 & 250 & 280 & 297 \\
			\bottomrule
		\end{tabular}
	\end{table}
	Let $y=\ln{P}$, $ae^{bt} = \ln{a} + bt$, $A=\ln{a}$, we have
	\begin{table}[H]
		\centering
		\begin{tabular}{*{7}{c}}
			\toprule
			$t$ & 7 & 14 & 21 & 28 & 35 & 42 \\ \midrule
			$y$ & 2.08 & 3.71 & 4.89 & 5.52 & 5.63 & 5.69 \\
			\midrule
			$t^2$ & 49 & 196 & 441 & 784 & 1225 & 1764 \\
			\midrule
			$t y$ & 14.56 & 51.99 & 102.70 & 154.60 & 197.22 & 239.14 \\
			\bottomrule
		\end{tabular}
	\end{table}
	Using Equation (3.4), we have
	\begin{align*}
		4459b + 147A &= 760.2\\
		147A + 6b &= 27.53
	\end{align*}
	Solving the equations, $A=2.14$, $b=0.1$, so $a = e^A = 8.5$.

	\item [8.]
	Fit the data with the models given, using least squares.
	\begin{table}[H]
		\centering
		\begin{tabular}{*{10}{c}}
			\toprule
			$x(\times 10^{-3})$ & 5 & 10 & 20 & 30 & 40 & 50 & 60 & 70 & 80 \\ \midrule
			$y(\times 10^{-5})$ & 0 & 19 & 57 & 94 & 134 & 173 & 216 & 256 & 297 \\
			\bottomrule
		\end{tabular}
	\end{table}
	\begin{enumerate}[a.]
		\item
		$y=ax$
		\begin{align*}
			a \sum_{i=1}^{9} x_i^2 &= \sum_{i=1}^{9} x_i y_i\\
			0.0204a &= 0.000728\\
			a &= 0.0356
		\end{align*}
		So, $y=0.0356x$.

		\item
		$y=b+ax$

		Solving
		\begin{align*}
			0.020425a + 0.365b &= 0.000728\\
			0.365a + 9b &= 0.01246
		\end{align*}
		we have $a=0.0396$ and $b=-0.000222$. So, $y=0.0396x - 0.000222$.
	\end{enumerate}
\end{enumerate}

\section*{Section 3.4}
\begin{enumerate}
	\item [7.]
	\begin{enumerate}
		\item [a.]
		In the following data, $W$ represents the weight of a fish and $l$ represents its length. Fit the model $W=kl^3$ to the data using the least-squares criterion.
		\begin{table}[H]
			\centering
			\begin{tabular}{*{9}{c}}
				\toprule
				Length, $l$ (in.) & 14.5 & 12.5 & 17.25 & 14.5 & 12.625 & 17.75 & 14.125 & 12.625 \\ \midrule
				Weight, $W$ (oz) & 27 & 17 & 41 & 26 & 17 & 49 & 23 & 16  \\
				\bottomrule
			\end{tabular}
		\end{table}
		Sum of square error
		\[
		S = \sum_{i=1}^{8} (W_i - kl_i^3)^2
		\]
		Setting the derivative to 0, we have
		\begin{align*}
			\frac{\partial S}{\partial k} &= 0\\
			-2 \sum_{i=1}^{8} (W_i - kl_i^3)l_i^3 &= 0\\
			k &= \frac{\sum_{i=1}^{8} W_i l_i^3}{\sum_{i=1}^{8} l_i^6}\\
			k &= 0.00844
		\end{align*}
		Thus, $W=0.00844l^3$.

		\item [b.]
		In the following data, $g$ represents the girth of a fish. Fit the model $W=klg^2$ to the data using the least-squares criterion.
		\begin{table}[H]
			\centering
			\begin{tabular}{*{9}{c}}
				\toprule
				Length, $l$ (in.) & 14.5 & 12.5 & 17.25 & 14.5 & 12.625 & 17.75 & 14.125 & 12.625 \\ \midrule
				Girth, $g$ (in.) & 9.75 & 8.375 & 11.0 & 9.75 & 8.5 & 12.5 & 9.0 & 8.5  \\ \midrule
				Weight, $W$ (oz) & 27 & 17 & 41 & 26 & 17 & 49 & 23 & 16  \\
				\bottomrule
			\end{tabular}
		\end{table}
		Sum of square error
		\[
		S = \sum_{i=1}^{8} (W_i - k l_i g_i^2)^2
		\]
		Setting the derivative to 0, we have
		\begin{align*}
			\frac{\partial S}{\partial k} &= 0\\
			-2 \sum_{i=1}^{8} (W_i - k l_i g_i^2) l_i g_i^2 &= 0\\
			k &= \frac{\sum_{i=1}^{8} W_i l_i g_i^2}{\sum_{i=1}^{8} l_i^2 g_i^4}\\
			k &= 0.0187
		\end{align*}
		Thus, $W=0.0187lg^2$.
	\end{enumerate}

	\item [8.]
	Use the data presented in Problem 7b to fit the models $W=cg^3$ and $W=kgl^2$. Interpret these models. Compute appropriate indicators and determine which model is best. Explain.

	To fit $W=cg^3$, setting the derivative to 0, we have
	\begin{align*}
		\frac{\partial S}{\partial c} &= 0\\
		-2 \sum_{i=1}^{8} (W_i - c g_i^3) g_i^3 &= 0\\
		c &= \frac{\sum_{i=1}^{8} W_i g_i^3}{\sum_{i=1}^{8} g_i^6}\\
		c &= 0.0276
	\end{align*}
	Thus, $W=0.0276g^3$.

	To fit $W=kgl^2$, setting the derivative to 0, we have
	\begin{align*}
		\frac{\partial S}{\partial k} &= 0\\
		-2 \sum_{i=1}^{8} (W_i - k l_i^2 g_i) l_i^2 g_i &= 0\\
		k &= \frac{\sum_{i=1}^{8} W_i l_i^2 g_i}{\sum_{i=1}^{8} l_i^4 g_i^2}\\
		k &= 0.0126
	\end{align*}
	Thus, $W=0.0126gl^2$.

	The summary of the results for all 4 models is shown below:
	\begin{table}[H]
		\centering
		\begin{tabular}{*{3}{c}}
			\toprule
			Model & $\sum [W_i - f(l_i, g_i)]^2$ & $\max \abs{W_i - f(l_i, g_i)} $ \\ \midrule
			$W=0.00844l^3$ & 12.1693 & 2.32212  \\ \midrule
			$W=0.0187lg^2$ & 17.6832 & 2.86328  \\ \midrule
			$W=0.0276g^3$ & 54.2605 & 4.90625  \\ \midrule
			$W=0.0126gl^2$ & 3.39977 & 1.17079  \\
			\bottomrule
		\end{tabular}
	\end{table}
	Using either least square or Chebyshev criterion, $W=0.0126gl^2$ is the best model.
\end{enumerate}

\end{document}


\part{Annex}
\label{part:annex}

\chapter{Overview of compilation}

\input{overview}

\chapter{Automata theory for lexing}

In this chapter, we present the basic notions of lexical analysis,
also known as \emph{lexing} or \emph{scanning}.

\input{token_spec}
\input{regexp}
\input{lex}
\input{token_recog}
\input{dfa}
\input{nfa}
\input{subset}
\input{enfa}
\input{re_to_enfa}


\documentclass[10pt,a4paper]{article}% ===> this file was generated automatically by noweave --- better not edit it
\usepackage{noweb}
\usepackage{hyperref}

\usepackage{tikz}
\usetikzlibrary{calc}

\noweboptions{smallcode,longchunks}
\begin{document}
\pagestyle{noweb}
\nwfilename{hello1.tex}\nwbegindocs{1}\nwdocspar
\section{Inorder Traversal}
\subsection{Introduction}
Inorder traversal of tree is means of traversal of tree by which we can visit every node.\\
\textbf{Inorder traversal}
\begin{enumerate}
\item First, visit all the nodes in the left subtree
\item Then the root node
\item Visit all the nodes in the right subtree
\end{enumerate}
\subsection{Recovery}
We can't recover binary tree from its inorder traversal with approach discussed in the document \href{http://www.cse.iitd.ernet.in/~sak/courses/ilfp/recover.pdf}{Rambling through Woods on a Sunny Morning}. \\
We have to add some extra information in inorder traversal to recover it. \\
One idea that will not work is to store the number of children for each node in inordertraversal. \\
Consider the following binary tree

\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {level distance=10mm,sibling distance = 30mm/#1}
  ]
  \node {1}
  child {node {2}
                child {edge from parent[draw = none]}
                child {node {4}}
        }
  child {node {3}
        child {node {5}}
        child {node {6}
                        child {node {7}}
                        child {edge from parent[draw = none]}
               }
        };
\end{tikzpicture}
\end{center}
\caption{Binary tree}
\end{figure}

If we encode number of children information to each node then we get following inorder traversal
\begin{center}
        [(2,1),(4,0),(1,2),(5,0),(3,2),(7,0),(6,1)]
\end{center}
where first element in tuple is node value and second element is its number of children. But there can be another tree with same inorder traversal and same children count. Following is the another tree with the same inorder traversal and same children count for each node.

\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {level distance=10mm,sibling distance = 30mm/#1}
  ]
  \node {3}
  child {node {1}
        child {node {2}
                child {edge from parent[draw = none]}
                child {node {4}}
        }
        child {node {5}}
        }
  child {node {6}
                child {node {7}}
                child {edge from parent[draw = none]}
        };
\end{tikzpicture}
\end{center}
\caption{Binary tree with same inorder traversal and number of children}
\end{figure}
So our idea of storing children count of each node with inorder traversal not works as we have counterexample. \\
So we have think about another idea.
\subsection{Recovery using level}
What if we store level of each node along with the inorder traversal?
Then we can find root of tree easily. Root of tree will be node with level 0. And there will be only one node with level 0. If we split inorder list about root, we also get inorder list for its left subtree and right subtree. The root for each subtree will be the node with minimum level.\\

\nwenddocs{}\nwbegincode{2}\sublabel{NW4896Yo-N7IVZ-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-N7IVZ-1}}}\moddef{inorder~{\nwtagstyle{}\subpageref{NW4896Yo-N7IVZ-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2dJGUc-1}}\nwenddeflinemarkup
local
        fun ino (Empty, Llist, Level) = Llist
                | ino (Node (N, LST, RST), Llist, Level) =
                        let val Mlist = ino (RST, Llist, Level+1)
                                val Nlist = ino (LST, (N,Level)::Mlist, Level+1)
                        in Nlist
                        end
in
        fun inorder T = ino (T, [],0)
end
\nwused{\\{NW4896Yo-2dJGUc-1}}\nwendcode{}\nwbegindocs{3}\nwdocspar

The above inorder function do inorder traversal of binary tree. While doing inorder traversal it also store level of node in tuple. It returns the list of tuples, where each tuple is made of node value and node level. \\
When we do inorder on example tree we get the following inorder traversal
\begin{center}
[(2,1),(4,2),(1,0),(5,2),(3,1),(7,3),(6,2)]
\end{center}
Here we can see that there is only one node with level 0 which has node value 1. So it must be root of tree and all left side of it is inorder traversal of its left subtree and all right side of it is inorder traversal of right subtree. \\
So here is the function of finding root of any subtree. \\

\nwenddocs{}\nwbegincode{4}\sublabel{NW4896Yo-yA1Ck-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-yA1Ck-1}}}\moddef{getRoot~{\nwtagstyle{}\subpageref{NW4896Yo-yA1Ck-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2dJGUc-1}}\nwenddeflinemarkup
fun getRoot [] = NONE
        |  getRoot(List) =
                let
                        fun getMin(List: ('a * int) list) =
                                if null (tl List) then (hd List)
                                else
                                        let
                                                val tl_ans = getMin(tl List)
                                        in
                                                if #2 (hd List) < #2 (tl_ans) then (hd List)
                                                else tl_ans
                                        end
                in
                        SOME (getMin(List))
                end
\nwused{\\{NW4896Yo-2dJGUc-1}}\nwendcode{}\nwbegindocs{5}\nwdocspar

Once we found the root, we need to split the list around root to get the inorder traversal of left subtree and right subtree.\\

\nwenddocs{}\nwbegincode{6}\sublabel{NW4896Yo-qWzDz-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-qWzDz-1}}}\moddef{split~{\nwtagstyle{}\subpageref{NW4896Yo-qWzDz-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2dJGUc-1}}\nwenddeflinemarkup
fun split(Min: int, L1: ('a * int) list, L2: ('a * int) list) =
        if Min = #2 (hd L2) then (L1, tl L2)
        else
                let
                        val new_l1 = L1@[hd L2]
                        val new_l2 = tl L2
                in
                        split(Min, new_l1, new_l2)
                end
\nwused{\\{NW4896Yo-2dJGUc-1}}\nwendcode{}\nwbegindocs{7}\nwdocspar

Once we split the list we can build the tree recursively.\\
\nwenddocs{}\nwbegincode{8}\sublabel{NW4896Yo-D88LJ-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-D88LJ-1}}}\moddef{inorderInverse~{\nwtagstyle{}\subpageref{NW4896Yo-D88LJ-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2dJGUc-1}}\nwenddeflinemarkup
fun inorderInverse [] = Empty
        |       inorderInverse List =
                let
                        val root = valOf(getRoot(List))
                        val rootVal = #1 root
                        val rootLevel = #2 root
                        val (LSTList, RSTList) = split(rootLevel, [], List)
                        val (LST, RST) = (inorderInverse(LSTList), inorderInverse(RSTList))
                in
                        Node(rootVal, LST, RST )
                end
\nwused{\\{NW4896Yo-2dJGUc-1}}\nwendcode{}\nwbegindocs{9}\nwdocspar
\subsection{Complexity}
In the above method we store node level along with the node value. So the our algorithm makes the two times size of inorder traversal list compared to normal inorder traversal.\\
\subsubsection{Comparison with leaf information storing}
If binary tree have N nodes then it have total floor \( \frac{N+1}{2} \) leaf node. So the leaf node information method will take at least 2*\( \frac{N+1}{2} \) = N+1 extra information to store along with node information. One extra information will also have to store for degree-1 node. So we were storing 2 extra information for each leaf node and 1 extra information for each degree-1 node\\
Our algorithm of storing level for each node only takes N extra information in all cases.\\
So our algorithm for storing extra information along with traversal take less number of information but important information.

\section{Making Module}
Now we will make module\\
Datatype representation of binary tree\\
\nwenddocs{}\nwbegincode{10}\sublabel{NW4896Yo-3FQtIp-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-3FQtIp-1}}}\moddef{datatype~{\nwtagstyle{}\subpageref{NW4896Yo-3FQtIp-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-GBdPh-1}}\nwenddeflinemarkup
datatype 'a bintree =Empty |
        Node of 'a * 'a bintree * 'a bintree
\nwused{\\{NW4896Yo-GBdPh-1}}\nwendcode{}\nwbegindocs{11}\nwdocspar

Signature of binary tree\\
\nwenddocs{}\nwbegincode{12}\sublabel{NW4896Yo-1h1obp-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-1h1obp-1}}}\moddef{signature~{\nwtagstyle{}\subpageref{NW4896Yo-1h1obp-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-GBdPh-1}}\nwenddeflinemarkup
signature BINTREE =
        sig

                val root : 'a bintree -> 'a
                val leftSubtree : 'a bintree -> 'a bintree
                val rightSubtree: 'a bintree -> 'a bintree
                val height : 'a bintree -> int
                val size : 'a bintree -> int
                val isLeaf : 'a bintree -> bool
                val inorder : 'a bintree -> ('a *int) list
                val getRoot : ('a * int) list -> ('a * int) option
                val split : int * ('a * int) list * ('a * int) list -> ('a * int) list * ('a * int) list
                val inorderInverse: ('a * int) list -> 'a bintree
        end (* sig *)
\nwused{\\{NW4896Yo-GBdPh-1}}\nwendcode{}\nwbegindocs{13}\nwdocspar

Now we will define basic binary tree functions\\
\nwenddocs{}\nwbegincode{14}\sublabel{NW4896Yo-1EKRTS-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-1EKRTS-1}}}\moddef{emptyexcetion~{\nwtagstyle{}\subpageref{NW4896Yo-1EKRTS-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
exception Empty_bintree;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{15}\nwdocspar
\nwenddocs{}\nwbegincode{16}\sublabel{NW4896Yo-M1ktB-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-M1ktB-1}}}\moddef{root~{\nwtagstyle{}\subpageref{NW4896Yo-M1ktB-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
fun root Empty = raise Empty_bintree
        | root (Node (x, _, _)) = x;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{17}\nwdocspar
\nwenddocs{}\nwbegincode{18}\sublabel{NW4896Yo-1t8HkM-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-1t8HkM-1}}}\moddef{leftsubtree~{\nwtagstyle{}\subpageref{NW4896Yo-1t8HkM-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
fun leftSubtree Empty = raise Empty_bintree
        | leftSubtree (Node (_, LST, _)) = LST;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{19}\nwdocspar
\nwenddocs{}\nwbegincode{20}\sublabel{NW4896Yo-mvyXW-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-mvyXW-1}}}\moddef{rightsubtree~{\nwtagstyle{}\subpageref{NW4896Yo-mvyXW-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
fun rightSubtree Empty = raise Empty_bintree
        | rightSubtree (Node (_, _, RST)) = RST;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{21}\nwdocspar
\nwenddocs{}\nwbegincode{22}\sublabel{NW4896Yo-13AddF-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-13AddF-1}}}\moddef{height~{\nwtagstyle{}\subpageref{NW4896Yo-13AddF-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
fun height Empty = 0
        | height (Node (_, left, right)) =
                let
                        val lh = height left
                        val rh = height right
                in 1 + Int.max(lh, rh)
                end;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{23}\nwdocspar
\nwenddocs{}\nwbegincode{24}\sublabel{NW4896Yo-4Hcf9c-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-4Hcf9c-1}}}\moddef{size~{\nwtagstyle{}\subpageref{NW4896Yo-4Hcf9c-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
fun size Empty = 0
        | size (Node (_, left, right)) =
                let
                        val ls = size left
                        val rs = size right
                in 1 + ls + rs
                end;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{25}\nwdocspar
\nwenddocs{}\nwbegincode{26}\sublabel{NW4896Yo-1VK7YD-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-1VK7YD-1}}}\moddef{isleaf~{\nwtagstyle{}\subpageref{NW4896Yo-1VK7YD-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-3lRlgj-1}}\nwenddeflinemarkup
fun isLeaf Empty = false
        | isLeaf (Node (_, Empty, Empty)) = true
        | isLeaf _ = false;
\nwused{\\{NW4896Yo-3lRlgj-1}}\nwendcode{}\nwbegindocs{27}\nwdocspar
So the basic binary tree functions will look like this \\

\nwenddocs{}\nwbegincode{28}\sublabel{NW4896Yo-3lRlgj-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-3lRlgj-1}}}\moddef{basicbintree~{\nwtagstyle{}\subpageref{NW4896Yo-3lRlgj-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2dJGUc-1}}\nwenddeflinemarkup
\LA{}emptyexcetion~{\nwtagstyle{}\subpageref{NW4896Yo-1EKRTS-1}}\RA{}
\LA{}root~{\nwtagstyle{}\subpageref{NW4896Yo-M1ktB-1}}\RA{}
\LA{}leftsubtree~{\nwtagstyle{}\subpageref{NW4896Yo-1t8HkM-1}}\RA{}
\LA{}rightsubtree~{\nwtagstyle{}\subpageref{NW4896Yo-mvyXW-1}}\RA{}
\LA{}height~{\nwtagstyle{}\subpageref{NW4896Yo-13AddF-1}}\RA{}
\LA{}size~{\nwtagstyle{}\subpageref{NW4896Yo-4Hcf9c-1}}\RA{}
\LA{}isleaf~{\nwtagstyle{}\subpageref{NW4896Yo-1VK7YD-1}}\RA{}
\nwused{\\{NW4896Yo-2dJGUc-1}}\nwendcode{}\nwbegindocs{29}\nwdocspar

Now structure of module will look like this\\
\nwenddocs{}\nwbegincode{30}\sublabel{NW4896Yo-2dJGUc-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-2dJGUc-1}}}\moddef{structure~{\nwtagstyle{}\subpageref{NW4896Yo-2dJGUc-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-GBdPh-1}}\nwenddeflinemarkup
structure Bintree : BINTREE =
   struct
   \LA{}basicbintree~{\nwtagstyle{}\subpageref{NW4896Yo-3lRlgj-1}}\RA{}
   \LA{}inorder~{\nwtagstyle{}\subpageref{NW4896Yo-N7IVZ-1}}\RA{}
   \LA{}getRoot~{\nwtagstyle{}\subpageref{NW4896Yo-yA1Ck-1}}\RA{}
   \LA{}split~{\nwtagstyle{}\subpageref{NW4896Yo-qWzDz-1}}\RA{}
   \LA{}inorderInverse~{\nwtagstyle{}\subpageref{NW4896Yo-D88LJ-1}}\RA{}
   end
\nwused{\\{NW4896Yo-GBdPh-1}}\nwendcode{}\nwbegindocs{31}\nwdocspar

Now complete module will look like this\\
\nwenddocs{}\nwbegincode{32}\sublabel{NW4896Yo-GBdPh-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-GBdPh-1}}}\moddef{2019MCS2565-module-complete.sml~{\nwtagstyle{}\subpageref{NW4896Yo-GBdPh-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
\LA{}datatype~{\nwtagstyle{}\subpageref{NW4896Yo-3FQtIp-1}}\RA{}
\LA{}signature~{\nwtagstyle{}\subpageref{NW4896Yo-1h1obp-1}}\RA{}
\LA{}structure~{\nwtagstyle{}\subpageref{NW4896Yo-2dJGUc-1}}\RA{}
\nwnotused{2019MCS2565-module-complete.sml}\nwendcode{}\nwbegindocs{33}\nwdocspar

\section{Test cases}
Test cases\\
\nwenddocs{}\nwbegincode{34}\sublabel{NW4896Yo-3kROYe-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-3kROYe-1}}}\moddef{use~{\nwtagstyle{}\subpageref{NW4896Yo-3kROYe-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2Ag6yI-1}\\{NW4896Yo-1VsZET-1}\\{NW4896Yo-1QUbrk-1}\\{NW4896Yo-DmyLe-1}}\nwenddeflinemarkup
use "2019MCS2565-module-complete.sml";
\nwused{\\{NW4896Yo-2Ag6yI-1}\\{NW4896Yo-1VsZET-1}\\{NW4896Yo-1QUbrk-1}\\{NW4896Yo-DmyLe-1}}\nwendcode{}\nwbegindocs{35}\nwdocspar
\nwenddocs{}\nwbegincode{36}\sublabel{NW4896Yo-Joiug-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-Joiug-1}}}\moddef{test~{\nwtagstyle{}\subpageref{NW4896Yo-Joiug-1}}}\endmoddef\nwstartdeflinemarkup\nwusesondefline{\\{NW4896Yo-2Ag6yI-1}\\{NW4896Yo-1VsZET-1}\\{NW4896Yo-1QUbrk-1}\\{NW4896Yo-DmyLe-1}}\nwenddeflinemarkup
val traversal = Bintree.inorder t1;
val tree = Bintree.inorderInverse traversal;
\nwused{\\{NW4896Yo-2Ag6yI-1}\\{NW4896Yo-1VsZET-1}\\{NW4896Yo-1QUbrk-1}\\{NW4896Yo-DmyLe-1}}\nwendcode{}\nwbegindocs{37}\nwdocspar
Test case 1\\
This is test case for skewed tree which has only right child
\nwenddocs{}\nwbegincode{38}\sublabel{NW4896Yo-2Ag6yI-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-2Ag6yI-1}}}\moddef{2019MCS2565-case1-complete.sml~{\nwtagstyle{}\subpageref{NW4896Yo-2Ag6yI-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
\LA{}use~{\nwtagstyle{}\subpageref{NW4896Yo-3kROYe-1}}\RA{}
val t4 = Node (4, Empty, Empty);
val t3 = Node (3, Empty, t4);
val t2 = Node (2, Empty, t3);
val t1 = Node (1, Empty, t2);
\LA{}test~{\nwtagstyle{}\subpageref{NW4896Yo-Joiug-1}}\RA{}
\nwnotused{2019MCS2565-case1-complete.sml}\nwendcode{}\nwbegindocs{39}\nwdocspar
Test case 2\\
This is test case for example tree given in this document
\nwenddocs{}\nwbegincode{40}\sublabel{NW4896Yo-1VsZET-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-1VsZET-1}}}\moddef{2019MCS2565-case2-complete.sml~{\nwtagstyle{}\subpageref{NW4896Yo-1VsZET-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
\LA{}use~{\nwtagstyle{}\subpageref{NW4896Yo-3kROYe-1}}\RA{}
val t7 = Node (7, Empty, Empty);
val t6 = Node (6, t7, Empty);
val t5 = Node (5, Empty, Empty);
val t4 = Node (4, Empty, Empty);
val t3 = Node (3, t5, t6);
val t2 = Node (2, Empty, t4);
val t1 = Node (1, t2, t3);
\LA{}test~{\nwtagstyle{}\subpageref{NW4896Yo-Joiug-1}}\RA{}
\nwnotused{2019MCS2565-case2-complete.sml}\nwendcode{}\nwbegindocs{41}\nwdocspar
Test case 3\\
This is test case for skewed tree which has only left child
\nwenddocs{}\nwbegincode{42}\sublabel{NW4896Yo-1QUbrk-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-1QUbrk-1}}}\moddef{2019MCS2565-case3-complete.sml~{\nwtagstyle{}\subpageref{NW4896Yo-1QUbrk-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
\LA{}use~{\nwtagstyle{}\subpageref{NW4896Yo-3kROYe-1}}\RA{}
val t4 = Node (4, Empty, Empty);
val t3 = Node (3, t4, Empty);
val t2 = Node (2, t3, Empty);
val t1 = Node (1, t2, Empty);
\LA{}test~{\nwtagstyle{}\subpageref{NW4896Yo-Joiug-1}}\RA{}
\nwnotused{2019MCS2565-case3-complete.sml}\nwendcode{}\nwbegindocs{43}\nwdocspar
Test case 4\\
This is test case single node tree
\nwenddocs{}\nwbegincode{44}\sublabel{NW4896Yo-DmyLe-1}\nwmargintag{{\nwtagstyle{}\subpageref{NW4896Yo-DmyLe-1}}}\moddef{2019MCS2565-case4-complete.sml~{\nwtagstyle{}\subpageref{NW4896Yo-DmyLe-1}}}\endmoddef\nwstartdeflinemarkup\nwenddeflinemarkup
\LA{}use~{\nwtagstyle{}\subpageref{NW4896Yo-3kROYe-1}}\RA{}
val t1 = Node (1, Empty, t1);
\LA{}test~{\nwtagstyle{}\subpageref{NW4896Yo-Joiug-1}}\RA{}
\nwnotused{2019MCS2565-case4-complete.sml}\nwendcode{}

\nwixlogsorted{c}{{2019MCS2565-case1-complete.sml}{NW4896Yo-2Ag6yI-1}{\nwixd{NW4896Yo-2Ag6yI-1}}}%
\nwixlogsorted{c}{{2019MCS2565-case2-complete.sml}{NW4896Yo-1VsZET-1}{\nwixd{NW4896Yo-1VsZET-1}}}%
\nwixlogsorted{c}{{2019MCS2565-case3-complete.sml}{NW4896Yo-1QUbrk-1}{\nwixd{NW4896Yo-1QUbrk-1}}}%
\nwixlogsorted{c}{{2019MCS2565-case4-complete.sml}{NW4896Yo-DmyLe-1}{\nwixd{NW4896Yo-DmyLe-1}}}%
\nwixlogsorted{c}{{2019MCS2565-module-complete.sml}{NW4896Yo-GBdPh-1}{\nwixd{NW4896Yo-GBdPh-1}}}%
\nwixlogsorted{c}{{basicbintree}{NW4896Yo-3lRlgj-1}{\nwixd{NW4896Yo-3lRlgj-1}\nwixu{NW4896Yo-2dJGUc-1}}}%
\nwixlogsorted{c}{{datatype}{NW4896Yo-3FQtIp-1}{\nwixd{NW4896Yo-3FQtIp-1}\nwixu{NW4896Yo-GBdPh-1}}}%
\nwixlogsorted{c}{{emptyexcetion}{NW4896Yo-1EKRTS-1}{\nwixd{NW4896Yo-1EKRTS-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{getRoot}{NW4896Yo-yA1Ck-1}{\nwixd{NW4896Yo-yA1Ck-1}\nwixu{NW4896Yo-2dJGUc-1}}}%
\nwixlogsorted{c}{{height}{NW4896Yo-13AddF-1}{\nwixd{NW4896Yo-13AddF-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{inorder}{NW4896Yo-N7IVZ-1}{\nwixd{NW4896Yo-N7IVZ-1}\nwixu{NW4896Yo-2dJGUc-1}}}%
\nwixlogsorted{c}{{inorderInverse}{NW4896Yo-D88LJ-1}{\nwixd{NW4896Yo-D88LJ-1}\nwixu{NW4896Yo-2dJGUc-1}}}%
\nwixlogsorted{c}{{isleaf}{NW4896Yo-1VK7YD-1}{\nwixd{NW4896Yo-1VK7YD-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{leftsubtree}{NW4896Yo-1t8HkM-1}{\nwixd{NW4896Yo-1t8HkM-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{rightsubtree}{NW4896Yo-mvyXW-1}{\nwixd{NW4896Yo-mvyXW-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{root}{NW4896Yo-M1ktB-1}{\nwixd{NW4896Yo-M1ktB-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{signature}{NW4896Yo-1h1obp-1}{\nwixd{NW4896Yo-1h1obp-1}\nwixu{NW4896Yo-GBdPh-1}}}%
\nwixlogsorted{c}{{size}{NW4896Yo-4Hcf9c-1}{\nwixd{NW4896Yo-4Hcf9c-1}\nwixu{NW4896Yo-3lRlgj-1}}}%
\nwixlogsorted{c}{{split}{NW4896Yo-qWzDz-1}{\nwixd{NW4896Yo-qWzDz-1}\nwixu{NW4896Yo-2dJGUc-1}}}%
\nwixlogsorted{c}{{structure}{NW4896Yo-2dJGUc-1}{\nwixd{NW4896Yo-2dJGUc-1}\nwixu{NW4896Yo-GBdPh-1}}}%
\nwixlogsorted{c}{{test}{NW4896Yo-Joiug-1}{\nwixd{NW4896Yo-Joiug-1}\nwixu{NW4896Yo-2Ag6yI-1}\nwixu{NW4896Yo-1VsZET-1}\nwixu{NW4896Yo-1QUbrk-1}\nwixu{NW4896Yo-DmyLe-1}}}%
\nwixlogsorted{c}{{use}{NW4896Yo-3kROYe-1}{\nwixd{NW4896Yo-3kROYe-1}\nwixu{NW4896Yo-2Ag6yI-1}\nwixu{NW4896Yo-1VsZET-1}\nwixu{NW4896Yo-1QUbrk-1}\nwixu{NW4896Yo-DmyLe-1}}}%
\nwbegindocs{45}\nwdocspar
\end{document}
\nwenddocs{}


\documentclass[11pt]{article}
\usepackage{enumitem}
\usepackage{listings}
\usepackage{tikz}
\usepackage{url}
%\usepackage{algorithm2e}
\usetikzlibrary{arrows,automata,shapes}
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
    text width=5em, text centered, rounded corners, minimum height=2em]
\tikzstyle{bt} = [rectangle, draw, fill=blue!20,
    text width=4em, text centered, rounded corners, minimum height=2em]

\lstset{ %
language=Java,
basicstyle=\ttfamily,commentstyle=\scriptsize\itshape,showstringspaces=false,breaklines=true,numbers=left}

\newtheorem{defn}{Definition}
\newtheorem{crit}{Criterion}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf Software Testing, Quality Assurance and Maintenance } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{Lecture #1}}
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in

\parindent 0in
\parskip 1.5ex
%\renewcommand{\baselinestretch}{1.25}

\usepackage{fontspec}
\setmonofont{Cousine}[Scale=MatchLowercase]

\begin{document}

\lecture{8 --- January 20, 2017}{Winter 2017}{Patrick Lam}{version 1}

\paragraph{Larger CFG example.} You can draw a 7-node CFG for this program:

\begin{lstlisting}[basicstyle=\scriptsize\ttfamily]
  /** Binary search for target in sorted subarray a[low..high] */
  int binary_search(int[] a, int low, int high, int target) {
    while (low <= high) {
      int middle = low + (high-low)/2;
      if (target < a[middle)
        high = middle - 1;
      else if (target > a[middle])
        low = middle + 1;
      else
        return middle;
    }
    return -1; /* not found in a[low..high] */
  }
\end{lstlisting}

\vspace*{-2em}
\begin{center}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm,
                    semithick,initial text=]
  \node[initial,bt,text width=12em]   (1)                     {\verb+1. if (low <= high)+ (L3)};
  \node[bt, accepting,text width=10em]          (8) [below right of=1,yshift=-1em,xshift=3em]  {\verb+3. return -1+ (L12)};
  \node[bt]           (2) [below of=1, yshift=-4em]  {\verb+2.+ (L4--5)};
  \node[bt]           (4) [below left of=2] {\verb+4.+ (L6)};
  \node[bt]           (5) [below right of=2] {\verb+5.+ (L7)};
  \node[bt]           (6) [below left of=5] {\verb+6.+ (L8)};
  \node[bt,text width=12em,accepting,xshift=3em]           (7) [below right of=5] {\verb+7. return middle (L9)+};

  \path (1) edge node[left] {T} (2)
  (1) edge node[right,xshift=.5em] {F} (8)
  (2) edge node[left,xshift=-.5em] {T} (4)
  (2) edge node[right,xshift=.5em]{F} (5)
  (4.west) edge [bend left] (1)
  (5) edge node {T} (6)
  (5) edge node {F} (7)
  (6.south west) edge [bend left=80] (1);
\end{tikzpicture}
\end{center}

Here are more exercise programs that you can draw CFGs for.
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily]
/* effects: if x==null, throw NullPointerException
            otherwise, return number of elements in x that are odd, positive or both. */
int oddOrPos(int[] x) {
  int count = 0;
  for (int i = 0; i < x.length; i++) {
    if (x[i]%2 == 1 || x[i] > 0) {
      count++;
    }
  }
  return count;
}

// example test case: input: x=[-3, -2, 0, 1, 4]; output: 3
\end{lstlisting}

\newpage
Finally, we have a really poorly-designed API (I'd give it a D at most,
maybe an F) because it's impossible to succinctly describe what it
does. {\bf Do not design functions with interfaces like this.} But we
can still draw a CFG, no matter how bad the code is.
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily]
  /** Returns the mean of the first maxSize numbers in the array,
      if they are between min and max. Otherwise, skip the numbers. */
  double computeMean(int[] value, int maxSize, int min, int max) {
    int i, ti, tv, sum;

    i = 0; ti = 0; tv = 0; sum = 0;
    while (ti < maxSize) {
      ti++;
      if (value[i] >= min && value[i] <= max) {
        tv++;
        sum += value[i];
      }
      i++;
    }
    if (tv > 0)
      return (double)sum/tv;
    else
      throw new IllegalArgumentException();
  }
\end{lstlisting}

\subsection*{Statement and Branch Coverage}
We defined Control-Flow Graphs so that we can give principled definitions of
statement and branch coverage. We can start with the definition of a test path:

\begin{defn} A \emph{test path} is a path $p$ (possibly of length 0)
that starts at some initial node (i.e. in $N_0$) and ends at some final node (i.e. in $N_f$).
\end{defn}

Here's a definition of coverage for graphs:
\begin{defn}
Given a set of test requirements \emph{TR} for a graph criterion $C$,
a test set $T$ satisfies $C$ on graph $G$ iff for every test requirement
\emph{tr} in \emph{TR}, at least one test path $p$ in $\mbox{path}(T)$
exists such that $p$ satisfies \emph{tr}.
\end{defn}
We'll use this notion to define a number of standard testing
coverage criteria. But first, what are test paths?

\paragraph{Test cases and test paths.} We connect test cases and
test paths with a mapping $\mbox{path}_G$ from test cases to test
paths; e.g. $\mbox{path}_G(t)$ is the set of test paths corresponding
to test case $t$.
\begin{itemize}
\item usually we just write $\mbox{path}$ since $G$ is obvious from the context.
\item we can lift the definition of $\mbox{path}$ to test sets $T$ by defining
$\mbox{path}(T) = \{ \mbox{path}(t) | t \in T \}$.
\item each test case gives at least one test path. If the software is
  deterministic, then each test case gives exactly one test path;
  otherwise, multiple test cases may arise from one test path.
\end{itemize}

\newpage
\paragraph{Example.} Here is a short method, the associated control-flow
graph, and some test cases and test paths.

\begin{center}
\begin{minipage}{10em}
\vspace*{-8em}
\begin{lstlisting}
int foo(int x) {
  if (x < 5) {
    x ++;
  } else {
    x --;
  }
  return x;
}
\end{lstlisting}
\end{minipage}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm,
                    semithick,text width=10em,initial text=]

  \node[initial,block,text width=10em]   (A)              {\tt (1) if (x < 5)};
  \node[block,text width=6em]           (T) [below right of=A] {\tt (2) x++};
  \node[block,text width=6em]           (F) [below left of=A] {\tt (3) x--};
  \node[accepting,block,text width=10em] (C) [below left of=T] {\tt (4) return x};

  \draw (A) -> (T) node [near end] {T};
  \draw (A) -> (F) node [yshift=.2em, xshift=-2.5em,at start] {F};
  \draw (T) -> (C);
  \draw (F) -> (C);
\end{tikzpicture}
\end{center}

\begin{itemize}
\item Test case: $x = 5$; test path: $[(1), (3), (4)]$.
\item Test case: $x = 2$; test path: $[(1), (2), (4)]$.
\end{itemize}

Note that (1) we can deduce properties of the test case from the test path; and
(2) in this example, since our method is deterministic, the test case
determines the test path.

\paragraph{Nondeterminism.}
I mentioned the mapping between test cases and test paths above.
The mapping is not one-to-one for nondeterminstic code.
Here's an example of deterministic and nondeterministic control-flow graphs:
\begin{center}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm,
                    semithick,initial text=]

  \node[initial,block,text width=5em]   (A)              {\tt if (x > 3)};
  \node[block]           (T) [below right of=A] {};
  \node[block]           (F) [below left of=A] {};
  \node[accepting,block] (C) [below left of=T] {};

  \draw (A) -> (T) node [near end] {$x = 5$};
  \draw (A) -> (F) node [yshift=.2em, xshift=-4em,at start] {$x = 0$};
  \draw (T) -> (C);
  \draw (F) -> (C);

\end{tikzpicture}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=1.5cm,
                    semithick,initial text=]

  \node[initial,block,text width=12em]   (A)              {\tt if (x.hashCode() > 3)};
  \node[bt]           (T) [below right of=A] {};
  \node[bt]           (F) [below left of=A] {};
  \node[accepting,bt] (C) [below left of=T] {};

  \path (A) edge              node {} (T)
        (A) edge              node {} (F)
        (T) edge              node {} (C)
        (F) edge              node {} (C);
\end{tikzpicture}
\end{center}
Causes of nondeterminism include dependence on inputs; on the thread
scheduler; and on memory addresses, for instance as seen in calls to
the default Java {\tt hashCode()} implementation.

Nondeterminism makes it hard to check test case output, since more
than one output might be a valid result of a single test input.

\newpage
As another (more abstract) example, consider the double-diamond graph $D$.
\begin{center}
\label{D}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                    semithick,initial text=]

  \node[initial,state]   (n0)                     {$n_0$};
  \node[state]           (n1) [below right of=n0] {$n_1$};
  \node[state]           (n2) [above right of=n0] {$n_2$};
  \node[state]           (n3) [below right of=n2] {$n_3$};
  \node[state]           (n4) [below right of=n3] {$n_4$};
  \node[state]           (n5) [above right of=n3] {$n_5$};
  \node[accepting,state] (n6) [below right of=n5] {$n_6$};

  \path (n0) edge              node {} (n1)
             edge              node {} (n2)
        (n1) edge              node {} (n3)
        (n2) edge              node {} (n3)
        (n3) edge              node {} (n4)
        (n3) edge              node {} (n5)
        (n4) edge              node {} (n6)
        (n5) edge              node {} (n6);
\end{tikzpicture}
\end{center}
Here are the four test paths in $D$:
\begin{eqnarray*}
&[n_0, n_1, n_3, n_4, n_6]& \\
&[n_0, n_1, n_3, n_5, n_6]& \\
&[n_0, n_2, n_3, n_4, n_6]& \\
&[n_0, n_2, n_3, n_5, n_6]&
\end{eqnarray*}

For the \emph{statement coverage} criterion, we get the following test requirements:
\[ \{ n_0, n_1, n_2, n_3, n_4, n_5, n_6 \} \]
That is, any test set $T$ which satisfies statement coverage on $D$ must include
test cases $t$; the cases $t$ give rise to test paths $\mbox{path}(t)$, and
some path must include each node from $n_0$ to $n_6$. (No single path must
include all of these nodes; the requirement applies to the set of
test paths.)

Let's formally define statement coverage.
\begin{defn}
Statement coverage: For each node $n \in \mbox{reach}_G(N_0)$, \emph{TR} contains
a requirement to visit node $n$.
\end{defn}

For our example,
\[ \mathit{TR} = \{ n_0, n_1, n_2, n_3, n_4, n_5, n_6\}. \]

Let's consider an example of a test set which satisfies statement coverage
on $D$.

Start with a test case $t_1$; assume that executing $t_1$ gives the
test path
\[ \mbox{path}(t_1) = p_1 = [n_0, n_1, n_3, n_4, n_6].\]
Then
test set $\{ t_1\}$ does not give statement coverage on $D$, because no
test case covers node $n_2$ or $n_5$. If we can find a test case $t_2$
with test path
\[\mbox{path}(t_2) = p_2 = [n_0, n_2, n_3, n_5, n_6],\]
then the test set $T = \{ t_1, t_2 \}$ satisfies statement coverage on $D$.

{\sf What is another test set which satisfies statement coverage on $D$?}\\[2em]

%We could also satisfy node coverage with test set $T'$ containing test
%cases $t_1$ and new cases $t_3$ and $t_4$ where $p_3 = [n_0, n_2, n_3,
%n_4, n_6]$ and $p_4 = [n_0, n_1, n_3, n_5, n_6]$.

Here is a more verbose definition of statement coverage.

\begin{defn}
Test set $T$ satisfies \emph{statement coverage} on graph $G$ if and only
if for every syntactically reachable node $n \in N$, there is some
path $p$ in $\mbox{path}(T)$ such that $p$ visits $n$.
\end{defn}

A second standard criterion is that of branch coverage.
\begin{crit}
{\bf Branch Coverage}. TR contains each reachable path of length up
to 1, inclusive, in $G$.
\end{crit}

{\sf Here are some examples of paths of length $\le 1$:}\\[1em]
%\[ [n_3]; \qquad [n_4, n_6]. \]
Note that since we're not talking about \emph{test paths}, these
reachable paths need not start in $N_0$.

In general, paths of length $\le 1$ consist of nodes and edges. {\sf (Why not just
say edges?)}\\[3em]

%Consider this graph:
%\begin{center}
%\begin{tikzpicture}...
%\node[state,initial,accepting] (A) {};
%\end{tikzpicture}
%\end{center}
Saying ``edges'' on the above graph would not be the same as saying ``paths
of length $\le 1$''.

\paragraph{Another example.} Here is a more involved example:

\tikzstyle{block} = [rectangle, draw, fill=blue!20,
    text width=3em, text centered, rounded corners, minimum height=2em]

\begin{center}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
                    semithick,initial text=]

  \node[initial,block]   (A)              {$n_0$};
  \node[block]           (B) [below right of=A] {$n_1$};
  \node[accepting,block] (C) [below left of=B] {$n_2$};

  \path (A) edge              node[right] {$x < y$} (B)
        (A) edge              node[left] {$x \ge y$} (C)
        (B) edge              node {} (C);
\end{tikzpicture}
\end{center}

Let's define
\begin{eqnarray*}
\mbox{path}(t_1) &=& [n_0, n_1, n_2] \\
\mbox{path}(t_2) &=& [n_0, n_2]
\end{eqnarray*}

Then
\begin{eqnarray*}
T_1 &=& \langle ? \rangle \hspace*{5em} \mbox{\sf satisfies statement coverage}\\
T_2 &=& \langle ? \rangle \hspace*{5em} \mbox{\sf satisfies branch coverage}
\end{eqnarray*}

\end{document}


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumerate}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{March 28, 2014}
\maketitle
result from last time (D'Alemberts' solution)
\begin{align*}
  \text{PDE}&&u_{tt}&=c^2u_{xx}&-\infty<&x<+\infty&0<&t<\infty\\
  \text{IC}&&u(x,0)&=f(x)\\
  &&u_t(x,0)&=g(x)\\
  \hline
  &&u(x,t)&=\frac{1}{2}(f(x-ct)+f(x+ct))+\frac{1}{2c}\int_{x-ct}^{x+ct}{g(s)\mathrm{d}s}\\
\end{align*}
\section*{18 properties of this solution}
\begin{enumerate}[C{a}se 1.]
\item
\begin{align*}
  \text{IC}&&\left.\begin{aligned}u(x,0)&=f(x)\\u_t(x,0)&=0\end{aligned}\right\}\text{ Solution }u(x,t)=\frac{1}{2}\left[\underbrace{f(x-ct)}_{\text{wave moving right}}+\underbrace{f(x+ct)}_{\text{wave moving left}}\right]
\end{align*}
\item
\begin{align*}
  \text{IC}&&\left.\begin{aligned}u(x,0)&=0\\u_t(x,0)&=g(x)\end{aligned}\right\}\text{ Solution }u(x,t)=\frac{1}{2c}\int_{x-ct}^{x+ct}{g(s)\,\mathrm{d}s}
\end{align*}
value of $g$ over a widening interval

see graphs on pages 139-141 (155-157)
\end{enumerate}
\begin{align*}
  \text{PDE}&&u_{tt}&=c^2u_{xx}&0<&x<\infty&0<&t<\infty\\
  \text{BC}&&u(0,t)&=0&&&0<&t<\infty\\
  \text{IC}&&u(x,0)&=f(x)&0<&x<\infty\\
  &&u_t(x,0)&=g(x)
\end{align*}
as last time $u(x,t)=\phi(x-ct)+\psi(x+ct)$ general solution -- IC's and BC are not used.

Match IC:
\begin{align*}
  \phi(x)+\psi(x)&=f(x)&0<&x<\infty\\
  -c\phi'(x)+c\psi'(x)&=g(x)&\to&-\phi(x)++\psi(x)\\
  &&=&\frac{1}{c}\int_0^x{g(s)\,\mathrm{d}s}+K\\
  \phi(x)&=\frac{1}{2}f(x)-\frac{1}{2c}\int_0^x{g(s)\,\mathrm{d}s}+ \frac{k}{2}\\
  \psi(x)&=\frac{1}{2}f(x)+\frac{1}{2c}\int_0^x{g(s)\,\mathrm{d}s}+ \frac{k}{2}\\
  u(x,t)&=\phi(x-ct)+\psi(x+ct)
\end{align*}
k cancel out. $x+ct>0$ so $\psi(x+ct)$ is no problem. $x-ct$ changes sign.

What is $\phi(x-ct)$ when $x-ct<0$?

now lets look at the boundary condition

\begin{align*}
  \text{BC}&&u(0,t)&=0=\phi(-ct)+\psi(ct)\text{ for}&0<&t<\infty\\
  \text{for }&&-\infty&<x<0,&\phi(x)&=-\psi(-x)\\
  \text{for }&&x-ct&>0,&u(x,t)&=\phi(x-ct)+\psi(x+ct)\\
  &&&&&=\frac{1}{2}\left[f(x-ct)+f(x+ct)\right]+\frac{1}{2c}\int_{x-ct}^{x+ct}{g(s)\,\mathrm{d}s}\\
  \text{for }&&x-ct&<0,&u(x,t)&=-\psi(x-ct)+\psi(x+ct)\\
  &&&&&=\frac{1}{2}\left[f(x-ct)+f(x+ct)\right]+\frac{1}{2c}\int_{x-ct}^{x+ct}{g(s)\,\mathrm{d}s}
\end{align*}
more on page 143(159)

homework \#28 \& \#29 due next friday (first friday of april) lession 17 exercise 3 and exercise 4
\end{document}


\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{float}
\usepackage{color}
\usepackage{url}
\usepackage{amsmath}
\definecolor{backcolour}{rgb}{0.96,0.96,0.96}
\definecolor{commentgreen}{rgb}{0.3,0.6,0.75}
\definecolor{keywordgreen}{rgb}{0.15,0.5,0.15}
\definecolor{stringmaroon}{rgb}{0.5,0,0}
\usepackage{amsmath}
\newcommand\norm[1]{\left\lVert#1\right\rVert}
\usepackage{geometry}
\geometry{left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{commentgreen},
    keywordstyle=\color{keywordgreen},
    stringstyle=\color{stringmaroon},
    basicstyle=\footnotesize\ttfamily,
    breaklines=true,
    captionpos=b,
    keepspaces=false,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=4
}
\lstset{style=mystyle}
\title{Assignment3}
\begin{document}
\maketitle
\section{Machine Learning \& Neural Networks}
\subsection{(a) Adam Optimizer}
(i) $m$ is the exponentially decaying average of past gradients. When $J(\theta)$ is in a ravine for one dimension, its gradient mean is small. In this case $abs(m)$'s would be small, or it would on the opposite direction compared with $\nabla_\theta J(\theta)$. Then $m$ would decrease the updating step for those directions whose mean is small or the new gradient's is on the opposite direction of the exponentially decaying mean. Then the updating step on such ravine would be smaller, then lead to smaller updating variance. The concept is similar like Inertia: a resistance of an object to any change in its velocity. \\\\
SGD will oscillate across the slopes of the ravine if we have a constant learning rate. But with $m$, for the directions that gradients' directions are unchanged, $m$ will increase gradually. So the momentum $m$ helps the learning progress by preferring larger steps on more stable directions.\\\\
(ii) Parameters with smaller accumulated updated gradients will get larger updates. It might help with learning because for those parameters that have large $v$, it means the neural network already gained enough information within recent learning steps, so we should decrease its updating step size. But for those parameters that have small $v$, its updating frequency is low. So we should increase their learning rate. One issue of such scaling is that, the $|v|$ is a monotonic increasing function. So learning rate would $\rightarrow 0$.
\subsection{(b) Dropout}
(i) $h_{drop} = \gamma d \odot h$ where $d \in \{0, 1\}^{D_h}$ is a mask vector where each entry is 0 with probability $p_{drop}$ and 1 with probability $1 - p_{drop}$. To ensure that $\mathbb{E}_{p_{drop}}.[h_{drop}]_i = h_i$, $\gamma = \frac{1}{1-p_{drop}}$. Because $\mathbb{E}_{p_{drop}}[h_{drop}]_i = \gamma[0(p_{drop})+h_i(1-p_{drop})] = h_i$\\\\
(ii) With limited training data, many parameters learned from the neural network
will be the result of sampling noise. They will exist in the training set but not in real
test data even if it is drawn from the same distribution. This leads to overfitting issue. So if we apply dropout during evaluation, the noise in training dataset will still be learned by the parameters, and dropout hidden unit at evaluation time will cause difference between expected output during training and evaluating output, thus causing bias.
\section{Neural Transition-Based Dependency Parsing}
(a)
\begin{center}
\begin{tabular}{ c|c|c|c }
Stack & Buffer & New dependency & Transition \\
\hline
[ROOT, parsed, this] & [sentence, correctly] & parsed$\rightarrow$ I & SHIFT \\
\hline
ROOT, parse & [sentence, correctly] & parsed$\rightarrow$ I, parsed$\rightarrow$ this & RIGHT-ARC \\
\hline
ROOT, parse, sentence & [correctly] & parsed$\rightarrow$ I, parsed$\rightarrow$ this & SHIFT \\
\hline
ROOT, parse & [correctly] & parsed$\rightarrow$ I, parsed$\rightarrow$ this, parsed$\rightarrow$ sentence & RIGHT-ARC \\
\hline
ROOT, parse, correctly & [] & parsed$\rightarrow$ I, parsed$\rightarrow$ this, parsed$\rightarrow$ sentence & SHIFT \\
\hline
ROOT, correctly & [] & \shortstack{parsed$\rightarrow$ I, parsed$\rightarrow$ this,\\ parsed$\rightarrow$ sentence, correctly $\rightarrow$ parse} & RIGHT-ARC \\
\hline
ROOT & [] & \shortstack{parsed$\rightarrow$ I, parsed$\rightarrow$ this, \\ \newline parsed$\rightarrow$ sentence, \\ correctly $\rightarrow$ parse, ROOT $\rightarrow$ correctly} & RIGHT-ARC \\
\end{tabular}
\end{center}
(b) $n$ words will lead to $n$ arcs in the final dependency set $A$. And each word need a $SHIFT$ action for being moved from buffer to stack, and an $ARC$ action for being moved from stack to the final set. So in total $2n$ steps.
\end{document}