\chapter{Decision Trees} \label{sec:dt}

\chapterquote{The words printed here are concepts.\\You must go through the experiences.}{Carl~Frederick}

%\chapterimageopt{serious.png}{width=5cm}{Courtesy XKCD}

\begin{learningobjectives}
\item Explain the difference between memorization and generalization.
\item Define ``inductive bias'' and recognize the role of inductive
  bias in learning.
\item Take a concrete task and cast it as a learning problem, with a
  formal notion of input space, features, output space, generating
  distribution and loss function.
\item Illustrate how regularization trades off between underfitting
  and overfitting.
\item Evaluate whether a use of test data is ``cheating'' or not.
\end{learningobjectives}

\dependencies{None.}

\newthought{At a basic level,} machine learning is about predicting
the future based on the past.  For instance, you might wish to predict
how much a user Alice will like a movie that she hasn't seen, based on
her ratings of movies that she has seen.  This means making informed
guesses about some unobserved property of some object, based on
observed properties of that object.

The first question we'll ask is: what does it mean to learn?  In order
to develop learning machines, we must know what learning actually
means, and how to determine success (or failure).  You'll see this
question answered in a very limited learning setting, which will be
progressively loosened and adapted throughout the rest of this book.
For concreteness, our focus will be on a very simple model of learning
called a \concept{decision tree}.

\begin{vignette}{Alice Decides which Classes to Take}
todo
\end{vignette}

\section{What Does it Mean to Learn?}

Alice has just begun taking a course on machine learning.  She knows
that at the end of the course, she will be expected to have ``learned''
all about this topic.  A common way of gauging whether or not she has
learned is for her teacher, Bob, to give her a exam.  She has done
well at learning if she does well on the exam.

But what makes a reasonable exam?  If Bob spends the entire semester
talking about machine learning, and then gives Alice an exam on
History of Pottery, then Alice's performance on this exam will
\emph{not} be representative of her learning.  On the other hand, if
the exam only asks questions that Bob has answered exactly during lectures,
then this is also a bad test of Alice's learning, especially if it's
an ``open notes'' exam.  What is desired is that Alice observes
\emph{specific} examples from the course, and then has to answer new, but
related questions on the exam.  This tests whether Alice has the
ability to \concept{generalize}.  Generalization is perhaps the most
central concept in machine learning.

As a running concrete example in this book, we will use that of a
course recommendation system for undergraduate computer science
students.  We have a collection of students and a collection of
courses.  Each student has taken, and evaluated, a subset of the
courses.  The evaluation is simply a score from $-2$ (terrible) to
$+2$ (awesome).  The job of the recommender system is to
\concept{predict} how much a particular student (say, Alice) will like
a particular course (say, Algorithms).

Given historical data from course ratings (i.e., the past) we are
trying to predict unseen ratings (i.e., the future).  Now, we could be
unfair to this system as well.  We could ask it whether Alice is
likely to enjoy the History of Pottery course.  This is unfair because
the system has no idea what History of Pottery even is, and has no
prior experience with this course.  On the other hand, we could ask it
how much Alice will like Artificial Intelligence, which she took last
year and rated as $+2$ (awesome).  We would expect the system to
predict that she would really like it, but this isn't demonstrating
that the system has learned: it's simply recalling its past
experience.  In the former case, we're expecting the system to
generalize \emph{beyond} its experience, which is unfair.  In the
latter case, we're not expecting it to generalize at all.

This general set up of predicting the future based on the past is at
the core of most machine learning.  The objects that our algorithm
will make predictions about are \concept{examples}.  In the
recommender system setting, an example would be some particular
Student/Course pair (such as Alice/Algorithms).  The desired
prediction would be the rating that Alice would give to Algorithms.

%\TODOFigure{dt:induction}{A figure showing training data to a
%  ML algorithm to a function $f$ and then predictions on test data.}

\Figure{dt:induction}{The general supervised approach to machine
  learning: a learning algorithm reads in training data and computes a
  learned function $f$.  This function can then automatically label
  future text examples.}

To make this concrete, Figure~\ref{fig:induction} shows the general
framework of \concept{induction}.  We are given \concept{training
  data} on which our algorithm is expected to learn.  This training
data is the examples that Alice observes in her machine learning
course, or the historical ratings data for the recommender system.
Based on this training data, our learning algorithm induces a function
$f$ that will map a new example to a corresponding prediction.  For
example, our function might guess that $f(\textsf{Alice/Machine
  Learning})$ might be high because our training data said that Alice
liked Artificial Intelligence.  We want our algorithm to be able to
make lots of predictions, so we refer to the collection of examples on
which we will evaluate our algorithm as the \concept{test set}.  The
test set is a closely guarded secret: it is the final exam on which
our learning algorithm is being tested.  If our algorithm gets to peek
at it ahead of time, it's going to cheat and do better than it should.

\thinkaboutit{Why is it bad if the learning algorithm gets to peek at the test data?}

The goal of inductive machine learning is to take some training data
and use it to induce a function $f$.  This function $f$ will be
evaluated on the test data.  The machine learning algorithm has
succeeded if its performance on the test data is high.

\section{Some Canonical Learning Problems}

There are a large number of typical inductive learning problems.  The
primary difference between them is in what type of \emph{thing}
they're trying to predict.  Here are some examples:

\begin{description}
\item[Regression:] trying to predict a real value.  For instance,
  predict the value of a stock tomorrow given its past performance.
  Or predict Alice's score on the machine learning final exam based on
  her homework scores.

\item[Binary Classification:] trying to predict a simple yes/no
  response.  For instance, predict whether Alice will enjoy a course
  or not.  Or predict whether a user review of the newest Apple
  product is positive or negative about the product.

\item[Multiclass Classification:] trying to put an example into one of
  a number of classes.  For instance, predict whether a news story is
  about entertainment, sports, politics, religion, etc.  Or predict
  whether a CS course is Systems, Theory, AI or Other.

\item[Ranking:] trying to put a set of objects in order of relevance.
  For instance, predicting what order to put web pages in, in response
  to a user query.  Or predict Alice's ranked preferences over courses
  she hasn't taken.
\end{description}

\thinkaboutit{For each of these types of canonical machine learning
  problems, come up with one or two concrete examples.}

The reason that it is convenient to break machine learning problems
down by the type of object that they're trying to predict has to do
with measuring error.  Recall that our goal is to build a system that
can make ``good predictions.''  This begs the question: what does it
mean for a prediction to be ``good?''  The different types of learning
problems differ in how they define goodness.  For instance, in
regression, predicting a stock price that is off by $\$0.05$ is
perhaps much better than being off by $\$200.00$.  The same does not
hold of multiclass classification.  There, accidentally predicting
``entertainment'' instead of ``sports'' is no better or worse than
predicting ``politics.''

%TODO? what have they heard of that does ML?  what haven't they heard of?

%batch versus online learning

\section{The Decision Tree Model of Learning}

The \concept{decision tree} is a classic and natural model of
learning.  It is closely related to the fundamental computer science
notion of ``divide and conquer.''  Although decision trees can be
applied to many learning problems, we will begin with the simplest
case: binary classification.

Suppose that your goal is to predict whether some unknown user will
enjoy some unknown course.  You must simply answer ``yes'' or ``no.''
In order to make a guess, your're allowed to ask binary questions
about the user/course under consideration.  For example:

{\bf You:} Is the course under consideration in Systems?\\
{\bf Me:}  Yes\\
{\bf You:} Has this student taken any other Systems courses?\\
{\bf Me:}  Yes\\
{\bf You:} Has this student like most previous Systems courses?\\
{\bf Me:}  No\\
{\bf You:} \emph{I predict this student will not like this course.}\\

The goal in learning is to figure out what questions to ask, in what
order to ask them, and what answer to predict once you have asked
enough questions.

%\TODOFigure{dt:example}{A simple picture of a decision tree for course recommendation, consistent with the text example.}

\MoveNextFigure{-10cm}
\Figure{dt:example}{A decision tree for a course recommender system,
  from which the in-text ``dialog'' is drawn.}

The decision tree is so-called because we can write our set of
questions and guesses in a tree format, such as that in
Figure~\ref{fig:dt:example}.  In this figure, the questions are
written in the internal tree nodes (rectangles) and the guesses are
written in the leaves (ovals).  Each non-terminal node has two
children: the left child specifies what to do if the answer to the
question is ``no'' and the right child specifies what to do if it is
``yes.''

In order to learn, I will give you training data.  This data consists
of a set of user/course examples, paired with the correct answer for
these examples (did the given user enjoy the given course?).  From
this, you must construct your questions.  For concreteness, there is a
small data set in Table~\ref{tab:data:course} in the Appendix of this
book.  This training data consists of 20 course rating examples, with
course ratings and answers to questions that you might ask about this
pair.  We will interpret ratings of $0$, $+1$ and $+2$ as ``liked'' and
ratings of $-2$ and $-1$ as ``hated.''

In what follows, we will refer to the questions that you can ask as
\concept{features} and the responses to these questions as
\concept{feature values}.  The rating is called the \concept{label}.
An example is just a set of feature values.  And our training data is
a set of examples, paired with labels.

There are a lot of logically possible trees that you could build, even
over just this small number of features (the number is in the
millions).  It is computationally infeasible to consider all of these
to try to choose the ``best'' one.  Instead, we will build our
decision tree \emph{greedily.}  We will begin by asking:

{\bf If I could only ask one question, what question would I ask?}

%\TODOFigure{dt:histogram}{A figure showing the histogram of labels
%  given features for the first four features from the data in the
%  appendix.}

\MoveNextFigure{-10cm}
\Figure{dt:histogram}{A histogram of labels for (a) the entire data
  set; (b-e) the examples in the data set for each value of the first
  four features.}

You want to find a feature that is \emph{most useful} in helping you
guess whether this student will enjoy this course.\sidenote{A
  colleague related the story of getting his 8-year old nephew to
  guess a number between 1 and 100.  His nephew's first four questions
  were: Is it bigger than 20?  (YES) Is it even?  (YES) Does it have a
  7 in it?  (NO) Is it 80?  (NO).  It took 20 more questions to get
  it, even though 10 should have been sufficient.  At 8, the nephew
  hadn't quite figured out how to divide and conquer.
  \url{http://blog.computationalcomplexity.org/2007/04/getting-8-year-old-interested-in.html}.}
A useful way to think about this is to look at the \concept{histogram}
of labels for each feature.  This is shown for the first four features
in Figure~\ref{fig:dt:histogram}.  Each histogram shows the frequency
of ``like''/``hate'' labels for each possible value of an associated
feature.  From this figure, you can see that asking the first feature
is not useful: if the value is ``no'' then it's hard to guess the
label; similarly if the answer is ``yes.''  On the other hand, asking
the second feature \emph{is} useful: if the value is ``no,'' you can
be pretty confident that this student will like this course; if the
answer is ``yes,'' you can be pretty confident that this student will
hate this course.

More formally, you will consider each feature in turn.  You might
consider the feature ``Is this a System's course?''  This feature has
two possible value: no and yes.  Some of the training examples have an
answer of ``no'' -- let's call that the ``NO'' set.  Some of the
training examples have an answer of ``yes'' -- let's call that the
``YES'' set.  For each set (NO and YES) we will build a histogram over
the labels.  This is the second histogram in
Figure~\ref{fig:dt:histogram}.  Now, suppose you were to ask this
question on a random example and observe a value of ``no.''  Further
suppose that you must \emph{immediately} guess the label for this
example.  You will guess ``like,'' because that's the more prevalent
label in the NO set (actually, it's the \emph{only} label in the NO
set).  Alternative, if you recieve an answer of ``yes,'' you will
guess ``hate'' because that is more prevalent in the YES set.

So, for this single feature, you know what you \emph{would} guess if
you had to.  Now you can ask yourself: if I made that guess on the
\emph{training data,} how well would I have done?  In particular, how
many examples would I classify \emph{correctly?}  In the NO set (where
you guessed ``like'') you would classify all $10$ of them correctly.
In the YES set (where you guessed ``hate'') you would classify $8$
(out of $10$) of them correctly.  So overall you would classify $18$
(out of $20$) correctly.  Thus, we'll say that the \emph{score} of the
``Is this a System's course?'' question is $18/20$.

\thinkaboutit{How many training examples would you classify correctly
  for each of the other three features from
  Figure~\ref{fig:dt:histogram}?}

You will then repeat this computation for each of the available
features to us, compute the scores for each of them.  When you must
choose which feature consider first, you will want to choose the one
with the highest score.

But this only lets you choose the \emph{first} feature to ask about.
This is the feature that goes at the \emph{root} of the decision tree.
How do we choose subsequent features?  This is where the notion of
divide and conquer comes in.  You've already decided on your first
feature: ``Is this a Systems course?''  You can now \emph{partition}
the data into two parts: the NO part and the YES part.  The NO part is
the subset of the data on which value for this feature is ``no''; the
YES half is the rest.  This is the \emph{divide} step.

The \emph{conquer} step is to recurse, and run the \emph{same}
routine (choosing the feature with the highest score) on the NO set
(to get the left half of the tree) and then separately on the YES set
(to get the right half of the tree).

At some point it will become useless to query on additional features.
For instance, once you know that this is a Systems course, you
\emph{know} that everyone will hate it.  So you can immediately
predict ``hate'' without asking any additional questions.  Similarly,
at some point you might have already queried every available feature
and still not whittled down to a single answer.  In both cases, you
will need to create a leaf node and guess the most prevalent answer in
the current piece of the training data that you are looking at.

\newalgorithm%
  {dt:train}%
  {\FUN{DecisionTreeTrain}(\VAR{data}, \VAR{remaining features})}%
  {
\SETST{guess} most frequent answer in \VAR{data} \COMMENT{default answer for this data}
\IF{the labels in \VAR{data} are unambiguous}
\RETURN \STR{Leaf}(\VAR{guess}) \COMMENT{base case: no need to split further}
\ELSIF{\VAR{remaining features} is empty}
\RETURN \STR{Leaf}(\VAR{guess}) \COMMENT{base case: cannot split further}
\ELSE[we need to query more features]
\FORALL{\VAR{f} $\in$ \VAR{remaining features}}
\SETST{NO} the subset of \VAR{data} on which \VAR{f}=\CON{no}
\SETST{YES} the subset of \VAR{data} on which \VAR{f}=\CON{yes}
\STATE \VAR{score}[\VAR{f}] $\leftarrow$ \# of majority vote answers in \VAR{NO}
\STATE \quad\quad\quad\quad + \# of majority vote answers in \VAR{YES}\\
\COMMENT{the accuracy we would get if we only queried on \VAR{f}}
\ENDFOR
\SETST{f} the feature with maximal \VAR{score}(\VAR{f})
\SETST{NO} the subset of \VAR{data} on which \VAR{f}=\CON{no}
\SETST{YES} the subset of \VAR{data} on which \VAR{f}=\CON{yes}
\SETST{left} \FUN{DecisionTreeTrain}(\VAR{NO}, \VAR{remaining features} $\without$ $\{$\VAR{f}$\}$)
\SETST{right} \FUN{DecisionTreeTrain}(\VAR{YES}, \VAR{remaining features} $\without$ $\{$\VAR{f}$\}$)
\RETURN \STR{Node}(\VAR{f}, \VAR{left}, \VAR{right})
\ENDIF
}

\newalgorithm%
  {dt:predict}%
  {\FUN{DecisionTreeTest}(\VAR{tree}, \VAR{test point})}
  {
\IF{\VAR{tree} is of the form \STR{Leaf}(\VAR{guess})}
\RETURN \VAR{guess}
\ELSIF{\VAR{tree} is of the form \STR{Node}(\VAR{f}, \VAR{left}, \VAR{right})}
\IF{\VAR{f} $=$ \CON{yes} in \VAR{test point}}
\RETURN \FUN{DecisionTreeTest}(\VAR{left}, \VAR{test point})
\ELSE
\RETURN \FUN{DecisionTreeTest}(\VAR{right}, \VAR{test point})
\ENDIF
\ENDIF
}

Putting this all together, we arrive at the algorithm shown in
Algorithm~\ref{alg:dt:train}.\sidenote{There are more nuanced
  algorithms for building decision trees, some of which are discussed
  in later chapters of this book.  They primarily differ in how they
  compute the \emph{score} funciton.}  This function,
\FUN{DecisionTreeTrain} takes two arguments: our data, and the set of
as-yet unused features.  It has two base cases: either the data is
unambiguous, or there are no remaining features.  In either case, it
returns a \STR{Leaf} node containing the most likely guess at this
point.  Otherwise, it loops over all remaining features to find the
one with the highest score.  It then partitions the data into a NO/YES
split based on the best feature.  It constructs its left and right
subtrees by recursing on itself.  In each recursive call, it uses one
of the partitions of the data, and removes the just-selected feature
from consideration.

\thinkaboutit{Is the Algorithm in Figure~\ref{fig:dt:train}
  guaranteed to terminate?}

The corresponding \emph{prediction} algorithm is shown in
Algorithm~\ref{alg:dt:test}.  This function recurses down the decision
tree, following the edges specified by the feature values in some
\VAR{test point}.  When it reaches a leave, it returns the guess
associated with that leaf.

TODO: define outlier somewhere!

\section{Formalizing the Learning Problem}

As you've seen, there are several issues that we must take into
account when formalizing the notion of learning.

\begin{itemize}
\item The performance of the learning algorithm should be measured on
  unseen ``test'' data.

\item The way in which we measure performance should depend on the
  problem we are trying to solve.

\item There should be a strong relationship between the data that our
  algorithm sees at training time and the data it sees at test time.
\end{itemize}

In order to accomplish this, let's assume that someone gives us a
\concept{loss function}, $\ell(\cdot,\cdot)$, of two arguments.  The
job of $\ell$ is to tell us how ``bad'' a system's prediction is in
comparison to the truth.  In particular, if $y$ is the truth and $\hat
y$ is the system's prediction, then $\ell(y,\hat y)$ is a measure of
error.

For three of the canonical tasks discussed above, we might use the
following loss functions:

\begin{description}
\item[Regression:] \concept{squared loss} $\ell(y,\hat y) = (y - \hat
  y)^2$\\ or \concept{absolute loss} $\ell(y,\hat y) = \ab{y - \hat y}$.

\item[Binary Classification:] \concept{zero/one loss} $\ell(y,\hat y)
  = \brack{0 & \textsf{if } y = \hat y\\ 1 & \textsf{otherwise}}$
  \marginnote[-.5em]{This notation means that the loss is zero if the
    prediction is correct and is one otherwise.}

\item[Multiclass Classification:] also zero/one loss.
\end{description}

\thinkaboutit{Why might it be a bad idea to use zero/one loss to
  measure performance for a regression problem?}

Note that the loss function is something that \emph{you} must decide
on based on the goals of learning.

Now that we have defined our loss function, we need to consider where
the data (training \emph{and} test) comes from.  The model that we
will use is the \emph{probabilistic} model of learning.  Namely, there
is a probability distribution $\cD$ over input/output pairs.  This is
often called the \concept{data generating distribution}.  If we write
$\vx$ for the input (the user/course pair) and $y$ for the output (the
rating), then $\cD$ is a distribution over $(\vx,y)$ pairs.

A useful way to think about $\cD$ is that it gives \emph{high
  probability} to reasonable $(\vx,y)$ pairs, and \emph{low
  probability} to unreasonable $(\vx,y)$ pairs.  A $(\vx,y)$ pair can
be unreasonable in two ways.  First, $\vx$ might an unusual input.
For example, a $\vx$ related to an ``Intro to Java'' course might be
highly probable; a $\vx$ related to a ``Geometric and Solid Modeling''
course might be less probable.  Second, $y$ might be an unusual rating
for the paired $\vx$.  For instance, if Alice were to take AI $100$
times (without remembering that she took it before!), she would give
the course a $+2$ almost every time.  Perhaps some semesters she might
give a slightly lower score, but it would be unlikely to see
$\vx=$Alice/AI paired with $y=-2$.

It is important to remember that we are not making \emph{any}
assumptions about what the distribution $\cD$ looks like.  (For
instance, we're not assuming it looks like a Gaussian or some other,
common distribution.)  We are also not assuming that we know what
$\cD$ is.  In fact, if you know \emph{a priori} what your data
generating distribution is, your learning problem becomes
significantly easier.  Perhaps the hardest think about machine
learning is that we \emph{don't} know what $\cD$ is: all we get is a
random sample from it.  This random sample is our training data.

Our learning problem, then, is defined by two quantities:

\thinkaboutit{Consider the following prediction task.  Given a
  paragraph written about a course, we have to predict whether the
  paragraph is a \emph{positive} or \emph{negative} review of the
  course.  (This is the sentiment analysis problem.)  What is a
  reasonable loss function?  How would you define the data generating
  distribution?}
\begin{enumerate}
\item The loss function $\ell$, which captures our notion of what is
  \emph{important} to learn.

\item The data generating distribution $\cD$, which defines what sort
  of data we expect to see.
\end{enumerate}

We are given access to \concept{training data}, which is a random
sample of input/output pairs drawn from $\cD$.  Based on this training
data, we need to \concept{induce} a function $f$ that maps new inputs
$\hat \vx$ to corresponding prediction $\hat y$.  The key property
that $f$ should obey is that it should do well (as measured by $\ell$)
on future examples that are \emph{also} drawn from $\cD$.  Formally,
it's \concept{expected loss} $\ep$ over $\cD$ with repsect to $\ell$
should be as small as possible:
\begin{align} \label{eq:expectederror}
\ep
&\defeq
\Ep_{(\vx,y) \sim \cD} \big[ \ell(y, f(\vx)) \big]
=
\sum_{(\vx,y)} \cD(\vx,y) \ell(y, f(\vx))
\end{align}

\begin{mathreview}{Expectated Values}
  remind people what expectations are and explain the notation in
  Eq~\eqref{eq:expectederror}.
\end{mathreview}

The difficulty in minimizing our \concept{expected loss} from
Eq~\eqref{eq:expectederror} is that we \emph{don't know what $\cD$
  is!}  All we have access to is some training data sampled from it!
Suppose that we denote our training data set by $D$.  The training
data consists of $N$-many input/output pairs, $(\vx_1,y_1),
(\vx_2,y_2), \dots, (\vx_N,y_N)$.  Given a learned function $f$, we
can compute our \concept{training error}, $\hat \ep$:
\begin{align} \label{eq:trainingerror}
\hat \ep
&\defeq
\frac 1 N \sum_{n=1}^N \ell(y_n, f(\vx_n))
\end{align}

That is, our training error is simply our \emph{average error} over
the training data.  \thinkaboutit{Verify by calculation that we can
  write our training error as $\Ep_{(\vx,y) \sim D} \big[ \ell(y,
  f(\vx)) \big]$, by thinking of $D$ as a distribution that places
  probability $1/N$ to each example in $D$ and probabiliy $0$ on
  everything else.}

Of course, we can drive $\hat \ep$ to zero by simply memorizing our
training data.  But as Alice might find in memorizing past exams, this
might not generalize well to a new exam!

This is the fundamental difficulty in machine learning: the thing we
have access to is our training error, $\hat \ep$.  But the thing we care
about minimizing is our expected error $\ep$.  In order to get the
expected error down, our learned function needs to
\concept{generalize} beyond the training data to some future data that
it might not have seen yet!

So, putting it all together, we get a formal definition of induction
machine learning: \bigemph{Given (i) a loss function $\ell$ and (ii) a
  sample $D$ from some unknown distribution $\cD$, you must compute a
  function $f$ that has low expected error $\ep$ over $\cD$ with
  respect to $\ell$.}

\section{Inductive Bias: What We Know Before the Data Arrives}

\TODOFigure{dt:bird}{bird training images}

\TODOFigure{dt:birdtest}{bird test images}

In Figure~\ref{fig:dt:bird} you'll find training data for a binary
classification problem.  The two labels are ``A'' and ``B'' and you
can see five examples for each label.  Below, in
Figure~\ref{fig:dt:birdtest}, you will see some test data.  These
images are left unlabeled.  Go through quickly and, based on the
training data, label these images.  (Really do it before you read
further!  I'll wait!)

Most likely you produced one of two labelings: either ABBAAB or
ABBABA.  Which of these solutions is right?

The answer is that you cannot tell based on the training data.  If you
give this same example to $100$ people, $60-70$ of them come up with
the ABBAAB prediction and $30-40$ come up with the ABBABA prediction.
Why are they doing this?  Presumably because the first group believes
that the relevant distinction is between ``bird'' and ``non-bird''
while the secong group believes that the relevant distinction is
between ``fly'' and ``no-fly.''

This preference for one distinction (bird/non-bird) over another
(fly/no-fly) is a bias that different human learners have.  In the
context of machine learning, it is called \concept{inductive bias}: in
the absense of data that narrow down the relevant concept, what type
of solutions are we more likely to prefer?  Two thirds of people seem
to have an inductive bias in favor of bird/non-bird, and one third
seem to have an inductive bias in favor of fly/no-fly.

\thinkaboutit{It is also possible that the correct classification on
  the test data is BABAAA.  This corresponds to the bias ``is the
  background in focus.''  Somehow no one seems to come up with this
  classification rule.}

Throughout this book you will learn about several approaches to
machine learning.  The decision tree model is the first such
approach.  These approaches differ primarily in the sort of inductive
bias that they exhibit.

Consider a variant of the decision tree learning algorithm.  In this
variant, we will not allow the trees to grow beyond some pre-defined
maximum depth, $d$.  That is, once we have queried on $d$-many
features, we cannot query on any more and must just make the best
guess we can at that point.  This variant is called a \concept{shallow
  decision tree}.

The key question is: What is the inductive bias of shallow decision
trees?  Roughly, their bias is that decisions can be made by only
looking at a small number of features.  For instance, a shallow
decision tree would be very good a learning a function like ``students
only like AI courses.''  It would be very bad at learning a function
like ``if this student has liked an odd number of his past courses, he
will like the next one; otherwise he will not.''  This latter is the
\emph{parity} function, which requires you to inspect every feature to
make a prediction.  The inductive bias of a decision tree is that the
sorts of things we want to learn to predict are more like the first
example and less like the second example.

\section{Not Everything is Learnable}

Although machine learning works well---perhaps astonishingly well---in
many cases, it is important to keep in mind that it is not magical.
There are many reasons why a machine learning algorithm might fail on
some learning task.

There could be \concept{noise} in the training data.  Noise can occur
both at the feature level and at the label level.  Some features might
correspond to measurements taken by sensors.  For instance, a robot
might use a laser range finder to compute its distance to a wall.
However, this sensor might fail and return an incorrect value.  In a
sentiment classification problem, someone might have a typo in their
review of a course.  These would lead to noise at the feature level.
There might also be noise at the label level.  A student might write a
scathingly negative review of a course, but then accidentally click
the wrong button for the course rating.

The features available for learning might simply be insufficient.  For
example, in a medical context, you might wish to diagnose whether a
patient has cancer or not.  You may be able to collect a large amount
of data about this patient, such as gene expressions, X-rays, family
histories, etc.  But, even knowing all of this information exactly, it
might still be impossible to judge for sure whether this patient has
cancer or not.  As a more contrived example, you might try to classify
course reviews as positive or negative.  But you may have erred when
downloading the data and only gotten the first five characters of each
review.  If you had the rest of the features you might be able to do
well.  But with this limited feature set, there's not much you can do.

Some example may not have a single correct answer.  You might be
building a system for ``safe web search,'' which removes offensive web
pages from search results.  To build this system, you would collect a
set of web pages and ask people to classify them as ``offensive'' or
not.  However, what one person considers offensive might be completely
reasonable for another person.  It is common to consider this as a
form of label noise.  Nevertheless, since you, as the designer of the
learning system, have some control over this problem, it is sometimes
helpful to isolate it as a source of difficulty.

Finally, learning might fail because the inductive bias of the
learning algorithm is too far away from the concept that is being
learned.  In the bird/non-bird data, you might think that if you had
gotten a few more training examples, you might have been able to tell
whether this was intended to be a bird/non-bird classification or a
fly/no-fly classification.  However, no one I've talked to has ever
come up with the ``background is in focus'' classification.  Even with
many more training points, this is such an unusual distinction that it
may be hard for anyone to figure out it.  In this case, the inductive
bias of the learner is simply too misaligned with the target
classification to learn.

Note that the inductive bias source of error is fundamentally
different than the other three sources of error.  In the inductive
bias case, it is the \emph{particular} learning algorithm that you are
using that cannot cope with the data.  Maybe if you switched to a
different learning algorithm, you would be able to learn well.  For
instance, Neptunians might have evolved to care greatly about whether
backgrounds are in focus, and for them this would be an easy
classification to learn.  For the other three sources of error, it is
not an issue to do with the particular learning algorithm.  The error
is a fundamental part of the learning problem.

\section{Underfitting and Overfitting}

As with many problems, it is useful to think about the \emph{extreme
  cases} of learning algorithms.  In particular, the extreme cases of
decision trees.  In one extreme, the tree is ``empty'' and we do not
ask any questions at all.  We simply immediate make a prediction.  In
the other extreme, the tree is ``full.''  That is, every possible
question is asked along every branch.  In the full tree, there may be
leaves with no associated training data.  For these we must simply
choose arbitrarily whether to say ``yes'' or ``no.''

Consider the course recommendation data from
Table~\ref{tab:data:course}.  Suppose we were to build an ``empty''
decision tree on this data.  Such a decision tree will make the same
prediction regardless of its input, because it is not allowed to ask
any questions about its input.  Since there are more ``likes'' than
``hates'' in the training data ($12$ versus $8$), our empty decision
tree will simply always predict ``likes.''  The training error, $\hat
\ep$, is $8/20 = 40\%$.

%TODO: earlier define what it means for an example to be assigned to a leaf

On the other hand, we could build a ``full'' decision tree.  Since
each row in this data is unique, we can guarantee that any leaf in a
full decision tree will have either $0$ or $1$ examples assigned to it
($20$ of the leaves will have one example; the rest will have none).
For the leaves corresponding to training points, the full decision
tree will always make the correct prediction.
Given this, the training error, $\hat \ep$, is $0/20 = 0\%$.

Of course our goal is \emph{not} to build a model that gets $0\%$
error on the training data.  This would be easy!  Our goal is a model
that will do well on \emph{future, unseen} data.  How well might we
expect these two models to do on future data?  The ``empty'' tree is
likely to do not much better and not much worse on future data.  We
might expect that it would continue to get around $40\%$ error.

Life is more complicated for the ``full'' decision tree.  Certainly if
it is given a test example that is identical to one of the training
examples, it will do the right thing (assuming no noise).  But for
everything else, it will only get about $50\%$ error.  This means that
even if every other test point happens to be identical to one of the
training points, it would only get about $25\%$ error.  In practice,
this is probably optimistic, and maybe only one in every $10$ examples
would match a training example, yielding a $35\%$ error.

\thinkaboutit{Convince yourself (either by proof or by simulation)
  that even in the case of imbalanced data -- for instance data that
  is on average $80\%$ positive and $20\%$ negative -- a predictor
  that guesses randomly (50/50 positive/negative) will get about
  $50\%$ error.}

So, in one case (empty tree) we've achieved about $40\%$ error and in
the other case (full tree) we've achieved $35\%$ error.  This is not
very promising!  One would hope to do better!  In fact, you might
notice that if you simply queried on a \emph{single} feature for this
data, you would be able to get very low training error, but wouldn't
be forced to ``guess'' randomly.

\thinkaboutit{Which feature is it, and what is it's training error?}

This example illustrates the key concepts of \concept{underfitting}
and \concept{overfitting}.  Underfitting is when you had the
opportunity to learn something but didn't.  A student who hasn't
studied much for an upcoming exam will be underfit to the exam, and
consequently will not do well.  This is also what the empty tree
does.  Overfitting is when you pay too much attention to
idiosyncracies of the training data, and aren't able to generalize
well.  Often this means that your model is fitting noise, rather than
whatever it is supposed to fit.  A student who memorizes answers to
past exam questions without understanding them has overfit the
training data.  Like the full tree, this student also will not do well
on the exam.  A model that is neither overfit nor underfit is the one
that is expected to do best in the future.

\section{Separation of Training and Test Data}

Suppose that, after graduating, you get a job working for a company
that provides persolized recommendations for pottery.  You go in and
implement new algorithms based on what you learned in her machine
learning class (you have learned the power of generalization!).  All
you need to do now is convince your boss that you has done a good job
and deserve a raise!

How can you convince your boss that your fancy learning algorithms are
really working?

Based on what we've talked about already with underfitting and
overfitting, it is not enough to just tell your boss what your
training error is.  Noise notwithstanding, it is easy to get a
training error of zero using a simple database query (or {\tt grep},
if you prefer).  Your boss will not fall for that.

The easiest approach is to \emph{set aside} some of your available
data as ``test data'' and use this to evaluate the performance of your
learning algorithm.  For instance, the pottery recommendation service
that you work for might have collected $1000$ examples of pottery
ratings.  You will select $800$ of these as \concept{training data}
and set aside the final $200$ as \concept{test data}.  You will run
your learning algorithms \emph{only} on the $800$ training points.
Only once you're done will you apply your learned model to the $200$
test points, and report your \concept{test error} on those $200$
points to your boss.

The hope in this process is that however well you do on the $200$ test
points will be indicative of how well you are likely to do in the
future.  This is analogous to estimating support for a presidential
candidate by asking a small (random!) sample of people for their
opinions.  Statistics (specifically, concentration bounds of which the
``Central limit theorem'' is a famous example) tells us that if the
sample is large enough, it will be a good representative.  The 80/20
split is not magic: it's simply fairly well established.  Occasionally
people use a 90/10 split instead, especially if they have a \emph{lot}
of data.

\thinkaboutit{If you have more data at your disposal, why might a
  90/10 split be preferable to an 80/20 split?}

They cardinal rule of machine learning is: never touch your test data.
Ever.  If that's not clear enough:

\noindent
\begin{tikzpicture}[transform shape, rotate=0]
  \node [draw=Red!5, fill=Red!20, very thick, rectangle, rounded corners, inner sep=5pt, inner ysep=5pt] (box) {
    \textcolor{red}{\textsf{\huge Never \emph{ever} touch your test data!}}
  };
\end{tikzpicture}

If there is only one thing you learn from this book, let it be that.
Do not look at your test data.  Even once.  Even a tiny peek.  Once
you do that, it is not test data any more.  Yes, perhaps your
algorithm hasn't seen it.  But you have.  And you are likely a better
learner than your learning algorithm.  Consciously or otherwise, you
might make decisions based on whatever you might have seen.  Once you
look at the test data, your model's performance on it is no longer
indicative of it's performance on future unseen data.  This is simply
because future data is unseen, but your ``test'' data no longer is.

\section{Models, Parameters and Hyperparameters}

The general approach to machine learning, which captures many existing
learning algorithms, is the \concept{modeling} approach.  The idea is
that we come up with some formal model of our data.  For instance, we
might model the classification decision of a student/course pair as a
decision tree.  The choice of using a \emph{tree} to represent this
model is \emph{our choice.}  We also could have used an arithmetic
circuit or a polynomial or some other function.  The model tells us
what sort of things we can learn, and also tells us what our inductive
bias is.

For most models, there will be associated parameters.  These are the
things that we use the data to decide on.  Parameters in a decision
tree include: the specific questions we asked, the order in which we
asked them, and the classification decisions at the leaves.  The job
of our decision tree learning algorithm \FUN{DecisionTreeTrain} is to take data
and figure out a good set of parameters.

Many learning algorithms will have additional knobs that you can
adjust.  In most cases, these knobs amount to tuning the inductive
bias of the algorithm.  In the case of the decision tree, an obvious
knob that one can tune is the \concept{maximum depth} of the decision
tree.  That is, we could modify the \FUN{DecisionTreeTrain} function so that it
\emph{stops} recursing once it reaches some pre-defined maximum depth.
By playing with this depth knob, we can adjust between underfitting
(the empty tree, depth$=0$) and overfitting (the full tree,
depth$=\infty$).

\thinkaboutit{Go back to the \FUN{DecisionTreeTrain} algorithm and modify it so
  that it takes a maximum depth parameter.  This should require adding
  two lines of code and modifying three others.}

Such a knob is called a \concept{hyperparameter}.  It is so called
because it is a parameter that controls other parameters of the model.
The exact definition of hyperparameter is hard to pin down: it's one
of those things that are easier to identify than define.  However, one
of the key identifiers for hyperparameters (and the main reason that
they cause consternation) is that they cannot be naively adjusted
using the training data.

In \FUN{DecisionTreeTrain}, as in most machine learning, the learning algorithm
is essentially trying to adjust the parameters of the model so as to
minimize training error.  This suggests an idea for choosing
hyperparameters: choose them so that they minimize training error.

What is wrong with this suggestion?  Suppose that you were to treat
``maximum depth'' as a hyperparameter and tried to tune it on your
training data.  To do this, maybe you simply build a collection of
decision trees, tree$_0$, tree$_1$, tree$_2$, $\dots$, tree$_{100}$,
where tree$_d$ is a tree of maximum depth $d$.  We then computed the
training error of each of these trees and chose the ``ideal'' maximum
depth as that which minimizes training error?  Which one would it
pick?

The answer is that it would pick $d=100$.  Or, in general, it would
pick $d$ as large as possible.  Why?  Because choosing a bigger $d$
will \emph{never hurt} on the training data.  By making $d$ larger,
you are simply encouraging overfitting.  But by evaluating on the
training data, overfitting actually looks like a good idea!

An alternative idea would be to tune the maximum depth on test data.
This is promising because test data peformance is what we really want
to optimize, so tuning this knob on the test data seems like a good
idea.  That is, it won't accidentally reward overfitting.  Of course,
it breaks our cardinal rule about test data: that you should never
touch your test data.  So that idea is immediately off the table.

However, our ``test data'' wasn't magic.  We simply took our $1000$
examples, called $800$ of them ``training'' data and called the other
$200$ ``test'' data.  So instead, let's do the following.  Let's take
our original $1000$ data points, and select $700$ of them as training
data.  From the remainder, take $100$ as \concept{development
  data}\sidenote{Some people call this ``\concept{validation data}''
  or ``\concept{held-out data}.''} and the remaining $200$ as test
data.  The job of the development data is to allow us to tune
hyperparameters.  The general approach is as follows:

\begin{enumerate}
\item Split your data into $70\%$ training data, $10\%$ development
  data and $20\%$ test data.

\item For each possible setting of your hyperparameters:

\begin{enumerate}
\item Train a model using that setting of hyperparameters on the
  training data.

\item Compute this model's error rate on the development data.
\end{enumerate}

\item From the above collection of models, choose the one that
  achieved the lowest error rate on development data.

\item Evaluate that model on the test data to estimate future test
  performance.
\end{enumerate}

\thinkaboutit{In step 3, you could either choose the model (trained on
  the $70\%$ training data) that did the best on the development data.
  Or you could choose the hyperparameter settings that did best and
  \emph{retrain} the model on the $80\%$ union of training and
  development data.  Is either of these options obviously better or
  worse?}

\section{Chapter Summary and Outlook}

At this point, you should be able to use decision trees to do machine
learning.  Someone will give you data.  You'll split it into
training, development and test portions.  Using the training and
development data, you'll find a good value for maximum depth that
trades off between underfitting and overfitting.  You'll then run the
resulting decision tree model on the test data to get an estimate of
how well you are likely to do in the future.

You might think: why should I read the rest of this book?  Aside from
the fact that machine learning is just an awesome fun field to learn
about, there's a lot left to cover.  In the next two chapters, you'll
learn about two models that have very different inductive biases than
decision trees.  You'll also get to see a very useful way of thinking
about learning: the geometric view of data.  This will guide much of
what follows.  After that, you'll learn how to solve problems more
complicated that simple binary classification.  (Machine learning
people like binary classification a lot because it's one of the
simplest non-trivial problems that we can work on.)  After that,
things will diverge: you'll learn about ways to think about learning
as a formal optimization problem, ways to speed up learning, ways to
learn without labeled data (or with very little labeled data) and all
sorts of other fun topics.

But throughout, we will focus on the view of machine learning that
you've seen here.  You select a model (and its associated inductive
biases).  You use data to find parameters of that model that work well
on the training data.  You use development data to avoid underfitting
and overfitting.  And you use test data (which you'll never look at or
touch, right?) to estimate future model performance.  Then you conquer
the world.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "example_chapter"
%%% End:


\documentclass[12pt]{article}
\usepackage[top=1in,bottom=1in,left=0.75in,right=0.75in,centering]{geometry}
\usepackage{fancyhdr}
\usepackage{epsfig}
\usepackage[pdfborder={0 0 0}]{hyperref}
\usepackage{palatino}
\usepackage{wrapfig}
\usepackage{lastpage}
\usepackage{color}
\usepackage{ifthen}
\usepackage[table]{xcolor}
\usepackage{graphicx,type1cm,eso-pic,color}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{wasysym}

\def\course{CS 4102: Algorithms}
\def\homework{M2: Divide and Conquer - Recurrence Relations}
\def\semester{Fall 2021}

\newboolean{solution}
\setboolean{solution}{false}

% add watermark if it's a solution exam
% see http://jeanmartina.blogspot.com/2008/07/latex-goodie-how-to-watermark-things-in.html
\makeatletter
\AddToShipoutPicture{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\ifthenelse{\boolean{solution}}{
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.95}%
{\fontsize{5cm}{3cm}\selectfont{\textsf{Solution}}}}}%
}{}
}}
\makeatother

\pagestyle{fancy}

\fancyhf{}
\lhead{\course}
\chead{Page \thepage\ of \pageref{LastPage}}
\rhead{\semester}
%\cfoot{\Large (the bubble footer is automatically inserted into this space)}

\setlength{\headheight}{14.5pt}

\newenvironment{itemlist}{
\begin{itemize}
\setlength{\itemsep}{0pt}
\setlength{\parskip}{0pt}}
{\end{itemize}}

\newenvironment{numlist}{
\begin{enumerate}
\setlength{\itemsep}{0pt}
\setlength{\parskip}{0pt}}
{\end{enumerate}}

\newcounter{pagenum}
\setcounter{pagenum}{1}
\newcommand{\pageheader}[1]{
\clearpage\vspace*{-0.4in}\noindent{\large\bf{Page \arabic{pagenum}: {#1}}}
\addtocounter{pagenum}{1}
\cfoot{}
}

\newcounter{quesnum}
\setcounter{quesnum}{1}
\newcommand{\question}[2][??]{
\begin{list}{\labelitemi}{\leftmargin=2em}
\item [\arabic{quesnum}.] {} {#2}
\end{list}
\addtocounter{quesnum}{1}
}

\definecolor{red}{rgb}{1.0,0.0,0.0}
\newcommand{\answer}[2][??]{
\ifthenelse{\boolean{solution}}{
\color{red} #2 \color{black}}
{\vspace*{#1}}
}

\definecolor{blue}{rgb}{0.0,0.0,1.0}

\begin{document}

\section*{\homework}

This homework will ask you to think about Divide and Conquer Algorithms with a focus on recurrence relations (this is module 2 after all!). Please try to follow the guidelines below when writing up your solutions to these problems:

\begin{itemize}
	\item Algorithm descriptions should be about 1 paragraph long. If you are writing more than one paragraph to describe your algorithm, then it is TOO long.
	\item There should be enough detail that I could implement the algorithm if I wanted to, but we don't need to see code. If you want to add pseudo-code for clarity, that would be fine.
	\item For the recurrence relation problems below, make sure to show your work clearly!
\end{itemize}

\vspace{14pt}

\question[3]{
You are a hacker, trying to gain information on a secret array of size $n$. This array contains $n-1$ ones and exactly $1$ two; you want to determine the index of the two in the array.\\
\\
Unfortunately, you don't have access to the array directly; instead, you have access to a function $f(l1, l2)$ that compares the sum of the elements of the secret array whose indices are in $l1$ to those in $l2$. This function returns $-1$ if the $l1$ sum is larger, $0$ if they are equal, and $1$ if the sum corresponding to $l2$ is larger.\\
\\
For example, if the array is $a=[1,1,1,2,1,1]$ and you call $f([0,2,4],[1,3,5])$ then the return value is $1$ because $a[0]+a[2]+a[4]=3<4=a[1]+a[3]+a[5]$. Design an algorithm to find the index of the $2$ in the array using the least number of calls to $f()$. Then, answer the following questions:

\begin{itemize}
	\item Describe your algorithm clearly (in a paragraph or so)
	\item Give the recurrence relation for the runtime of your algorithm. Make sure to write this in terms of $f_r$, which we will use to represent the runtime of $f()$.
	\item Suppose you discover that $f()$ runs in $\Theta(max(|l1|,|l2|))$, what is the overall runtime of your algorithm in this case?
\end{itemize}
}

\vspace{12pt}

%----------------------------------------------------------------------
\noindent Directly solve, by unrolling the recurrence, the following relation to find its exact solution. Make sure to show your work.

\question[2]{
$T(n) = T(n-1) + n$
}

\vspace{12pt}

%----------------------------------------------------------------------

\noindent Use induction to show bounds on the following recurrence relations.

\question[2]{
Show that $T(n)=4T(\frac{n}{3})+n \in O(n^{log_3(4)})$. You'll need to subtract off a lower-order term to make the induction work here.
}

\answer[0 in]{
...
}

\vspace{12pt}

%----------------------------------------------------------------------

\noindent Use the master theorem to solve the following recurrence relations. State which case of the theorem you are using and why.

\question[2]{
$T(n)=2T(\frac{n}{4})+1$
}

\answer[0 in]{
...
}

\question[2]{
$T(n)=2T(\frac{n}{4})+\sqrt{n}$
}

\answer[0 in]{
...
}

\question[2]{
$T(n)=2T(\frac{n}{4})+n$
}

\answer[0 in]{
...
}

\question[2]{
$T(n)=2T(\frac{n}{4})+n^2$
}

\answer[0 in]{
...
}
\end{document}


\documentclass{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}

\usetikzlibrary{automata,positioning}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\lhead{\hmwkAuthorName}
\chead{\hmwkClass\ (\hmwkClassInstructor\ \hmwkClassTime): \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{0pt}

%
% Create Problem Sections
%

\newcommand{\enterProblemHeader}[1]{
    \nobreak\extramarks{}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
}

\newcommand{\exitProblemHeader}[1]{
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \stepcounter{#1}
    \nobreak\extramarks{Problem \arabic{#1}}{}\nobreak{}
}

\setcounter{secnumdepth}{0}
\newcounter{partCounter}
\newcounter{homeworkProblemCounter}
\setcounter{homeworkProblemCounter}{1}
\nobreak\extramarks{Problem \arabic{homeworkProblemCounter}}{}\nobreak{}

%
% Homework Problem Environment
%
% This environment takes an optional argument. When given, it will adjust the
% problem counter. This is useful for when the problems given for your
% assignment aren't sequential. See the last 3 problems of this template for an
% example.
%
\newenvironment{homeworkProblem}[1][-1]{
    \ifnum#1>0
        \setcounter{homeworkProblemCounter}{#1}
    \fi
    \section{Problem \arabic{homeworkProblemCounter}}
    \setcounter{partCounter}{1}
    \enterProblemHeader{homeworkProblemCounter}
}{
    \exitProblemHeader{homeworkProblemCounter}
}

%
% Homework Details
%   - Title
%   - Due date
%   - Class
%   - Section/Time
%   - Instructor
%   - Author
%

\newcommand{\hmwkTitle}{Homework\ \#1}
\newcommand{\hmwkDueDate}{January 17th, 2017}
\newcommand{\hmwkClass}{Elements of Algebra I}
\newcommand{\hmwkClassTime}{Section 161}
\newcommand{\hmwkClassInstructor}{Professor Deepam Patel}
\newcommand{\hmwkAuthorName}{Yao Xiao}

%
% Title Page
%

\title{
    \vspace{2in}
    \textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
    \normalsize\vspace{0.1in}\small{Due\ on\ \hmwkDueDate\  }\\
    \vspace{0.1in}\large{\textit{\hmwkClassInstructor\ \hmwkClassTime}}
    \vspace{3in}
}

\author{\textbf{\hmwkAuthorName}}
\date{}

\renewcommand{\part}[1]{\textbf{\large Part \Alph{partCounter}}\stepcounter{partCounter}\\}

%
% Various Helper Commands
%

% Useful for algorithms
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}}

% For derivatives
\newcommand{\deriv}[1]{\frac{\mathrm{d}}{\mathrm{d}x} (#1)}

% For partial derivatives
\newcommand{\pderiv}[2]{\frac{\partial}{\partial #1} (#2)}

% Integral dx
\newcommand{\dx}{\mathrm{d}x}

% Alias for the Solution section header
\newcommand{\solution}{\textbf{\large Solution}}

% Probability commands: Expectation, Variance, Covariance, Bias
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\Bias}{\mathrm{Bias}}

\begin{document}

\maketitle

\pagebreak

\newpage
\newpage

\begin{homeworkProblem}
1. The elements of the set are all numbers between 1 and 3, excluding 1 and 3 \\
2. An empty set
\end{homeworkProblem}

\begin{homeworkProblem}
\[
\begin{split}
A \cap (B \setminus C) &:= \{x: x \in A \ and (\ x \in B \ and \ x \not \in C) \} \\
&= \{x: x \in A \ and  \ \ x \in B \ and \ x \not \in C) \} \\
&= \{x: (x \in A \ and  \ \ x \in B ) \ and \ x \not \in C) \}\\
&= \{x: x \in A \cap B \ and \ x \not \in C) \}\\
&= (A \cap B) \setminus C
	\end{split}
	 \]
\end{homeworkProblem}

\begin{homeworkProblem}
\[
\begin{split}
(A \cap B) ^c :&= \{x \in S \ and \  x \notin (A \cap B)\}\\
&= \{x \in S \ and \  x \notin A \ and \ x \notin B)\}\\
&= \{x \in S \ and \  x \notin A \}  \cap \{x \in S \ and \ x \notin B) \}\\
&= \{x \in S \ and \  x \in A^c \}  \cap \{x \in S \ and \ x \in B^c) \}\\
&= A^c \cap B^c
	\end{split}
	 \]
\end{homeworkProblem}

%4
\begin{homeworkProblem}
\[
\begin{split}
(A \times B) \cap (C \times D) &= \{(x,y): x \in A \ and \ y \in B \}
\cap \{(x,y): x \in C \ and \ y \in D \}\\
&= \{(x,y): x \in A \ and \ y \in B \ and \ x \in C \ and \ y \in D \}\\
&= \{(x,y): x \in A \ and \ x \in C \ and \ y \in B \ and \ y \in D \}\\
&= \{(x,y): x \in A \cap C \ and \ y \in B \cap  D \}\\
&= (A \cap C) \times (B \cap D)
	\end{split}
	 \]
\end{homeworkProblem}

%5
\begin{homeworkProblem}
\[
\begin{split}
LHS = (\neg (p \rightarrow q)) = \neg (\neg p \lor (p \land q))
=p \land \neg (p \land q) = p \land (\neg p \lor \neg q) =  (p \land \neg p) \lor (p \land \neg q)\\
 = False \lor  p \land \neg q = p \land \neg q = RHS
	\end{split}
	 \]
\end{homeworkProblem}

%6
\begin{homeworkProblem}
Suppose the two numbers are a and b
\[
\begin{split}
\exists x,y \in \mathbb{Z} \ a=2x+1, b=2y+1 \\ a+b=2x+2y+2=2(x+y+1)
	\end{split}
\]
So the sum of the two odd number is even
\end{homeworkProblem}

%7
\begin{homeworkProblem}
Suppose x+4 is odd, then \( \exists y \ x+4=2y+1 \ x+7=x+4+3=2y+1+3=2(y+2) \)\\
So x+7 is even.\\
On the other hand, suppose x+7 is even then \( \exists y \ x+7=2y \ x+4=x+7-3=2y-3=2(y-2)+1 \)\\
So x+4 is odd
\end{homeworkProblem}

%8
\begin{homeworkProblem}
\[
\begin{split}
A = \{1,2\}\\
B = \{2,3\}\\
A \setminus B = \{1\}\\
B \setminus A = \{3\}\\
\end{split}
\]
\end{homeworkProblem}

\end{document}


\documentclass[12pt]{article}

\include{preamble}

\title{Math 341 / 650 Spring 2019 \\ Midterm Examination Two}
\author{Professor Adam Kapelner}

\date{Tuesday, April 16, 2019}

\begin{document}
\maketitle

\noindent Full Name \line(1,0){410}

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using an unauthorized cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent I acknowledge and agree to uphold this Code of Academic Integrity. \\

\begin{center}
\line(1,0){250} ~~~ \line(1,0){100}\\
~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
\end{center}

\normalsize

\section*{Instructions}

This exam is seventy five minutes and closed-book. You are allowed \textbf{one} page (front and back) of a \qu{cheat sheet.} You may use a graphing calculator of your choice. Please read the questions carefully. If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in \textit{any} widely accepted mathematical notation which could be resolved to an exact or approximate number with the use of a computer. I advise you to skip problems marked \qu{[Extra Credit]} until you have finished the other questions on the exam, then loop back and plug in all the holes. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\input{R_equations_table}

\problem Uber is evaluating a new pool product for intercity ridesharing. They are first trying to estimate the demand in number of riders $\theta$ going from New York City to Philadelphia per weekday.

\begin{figure}[htp]
\centering
\includegraphics[width=3.5in]{nyc_philly.jpg}
\end{figure}

To estimate this demand, they have piloted the program among their VIP customers and collected the number of people who requested such pooled rides for $n = 20$ weekdays. The data is below:

\begin{verbatim}
       39 50 57 50 42 50 56 68 49 50 53 53 48 44 56 30 57 49 54 48
\end{verbatim}

\noindent Summary statistics for this data are: $\xbar = 50.15$ and $s = 7.809$.\pagebreak

\benum

\subquestionwithpoints{6} In modeling this dataset, assume $\mathcal{F}: X_1, \ldots, X_{20}~|~\theta \iid \poisson{\theta}$. Explain why this assumption is reasonable.\spc{6}

\subquestionwithpoints{6} Find the posterior distribution (including the values of the hyperparameters to two decimal places). List all assumptions explicitly. \spc{3}

\subquestionwithpoints{6} Using your answer from (b), provide a Bayesian point estimate of $\theta$. \spc{1}

\subquestionwithpoints{6} Uber will only turn a profit if there are 55 or more riders going from NYC to Philadelphia per weekday. Find the probability they will turn a profit using your answer from (b). \spc{2}

\subquestionwithpoints{6} Find the probability there are 55 or more riders going from NYC to Philadelphia tomorrow (Wednesday) using the data and whatever assumptions you made in (b). \spc{7}

\eenum

\problem Consider the geometric model $\mathcal{F} : \Xoneton~|~\theta \iid \text{Geom}(\theta) := (1 - \theta)^x \theta$, its PMF, $\theta \in \Theta = (0, 1)$, Supp$\bracks{X} = \braces{0, 1, 2, \ldots}$, $\expe{X} = \frac{1 - \theta}{\theta}$ and $\var{X} = \frac{1 - \theta}{\theta^2}$.

\benum

\subquestionwithpoints{6} Find $\cprob{X}{\theta}$ where $X$ refers to all data (i.e. $\Xoneton$). Simplify.\spc{4}

\subquestionwithpoints{6} Show that the beta distribution is conjugate for the geometric likelihood.\spc{5}

\subquestionwithpoints{9} Show that Jeffrey's Prior is a beta distribution. \spc{7}

\subquestionwithpoints{1} Is Jeffrey's prior you found in the previous question proper? Yes / no. \spc{9}

\subquestionwithpoints{5} [Extra Credit] Find $\cprob{X^*}{X}$ for one new sample (i.e. $n^* = 1$) and simplify. \spc{8}

\eenum

\problem This question is about building a model to understand the accuracy of this beverage-filling machine

\begin{figure}[htp]
\centering
\includegraphics[width=3.7in]{milk_filling.jpg}
\end{figure}

\noindent which fills 12oz plastic bottles. We decide to do an experiment and select $n = 50$ bottles at random and measure the amount of liquid in each bottle. Here are the volumes (in oz):

\begin{verbatim}
       10.76 11.02 11.62 10.20 12.03 12.18 12.06 11.42 11.93 10.68
       11.81 11.27 11.68 11.31 11.29 12.37 12.00 10.74 12.04 10.69
       11.82 11.50 11.48 11.81 11.24 11.67 11.02 11.52 11.98 11.99
       11.99 11.85 11.42 11.95 10.77 11.56 11.77 11.43 12.03 11.89
       11.50 11.75 11.30 12.26 12.22 12.94 10.80 12.01 11.61 12.35
\end{verbatim}

\noindent and sample statistics: $\xbar = 11.61$oz and $s = 0.533$oz. We will consider these measurements realizations from the model $\mathcal{F} : \Xoneton~|~\theta, \sigsq \iid \normnot{\theta}{\sigsq}$.
\benum

\subquestionwithpoints{8} The company is interested in testing if the bottles are being underfilled (i.e. less than 12oz). Even though it is unrealistic, assume that $\sigsq = s^2 = 0.533^2 = 0.284$. Use an uninformative prior and set the significant level of the test to be $\alpha = 5\%$. Calculate as exactly as you can.\spc{4}

\subquestionwithpoints{6} What is the probability a bottle selected at random will be underfilled? Leave your answer in terms of the \texttt{pnorm} function with arguments rounded to two decimal places. Do not solve explicitly.\spc{2}

\subquestionwithpoints{6} Design a prior centered at 12oz with the strength of 100 samples. Note: this is a standalone question.\spc{3}

\subquestionwithpoints{6} The company is interested understanding if the bottles are being filled too erratically. Even though it is unrealistic, assume the machine is filling accurately on average (i.e. $\theta  =12$oz exactly). Find a Bayesian estimate of $\sigsq$ explicitly to two decimal places (an answer using a function in Table~\ref{tab:eqs} is not acceptable here). I have precomputed $\sigsqhat_{MLE} = \frac{1}{50} \sum_{i=1}^{50} (x_i - 12)^2 = 0.4305.$ List your assumptions explicitly. \spc{10}

\subquestionwithpoints{6} Given the assumptions from the previous question of mean being known and an uninformative prior for $\sigsq$, find the probability a bottle selected at random will be overfilled (i.e. 14oz or more).\spc{10}

\eenum

\problem Below are theoretical questions that are independent of each other.

\benum

\subquestionwithpoints{6} If $X~|~\theta,n \sim \binomial{n}{\theta}$ where $n$ is known, the prior on theta is $\betanot{\alpha}{\beta}$ where $\alpha$ and $\beta$ are both $>0$ and non-integer and $X^*$ is the number of successes in the next $n^*$ trials, find the kernel of the posterior predictive distribution, i.e. $k(X^*~|~X, n)$. Simplify as much as possible.\spc{16}

\subquestionwithpoints{6} Compute the following integral as a function of $a$, $b$ and fundamental constants. To get full credit you must show and justify all steps. \\~\\
$\displaystyle\int_\reals e^{ax - bx^2} dx = $\spc{5}

\subquestionwithpoints{4}
\vspace{-1cm}
\begin{figure}[h]
\centering
\includegraphics[width=3.3in]{comic.jpg}
\end{figure}

Choose the best sentence completion. \qu{The comic above is an example of ...} \\~\\
(I) normal-normal conjugacy (II) using an informative prior  (III) using the uninformative Jeffrey's prior (IV) compound distribution (V) point estimation using $\thetahatmle$.

\eenum

\end{document}


\section{Bearing Selection}

This section details the process in selecting bearings from the SKF catalogue. However, it is up to you to do your own research and attend the lectures to understand and determine the most appropriate bearings for your design problem.

\subsection{Bearing Types and Locating Bearings}

\begin{framed}
    \vspace{1cm}
    \begin{center}
        \Large
        -- Own Research \& Lectures --
    \end{center}
    \vspace{1cm}
\end{framed}

%\marginnote{Deep-Groove Ball \\ \includegraphics[width=0.06\textwidth]{single-deep-groove-ball-bearing.png}}

%\vspace{3cm}

%\marginnote{Angular Contact Bearing \\ \includegraphics[width=0.07\textwidth]{angular-contact-bearing.png}}

%\vspace{3cm}

%\marginnote{Taper Roller \\ \includegraphics[width=0.07\textwidth]{taper-bearing.png}}

%\vspace{3cm}

%\marginnote{Cylindrical Roller \\ \includegraphics[width=0.07\textwidth]{cylindrical-roller-bearing.png}}

%\vspace{3cm}

%\marginnote{Y-Bearing \\ \includegraphics[width=0.2\textwidth]{y-bearing.png}}

%\vspace{3cm}

%\subsection{Locating Bearings}

%\begin{framed}
%  \vspace{2cm}
%    \begin{center}
%      {\fontsize{50}{60}\selectfont \faQuestion{}}\\
%      Research Required
%    \end{center}
%  \vspace{2cm}
%\end{framed}

%\marginnote{\it float}

%\marginnote{\it stepped shaft}

%\marginnote{\it fillet radius}

%\marginnote{\it spacer}

%\marginnote{\it undercut}

%\marginnote{\it pre-load}

\subsection{Bearing Calculations}

Once you have decided upon the bearing arrangement for your shaft, we can use SKF's bearing selection process to determine the size of bearing that is required. Manufacturers often provide a selection process for their bearings, which they have developed over years of experience in designing and manufacturing their products. The SKF process requires us to determines the basic dynamic \(C\) and static \(C_0\) load ratings that we require for our bearings, which are in accordance of ISO 281:2007 and ISO 76:2006, respectively.

Lets\marginnote{Basic Static Load Rating (\(C_0\))}  start with calculating the basic static load rating for a bearing. This can be calculated using Equation~(\ref{equ-basic-load-rating}) and is where the equivalent static load \(P_0\) is multiplied by a safety factor \(S_0\).

\begin{equation}
    C_0 = P_0 S_0
    \label{equ-basic-load-rating}
\end{equation}

Thus, we need to know both \(P_0\) and \(S_0\).

The\marginnote{Equivalent Static Load (\(P_0\))} calculation for \(P_0\) is dependent upon the type of bearing you're looking to selecting. In the case of a deep-groove ball bearing, \(P_0\) is calculated by:

\begin{equation}
    \text{Calculate } P_0 = 0.6F_r+0.5F_a \text{ then if, } P_0 \le F_r \text{ we set, }P_0 = F_r
\end{equation}

This\marginnote{Safety Factor (\(S_0\))} is then multiplied by the Safety Factor (\(S_0\)), which is selected based on the shafts operating condition.

\begin{itemize}
    \item 0.5 for smooth and shock free
    \item 1.0 for normal
    \item 1.5 for shock and vibration
\end{itemize}

Now\marginnote{Basic Dynamic Load Rating (\(C\))}  we have calculated \(C_0\), we can move onto the basic dynamic load raring (\(C\)), which is calculated through the re-arrangement of the following equation:

\begin{equation}
    L_{10} = \left(\frac{C}{P}\right)^p
    \label{equ-bearing-life}
\end{equation}

\noindent{} Where:

\begin{description}
    \item[\(L_{10}\)] Is the number of revolutions at constant speed that 90\% of bearings tested will complete or exceed before the first evidence of failure develops. This is usually standardised to 1,000,000 cycles in accordance with ISO 281:2007.
    \item[\(C\)] basic dynamic loading rating (\si{\kilo\newton})
    \item[\(P\)] equivalent dynamic bearing load (\si{\kilo\newton})
    \item[\(p\)] bearing factor
    \begin{itemize}
        \item Ball Bearing, \(p=3\)
        \item Roller Bearing, \(p=\frac{10}{3}\)
    \end{itemize}
\end{description}

Thus, to calculate \(C\), we need to know both \(L_{10}\) and \(P\).

To\marginnote{life rating (\(L_{10}\))} determine the life rating (in cycles),

\begin{equation}
    L_{10} = \text{Hours of Operation} \times \text{rpm} \times 60
\end{equation}

With\marginnote{Equivalent Dynamic Bearing Load (\(P\))} \(L_{10}\) now known, we can focus our attention onto \(P\). The calculation for \(P\) is dependent upon the type of bearing you're looking to selecting. In the case of a deep-groove ball bearing (SKF p.316).

\begin{equation}
    P = XF_r + YF_a
\end{equation}

To determine the coefficients \(X\) and \(Y\), we need to find the calculation factors \(f_0\) and \(e\).

From the catalogue, \(f_0\) is calculated using:

\begin{equation}
    f_0 = \frac{F_a}{C_0}
\end{equation}

And \(e\) can be found using the look-up table on page XX in the catalogue. In this case, \(e=0.22\). With these values, we can then decide on, which equivalent dynamic bearing load calculation we require (SKF p.316). One example is:

\begin{equation}
    \text{if, }\frac{F_a}{F_e}\le e \Rightarrow P=F_r \text{ else, } \frac{F_a}{F_e}\ge e \Rightarrow P=XF_r+YF_a
\end{equation}

Once we have \(P\), we can calculate \(C\) be re-arranging the life rating equation (\cref{equ-bearing-life}).

\begin{equation}
    \sqrt[p]{L_{10}}\times P = C
\end{equation}

With\marginnote{Finding a Bearing} \(C_0\) and \(C\) calculated, it is then just a case of looking through the bearing tables and finding a bearing that meets these criteria.

\subsection{Example: Deep Groove Ball Bearing Selection}

A deep groove ball bearing is needed to support a \SI{15}{\kilo\newton} radial and \SI{3}{\kilo\newton} axial loading rotating at \SI{30}{rpm}, on a shaft of \SI{30}{\milli\metre} diameter. The bearing will operate in a smooth-running environment without shock loading, and must be reliable for \SI{5000}{\hour} of operation.

From this description, we can quickly ascertain that:

\begin{itemize}
    \item Deep Groove Ball. Therefore, \(p=3\)
    \item Running speed \(\ge \SI{10}{rpm}\). Therefore, the dynamic load rating is required \(P=XF_r+YF_a\)
    \item Smooth running, vibration free and low shock loading. Therefore, the safety factor \(S_0\) is 1.
    \item \(L_{10} = 5000 \times 60 \times 30 = 9,000,000\) cycles
\end{itemize}

\marginnote{Equivalent Static Load (\(P_0\))} Using the SKF catalogue, the equivalent static bearing load is given by:

\begin{equation}
    P_0 = 0.6F_r+0.5F_a \text{ then if, } P_0 \le F_r \text{ we set, }P_0 = F_r
\end{equation}

\noindent{} Hence, for this case, \(P_0\) is:

\begin{equation}
    P_0=0.6F_r+0.5F_a = 0.6\times\SI{15}{\kilo\newton} + 0.5\times\SI{3}{\kilo\newton} = \SI{10.5}{\kilo\newton}
\end{equation}

\noindent{} As \(P_0 \le F_r \), we set \(P_0 = F_r = 15\si{\kilo\newton} \).

Now\marginnote{Basic Static Load Rating (\(C_0\))} we can calculate our static load rating \(C_0\) using:

\begin{equation}
    C_0=P_0S_0
\end{equation}

\noindent{} And for our case, \(C_0\) becomes:

\begin{equation}
    C_0= \SI{15}{\kilo\newton} \times 1 = \SI{15}{\kilo\newton}
\end{equation}

Having\marginnote{Equivalent Dynamic Load (\(P\))} calculated \(C_0\), we can move onto calculating the equivalent dynamic load \(P\) so that we can attain the basic dynamic load rating \(C\):

\begin{equation}
    P = XF_r + YF_a
\end{equation}

To determine the coefficients \(X\) and \(Y\), we need to find the calculation factors \(f_0\) and \(e\).

From the catalogue, \(f_0\) is calculated using:

\begin{equation}
    f_0 = \frac{F_a}{C_0} = \frac{3\si{\kilo\newton}}{15\si{\kilo\newton}} = 0.2
\end{equation}

And \(e\) can be found using the look-up table on p.315 in the catalogue. In this case, \(e=0.22\). With these values, we can then decide on, which equivalent dynamic bearing load calculation we require (SKF p.316).

\begin{equation}
    \text{if, }\frac{F_a}{F_r}\le e \Rightarrow P=F_r \text{ else, } \frac{F_a}{F_r}\ge e \Rightarrow P=XF_r+YF_a
\end{equation}

Therefore, in our case:

\begin{equation}
    \frac{F_a}{F_r}=\frac{3\si{\kilo\newton}}{15\si{\kilo\newton}}=0.2
\end{equation}

\noindent{} and this value is \(\le e\). Thus,

\begin{equation}
    P=F_r=15\si{\kilo\newton}
\end{equation}

Now\marginnote{Equivalent Dynamic Load Rating (\(C\))} we have all the information we need to determine the equivalent dynamic load rating \(C\) by re-arranging the life rating equation (Equation~\ref{equ-bearing-life}).

\begin{equation}
    \sqrt[p]{L_{10}}\times P = C = \SI{31.2}{\kilo\newton}
\end{equation}

Now looking through the available bearings in the catalogue and knowing we have to fit onto a \SI{30}{\milli\metre} shaft, two bearings are potentially suitable:

\begin{itemize}
    \item 6306 ETN9
    \item 6406
\end{itemize}

Looking into more detail of the designations of the bearings reveals that ETN9 means that the bearing is reinforced. This is likely to be an over-specification for our application. Therefore, we shall select 6406.

%\subsection{Bearing Selection -- Roller Bearing Example}

%A locating roller bearing is needed to support a 60\si{\kilo\newton} radial and 25\si{\kilo\newton} axial loading rotating at 100\si{rpm}, on a shaft of 75\si{\milli\metre} diameter. The bearing will operate in a normal environment, and must be reliable for 11,000\si{\hour} of operation.

%\begin{itemize}
%  \item Roller. Therefore, \(p=\frac{10}{3}\)
%  \item Running speed \(\ge 10\si{rpm}\). Therefore, the dynamic load rating is required.
%  \item Normal running environment. Therefore, the safety factor \(S_0\) is 1.
%  \item \(L_{10} = 11,000 \times 60 \times 100 = 66,000,000\) cycles
%\end{itemize}

%\marginnote{Basic Static Load Rating} [TO DO]

%\marginnote{Equivalent Dynamic Load \(P\)} Looking through the SKF catalogue, we identify the equation for \(P\) for locating bearings (p.594).

%\begin{equation}
%\text{If } \frac{F_a}{F_r} \leq e \text{ then } P=F_r \text{ else } P=0.92F_r+YF_a
%\end{equation}

%\begin{equation}
%  \frac{F_a}{F_r}=\frac{25\si{\kilo\newton}}{60\si{\kilo\newton}} = 0.417 > e
%\end{equation}

%To determine \(Y\), we look up the value in the respective SKF table (p.593). In this case, \(Y=0.6\). Thus, \(P\) becomes:

%\begin{equation}
%  P=0.92F_r+0.6F_a=70.2\si{\kilo\newton}
%\end{equation}

%\marginnote{Basic Dynamic Load Rating \(C\)} Now we have \(P\), we can calculate the basic dynamic load rating \(C\).

%\begin{equation}
%  \sqrt[p]{L_{10}}\times P = C = \sqrt[\frac{10}{3}]{66,0000,000}\times 70,200=246\si{\kilo\newton}
%\end{equation}

%Looking through the SKF tables, we can find a range of suitable bearings with NUP designated bearings able to take axial loads. Focusing on these bearings, two options are available to us:

%\begin{itemize}
%  \item NUP 135 ECP
%  \item NUP 2135 ECP
%\end{itemize}


\documentclass[main.tex]{subfiles}
\begin{document}

\subsection{Autocorrelation}

\marginpar{Tuesday\\ 2020-11-10, \\ compiled \\ \today}

Successive points in a chain are correlated, and the Monte Carlo error scales like \(1/\sqrt{N}\), where \(N\) is the number of \emph{uncorrelated} points.
How do we calculate the effective number of uncorrelated points?

The autocorrelation for points \(\tau \) steps apart is
%
\begin{align}
\rho_\theta (\tau ) = \frac{n}{n - \tau } \frac{\sum _{i=1}^{n-\tau } (\theta _i - \overline{\theta}) (\theta _{i+\tau } - \overline{\theta})}{\sum _{i=1}^{n-\tau } (\theta _i - \overline{\theta})^2}
\,.
\end{align}

Then, the variance of the parameter \(q\) is
%
\begin{align}
\var{\theta } = \frac{\tau _f}{N} \sigma_\theta^2
\,,
\end{align}
%
where \(\tau _f\) is the integrated autocorrelation time:
%
\begin{align}
\tau _f = \sum _{\tau =- \infty }^{+ \infty } \rho _\theta (\tau )
\approx 1 + 2 \sum _{\tau = 1}^{N} \rho _\theta (\tau )
\,.
\end{align}

So, \(N / \tau _f\) is the effective number of points.
A note on numerical stability: in computing the previous expression one must be careful to not extend the sum to a region in which the estimation of \(\rho _\theta (\tau )\) is imprecise: for very large \(\tau \) we have very few samples of these large delays.

It is best if the proposal distribution \(Q\) is as close as possible to the true distribution.
Preprocessing should include estimating the covariance matrix of the data, or even running a short chain.

Gibbs sampling does not work well if the parameters are heavily correlated, since it moves along the conditional distributions.

Examples of libraries.

\chapter{Real datasets}

\section{The CMB}

At roughly \SI{3e5}{yr} after the Big Bang the universe had a temperature of roughly \SI{3e3}{K}, and electrons and protons combined into hydrogen atoms.
Since the cross-section of Compton scattering is proportional to the inverse square mass of the particle, the optical depth of the gas decreased drastically: it became transparent.

We observe that light as a near-blackbody spectrum at \(T _{\text{CMB}} \approx \SI{2.7255}{K}\).
We see slight anisotropies, of the order of \(\Delta T / T \sim \num{e-5}\).

These fluctuations are very sensitive to inflationary cosmological parameters.

The quantum process which generates the anisotropies is inherently random. We must treat it as a random field.
Given a random field \(X(\vec{t})\) (where \(\vec{t}\) is a position vector) with a pdf \(P(X)\) we can compute its average value as
%
\begin{align}
\mathbb{E}_X(\vec{t}) = \int X(\vec{t}) P(X) \dd{X}
\,,
\end{align}
%
and its auto-covariance and auto-correlation functions:
%
\begin{align}
C(\vec{t}, \vec{s}) &= \text{cov} \qty( X(\vec{t}), X(\vec{s}))  \\
\rho (\vec{t}, \vec{s}) &= \frac{C(\vec{t}, \vec{s})}{\sqrt{C(\vec{t}, \vec{t}) C(\vec{s}, \vec{s})}}
\,.
\end{align}

The hypothesis of stationarity can be stated by saying that \(C(\vec{t}, \vec{s})\) is only a function of \(\vec{t}-\vec{s}\).
Isotropy means that it is only a function of \(\abs{\vec{t} - \vec{s}}\).

A Gaussian random field is such that the joint probability of the field attaining certain values at certain points is a multivariate Gaussian.

By homogeneity and isotropy many fields in cosmology can be treated as Gaussian, stationary and isotropic: they can then be described through their power spectrum alone.

We Fourier transform the field \(F(\vec{x})\) into \(F(\vec{k})\), and define the power spectrum \(P_F\) as
%
\begin{align}
\expval{F(\vec{k}_1) F(\vec{k}_2)} = (2 \pi )^3 P_F(k) \delta^{(D)} (\vec{k}_1 + \vec{k}_2)
\,,
\end{align}
%
where \(k = \abs{k_1 } = \abs{ k_2 }\).

By the \textbf{Wiener-Kintchine} theorem the power spectrum is the 3D Fourier transform of the two-point correlation function.

We must work in spherical harmonics:
%
\begin{align}
\frac{\Delta T}{T} (\theta , \varphi ) = \sum _{\ell>2} \sum _{\abs{m} \leq \ell} a_{\ell m} Y_{\ell m} (\theta ,\varphi )
\,.
\end{align}

The angular frequency \(\ell\) gives  us the angular scale of the oscillations: roughly speaking, \(\theta \sim \pi / \ell\).
Then we define the power spectrum as
%
\begin{align}
\expval{a^{*}_{\ell_1 m_1 } a_{\ell_2 m_2 }} = C_{\ell_1} \delta_{\ell_1 \ell_2 } \delta_{m_1 m_2 }
\,.
\end{align}

We skip the monopole and the dipole: the monopole is just an offset, and the Doppler is given by the motion of the Solar System through the galaxy, and of the Galaxy through the CMB reference.

\end{document}


\input{header.tex}

%\setlength{\headsep}{-10pt}
\setlength{\parskip}{0.2em}
%\setlength{\textheight}{11 in}
\setlength{\skip\footins}{20pt}

% opening
\title{Plotting large datasets, Part 2: Making movies}
\author{Colin Leach, March 2020}
\date{\vspace{-3ex}}

\hyphenpenalty=1000

\begin{document}

\maketitle

We are trying to follow a simulation through time, so movie files may have an advantage over static images.

The simplest approach is to make a series of 2D plots then link them in an mp4 file. The NASA movies are clearly much more sophisticated but we don't know how they were made.

\section{Within matplotlib}

There are various Animation classes in Matplotlib that can do this:
\url{https://matplotlib.org/api/animation_api.html}

It's a while since I did this so I don't have a simple working example. Google ``matplotlib animation'' and it will find many blog posts that may be useful.

A personal opinion: this is great for generating simple movies on the fly within Jupyter, for immediate display in the browser. For more serious work I prefer the ffmpeg approach in the next section. You may very well disagree, which is fine.

\section{Using ffmpeg}

Matplotlib can export the plot to a PNG or JPEG file and it's easy to write a python loop to produce a bunch of these with names like plot\_000.png, plot\_001.png, etc for the various snaps.

The (HUGE) video editing package ffmpeg can stitch these together into an mp4 movie file. The syntax is something like this (pretty short by ffmpeg standards):

\lstdefinestyle{py}{
	belowcaptionskip=1\baselineskip,
	breaklines=true,
	frame=L,
	xleftmargin=\parindent,
	language=Python,
	showstringspaces=false,
	basicstyle=\footnotesize\ttfamily,
	keywordstyle=\bfseries\color{green!40!black},
	commentstyle=\itshape\color{purple!40!black},
	identifierstyle=\color{blue},
	stringstyle=\color{orange},
}

\lstset{style=py}
\begin{lstlisting}
ffmpeg -r 10 -start_number 0 -s 1920x1080 -i plot_%03d.png -vcodec libx264 \
  -vf fps=25 -crf 25 -pix_fmt yuv420p my_movie.mp4
\end{lstlisting}

Steps:

\begin{itemize}
	\item Install ffmpeg if it's not already on your system. Linux/Mac users can probably get it from a repository, Windows users (or anyone) can get it from \url{https://ffmpeg.org/download.html}. It works well on Windows.
	\item Make the plot files with names having \textbf{consecutive numbering} and and \textbf{zero-padded to a constant length}. So using a list like 000, 001, 002... is fine, 000, 005, 010... will fail (not consecutive) and 8, 9, 10... will fail (not same length). Can you guess I learned that the hard way?
	\item In Python, the file names are easy to generate with f-strings:
	\begin{lstlisting}
	 fname = f'plot_{snap:03}.png'
	\end{lstlisting}
	The snap then is padded to 3 digits with leading zeros, and this matches the `\%03d' in the ffmepg command (after the -i switch, for input). To use non-consecutive snaps, keep a separate counter variable:
	\begin{lstlisting}
	for i, snap in enumerate(np.arange(0, 20, 5)):
		# get data for snap, then...
		fname = f'plot_{i:03}.png'
	\end{lstlisting}

	\item The command may take a few attempts to get right. Maybe best assembled in a text editor rather than directly in the command shell. It's really all one line, but line continuation characters are your friend: backslash \textbackslash\ for Linux (and Mac?), carat \^\ for Windows.
	\item Set start\_number for your first file. You can't specify last\_number or step\_by. It stops automatically when it fails to find the next highest number.
	\item Other options (-r, -vf, -crf) are in the ffmeg documentation (good luck!) and many StackExchange posts. I resorted to trial and error.
\end{itemize}

There are other gotchas to fall into. Obviously the $x$ and $y$ limits need to be hard-coded in matplotlib, otherwise the frames will jump about and make you cross-eyed.

Less obviously, the pixel height of every image must be an even number. Matplotlib doesn't think about that but I found a hack online which seems to work.

\begin{lstlisting}
# The simple figsize format is OK for static images:
# fig = plt.figure(figsize=(20,9))

# If saved files are to be used for animations with ffmpeg, row
# count must be an even number. This hack ensures that.
fig = plt.figure()
DPI = fig.get_dpi() # dots per inch of your display
fig.set_size_inches(1200.0/float(DPI),610.0/float(DPI))
\end{lstlisting}

DPI is 72 on my machine (your result may be different). Make sure your pixel count (1200, 610 in this case) uses even numbers and hope for the best.

\end{document}


\section{Beacon of Light}\label{prayer:beaconOfLight}
\textbf{Cost:} 100 CP\\
\textbf{Requirements:} Disciple of Avior, 3 Piety Points \\
\textbf{Active, Prayer, Weapon, Repeatable, Source(50 Gold)}\\
Pray for 8 AP. You become engulfed in a radiant glow, shedding bright light in a radius of \passus{6}.
You can then point at a creature of your choice, shooting a bolt of radiant light at them.
This costs another 2 AP, and ends the effect of the prayer, including the lighting.
The bolt is a ranged weapon attack with a range of \passus{12} that deals 1d12 radiant damage.\\
\\
Level Progression:\\
II: 500 CP, 7 Piety Points, you can shoot two bolts before the prayer ends\\
III: 1,000 CP, 12 Piety Points, you can shoot three bolts before the prayer ends\\
IV: 2,500 CP, 18 Piety Points, you can shoot four bolts before the prayer ends\\
V: 5,000 CP, 25 Piety Points, you can shoot five bolts before the prayer ends\\


\documentclass[letterpaper,10pt]{article}
\usepackage[margin=2cm]{geometry}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[colorlinks]{hyperref}

\newcommand{\panhline}{\begin{center}\rule{\textwidth}{1pt}\end{center}}

\title{\textbf{Linear Algebra}}
\author{Danish (Instructor), HMW-Alexander (Noter)}

\begin{document}

\maketitle

\panhline
\href{../index.html}{Back to Index}

\panhline
\tableofcontents

\section*{Resources}

\begin{itemize}
	\item \href{../../Lectures/R2_LinearAlgebra.pdf}{Lecture}
\end{itemize}

\panhline

\section{Vector Spaces}

A vector space ($V$) is a collection of objects called vectors, which may be added together and multiplied by numbers, called scalars in this context.

\section{Trace}

$$tr: \mathbb{R}^{n\times n} \rightarrow \mathbb{R}$$
$$tr(A)=\sum_{i=1}^{n}A_{ii}$$

\begin{itemize}
	\item $tr(A)=tr(A^T)$
	\item $tr(A+B)=tr(A)+tr(B)$
	\item $tr(AB)=tr(BA)$
\end{itemize}

\section{Norms}

A vector norm is any function $f: \mathbb{R}^n \rightarrow \mathbb{R}$ with:
\begin{itemize}
	\item $f(x)\geq 0$ and $f(x)=0 \Leftrightarrow x=0$
	\item $f(ax)=|a|f(x)$ for $a\in \mathbb{R}$
	\item $f(x+y)\leq f(x)+f(y)$
\end{itemize}

Norms of vectors:
\begin{itemize}
	\item $l_2$: $$||x||_2=\sqrt{x^Tx}=\sqrt{\sum{x_i^2}}$$
	\item $l_1$: $$||x||_1=\sum|x_i|$$
	\item $l_\infty$: $$||x||_\infty=\max(|x_i|)$$
\end{itemize}

Geometric interpretation:
\begin{figure}[!h]
	\centering
	\includegraphics[width=2cm]{./img/norms.png}
\end{figure}

Norms of Matrix:

$$||A||_p=\sup_{x\neq0}\frac{||A\vec{x}||_p}{||x||_p}$$
\begin{itemize}
	\item $||A_{m\times n}||_1=\max_{1\leq j \leq n}\sum_{i=1}^{m}|a_{ij}|$, which is simply the maximum absolute column sum of the matrix.
	\item $||A_{m\times n}||_\infty=\max_{1\leq i \leq m}\sum_{j=1}^{n}|a_{ij}|$, which is simply the maximum absolute row sum of the matrix.
	\item $||A_{m\times n}||_2 = \sqrt{\lambda_{max}(A^*A)}=\sigma_{max}(A)$ (spectral norm)
	\item $||A_{m\times n}||_2 \leq (\sum_{i=1}^{m}\sum_{j=1}^{n}|a_{ij}|^2)^{1/2}=||A||_F$ (Frobenious norm, $l_{2,2}$)
\end{itemize}
Generally, the subordinate matrix norm on $K_{m\times n}$ induced by $||\cdot||_\alpha$ on $K_n$, and $||\cdot||_\beta$ on $K_m$ as: $$||A||_{\alpha,\beta}=\max_{x\neq 0}\frac{||A\vec{x}||_\beta}{||\vec{x}||_\alpha}$$

\section{The Matrix Inverse}

$$AA^{-1}=I$$

$$A^{-1} \text{ exists } \Leftrightarrow Ax\neq 0 \text{ for all } x\neq 0$$

Properties:
\begin{itemize}
	\item $(A^{-1})^{-1}=A$
	\item $(AB)^{-1}=B^{-1}A^{-1}$
	\item $(A^T)^{-1}=(A^{-1})^T$
\end{itemize}

\section{Linear Independence and Rank}

$$\text{iff }\alpha_i=0, \sum_{i} \alpha_i \vec{x}_i = 0 \Rightarrow \text{ linear independence}$$

RREF (Reduced Row Echlon Form) to get rank of a matrix.

column rank = row rank

\section{Orthogonality}

$$\vec{x}^T\vec{y}=0$$

Orthonormal:$||\vec{x}||_2=1$

A matrix is orthogonal if all it's columns are orthonormal: $U^TU=I$, and its column vectors are linearly independent.

\section{Eigenvalues and Eigenvectors}

$$A\vec{x}=\lambda\vec{x}$$
$$det(\lambda I - A)=0$$

\section{Diagonalization}

For all eigenvectors and eigenvalues, construct:
$$AX=X\Lambda \Rightarrow A=X\lambda X^{-1}$$
If $X$ is invertible, then A is diagonalizable.

Properties of eigenvectors and eigenvalues:
\begin{itemize}
	\item $tr(A)=\sum_i{\lambda_i}$
	\item $det(A)=\prod_i{\lambda_i}$
	\item $rank(A)=$ number of non-zero eigenvalues.
	\item Eigenvalues of $A^{-1}$ are $1/\lambda_i$, and the eigenvectors keep same.
\end{itemize}

\end{document}