\documentclass[9pt, a4paper, oneside]{amsart}

\usepackage[final]{pdfpages}

\usepackage{enumitem}
\usepackage{parskip}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{multicol}
\pagestyle{fancy}

\newlist{questions}{enumerate}{1}
\setlist[questions, 1]{label = \bf Q.\arabic*., itemsep=1em}

\lhead{\scshape Apurva Nakade}
\rhead{\scshape Honors Single Variable Calculus}
\renewcommand*{\thepage}{\small\arabic{page}}
\title{Problem Set 05}

\begin{document}

\maketitle
\thispagestyle{fancy}

\section*{Part 1 - Differentiation}
\begin{questions}
	\item Do \textbf{Q.1} (parts (i) - (viii)) and \textbf{Q.3} from Chapter 10 of the book.

	\item If $ f$ is three times differentiable and $ f'(x) \neq 0$ the \textbf{Schwarzian derivative} of $ f$ at $ x$ is defined to be
	\begin{align*}
		\frak{D}f(x) & = \dfrac{f'''(x)}{f'(x)} - \dfrac{3}{2} \left(\dfrac{f''(x)}{f'(x)}\right)^2
	\end{align*}
	Show that $ \frak{D}g=0$ for the function $ g(x) = \dfrac{ax + b}{cx + d}$ with $ ad - bc \neq 0$.

	\item
	\begin{enumerate}
		\item A number $ a$ is called a \textbf{double root} of a polynomial $ f$ if $ f(x) = (x-a)^2 g(x)$ for some polynomial $ g$. Prove that $ a$ is a double root of $ f$ if and only if both $ f$ and $ f'$ vanish at $ a$.
		\item When does $ f(x) = ax^2 + bx + c$ have a double root? What does the condition say geometrically?
	\end{enumerate}

	\item
	\begin{enumerate}\item Try to prove the following formulae using the definition of derivative\footnote{You can use the following trigonometric identities
			\begin{align*}
				\sin(a + b) & = \sin a \cos b + \sin b \cos a \\
				\cos(a + b) & = \cos a \cos b - \sin a \sin b
			\end{align*}}
		\begin{align*}
			(\sin x)' = \cos x &   & (\cos x)' = -\sin x
		\end{align*}
		What identities \emph{should be true} (in terms of limits $ \lim \limits_{h \rightarrow 0}$) for the above formulae to hold?
		\item Look at the graphs of $ \sin x$ and $ \cos x$ near $ x=0$, and come up with a {heuristic argument} as to why these identities might true.
	\end{enumerate}
\end{questions}

\newpage
\section*{Part 2 - Inverse Functions}
\begin{questions}[resume]
	\item For this problem assume that $ x \neq 0$.
	\begin{enumerate}
		\item Using induction and product rule show that $ (x^n)' = n x^{n-1}$ when $ n$ is a non-negative integer.
		\item Using the quotient rule show that $ (x^n)' = n x^{n-1}$ when $ n$ is a negative integer.
		\item Using the fact that $ x^{1/n}$ is the inverse of $ x^n$ show that $ (x^n)' = n x^{n-1}$ when $ n = 1/m$ for some non-zero integer $ m$.
		\item Using chain rule show that $ (x^n)' = n x^{n-1}$ when $ n$ is a rational number.
		\item Can you think of some way of extending this to the case when $ n$ is an irrational number?
	\end{enumerate}

	\item Determine the derivatives of $ \sin^{-1}x$ and $ \tan^{-1}x$. (Later on we'll use these inverse functions to rigorously define $ \sin x$.)

	\item Suppose the functions $ f$ and $ g$ are increasing everywhere i.e. $ f(x) > f(y)$ and $ g(x) > g(y)$ for all $ x > y$.
	\begin{enumerate}
		\item Which of the functions $ f+g$, $ f.g$ and $ f \circ g$ are necessarily increasing.
		\item Show that $ f^{-1}$ is also an increasing function.
		\item Determine $ (f \circ g)^{-1}$ in terms of $ f^{-1}$ and $ g^{-1}$.
		\item Find $ g^{-1}$ in terms of $ f^{-1}$ if $ g(x) = 1+ f(x)$.
	\end{enumerate}

\end{questions}

\includepdf[pages=-]{Mathematical_Induction.pdf}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{\large{\textsc{In-Class 2: Linked Lists, Stacks and Queues}}}
\date{}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage[a4paper, portrait, margin=0.8in]{geometry}

\usepackage{listings}

\newcommand\perm[2][n]{\prescript{#1\mkern-2.5mu}{}P_{#2}}
\newcommand\comb[2][n]{\prescript{#1\mkern-0.5mu}{}C_{#2}}
\newcommand*{\field}[1]{\mathbb{#1}}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\newcommand{\Mod}[1]{\ (\text{mod}\ #1)}

\begin{document}

\maketitle

\begin{lstlisting}[language=Java]
Node {
    int data;
    Node next;
}

\end{lstlisting}

\noindent For each of the following problems, assume you are only given a \texttt{Node head} (or two), which is the head of a linked list. Focus on first getting a working solution, and then optimizing to a O(N) time O(1) space.

\begin{enumerate}

\setcounter{enumi}{1}

\item Find the length of a linked list.

\item Find and return the $n^{th}$ node from the end of a linked list (inputting 1 would return the last node. Inputting 2 would return the node before the last node. etc.).

\begin{figure}[h!]
\includegraphics[width=4cm]{Y-ShapedLinked-List}
\centering
\end{figure}

\item Given two singly linked lists, determine if and where they have an intersection point. Return the intersection point node. Return \texttt{null} if there is no intersection. For example, for the above linked list, the function would return the Node whose value is 15.

\end{enumerate}

\noindent Finally, some doubly-linked list problems!

\begin{enumerate}

\begin{lstlisting}[language=Java]
Node {
    int data;
    Node next, prev;
}
\end{lstlisting}

\setcounter{enumi}{8}

\item Given a DLL, print the elements in reverse order

\item Check if a DLL is a palindrome.

\end{enumerate}

\subsection*{}

Assumptions:

\begin{itemize}
  \item All stacks are listed from top to bottom.
  \item All problems can be solved using O(N) time and O(N) space unless noted otherwise.
  \item Stacks support: \texttt{push}, \texttt{pop}, \texttt{peek}, and \texttt{isEmpty}. They have no other methods.
\end{itemize}

\subsection*{}

\begin{enumerate}

\item Given a stack, return a stack with all the elements in reverse order

\item Given two integer sequences without duplicates, if the first is the push sequence of a stack, determine if the second is a possible pop sequence of the same stack. Ex: if \texttt{[1, 2, 3, 4, 5]} is a push sequence, \texttt{[4, 5, 3, 2, 1]} is a possible pop sequence (Push 1, 2, 3, 4. Pop 4. Push 5. Pop 5, 3, 2, 1), but \texttt{[4, 3, 5, 1, 2]} is not. Drawing an example stack illustration could be helpful.

\item Given a string of open and close parenthesis, determine the longest substring that contains a valid expression. eg
\begin{itemize}
  \item \texttt{"(()))("} would return 4.
  \item \texttt{"(()()))()"} would return 6.
  \item \texttt{"))(("} would return 0.
\end{itemize}
If you want an additional challenge, restrict yourself to using only one stack as additional storage.

\item Sort a stack in ascending order (with smallest items on top). The only extra memory you may use is one additional stack to hold items (you cannot use any arrays). Return the sorted stack. O(N\textsuperscript{2}) time.

\item Write a function to solve the Tower of Hanoi puzzle. Your function header should look like: \texttt{solveHanoi(Stack left, Stack middle, Stack right, int n)}, Discs are represented as integers, where smaller ints must be stacked on top of larger ints. \texttt{middle}, and \texttt{right} are empty to start, while \texttt{left} has \texttt{[1, 2, ..., n]} on the stack. Move all the ints over to \texttt{right} using only the four supported \texttt{Stack} operations. Larger ints may never be placed on top of smaller ints. Find an instructor and discuss the runtime complexity of your solution.
\end{enumerate}

\clearpage

\begin{lstlisting}[language=Python]
def swap(arr, i, j):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp

# Find either the min or max element's index, starting from start
def index_find(arr, func, start=0):
    m = start
    for i in range(start+1, len(arr)):
        if func=='min' and arr[i] < arr[m]:
            m = i
        if func=='max' and arr[i] > arr[m]:
            m = i
    return m

# O(N^2) time
def min_pairs(arr):
    f_map = {0: 'min', 1: 'max'}
    # alternate placing minimum and maximum elements
    for i in range(len(arr)):
        swap(arr, i, index_find(arr, f_map[i%2], i))
    return arr

# If you're curious, here's the NlogN time solution
def min_pairs_faster(arr):
    arr = sorted(arr) # O(NlogN)
    for i in range(1, len(arr)/2, 2): # O(N)
        swap(arr, i, len(arr)-i)
    return arr

def equal(head1, head2):

    while (head1 is not None) and (head2 is not None):
        if head1.data != head2.data:
            return False
        head1 = head1.next
        head2 = head2.next

    return (head1 is None and head2 is None)

def reverseList(head):
    prev = None
    while head:
        curr = head
        head = head.next
        curr.next = prev
        prev = curr
    return prev

def hasCycle(head):
    slow = head
    fast = head
    while fast!=None and fast.next!=None:
        slow = slow.next
        fast = fast.next.next
        if slow is fast:
            return True
    return False

def find_nth(head, n):
    while n > 0 and head is not None:
        head = head.next
        n -= 1
    return head

def find_nth_from_end(head, n):
    fast = head
    slow = head
    for i in range(n):
        fast = fast.next
    while fast is not None:
        fast = fast.next
        slow = slow.next
    return slow

def length(head):
    l = 0
    while head is not None:
        head = head.next
        l+=1
    return l

def palindrome(head):

    l = length(head)

    if l <= 1:
        return True

    left_of_middle = find_nth(head, l/2 - 1)
    # if linked list has odd number of elements, drop the middle element
    if (l % 2) == 1:
        left_of_middle.next = left_of_middle.next.next

    # reverse the back half of the linked list
    left_of_middle.next = reverseList(left_of_middle.next)

    # seperate the front and back half of the LL into seperate Lists
    middle = left_of_middle.next
    left_of_middle.next = None

    # check equality
    return equal(head, middle)

def intersection(head1, head2):
    l1 = length(head1)
    l2 = length(head2)
    long_list, short_list = head1, head2
    if l2 > l1:
        long_list, short_list = head2, head1
    long_list = find_nth(long_list, abs(l1-l2))
    while long_list is not None:
        if long_list == short_list:
            return long_list
        long_list = long_list.next
        short_list = short_list.next
    return None

def print_rev(head):
    last_elem = find_nth(length(head) - 1)
    while last_elem is not None:
        print last_elem
        last_elem = last_elem.prev

def palindrome_dll(head):
    l = length(head)
    if l <= 1:
        return True
    last_elem = find_nth(l - 1)
    while head is not None:
        if head != last_elem:
            return False
        head = head.next
        last_elem = last_elem.prev
    return True

#Start of Stacks and Queues

def merge_ll(head1, head2):
    # Base cases
    if head1 is None:
        return head2
    if head2 is None:
        return head1

    # Keep a pointer to the head of the new list
    new_ll = head1

    # Use the correct head as the first element
    if head2.data < head1.data:
        new_ll = head2
        head2 = head2.next
    else:
        head1 = head1.next

    # Iterate until both heads are None, selecting the smaller
    # element and adding onto the end of the list
    new_head = new_ll
    while not (head1 is None and head2 is None):
        if (head1 is None) or ((head2 is not None) and (head1.data > head2.data)):
            new_head.next = head2
            head2 = head2.next
        else:
            new_head.next = head1
            head1 = head1.next
        new_head = new_head.next
    return new_ll

def num_palindrome(i):
    j = i
    stack = Stack()
    while j != 0:
        stack.push(j%10)
        j/=10
    while not stack.isEmpty():
        if stack.pop() != i%10:
            return False
        i/=10
    return True

def reverse(stack):
    rev = Stack()
    while not stack.isEmpty():
        rev.push(stack.pop())
    return rev

def valid_expr(s):
    stack = Stack()
    for c in s:
        if not stack.isEmpty() and c == ')' and stack.peek() == '(':
            stack.pop()
        else:
            stack.push(c)
    return stack.isEmpty()

def is_pop_sequence(s1, s2):
    s3 = Stack()
    while not s1.isEmpty():
        s3.push(s1.pop())
        while not s3.isEmpty() and s3.peek() == s2.peek():
            s3.pop()
            s2.pop()
    return s3.isEmpty()

def sorted_stack_one_stack(stack):
    sorted_stack = Stack()
    while not stack.isEmpty():
        var = stack.pop()
        while not sorted_stack.isEmpty() and sorted_stack.peek() < var:
            stack.push(sorted_stack.pop())
        sorted_stack.push(var)
    return sorted_stack

def longest_valid_substring(s):
    stack = Stack()
    for i, c in enumerate(s):
        if not stack.isEmpty() and c == ')' and s[stack.peek()] == '(':
            stack.pop()
        else:
            stack.push(i)
    end_valid = len(s)
    start_valid = len(s)
    longest = 0

    for i in range(len(s) - 1, -1, -1):
        if not stack.isEmpty() and stack.peek() == i:
            end_valid = i
            stack.pop()
        else:
            start_valid = i
            if end_valid - start_valid > longest:
                longest = end_valid - start_valid

    return longest

def solveHanoiHelper(fr, to, other, n):
    if n == 1:
        to.push(fr.pop())
    else:
        solveHanoiHelper(fr, other, to, n-1)
        solveHanoiHelper(fr, to, other, 1)
        solveHanoiHelper(other, to, fr, n-1)

def solveHanoi(left, middle, right, n):
    solveHanoiHelper(left, right, middle, n)

\end{lstlisting}

\end{document}


This appendix will contain more details on R, things that the reader doesn't need to read on the first pass through.

\label{chap:more-about-r}

\subsection{Setting R Up}

REVISE THIS

\label{sec:setting-r-up}
You can skip this section if you want to jump ahead to read about how R works.

\index{C}{R software!installation}

Better, though, if you try out the commands as they are shown, using
the R software on your own computer.

To do this, you will need to start the R software.  If R is not
already installed on your computer (this is likely if you are using
your own computer and have never used R before), the first step is to
install it.

If you are used to installing software, you will find R follows the
usual pattern.
\begin{enumerate}
\item Use a web browser to go to \url{www.r-project.org}. Select
the Download/CRAN link on the left of the page. This will bring you to
a list of download sites.

\item  Choose one in your own region of the
world.  This will bring up a page with a choice of Linux, Mac OS X, or
Windows.  Choose whichever is appropriate for your computer.

\item  For Windows: choose the link for the ``base'' distribution, and
  then download the link that looks like ``R-2.9.0-win32.exe.''  The
  name may be slighly different, depending on what new versions have
  been released.  Run this program, accepting the defaults.

  For Macintosh, follow the link that looks like ``R-2.9.0.dmg'' and
  follow the instructions.  Again, the name may be slightly different,
  depending on what new versions have been released.

  If you are using Linux, you probably don't need any instructions on
  how to install software.
\end{enumerate}

\subsubsection{Defining your own operators}

Occasionally, you may need to define your own operators.  This is
convenient if you need to repeat an operation many times or if you
need to define a mathematical function.
\index{P}{function@\texttt{function}}
\index{P}{R Syntax!function@\texttt{function}}

It's important to keep in mind the difference between an operator and
a command.  A command is an instruction to perform a particular
computation on a particular input argument or set of input arguments.
The input arguments always have to be values, though of course you can
refer to the value by giving the name of an object that has already
been assigned a value.

In contrast, in defining an operator, you can treat the arguments
abstractly; just a name without a value having been assigned.  To
illustrate, here is a command that creates the mathematical
function $f(x) = 3 x^2 + 2$ and stores it in an operator named \code{f}:
\begin{Schunk}
\begin{Sinput}
> f = function(x) {
     3 * x^2 + 2
 }
\end{Sinput}
\end{Schunk}
One you have defined the function, you can invoke it in the standard
way.  For example:
\begin{Schunk}
\begin{Sinput}
> f(3)
\end{Sinput}
\begin{Soutput}
[1] 29
\end{Soutput}
\begin{Sinput}
> f(10)
\end{Sinput}
\begin{Soutput}
[1] 302
\end{Soutput}
\end{Schunk}

\index{C}{syntax!function definition}
There are some novel features to the syntax used to define a new
operator.  First, the arguments to \code{function} aren't treated as
values but as pure names.  Second, the contents of the
curly braces \verb+{+ and \verb+}+ ---  the
function contents --- are the commands that will be
evaluated when the function is invoked.

It doesn't matter what names you use in the function contents so long
as they match the names used in the arguments to \code{function}.  For
example, here is another operator, called \code{g}, that will perform
exactly the same computation as \code{f} when invoked:
\begin{Schunk}
\begin{Sinput}
> g = function(marge) {
     3 * marge^2 + 2
 }
\end{Sinput}
\end{Schunk}

\index{C}{invoking an operator}
When you invoke an operator, the interpreter carries out several
steps.  Consider the invocation
\begin{Schunk}
\begin{Sinput}
> g(7)
\end{Sinput}
\begin{Soutput}
[1] 149
\end{Soutput}
\end{Schunk}
In carrying out this command, the interpreter will:
\begin{enumerate}
\item Temporarily define or redefine an object \code{marge} that has
  the value 7.
\item Execute the function contents.
\item Return the value of these contents as the return value of the
  command.
\item Discard the definition or redefinition in (1).
\end{enumerate}

Operators can have more than one argument.  For instance, here is an
operator \code{hypotenuse} that computes the length of the hypotenuse
of a right triangle given the lengths of the legs
\begin{Schunk}
\begin{Sinput}
> hypotenuse = function(a, b) {
     sqrt(a^2 + b^2)
 }
\end{Sinput}
\end{Schunk}

When programmers create new operators that they expect to use on many
different occasions, they put the commands
to define the operators into a text file called a
\newword{source file}.
This file can be read into R using a special operator, called
\code{source} that causes the commands to be executed, thereby
defining the new operators.
\index{P}{source@\texttt{source}}
\index{P}{R System!source@\texttt{source}}

\subsubsection{Saving and Documenting Your Work}

\index{P}{Saving your work in R}
\index{P}{R System!saving your work}

Up to now, the commands used as examples have been simple one-line
statements.  As you work further, you will build more elaborate
computations by combining simpler ones.  It will become important to
be able to document what you have done, providing a record so that
others can confirm your results and so that you and others can modify
your work as needed.

One way in which a record is created of your interaction with the
computer is the dialog in the interpreter console itself.  In some
ways this is analogous to a document created by a word processor: for
example, you can copy the contents and paste it into another document.

But the idea of dialog-as-document is flawed.  For example, in a
word-processor, when you correct a mistake the old version is erased.
But in the R dialog, to fix a mistake you give a new command --- the
old, mistaken command is still there in the dialog.

You should keep in mind that there are several different components,
some of which are more appropriate than others for your documentation.

\begin{description}
\item[Your Commands]  The commands that you execute are what defines
  the computation being performed.  These commands themselves are a
  valuable form of documentation.

\item[The objects you create] These objects, and the values that are
  stored in them, reflect the \newword{state} of the computation.  If
  you want to pick up on your work where you left off, you can save
  these objects.  This is called ``saving the \newword{workspace}.''

\item[Side effects]  This refers to the output printed by the
  interpreter and plots.  Sometimes you will want to include this in your
  documentation, but usually just select elements.\index{C}{side effects}
\end{description}

\subsection{Using Customizations to R}

\label{sec:R-customizations}

\index{C}{ISM.Rdata}
\index{P}{ISM.Rdata}
\index{P}{R System!ISM.Rdata workspace}

One of the features that makes R so powerful is that new commands can easily be
added to the system.  This makes it possible, for instance, to
customize the software to make routine tasks easier.  Such
customizations have been written specifically for the people following
this book.  Most of them are contained in the \texttt{mosaic}
package.

To use the commands in \texttt{mosaic}, you need to instruct R to
``load'' the package.  This can be done with the following command:
\begin{Schunk}
\begin{Sinput}
> require(mosaic)
\end{Sinput}
\end{Schunk}

If you get an error message, it is likely because the package has not
yet been installed on your system.  Installation is a one-time process
described in Appendix \ref{appendix:installing-R}.

\subsubsection{Logical Data and Logical Operators}

\index{C}{logical!operators|(}
\index{C}{data!selecting subsets}
Many computations involve selections of subsets of data that meet some
criterion.  For example, in studying the health of newborn babies, you
might want to focus only on those below a certain birthweight or
perhaps those babies whose mother smoked during pregnancy.  The
question of whether the case satisfies the criterion boils down to a
yes-or-no answer.

Logic is the study of valid inference; it is intimately tied up with
the idea of truth versus falsehood.  In computer languages,
\newworddef{logical data}{logical!data type} refers to a type of data that can represent
whether something is true or false.  To illustrate, consider the
simple sequence 1 to 7
\begin{Schunk}
\begin{Sinput}
> x = seq(1, 7)
\end{Sinput}
\end{Schunk}
Now a simple question about the values in \code{x}: Are any of them
greater than $\pi$?  Here's how you can ask that question:
\begin{Schunk}
\begin{Sinput}
> x > pi
\end{Sinput}
\begin{Soutput}
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
\end{Soutput}
\end{Schunk}

A computer command like \code{x > pi} is not the same as an
algebraic statement like $x > \pi$.  The algebraic statement describes
the relationship between $x$ and $\pi$, namely that $x$ is greater than
$\pi$.  The computer command asks a question: Is the value of
\code{x} greater than the value of \code{pi}?  Asking this question
invokes a computation; the returned value is the answer to the
question, either \code{TRUE} or \code{FALSE}.

Here are some of the operators for asking such questions:

\index{C}{comparison operators}
\index{C}{logical!comparison operator}
\index{P}{.@\texttt{==}}
\index{C}{character string!comparison}
\index{P}{R Syntax!.@\texttt{==}}

\bigskip
\centerline{\begin{tabular}{ll}
\code{x < y} & Is \code{x} less than \code{y}?\\
\code{x <= y} & Is \code{x} less than or equal to \code{y}?\\
\code{x > y} & Is \code{x} greater than \code{y}?\\
\code{x >= y} & Is \code{x} greater than  or equal to \code{y}?\\
\code{x == y} & Is \code{x} equal to \code{y}?\\
\code{x != y} & Is \code{x} unequal to \code{y}?\\
\end{tabular}}
\bigskip
Notice the double equal signs in \code{x == y}.  A single equal sign
would be the assignment operator.

\index{P}{comparison!operators}

Mostly, these comparison operators apply to numbers.  The \code{==}
and \code{!=} operators also apply to character strings.  To
illustrate, I'll define two objects \code{v} and \code{w}:
\begin{Schunk}
\begin{Sinput}
> v = "hello"
> w = seq(1, 15, by = 3)
> w
\end{Sinput}
\begin{Soutput}
[1]  1  4  7 10 13
\end{Soutput}
\begin{Sinput}
> w < 12
\end{Sinput}
\begin{Soutput}
[1]  TRUE  TRUE  TRUE  TRUE FALSE
\end{Soutput}
\begin{Sinput}
> w > 7
\end{Sinput}
\begin{Soutput}
[1] FALSE FALSE FALSE  TRUE  TRUE
\end{Soutput}
\begin{Sinput}
> w != 4
\end{Sinput}
\begin{Soutput}
[1]  TRUE FALSE  TRUE  TRUE  TRUE
\end{Soutput}
\begin{Sinput}
> v == "goodbye"
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\begin{Sinput}
> v != "hi"
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
> v == "hello"
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
> v == "Hello"
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{Schunk}
The \code{FALSE} in the last line results from taking into account the
difference between upper-case and lower-case letters.

\index{P}{R Syntax!true@\texttt{TRUE} \& \texttt{FALSE}}
\index{P}{false@\texttt{FALSE}}
\index{P}{true@\texttt{TRUE}}
\index{P}{numerical comparison}

Sometimes you need to combine more than one logical result.  For
example, to ask, ``Is \code{w} between 7 and 12?'' involves combining
two separate questions: ``Is \code{w} greater than 7 AND
is it less than 12?''  In the computer language, this question would
be stated thus:
\begin{Schunk}
\begin{Sinput}
> w > 7 & w < 12
\end{Sinput}
\begin{Soutput}
[1] FALSE FALSE FALSE  TRUE FALSE
\end{Soutput}
\end{Schunk}

There are also logical operators for ``or'' and ``not''.  For
instance, you might ask whether \code{w} is less than 5 OR greater
then 9:
\begin{Schunk}
\begin{Sinput}
> w < 5 | w > 9
\end{Sinput}
\begin{Soutput}
[1]  TRUE  TRUE FALSE  TRUE  TRUE
\end{Soutput}
\end{Schunk}
The ``not'' operator just flips \code{TRUE} and \code{FALSE}:
\begin{Schunk}
\begin{Sinput}
> !(w < 5 | w > 9)
\end{Sinput}
\begin{Soutput}
[1] FALSE FALSE  TRUE FALSE FALSE
\end{Soutput}
\end{Schunk}
As this example illustrates, you can group logical operations in just
the same way as arithmetic operations.

\index{C}{logical!operators|)}

\subsubsection{Missing Data}

\index{C}{missing data}
\index{C}{data!missing}
\index{C}{NA, missing data}
\index{P}{NA@\texttt{NA}, missing data}
\index{P}{Data!missing (\texttt{NA})}
When recording data from an experiment or an observational study, it
sometimes happens that a particular measurement can't be made or is
lost or is otherwise unavailable.  In R, such \newword{missing data}
can be recorded with the special code \code{NA}.  As you might expect,
arithmetic and other operations on missing data can't be sensibly
performed: giving NA as an input produces NA as an output.
\begin{Schunk}
\begin{Sinput}
> 7 == NA
\end{Sinput}
\begin{Soutput}
[1] NA
\end{Soutput}
\begin{Sinput}
> NA == "Hello"
\end{Sinput}
\begin{Soutput}
[1] NA
\end{Soutput}
\begin{Sinput}
> NA == NA
\end{Sinput}
\begin{Soutput}
[1] NA
\end{Soutput}
\end{Schunk}

In order to test whether there is missing data, a special operator
\code{is.na} can be used:
\begin{Schunk}
\begin{Sinput}
> is.na(NA)
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\end{Schunk}
\index{P}{is.na@\texttt{is.na}}
\index{P}{Data!isna@\texttt{is.na}}
\index{P}{missing data!is.na@\texttt{is.na}}

\subsubsection{Collections}

\index{P}{collections!c@\texttt{c}}
\index{P}{c@\texttt{c}}
\index{P}{Data!small collections with \texttt{c}}
R can work with collections of numbers and
character strings.  Some operators work on each item in the
collection, while others combine the items together in some way.  To
illustrate, I'll define three small collections, \code{x}, \code{y},
and \code{fruits}:
\begin{Schunk}
\begin{Sinput}
> x = seq(1, 7)
> x
\end{Sinput}
\begin{Soutput}
[1] 1 2 3 4 5 6 7
\end{Soutput}
\begin{Sinput}
> y = c(7, 8, 9)
> y
\end{Sinput}
\begin{Soutput}
[1] 7 8 9
\end{Soutput}
\begin{Sinput}
> fruits = c("apple", "berry", "cherry")
> fruits
\end{Sinput}
\begin{Soutput}
[1] "apple"  "berry"  "cherry"
\end{Soutput}
\end{Schunk}
The \code{c} operator used in defining \code{y} and \code{fruits} is
useful for creating a small collection ``by hand.''  Often, however,
collections will be created by reading in data from a file or using
some other operator.  I'll introduce these as needed for specific tasks.

\index{C}{arithmetic!on collections}
\index{P}{arithmetic!on collections}
\index{P}{comparison!on collections}

Arithmetic and comparison operators often work item-by-item on the
collection.  For example:
\begin{Schunk}
\begin{Sinput}
> x + 100
\end{Sinput}
\begin{Soutput}
[1] 101 102 103 104 105 106 107
\end{Soutput}
\begin{Sinput}
> sqrt(y)
\end{Sinput}
\begin{Soutput}
[1] 2.65 2.83 3.00
\end{Soutput}
\begin{Sinput}
> fruits == "cherry"
\end{Sinput}
\begin{Soutput}
[1] FALSE FALSE  TRUE
\end{Soutput}
\end{Schunk}
If the operator involves two collections, they have to be the same
size, or R will reuse the smaller collection to match the size of the
larger one:
\begin{Schunk}
\begin{Sinput}
> x == y
\end{Sinput}
\begin{Soutput}
[1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
\end{Soutput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Warning message:
In trellis.par.set(theme = col.mosaic()) :
  Note: The default device has been opened to honour attempt to modify trellis settings
\end{Soutput}
\end{Schunk}
The warning message is displayed when some aspect of the computation
is deemed suspect or odd.  Pay attention to such messages since they
may signal that the computation the interpreter carried out is not the
one you intended.

It's usually obvious what sorts of operators will combine the items
of the collection rather than working on them item by item.  Here are
some examples:
\begin{Schunk}
\begin{Sinput}
> x
\end{Sinput}
\begin{Soutput}
[1] 1 2 3 4 5 6 7
\end{Soutput}
\begin{Sinput}
> y
\end{Sinput}
\begin{Soutput}
[1] 7 8 9
\end{Soutput}
\begin{Sinput}
> mean(x)
\end{Sinput}
\begin{Soutput}
mean
   4
\end{Soutput}
\begin{Sinput}
> median(y)
\end{Sinput}
\begin{Soutput}
[1] 8
\end{Soutput}
\begin{Sinput}
> min(x)
\end{Sinput}
\begin{Soutput}
[1] 1
\end{Soutput}
\begin{Sinput}
> max(x)
\end{Sinput}
\begin{Soutput}
[1] 7
\end{Soutput}
\begin{Sinput}
> sum(x)
\end{Sinput}
\begin{Soutput}
[1] 28
\end{Soutput}
\begin{Sinput}
> any(fruits == "cherry")
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
> all(fruits == "cherry")
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{Schunk}

\index{P}{sum@\texttt{sum}}

The \code{length} operator tells how many items there are in the
collection: \index{P}{length}
\begin{Schunk}
\begin{Sinput}
> length(x)
\end{Sinput}
\begin{Soutput}
[1] 7
\end{Soutput}
\begin{Sinput}
> length(y)
\end{Sinput}
\begin{Soutput}
[1] 3
\end{Soutput}
\begin{Sinput}
> length(fruits)
\end{Sinput}
\begin{Soutput}
[1] 3
\end{Soutput}
\end{Schunk}

When there are too many items in a collection to display conveniently
in one line, the R interpreter will break up the display over multiple
lines.
\begin{Schunk}
\begin{Sinput}
> seq(3, 19)
\end{Sinput}
\begin{Soutput}
 [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
\end{Soutput}
\end{Schunk}
At the start of each line, the number in brackets tells the
index of the item that starts that line.  In the above, for instance,
the item 3 is displayed following a \code{[1]} because 3 is the first
item in the collection.  Similarly, the \code{[10]} indicates that the
item 12 is the tenth item in the collection.  The brackets are just
for display purposes; they are not part of the collection itself.

\section{Stuff about Characters}

 Sometimes, you will want to convert
non-text items to text in order to display them in graphs.  There is a
special operator, \code{as.character} for doing this:
\begin{Schunk}
\begin{Sinput}
> as.character(3)
\end{Sinput}
\begin{Soutput}
[1] "3"
\end{Soutput}
\end{Schunk}
The quotes in the output show that it is character type rather then
numeric type.  This isn't terribly important to the human reader, but
the computer regards \code{"3"} as a different thing than \code{3}.
For instance, you can do arithmetic on numbers but not on characters.
\index{P}{conversion!numeric to character}
\index{P}{R Syntax!character text}
\begin{Schunk}
\begin{Sinput}
> 3 + 2
\end{Sinput}
\begin{Soutput}
[1] 5
\end{Soutput}
\end{Schunk}
\begin{Schunk}
\begin{Sinput}
> as.character(3) + 2
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Error in as.character(3) + 2 : non-numeric argument to binary operator
\end{Soutput}
\end{Schunk}
\index{C}{character string!object type)}
\index{C}{data!character)}

\subsection{Technical Notes: Missing Data}

\index{C}{missing data}

Occasionally, you may have a data frame with missing data.  The
\code{lm} program will handle this sensibly by excluding those cases
where one or more of the variables used in the model are missing.
Those cases with missing data will show up in the lists of residuals
and fitted values as NAs. (This depends on the
setting of the \code{na.action} argument to \code{lm}.  In the ISM
library, this has been set to \code{na.exclude}.)
\index{P}{missing data}

The NAs can cause a problem in calculations involving the residuals or
the fitted values.  For instance, suppose you have constructed a model
called \code{mymodel} fitted to a data frame with missing data.  Here
is the sum of squares of the fitted values:
\begin{verbatim}
> sum( fitted(mymodel)^2 )
[1] NA
\end{verbatim}
Any NAs in the fitted values propagate through to the overall sum.

\index{P}{na.rm@\texttt{na.rm=}}
\index{P}{Data!na.rm@\texttt{na.rm=}}

There are a couple of ways to deal with this:
\begin{verbatim}
> sum( fitted(mymodel)^2, na.rm=TRUE )
[1] 683434.3
> sum( na.omit(fitted(mymodel))^2 )
[1] 683434.3
\end{verbatim}

\index{P}{na.omit@\texttt{na.omit}}
\index{P}{Data!na.omit@\texttt{na.omit}}

You can also delete any cases with missing data from the data frame
{\em before} fitting the model:
\begin{verbatim}
> cleaned = na.omit(swim)
\end{verbatim}
Be careful, however, since this will exclude any cases that have any
missing variables, even if those variables are not involved in the model.

\index{P}{outlier@\texttt{outlier}*}
\index{P}{Descriptive Statistics!outlier@\texttt{outlier}*}
\index{C}{residual!outliers}
Sometimes it's helpful to look for outliers in the residuals, and to plot
the residuals versus the fitted model values or versus explanatory variables.
For instance:
<<>>=
bwplot( as.numeric(resid(mod1)) )
subset(swim, outlier(resid(mod1)))
xyplot( resid(mod1) ~ fitted(mod1) )
xyplot( resid(mod1) ~ year, data=swim)
@
(Technical note: The \code{as.numeric} operator translates the
residuals to a format that \code{bwplot} will work with.)
\index{P}{as.numeric@\texttt{as.numeric}}
\index{P}{Data!as.numeric@\texttt{as.numeric}}


\documentclass{article}

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

\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\ : \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

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

\setlength\parindent{0pt}

%
% Create Problem Sections
%
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\bes}{\begin{equation*}}
\newcommand{\ees}{\end{equation*}}
\newcommand{\bea}{\begin{flalign*}}
\newcommand{\eea}{\end{flalign*}}

\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}{Assignment\ \#2}
\newcommand{\hmwkDueDate}{Due on 4th February, 2019}
\newcommand{\hmwkClass}{Advanced Statistical Mechanics}
\newcommand{\hmwkClassTime}{}
\newcommand{\hmwkClassInstructor}{}
\newcommand{\hmwkAuthorName}{\textbf{Aditya Vijaykumar}}

%
% Title Page
%

\title{
    %\vspace{2in}
    \textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
    \normalsize\vspace{0.1in}\small{\hmwkDueDate\ }\\
%    \vspace{3in}
}

\author{\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
(\textbf{Acknowledgements} - I would like to thank Aditya Sharma and Junaid Majeed for discussions.)

\begin{homeworkProblem}[1]
	The two-particle Virial Coefficient $ b_2 $ is given by,
	\begin{align*}
	b_2 &= \int \dd^d \va{q}_1 \dd^d \va{q}_2 U(\va{q}_1  - \va{q}_2) \\
	&= A S_{d-1}^2 \int \dd q_1  \dd{q_2}  q_1^{d-1} q_2^{d-1}  \dfrac{1}{\abs{q_1 - q_2}^\sigma}
	\end{align*}
\end{homeworkProblem}

\begin{homeworkProblem}[2]
	\textcolor{red}{Do Part (a)}\\
	\textbf{Part (b)}\\
	Let's denote the Vandermonde determinant by $ D_n $,
	\begin{align*}
	D_n = \mdet{1  & x_1 & x_1^2 & \ldots & x_1^{n-1}\\ 1  & x_2 & x_2^2 & \ldots & x_2^{n-1} \\ \vdots & \vdots & \vdots & \vdots & \vdots \\ 1  & x_n & x_n^2 & \ldots & x_n^{n-1}}
	\end{align*}
	We prove the required statement by using row and column operations. We first use $ R_n \rightarrow R_n - R_{n-1}, n=2,3 \ldots, n $. We then have,
	\begin{align*}
	D_n = \mdet{1  & x_1 & x_1^2 & \ldots & x_1^{n-1}\\ 0  & x_2 - x_1 & x_2^2 - x_1^2 & \ldots & x_2^{n-1} - x_1^{n-1} \\ \vdots & \vdots & \vdots & \vdots & \vdots \\ 0  & x_n - x_{1} & x_n^2 - x_{1}^2 & \ldots & x_n^{n-1} - x_{1}^{n-1}}
	\end{align*}
	We now proceed to make the topmost row elements $ 0 $, save for the first element.
	We use $ C_n \rightarrow C_n - x_1 C_{n-1}, n=2,3 \ldots, n $,

	\begin{align*}
	D_n &= \mdet{1  & 0 & 0& \ldots & 0\\ 0  & x_2 - x_1 & x_2(x_2 - x_1) & \ldots & x_2^{n-2}(x_2 - x_1) \\ \vdots & \vdots & \vdots & \vdots & \vdots \\ 0  & x_n - x_{1} & x_n(x_n - x_{1}) & \ldots & x_n^{n-2}(x_n - x_{1}) } \\
	&=\mdet{ x_2 - x_1 & x_2(x_2 - x_1) & \ldots & x_2^{n-2}(x_2 - x_1) \\ \vdots & \vdots & \vdots & \vdots & \vdots \\  x_n - x_{1} & x_n(x_n - x_{1}) & \ldots & x_n^{n-2}(x_n - x_{1}) } \\
	&=\qty(\prod_{i=2}^{n} x_i - x_{1}) \mdet{1  & x_2 & x_2^2 & \ldots & x_2^{n-2}\\ 1  & x_3 & x_3^2 & \ldots & x_3^{n-2} \\ \vdots & \vdots & \vdots & \vdots & \vdots \\ 1  & x_n & x_n^2 & \ldots & x_n^{n-2}} \\
	\end{align*}

	\begin{align*}
	\implies D_n &= \qty(\prod_{i=2}^{n} x_i - x_{1}) D_{n-1} \\
	D_n &= \qty(\prod_{i=2}^{n} x_i - x_{1}) \qty(\prod_{i=3}^{n-1} x_i - x_{2}) D_{n-2}\\
	&=  \qty(\prod_{i=2}^{n} x_i - x_{1}) \qty(\prod_{i=3}^{n-1} x_i - x_{2}) \ldots D_2\\
	D_n &= \prod_{1\le j<i \le n} x_i - x_j
	\end{align*}
	Hence Proved.

\end{homeworkProblem}

\begin{homeworkProblem}[4]
	For $ N  $ particles in a harmonic trap,
	\begin{align*}
	\psi(x_1, x_2 , \ldots x_N) = \dfrac{1}{\sqrt{N!}} \det\phi_j (x_j) = \dfrac{1}{N!} \det A_{ij}  \\
	\qq{where} \phi_i(x) = \qty(\dfrac{a^2}{\pi})^{1/4} \dfrac{1}{\sqrt{2^i i!}} H_i(a x) e^{-a^2 x^2/2} \qq{,} a^2 = \dfrac{m \omega}{\hbar}
	\end{align*}

	The probability density is,
	\begin{align*}
	P(\{ x_i\}) &= \dfrac{1}{N!} \det A^T A  = \dfrac{1}{N!} \det K
	\end{align*}
	We define the average number density as,
	\begin{align*}
	\ev{\rho(x)} &= \sum_i \ev{\dfrac{1}{N} \delta (x- x_i)}\\
	&= \dfrac{1}{N}  \sum_i \int \prod_j \dd{x_j} \delta (x- x_i) P(\{x_k\})
	\end{align*}
	Let's have a closer look at the integral above. The integral over the delta function with replace the $ x_i $ in $ P(\{x_k\}) $ with $ x $. Expanding the sum over $ i $ will give us $ N $ terms, each having one argument replaced by $ x $. But we know that $ P(x_1, x_2) = P(x_2, x_1)$, and hence we can relabel the terms in the summation, and get the following expression,
	\begin{align*}
	\ev{\rho(x)} &= \int \prod_{j=1}^{N-1} \dd{x_j}  P(x, x_1, x_2 , \ldots, x_{N-1})\\
	&= \int \prod_{j=1}^{N-1} \dd{x_j} \dfrac{1}{N!} \det K
	\end{align*}
\end{homeworkProblem}

\end{document}


\chapter{Using JAGS}
JAGS stands for ``Just Another Gibbs Sampler''. JAGS is a computer program that
allows the user
to implement Markov Chain Monte Carlo (MCMC) on fairly complicated problems
very quickly. The ``Gibbs Sampling'' mentioned in the name of JAGS
is a particular MCMC technique that is beyond the
scope of this course: however, it is not so different from the Metropolis-Hastings
method that we do study in this course. If you are faced with a statistics
problem which you would like to solve in a Bayesian way, JAGS makes it very
straightforward to implement a Bayesian model. Essentially, you just need to tell
JAGS the following things:
\begin{itemize}
\item The names of your unknown parameter(s), and their prior distributions
\item The likelihood
\end{itemize}
Then we simply load in the data and let it go! JAGS will run MCMC, automatically
choosing an appropriate starting point based on your prior,
and moving the parameter
values around so the posterior distribution is well sampled.
In this chapter we will see how to use JAGS with a simple example, and we will
see some more features of JAGS when we come to study more complex examples.
There are some more advanced features of JAGS that we will not use in this
course.

JAGS is not the first program of its kind, and it is related to many other
available programs.
Starting in the late 1980s, a program called BUGS (Bayesian inference Using
Gibbs Sampling) was developed, and this evolved into the program WinBUGS. These
programs had a huge effect on the uptake of Bayesian statistics, by dramatically
reducing the amount of work it takes to get an MCMC simulation to run. Instead
of having to code up your own implementation of the Metropolis algorithm or
an alternative MCMC method, all you had to do was tell WinBUGS what your prior
and likelihood were, and it would automatically use appropriate and sophisticated
MCMC methods.

Up until 2012, STATS 331 was taught using WinBUGS.
However, there are a number of disadvantages to
WinBUGS, so I decided to switch over to JAGS in 2013. The main advantages of
JAGS over WinBUGS are: i) it is open source software and works on all
operating systems, ii) it allows a more concise version of notation that can
save a lot of space and make the code easier to read and write, and iii)
WinBUGS is not actively developed or maintained any more. In addition, a lot of time in previous iterations of 331 was spent teaching
different ways of using WinBUGS (i.e. calling it from R, vs. using the graphical
interface, vs. writing a script). In 2013 we will use JAGS in just one way (by calling it from R), which frees up time for us to concentrate on the stats!
There is another up to date BUGS program called OpenBUGS, but it too is only
a Windows program. Most of what you learn about JAGS can be transferred to
WinBUGS or OpenBUGS quite easily, if necessary, with only minor changes.

\section{Basic JAGS Example}
Since we have used it a lot already, it makes sense to look at how the bus
problem looks in JAGS. Recall we had a
single unknown parameter $\theta$, with a uniform prior between 0 and 1.
We also had a binomial likelihood, which we could write as
$x \sim \textnormal{Binomial}(N, \theta)$.
To implement this model in JAGS, the code looks like this:
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
model
{
    # Parameters and the priors
    theta ~ dunif(0, 1)

    # Likelihood
    x ~ dbin(theta, N)
}
\end{minted}
As in R, comments (statements that have no effect but help to annotate the
code) can be written with a \# symbol.
The names of the distributions in JAGS are very similar to (but not always
exactly the same as) the names of the R functions for evaluating the probability
densities or mass functions. In this example, {\tt dunif} is the uniform
distribution and {\tt dbin} is the binomial distribution. One final thing to
note is the order of the parameters in the binomial distribution. In JAGS the
success probability comes first and the number of trials comes second.
There are some quirks with the other distributions as well, such as the normal
distribution, which we will see in later chapters.

The code for implementing a Bayesian model in JAGS belongs inside
a {\tt model\{   \}} block.
In our example,
the first statement inside the model is {\tt theta \~{ } dunif(0, 1)}. As you
can probably guess, {\tt theta}
is simply the name of our parameter. We are free to name it as we
wish, and in different situations we will give the parameters different names.
The tilde sign is like the ``$\sim$'' notation for probability distributions.
We are
about to specify a probability distribution that applies to {\tt theta}. Finally,
the actual distribution is given, which is a uniform distribution between 0 and 1. Note
the command used for a uniform distribution is {\tt dunif} and not {\tt uniform}.
{\it Our line of code {\tt theta \~{ } dunif(0, 1)}
tells JAGS
there is a parameter called {\tt theta} and we want to use a uniform prior between
0 and 1}.

The notation for the likelihood is very similar. We write the name of the data
followed by ``{\tt \~{ }}'' and then the distribution, in this case the
binomial distribution.
One annoying this about the binomial distribution is the ordering of the parameters.
Usually people write the number of trials first and the success probability
second, but in JAGS it's the other way around.

Interestingly, the likelihood part of the code looks exactly like the
prior part. So how does JAGS know that {\tt x} is data and not just another
parameter? Well, when we call JAGS from R, we will pass to it an R list containing
the data. There will be a value for {\tt x} in this list, which tells JAGS that
it is a fixed and known quantity, and not another unknown parameter like {\tt theta}.

Above, we specified the JAGS model, but this isn't the only thing we need.
We also need a way to actually run JAGS! The most
convenient way to use JAGS is to call it from R, using the R library
{\tt rjags}. This way, the output (samples from the posterior distribution for
the parameters) is available in R for postprocessing such as plots and
smmaries. The
{\tt rjags} library has many features and options, and it can be a bit
overwhelming to figure out all of them using the documentation.
Therefore, I have written a template
R script called {\tt use\_jags.R} where you can specify the data, the JAGS
model, and some options at the
top of the file, and you do not have to worry about all the functions for
calling JAGS from R.

The first part of {\tt use\_jags.R} is given below. This is the part you
can modify to load different data, change the model assumptions, and decide
how long (for how many iterations or steps) you would like the MCMC to run for.

The {\it burn-in} is an initial part of the
MCMC run where results are not saved. This is beneficial because sometimes
it can take a while for the MCMC to locate the regions of high posterior
probability, and if you include the initial parts of the run in you results,
you can get incorrect answers. For most of our models in STATS 331, we do not
need a long burn-in period.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
model = "model
{
  theta ~ dunif(0, 1)
  x ~ dbin(theta, N)
}
"

# The data (use NA for no data)
data = list(x=2, N=5)

# Variables to monitor
variable_names = c('theta')

# How many burn-in steps?
burn_in = 1000

# How many proper steps?
steps = 10000

# Thinning?
thin = 1
\end{minted}

The second part of {\tt use\_jags.R} actually runs JAGS. You won't need to edit
this or know much about it, but for completeness, here it is:

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# NO NEED TO EDIT PAST HERE!!!
# Just run it all and use the results list.

library('rjags')

# Write model out to file
fileConn=file("model.temp")
writeLines(model, fileConn)
close(fileConn)

if(all(is.na(data)))
{
    m = jags.model(file="model.temp")
} else
{
    m = jags.model(file="model.temp", data=data)
}
update(m, burn_in)
draw = jags.samples(m, steps, thin=thin, variable.names = variable_names)
# Convert to a list
make_list <- function(draw)
{
  results = list()
  for(name in names(draw))
  {
    # Extract "chain 1"
    results[[name]] = as.array(draw[[name]][,,1])

    # Transpose 2D arrays
    if(length(dim(results[[name]])) == 2)
      results[[name]] = t(results[[name]])
  }
  return(results)
}
results = make_list(draw)
\end{minted}

When this code is executed, it creates an R list called {\tt results}.
Inside {\tt results}, there is a vector
for each variable that you chose to ``monitor'' by listing its name in
the {\tt variable\_names} vector. In this example there is only one parameter,
so it seems obvious we would like to monitor it. In more complex situations there
may be many parameters, and only some of them are actually interesting, the others
are ``nuisance parameters''. The {\tt variable\_names} vector allows you to choose
just the parameters you really care about.
Notice also the various options such as the number of steps, and the {\tt thin}
option. If {\tt thin} is set to 10, for example, only every 10th iteration
of the MCMC will appear in the results. This is useful for keeping the size
of the {\tt results} list manageable, even if you run the MCMC for a very
long time.

One of the most important things to check after running JAGS is a {\it trace
plot} of the parameters. A trace plot is a plot of the value of the parameter
over time, as the MCMC was running.
To plot a trace plot of the MCMC run, we can simply use the following code,
for a parameter called {\tt theta}. If the parameter has a different name,
replace {\tt theta} with the actual name of the parameter.
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
plot(results$theta, type='l')
\end{minted}

You could look at the posterior distribution using a
histogram, and you can compute summaries using the methods discussed in
Chapter~\ref{chapter:summaries}. The code for the histogram for a parameter
{\tt theta} is given below.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
hist(results$theta, breaks=100)
\end{minted}

Examples of a trace plot and a histogram are given in Figure~\ref{fig:results}.
Trace plots are the best diagnostic tool for seeing whether MCMC is working
properly. Ideally, trace plots should look like ``noise'', without strong
correlations
between one point and the next. If there are strong correlations in the
trace plot, the MCMC will need to be run for a longer period of time to obtain
effectively independent samples from the posterior distribution.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.5]{Figures/jags_example.pdf}
\caption{\it The trace plot and histogram of the MCMC samples returned by
JAGS. The trace plot (top) is zoomed in on the first 500 samples, and shows the
{\tt theta} value moving around as the MCMC proceeds. The histogram of {\tt
theta} values, sampled from the posterior distribution, is given in the lower
panel. You can compare this with Figure~\ref{fig:bus_inference}.
\label{fig:results}}
\end{center}
\end{figure}

\section{Checklist for Using JAGS}
When running JAGS using the {\tt use\_jags.R} script provided, there are several
things you need to ensure. These are listed below.

\begin{itemize}
\item The data you want to analyse must be contained in an R
list called {\tt data}. Inside this list you should also include variables
such as the size of the data set (which I usually call {\tt N}),
or any other known constants that are
referred to in the model.
\item The JAGS model must be correctly written inside the R string called
{\tt model}. In the likelihood part of the JAGS model, you must ensure that
the names of the data variables match the names in your {\tt data} list. In
the example of this chapter, the number of successes is called {\tt x} in both
the data list and in the JAGS model.
\item The {\tt variable\_names} vector, which lists the parameters you are
interested in, can only list parameters that actually exist in the JAGS model.
If you try to monitor a variable that isn't in your model, you will get an
error message.
\end{itemize}


\section{Button}\label{s:button}

\project{Push button for physical input}{In this project, you will learn how to wire and program a push button for physical input.}

\subsection*{Equipment Required}

The circuit build in Section~\ref{s:leds} plus the following:
\begin{itemize}
\item push button
\item 1 x M/F jumper wire
\item 1 x M/M jumper wire
\end{itemize}

\subsection*{Additional Parts}

You will be adding a switch to the LED circuit that you made in Section~\ref{s:leds}.  Here are the additional components.  You may skip this section if you already know about these components.

\subsubsection*{Push Button}

\rimage{0.1}{switch}

A push button will complete a circuit when the button is pressed.  What this means is that a current will not flow across the button until it is pressed.  When it is released, the circuit will be `broken'.

Normally, if you try to detect the state of an input pin when there is no current, it will randomly be either on or off due to interference of other components.  BMC64 solves this by internally `pulling up' the input to 3.3 volts through a large resistor.  When read, the input pin will be seen as `on'.  The resistor ensures that the current is very low so that your Pi is not damaged!

BMC64 handles this automatically for you whenever a pin is specified as an input.  This means your circuit usually only needs to be connected to ground.  Input pins are `pulled high' internally when there is no external connection and outputs are set high internally when you set the user port to 1.

This is also why your LEDs may be on when you first connect them.  The user port is in input mode and the pins are `high' when the computer is turned on.  They will only glow faintly because of the large resistor.  This is not a problem.  This is also why the programs in these worksheets start by turning all the pins off in the user port, once the output pins have been specified in the data direction register.

When you press the switch, another circuit is made which will make the current `flow to ground', which means that the input pin will be seen as being `off'.  This is how you will detect the switch.

\subsubsection*{Jumpers}

\limage{0.2}{more-jumpers}

You will be adding one more Male/Female jumper wire and one Male/Male (M/M) jumper, which has a pin on each end.  The M/M jumper will be used for joining two rows or columns on the breadboard together.

The jumper wires supplied in the EduKit will vary in colour and are unlikely to match the colours used in the diagrams.

\subsection*{Building the Circuit}

Before you connect additional components to your circuit, you should turn off your Pi.  Leave the LED circuit from the previous worksheets in place.

Add the button to the breadboard with the pins straddling the split in the middle of the board.  It will only fit one way round.  Push down hard to ensure that the pins are inserted fully into the breadboard.

\limage{0.5}{switch-circuit}

Connect one of the jumpers (shown in white here) from one side of the button to the input pin you are going to use on the Raspberry Pi.  You are using pin 8.

Remember that GPIO 8 is bit 3 of the user port and has a value of 8.  We will need this value later to be able to read the input state of the switch.

Finally, connect the `ground rail' to the other side of the switch, shown here by the purple wire.

So, how does the switch work?  When the switch is not being pressed, the pin will be internally `pulled high' to 3.3v and read as `on'.

When the switch is pressed, the current takes another route.  It goes through the switch and back to ground (0v).  This splits enough of the current off that the voltage across the white wire is much lower and no longer high enough for the input GPIO pin to be seen as `on'.

To be able to tell when the button has been pressed, we need to be able to isolate the bit of the user port we are interested in.  This is because when we read the user port, we will read back any output bits that are set (if we have turned any LEDs on) and we will also read 1 for each input bit that is not grounded.

We can use the AND operator to isolate an individual bit from a value.  If we take the user port value and AND with the bit value we are interested in, then we will extract just that bit.  The result will be 0 if there is no input (the pin is low, which means the button is pressed) or the bit value if the pin is high (which means the button is not pressed).

\subsection*{Code}

Type in the following code:
\begin{basic}
10 REM BUTTON
20 REM SETUP LOCATIONS
30 UP=56577:DDR=56579
40 REM PIN VALUES FOR LEDS AND BUTTON
50 R=1:Y=2:G=4:B=8:A=R+Y+G
60 REM SET ONLY PINS 0,1,2 FOR OUTPUT
70 POKE DDR,A
80 REM CLEAR USER PORT (ALL PINS OFF)
90 POKE UP,0
100 PRINT "-------------"
110 PRINT "BUTTON + GPIO"
120 PRINT "-------------"
130 P=PEEK(UP) AND B:REM ONLY GET BUTTON VALUE
140 REM IF BUTTON IS PRESSED THEN P WILL BE 0
150 IF P=0 THEN GOSUB 200
160 IF P<>0 THEN GOSUB 300
170 GOTO 130
200 PRINT "BUTTON PRESSED"
210 PRINT P
220 FOR D=1 TO 720:NEXT D:REM SLEEP 1 SECOND
230 RETURN
300 PRINT CHR\$(147);:REM CLEAR THE SCREEN
310 PRINT "WAITING FOR YOU TO PRESS THE BUTTON"
320 FOR D=1 TO 360:NEXT D:REM SLEEP 0.5 SECONDS
330 RETURN
\end{basic}

Save the program as ``5 BLINK''.

\subsection*{Concepts}

What is happening in the code?  Let us go through some of the important concepts before looking at the code:
\begin{itemize}
\item A `variable' is a name that contains a value.  That value can be changed within your code at any time.  It is often easier to use a variable to contain a number because it is easier to remember the name.
\item A ``GOSUB'' statement tells BASIC to go and run some code from somewhere else until a ``RETURN'' statement is reached.  When execution reaches ``RETURN'', BASIC will go back to the statement after the original ``GOSUB''.
\end{itemize}

\subsection*{Explanation of the Code}

\begin{basic}
10 REM BUTTON
20 REM SETUP LOCATIONS
30 UP=56577:DDR=56579
40 REM PIN VALUES FOR LEDS AND BUTTON
50 R=1:Y=2:G=4:B=8:A=R+Y+G
60 REM SET ONLY PINS 0,1,2 FOR OUTPUT
70 POKE DDR,A
80 REM CLEAR USER PORT (ALL PINS OFF)
90 POKE UP,0
\end{basic}

\explain{This is the same setup code we have used before except we have set a `variable' B to hold the value of the pin that we are using for input from the button.}

\begin{basic}
130 P=PEEK(UP) AND B:REM ONLY GET BUTTON VALUE
140 REM IF BUTTON IS PRESSED THEN P WILL BE 0
\end{basic}

\explain{`PEEK' will read the value of the entire user port.  Using AND, we can pick out just the bit representing the button input.  When the pin detects a voltage nearing 3.3v, it is read as `on' and this bit will be 1.  Since this bit has a `value' of 8, the result will be 8.  If the voltage nears 0v, then it is `off' and the result will be 0.}

\begin{basic}
150 IF P=0 THEN GOSUB 200
\end{basic}

\explain{The `IF' statement checks the value of the pin to see if it is 0 (so the button is being pressed).  If so, then the code in lines 200 to 230 will execute.}

\begin{basic}
160 IF P<>0 THEN GOSUB 300
\end{basic}

\explain{This `IF' statement checks the value of the pin to see if it is \textbf{not} 0 (so the button is not being pressed).  If so, then the code in lines 300 to 330 will execute.}

\begin{basic}
170 GOTO 130
\end{basic}

\explain{Once the status of the button has been printed, we go back and repeat the process again.  This `loop' will run forever, or until `RUN/STOP' is pressed.}

\subsection*{Running the Code}

Run the code.  The program will wait for the button to be pressed and report the status to the screen.  This will continue until you press `RUN/STOP'.


\documentclass[color=usenames,dvipsnames]{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, 0, 0}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.69,0.494,0}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.749,0.012,0.012}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.514,0.506,0.514}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0,0.341,0.682}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.004,0.004,0.506}{#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}
%\documentclass[color=usenames,dvipsnames,handout]{beamer}

\usepackage[sans]{../../lab1}
\usepackage{bm}

\hypersetup{pdftex,pdfstartview=FitV}

%% New command for inline code that isn't to be evaluated
\definecolor{inlinecolor}{rgb}{0.878, 0.918, 0.933}
\newcommand{\inr}[1]{\colorbox{inlinecolor}{\texttt{#1}}}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

% \setlength\fboxsep{0pt}

\begin{frame}[plain]
  \huge
  \centering \par
  {\color{RoyalBlue}{Lab 13 -- Generalized Linear Models}} \\
  \vspace{1cm}
  \LARGE
%  November 12 \& 13, 2018 \\
  FANR 6750 \\
  \vfill
  \large
  Richard Chandler and Bob Cooper
\end{frame}

\section{Logistic regression}

\begin{frame}
  \frametitle{Logistic Regression}
  \large
    \begin{gather*}
      \mathrm{logit}(p_i) = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \cdots \\
      y_i \sim \mathrm{Binomial}(N, p_i)
  \end{gather*}
  \pause
  {%\bf
    where: \\}
  $N$ is the number of ``trials'' (e.g. coin flips) \\
  $p_i$ is the probability of a success for sample unit $i$
\end{frame}

\begin{frame}[fragile]
  \frametitle{Presence-absence and abundance}

\begin{center}
\tiny
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{frogData[}\hlnum{1}\hlopt{:}\hlnum{25}\hlstd{,]} \hlcom{# First 25 rows}
\end{alltt}
\begin{verbatim}
##    presence abundance elevation habitat
## 1         0         0        58     Oak
## 2         1         7       191     Oak
## 3         0         0        43     Oak
## 4         1        11       374     Oak
## 5         1        11       337     Oak
## 6         1         1        64     Oak
## 7         1         4       195     Oak
## 8         1         6       263     Oak
## 9         0         0       181     Oak
## 10        1         1        59     Oak
## 11        1        50       489   Maple
## 12        1         5       317   Maple
## 13        0         0        12   Maple
## 14        1         4       245   Maple
## 15        1        47       474   Maple
## 16        1         1        83   Maple
## 17        1        46       467   Maple
## 18        1        51       485   Maple
## 19        1        23       335   Maple
## 20        0         0        20   Maple
## 21        1        27       430    Pine
## 22        1         2       223    Pine
## 23        0         0        68    Pine
## 24        1        50       483    Pine
## 25        0         0        78    Pine
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{center}
\end{frame}

%% \begin{frame}[fragile]
%%   \frametitle{Raw data}
%%   \tiny
%% %  \vspace{-0.5cm}
%% \begin{center}
%% <<raw-elev,fig=true,include=false>>=
%% plot(presence ~ elevation, frogData,
%%      xlab="Elevation", ylab="Frog Occurrence")
%% @
%%   \includegraphics[width=0.7\textwidth]{lab13-GLMs-raw-elev}
%% \end{center}
%% \end{frame}

\begin{frame}[fragile]
  \frametitle{Logistic regression using {\tt glm}}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{fm1} \hlkwb{<-} \hlkwd{glm}\hlstd{(presence} \hlopt{~} \hlstd{habitat} \hlopt{+} \hlstd{elevation,}
           \hlkwc{family}\hlstd{=}\hlkwd{binomial}\hlstd{(}\hlkwc{link}\hlstd{=}\hlstr{"logit"}\hlstd{),} \hlkwc{data}\hlstd{=frogData)}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(fm1)}
\end{alltt}
\begin{verbatim}
##
## Call:
## glm(formula = presence ~ habitat + elevation, family = binomial(link = "logit"),
##     data = frogData)
##
## Deviance Residuals:
##     Min       1Q   Median       3Q      Max
## -1.8843  -0.6169   0.1674   0.6050   1.3775
##
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -1.092759   1.055124  -1.036   0.3004
## habitatMaple  0.096781   1.367518   0.071   0.9436
## habitatPine  -0.240443   1.154650  -0.208   0.8350
## elevation     0.013658   0.006011   2.272   0.0231 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 34.795  on 29  degrees of freedom
## Residual deviance: 23.132  on 26  degrees of freedom
## AIC: 31.132
##
## Number of Fisher Scoring iterations: 6
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

%% \begin{frame}[fragile]
%%   \frametitle{Test main effects}
%%   \footnotesize
%% <<>>=
%% anova(fm1, test="Chisq")
%% @
%% \end{frame}

\begin{frame}[fragile]
  \frametitle{Occurrence probability and elevation}
  \footnotesize
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{predData.elev} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{elevation}\hlstd{=}\hlkwd{seq}\hlstd{(}\hlnum{12}\hlstd{,} \hlnum{489}\hlstd{,} \hlkwc{length}\hlstd{=}\hlnum{50}\hlstd{),}
                       \hlkwc{habitat}\hlstd{=}\hlstr{"Oak"}\hlstd{)}
\hlkwd{head}\hlstd{(predData.elev)}
\end{alltt}
\begin{verbatim}
##   elevation habitat
## 1  12.00000     Oak
## 2  21.73469     Oak
## 3  31.46939     Oak
## 4  41.20408     Oak
## 5  50.93878     Oak
## 6  60.67347     Oak
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
{%\bf
  To get confidence intervals on probability scale, predict on logit
  (link) scale and then backtransform using inverse-link}
\pause
%\scriptsize
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{pred.link} \hlkwb{<-} \hlkwd{predict}\hlstd{(fm1,} \hlkwc{newdata}\hlstd{=predData.elev,} \hlkwc{se.fit}\hlstd{=}\hlnum{TRUE}\hlstd{,} \hlkwc{type}\hlstd{=}\hlstr{"link"}\hlstd{)}
\hlstd{predData.elev}\hlopt{$}\hlstd{p} \hlkwb{<-} \hlkwd{plogis}\hlstd{(pred.link}\hlopt{$}\hlstd{fit)}
\hlstd{predData.elev}\hlopt{$}\hlstd{lower} \hlkwb{<-} \hlkwd{plogis}\hlstd{(pred.link}\hlopt{$}\hlstd{fit} \hlopt{-} \hlnum{1.96}\hlopt{*}\hlstd{pred.link}\hlopt{$}\hlstd{se.fit)}
\hlstd{predData.elev}\hlopt{$}\hlstd{upper} \hlkwb{<-} \hlkwd{plogis}\hlstd{(pred.link}\hlopt{$}\hlstd{fit} \hlopt{+} \hlnum{1.96}\hlopt{*}\hlstd{pred.link}\hlopt{$}\hlstd{se.fit)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Occurrence probability and elevation}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(p} \hlopt{~} \hlstd{elevation,} \hlkwc{data}\hlstd{=predData.elev,} \hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{,} \hlkwc{ylim}\hlstd{=}\hlkwd{c}\hlstd{(}\hlnum{0}\hlstd{,}\hlnum{1}\hlstd{),}
     \hlkwc{xlab}\hlstd{=}\hlstr{"Elevation"}\hlstd{,} \hlkwc{ylab}\hlstd{=}\hlstr{"Probability of occurrence"}\hlstd{)}
\hlkwd{points}\hlstd{(presence} \hlopt{~} \hlstd{elevation, frogData)}
\hlkwd{lines}\hlstd{(lower} \hlopt{~} \hlstd{elevation,} \hlkwc{data}\hlstd{=predData.elev,} \hlkwc{lty}\hlstd{=}\hlnum{2}\hlstd{)}
\hlkwd{lines}\hlstd{(upper} \hlopt{~} \hlstd{elevation,} \hlkwc{data}\hlstd{=predData.elev,} \hlkwc{lty}\hlstd{=}\hlnum{2}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\centering
\includegraphics[width=0.75\textwidth]{figure/pred1-1} \\
\end{frame}

\begin{frame}[fragile]
  \frametitle{Occurrence probability and habitat}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{predData.hab} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{habitat}\hlstd{=}\hlkwd{c}\hlstd{(}\hlstr{"Oak"}\hlstd{,} \hlstr{"Maple"}\hlstd{,} \hlstr{"Pine"}\hlstd{),} \hlkwc{elevation}\hlstd{=}\hlnum{250}\hlstd{)}
\hlstd{pred} \hlkwb{<-} \hlkwd{predict}\hlstd{(fm1,} \hlkwc{newdata}\hlstd{=predData.hab,} \hlkwc{se.fit}\hlstd{=}\hlnum{TRUE}\hlstd{,} \hlkwc{type}\hlstd{=}\hlstr{"link"}\hlstd{)}
\hlstd{bp} \hlkwb{<-} \hlkwd{barplot}\hlstd{(}\hlkwd{plogis}\hlstd{(pred}\hlopt{$}\hlstd{fit),} \hlkwc{ylab}\hlstd{=}\hlstr{"Probability of occurrence"}\hlstd{,} \hlkwc{cex.lab}\hlstd{=}\hlnum{1.5}\hlstd{,}
              \hlkwc{names}\hlstd{=}\hlkwd{c}\hlstd{(}\hlstr{"Oak"}\hlstd{,} \hlstr{"Maple"}\hlstd{,} \hlstr{"Pine"}\hlstd{),} \hlkwc{col}\hlstd{=}\hlstr{"lightblue"}\hlstd{,} \hlkwc{ylim}\hlstd{=}\hlkwd{c}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{1.1}\hlstd{))}
\hlkwd{arrows}\hlstd{(bp,} \hlkwd{plogis}\hlstd{(pred}\hlopt{$}\hlstd{fit), bp,} \hlkwd{plogis}\hlstd{(pred}\hlopt{$}\hlstd{fit} \hlopt{+} \hlstd{pred}\hlopt{$}\hlstd{se.fit),}
       \hlkwc{angle}\hlstd{=}\hlnum{90}\hlstd{,} \hlkwc{code}\hlstd{=}\hlnum{3}\hlstd{,} \hlkwc{length}\hlstd{=}\hlnum{0.1}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\centering
\includegraphics[width=0.6\textwidth]{figure/habitat-1} \\
\end{frame}

\section{Poisson regression}

\begin{frame}
  \frametitle{Poisson Regression}
    \begin{gather*}
      \mathrm{log}(\lambda_i) = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \cdots \\
      y_i \sim \mathrm{Poisson}(\lambda_i)
  \end{gather*}
  \pause
  {%\bf
    where: \\}
  $\lambda_i$ is the expected value of $y_i$
\end{frame}

\begin{frame}[fragile]
  \frametitle{Poisson regression using {\tt glm}}
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{fm2} \hlkwb{<-} \hlkwd{glm}\hlstd{(abundance} \hlopt{~} \hlstd{habitat} \hlopt{+} \hlstd{elevation,}
           \hlkwc{family}\hlstd{=}\hlkwd{poisson}\hlstd{(}\hlkwc{link}\hlstd{=}\hlstr{"log"}\hlstd{),} \hlkwc{data}\hlstd{=frogData)}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(fm2)}
\end{alltt}
\begin{verbatim}
##
## Call:
## glm(formula = abundance ~ habitat + elevation, family = poisson(link = "log"),
##     data = frogData)
##
## Deviance Residuals:
##     Min       1Q   Median       3Q      Max
## -2.6308  -1.0810  -0.1067   0.3353   2.7935
##
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -0.9403442  0.2430730  -3.869 0.000109 ***
## habitatMaple  0.1533915  0.1971009   0.778 0.436428
## habitatPine   0.0881110  0.1994981   0.442 0.658733
## elevation     0.0097836  0.0006291  15.551  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for poisson family taken to be 1)
##
##     Null deviance: 700.762  on 29  degrees of freedom
## Residual deviance:  44.891  on 26  degrees of freedom
## AIC: 140.13
##
## Number of Fisher Scoring iterations: 5
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Abundance and elevation}
%<<newdat2,size='scriptsize'>>=
%newdat <- data.frame(elevation=seq(12, 489, length=50),
%                     habitat="Oak")
%head(newdat)
%@
\footnotesize
%\pause
{%\bf
  To get confidence intervals on (0,$\infty$) scale, predict on log
  (link) scale and then backtransform using inverse-link}
\pause
%\scriptsize
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{pred.link} \hlkwb{<-} \hlkwd{predict}\hlstd{(fm2,} \hlkwc{newdata}\hlstd{=predData.elev,} \hlkwc{se.fit}\hlstd{=}\hlnum{TRUE}\hlstd{,} \hlkwc{type}\hlstd{=}\hlstr{"link"}\hlstd{)}
\hlstd{predData.elev}\hlopt{$}\hlstd{lambda} \hlkwb{<-} \hlkwd{exp}\hlstd{(pred.link}\hlopt{$}\hlstd{fit)} \hlcom{# exp is the inverse-link function}
\hlstd{predData.elev}\hlopt{$}\hlstd{lower} \hlkwb{<-} \hlkwd{exp}\hlstd{(pred.link}\hlopt{$}\hlstd{fit} \hlopt{-} \hlnum{1.96}\hlopt{*}\hlstd{pred.link}\hlopt{$}\hlstd{se.fit)}
\hlstd{predData.elev}\hlopt{$}\hlstd{upper} \hlkwb{<-} \hlkwd{exp}\hlstd{(pred.link}\hlopt{$}\hlstd{fit} \hlopt{+} \hlnum{1.96}\hlopt{*}\hlstd{pred.link}\hlopt{$}\hlstd{se.fit)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Abundance and elevation}
  \tiny
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(lambda} \hlopt{~} \hlstd{elevation, predData.elev,} \hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{,} \hlkwc{ylim}\hlstd{=}\hlkwd{c}\hlstd{(}\hlnum{0}\hlstd{,}\hlnum{60}\hlstd{),}
     \hlkwc{xlab}\hlstd{=}\hlstr{"Elevation"}\hlstd{,} \hlkwc{ylab}\hlstd{=}\hlstr{"Expected abundance"}\hlstd{)}
\hlkwd{points}\hlstd{(abundance} \hlopt{~} \hlstd{elevation, frogData)}
\hlkwd{lines}\hlstd{(lower} \hlopt{~} \hlstd{elevation, predData.elev,} \hlkwc{lty}\hlstd{=}\hlnum{2}\hlstd{)}
\hlkwd{lines}\hlstd{(upper} \hlopt{~} \hlstd{elevation, predData.elev,} \hlkwc{lty}\hlstd{=}\hlnum{2}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\centering
\includegraphics[width=0.75\textwidth]{figure/pred2-1} \\
\end{frame}

%% \begin{frame}[fragile]
%%   \frametitle{Abundance, elevation, and habitat}
%%   \tiny
%% \begin{center}
%% <<habitat2,fig=true,include=false>>=
%% nd <- data.frame(habitat=rep(c("Oak", "Maple", "Pine"), each=20),
%%                  elevation=rep(seq(12, 489, length=20), times=3))
%% pred <- predict(fm2, newdata=nd, se.fit=TRUE, type="link")
%% nd$lambda <- exp(pred$fit)
%% plot(lambda ~ elevation, nd, type="l", ylim=c(0,55), subset=habitat=="Oak",
%%      xlab="Elevation", ylab="Expected abundance")
%% lines(lambda ~ elevation, nd, subset=habitat=="Maple", lty=2)
%% lines(lambda ~ elevation, nd, subset=habitat=="Pine", lty=3)
%% @
%%   \includegraphics[width=0.5\textwidth]{lab13-GLMs-habitat2}
%% \end{center}
%% \end{frame}

%\begin{frame}[fragile]

%\end{frame}

\begin{frame}
  \frametitle{Assignment}
  Researchers want to know how latitude and landscape type influence the
  probability that American Crows are infected by West Nile Virus. One
  hundred crows are captured and tested for West Nile Virus in urban
  and rural landscapes spanning a latitude gradient.
  \begin{enumerate}[\bf (1)]
    \item Fit a logistic regression model to the {\tt crowData.csv}
      dataset to assess the effects of latitude and landscape type
    \item Interpret the parameter estimates
    \item Plot the relationship between infection probability and
      latitude, for rural and urban landscapes, on the same graph
    \item Include the data points (color coded by landscape) and a legend in
      the graph
    \item Include confidence intervals
  \end{enumerate}
\end{frame}

\end{document}

% \section{Simulating data}

% \begin{frame}
%   \frametitle{Simulating data}
%   \large
%   {\bf R} has many functions for generating random variables
%   \begin{center}
%     \begin{tabular}{lr}
%       \hline
%       Distribution & {\bf R} function \\
%       \hline
%       Uniform & {\tt runif} \\
%       Normal & {\tt rnorm} \\
%       Binomial & {\tt rbinom} \\
%       Poisson & {\tt rpois} \\
%       Negative binomial & {\tt rnbinom} \\
%       \hline
%     \end{tabular}
%   \end{center}
% \end{frame}

% \begin{frame}[fragile]
%   \frametitle{Random Poisson variables}
%   {\bf Ten Poisson variates with $\lambda=0.5$}
% <<rpois1>>=
% rpois(n=10, 0.5)
% @
% \vfill
% \pause
%   {\bf Ten Poisson variates with $\lambda=5$}
% <<rpois2>>=
% rpois(n=10, 5)
% @
% \vfill
% \pause
%   {\bf Ten Poisson variates with $\lambda=15$}
% <<rpois3>>=
% rpois(n=10, 15)
% @
% \end{frame}

% \begin{frame}[fragile]
%   \frametitle{Simulate Poisson variables}
%   {\bf 1000 Poisson variabes with $\lambda=3$}
%   \small
% <<p1000,fig.show='hide'>>=
% x <- rpois(n=1000, 3)
% hist(x, col="purple")
% @
% \centering
% \includegraphics[width=0.6\textwidth]{figure/p1000-1} \\
% \end{frame}

% \begin{frame}[fragile]
%   \frametitle{Simulate Poisson regression data}
%   {\bf Create a design matrix}
% <<X>>=
% X <- model.matrix(~habitat + elevation, frogData)
% @
% \pause
% \vfill
% {\bf Choose values of $\bm \beta$ parameters
% <<beta>>=
% beta <- c(0.5, 1, -3, 0.01)
% @
% \pause
% \vfill
% {\bf Calculate $\mu$, the expected value of $y$}
% <<lambda>>=
% lambda <- exp(X %*% beta)
% @
% \pause
% \vfill
% {\bf Simulate $y$, the response variable}
% <<sampleSize>>=
% sampleSize <- nrow(X)
% y <- rpois(sampleSize, lambda)
% @
% \end{frame}

% \begin{frame}[fragile]
%   \frametitle{Fit model to simulated data}
%   \small
% <<fm3,size='small'>>=
% fm3 <- glm(y ~ habitat + elevation, family=poisson, frogData)
% @
% \scriptsize
% <<summary-fm3,size='scriptsize'>>=
% summary(fm3)
% @
% \end{frame}

% \begin{frame}[fragile]
%   \frametitle{Check confidence intervals}
% <<CI>>=
% CI <- confint(fm3)
% cbind(CI, beta)
% @
% \end{frame}

% \begin{frame}[fragile]
% <<echo=FALSE>>=
% set.seed(340)
% vegData <- data.frame(veght=rnorm(30, 5, 1))
% write.csv(vegData, "vegData.csv", row.names=FALSE)
% @
%   \frametitle{Assignment}
%   \begin{enumerate}[\bf (1)]
%     \item Import the data {\tt vegData.csv}
%     \item Simulate a Poisson response variable assuming the model:
%       $\lambda_i = \exp(\beta0 + \beta1\mbox{VegHt}_i)$, where $\beta_0=-1$ and
%       $\beta_1 = 0.5$
%     \item Fit the Poisson regression model to the data
%     \item Do the confidence intervals include the data generating
%       $\bm \beta$ values?
%     \item Plot the estimated relationship between abundance and
%       vegetation height. Include 95\% confidence intervals.
%   \end{enumerate}
% \end{frame}

%% \begin{frame}
%%   \frametitle{Goodness-of-fit}
%%   Parametric bootstrap
%%   \begin{enumerate}[<+->]
%%     \item Fit a model
%%     \item Pick a fit statistic
%%     \item Calculate the fit statistic for the
%%   \end{enumerate}
%% \end{frame}

%% \begin{frame}[fragile]
%%   \frametitle{Goodness-of-fit}
%% <<>>=
%% fitStat <- function(fm) sum(resid(fm)^2)
%% boot(fm2$data, fitStat, sim="parametric", ran.gen=simulate, R=100)
%% @
%% \end{frame}

%% \begin{frame}[fragile]
%%   \frametitle{Goodness-of-fit}
%% <<>>=
%% nSims <- 1000
%% fitStats <- rep(NA, nSim)
%% for(i in 1:nSims) {
%%     frogData$abundance.star <- as.numeric(simulate(fm2))
%%     fit.star <- glm(abundance.star ~ habitat + elevation,
%%                  family=poisson(link="log"), data=frogData)
%%     fitStats[i] <- SSE
%% }
%% @
%% \end{frame}


% !TEX program = xelatex
\documentclass[compress]{beamer}
\usepackage[beamer, darkblue]{ahsan}

\title{Bijection}
\subtitle{A powerful tool in mathematics}
\author{M Ahsan Al Mahir}
\date{\today}

\begin{document}

\begin{frame}[plain,noframenumbering]
    \maketitle
\end{frame}

\section{Bijection}

\subsection{New way of solving}

\begin{frame}
    \textcolor{NordOrange}{Suppose we want to compute the following sum
    \[{n \choose 0}+{n \choose 1} + \dots  + {n \choose n}\] }

    How would you do it?

    \pause \vspace{1em}

    Definitely we wouldn't actually start computing by hand! Because that
    would be \textcolor{NordRed}{REALLY} hard to say the least.
\end{frame}

\begin{frame}
    But we can try to find some values for smaller $n$'s:
    \pause\vspace{1em}

    \[1: {1 \choose 0} + {1 \choose 1} = 2\]\pause
    \[2:\ {2 \choose 0} + {2 \choose 1} + {2 \choose 2} = 4\] \pause
    \[3:\ {3 \choose 0} + {3 \choose 1} + {3 \choose 2} + {3 \choose 3} = 8\]

    \pause\vspace{1em}

    Do you see any patterns here? Can you guess why?
\end{frame}

\begin{frame}
    Let us start by seeing what these numbers actually mean.
    \textcolor{NordBrightBlue}{We can interpret ${n\choose k}$ as choosing $k$
    items from a box of $n$ balls, for all $k$.}

    \pause\vspace{1em}

    And ${n \choose 0}+{n \choose 1} + \dots  + {n \choose n}$ means...

    \pause\vspace{1em}
    \begin{center}
        \begin{minipage}{.85\linewidth}
            \textcolor{NordRed}{Choosing some number of balls (any number, $0,
                1, 2, n-1$ or
            $n$) from the set of $n$ balls.}
        \end{minipage}
    \end{center}

    \pause \vspace{1em}

    Now the interesting part. How do we actually count it? There are many ways
    to do this, but we will use \textbf{Bijection}.
\end{frame}

\begin{frame}
    \textcolor{NordBrightBlue}{What if we think about selecting a ball as
    labeling it with $1$, and not selecting means marking it with $0$.}

    \pause\vspace{1em}

    For example, selecting $b_2, b_3, b_5$ from a set of $5$ balls is the same
    is marking them like the following:

    \vspace{1em}

    \begin{center}
        \begin{tabular}{ccccc}
            $b_1$ & $b_2$ & $b_3$ & $b_4$ & $b_5$\\
            $0$ & $1$ & $1$ & $0$ & $1$
        \end{tabular}
    \end{center}
\end{frame}

\begin{frame}

    \textcolor{NordRed}{So we have $n$ balls, each labeled with either $1$ or
    $0$, which is just a binary number with lenght $n$   !!}

    \vspace{1em}
    And every binary number represents a different set of balls.

    \pause\vspace{1em}

    Can you see why?
\end{frame}

\begin{frame}
    That means the number of ways to select a set of balls is the same as the
    number of binary numbers of length $n$. Which is precisely
    \[\boxed{2^n}\]
    (Because we have two options, $0, 1$, for each of the $n$ positions)
    \pause \vspace{1em}

    And so we have:
    \[{n \choose 0}+{n \choose 1} + \dots  + {n \choose n}=2^n\]
\end{frame}

\subsection{Defining Bijection}

\begin{frame}

    Just as we said earlier, when we need to count something, we can change our
    interpretation to count something else, that is way easier than the one we
    had to before.\pause
    \vspace{1em}

    That's exactly what bijection does. It gives us a way to turn something
    hard into something easier.

\end{frame}

\begin{frame}
    \texttt{Suppose we have two sets $X, Y$. And for all elements
        of $X$, we can connect it with exactly one element of $Y$. And also for
        all element of $Y$, we can connect it with exactly one element of $X$.
    Then we say that there is a ``bijection'' between $X$ and $Y$.}

    \vspace{1em}
    \begin{figure}
        \begin{center}
            \includegraphics[width=.3\linewidth]{Bijection.pdf}
        \end{center}
    \end{figure}
\end{frame}

\begin{frame}
    And how does that help us?\pause

    \vspace{1em}

    \begin{center}
        \begin{minipage}{.8\linewidth}
            It tells us that the number of elements of $X$ is equal to the number of
            elements of $Y$!!
        \end{minipage}
    \end{center}

    \pause\vspace{1em}

    So if we want to find the number of elements of $X$, then we can
    find a set $Y$ that has a bijection with $X$ and count $Y$ instead!!

    \pause\vspace{1em}

    In our earlier example, we found a bijection between
    \vspace{1em}

    \begin{center}
        \begin{minipage}{.45\linewidth}
            The number of ways to select a set of balls from a box of $n$ balls
        \end{minipage}\hfill\hspace{.01\linewidth}
        \begin{minipage}{.04\linewidth}
            $\Leftrightarrow$
        \end{minipage}\hfill%
        \begin{minipage}{.45\linewidth}
            The number of binary numbers of length $n$
        \end{minipage}
    \end{center}

\end{frame}

\begin{frame}
    Before we jump off to seeing some problems, here is another trivial
    example. \pause
    \vspace{1em}

    Prove that \[{n \choose k} = {n \choose n-k}\]

    \pause\vspace{1em}

    Ponder for a moment how we would solve this without computation...

    \pause\vspace{1em}

    We solve it by finding a bijection between \textcolor{NordBlue}{choosing
        $k$ balls from a set of $n$ balls} and \textcolor{NordBlue}{removing
        $n-k$ balls from the set of $n$ balls to be left with $k$ balls}.
\end{frame}

\begin{frame}
    Another example would be the following identity:
    \[{n+1 \choose k+1} = {n \choose k+1} + {n \choose k}\]

    \pause\vspace{1em}

    We can solve it by thinking about taking $k+1$ balls from $n+1$ balls as
    taking the ball no. $1$ and then taking $k$ balls from the rest of the
    $n$ balls, or not taking the ball no. $1$ and taking $k+1$ balls from the
    $n$ balls.

    \pause\vspace{1em}

    Where did we use bijection?
\end{frame}

\subsection{Bijection in Action}

\begin{frame}
    Well that's nice, but how do we solve problems with bijection??

    \pause\vspace{1em}

    Let's start by seeing another easy application.
\end{frame}

\begin{frame}
    \textcolor{NordOrange}{
        In how many ways can $n$ be written as sum of
        integers? For example, $3$ can be written in $4$ ways
        \[1 + 1+ 1 = 1+2 = 2 + 1 = 3\]
    }

    \pause\vspace{1em}

    I will first give you a hint:

    \vspace{1em}

    \begin{center}
        \begin{tabular}{ccccccccccccc}
            ( & 1 & )& +  & ( & 1 & ) &+& ( & 1 & ) & = & 3\\
            ( & 1 & ) &+& ( & 1 & & +& & 1 & ) & = & 3\\
            ( & 1 &  & +& & 1 & ) &+ & ( & 1 & ) & = & 3\\
            ( & 1 &  & +& & 1 &  & +& & 1 & ) & = & 3\\
        \end{tabular}
    \end{center}
\end{frame}

\begin{frame}
    Now we are off to the solution.
    \pause\vspace{1em}

    Consider the $n-1$ spaces between $n$ $1$'s in the following equation:

    \textcolor{NordRed}{
        \[\left(1 \textunderscore\textunderscore 1
                \textunderscore\textunderscore 1 \textunderscore\textunderscore 1
        \dots \textunderscore\textunderscore 1\right) \]
    }

    \pause\vspace{1em}

    If we place \textcolor{NordRed}{$+$} in some of the spaces and
    \textcolor{NordRed}{$) + ($} in the other spaces, then we get a
    ``partition'' like the one we saw before.

    \pause\vspace{1em}

    \begin{center}
        \begin{tabular}{ccccccccccccc}
            ( & 1 & )& +  & ( & 1 & ) &+& ( & 1 & ) & = & 3\\
            ( & 1 & ) &+& ( & 1 & & +& & 1 & ) & = & 3\\
            ( & 1 &  & +& & 1 & ) &+ & ( & 1 & ) & = & 3\\
            ( & 1 &  & +& & 1 &  & +& & 1 & ) & = & 3\\
        \end{tabular}
    \end{center}

\end{frame}

\begin{frame}
    Now I assume you can tell me the answer to the question? Write in the chat
    if you've found the answer.

    \pause\vspace{1em}

    \textcolor{NordBrightBlue}{Exactly! The answer is $2^{n-1}$. }

    \pause\vspace{1em}

    That's because we have $n-1$ places where we can put either $+$ or $)+($,
    so two options.
\end{frame}

\begin{frame}
    Can you explain the bijection here?

    \pause\vspace{1em}

    Yes, \textcolor{NordBrightBlue}{we found a bijection from the set of ways
    to write $n$ between the set of binary numbers of length $n-1$.} And the
    second set is MUCH easier to compute.
\end{frame}

\begin{frame}
    Let's do another similar problem. \vspace{1em}

    \textcolor{NordOrange}{
        In how many ways can $n$ be written as the sum of $k$ non negative
        integers?
    }

    \pause\vspace{1em}

    Like the previous example, \textcolor{NordBlue}{we have $n $ $1$'s, and we
    want to put $k-1$ either $+$ or $)+($ between them} to group them into
    numbers that add upto $n$.  But this time \textcolor{NordRed}{we are
    allowed to put two $)+($'s side by side.}

    \pause\vspace{1em}

    Why?

    \pause\vspace{1em}

    \textcolor{NordBlue}{Because we also need to count the sum when some of
    the integers are $0$, and $)+(\ )+($ will produce a $0$ in the middle.}
\end{frame}

\begin{frame}
    That's why, we first make $n+k-1$ spaces:
    \textcolor{NordRed}{
        \[\underbrace{\textunderscore\textunderscore \ \
                \textunderscore\textunderscore \ \ \dots
        \textunderscore\textunderscore}_{n+k-1 \text{ dashes}} \]
    }

    \pause\vspace{1em}

    And put $k-1$ $)+($'s in some of them. What will happen then?

    \pause\vspace{1em}

    \textcolor{NordRed}{
        \[\left(\textunderscore\textunderscore )+( \textunderscore\textunderscore
        \dots)+\left(\right) + ( \textunderscore\textunderscore\right) \]
    }

    \pause\vspace{1em}

    \textcolor{NordBlue}{We will have $k$ different groups of spaces, some of
    them being empty.}

    \pause\vspace{1em}

    And if we put a $1$ in each of the $\textunderscore\textunderscore$, we will
    get $k$ different nonnegative integers adding upto $n$!
\end{frame}

\begin{frame}
    So how many ways are there?

    \pause\vspace{1em}

    We chose $k-1$ spaces to put $)+($ on from $n+k-1$ choices. So the answer
    is?

    \pause\vspace{1em}

    \[\boxed{{n+k-1 \choose k-1}}\]

    \pause\vspace{1em}

    Where did we use bijection here?

    \pause\vspace{1em}

    We found a bijection between \textcolor{NordBlue}{the set of $k$
    non-negative integers adding up to $n$} and \textcolor{NordBlue}{the
    number of ways to select $k-1$ items from $n+k-1$ choices}.
\end{frame}

\begin{frame}

    \textcolor{NordOrange}{
        Ten points are selected on the positive $x$-axis and five points are
        selected on the positive $y$ -axis. The fifty segments connecting the
        ten points on $x$-axis to the five points on $y$-axis are drawn.
        What is the maximum possible number of points of intersection of these
        fifty segments in the interior of the first quadrant?
    }

    \begin{figure}
        \begin{center}
            \includegraphics[width=.35\linewidth]{problem_1.pdf}
        \end{center}
    \end{figure}
\end{frame}

\begin{frame}
    The question we have to ask here is, when do two segments intersect? Can
    you answer it?

    \pause\vspace{1em}

    Yes, they intersect whenever \textcolor{NordBrightBlue}{two segments form
    an $\times$.}

    \pause\vspace{1em}

    No brainer right? But now answer, whend does an $\times$ appear?

    \pause\vspace{1em}

    \textcolor{NordBrightBlue}{An unique cross appears when we select two
    points from the $x$ axis and two points from the $y$ axis.}
\end{frame}

\begin{frame}
    So we have a bijection from the \texttt{number of intersection points} to
    \texttt{the number of crosses} to \texttt{the number of pairs of pairs} from
    \textcolor{NordRed}{$x$-axis and pairs of points from $y$-axis}.

    \pause\vspace{2em}

    Now we are ready to count the answer.

    \pause\vspace{2em}

    \textcolor{NordRed}{
        There are a total of ${10 \choose 2}$ ways to select two points from
        $x$-axis. And there are ${5 \choose 2}$ ways to select two points from
        $y$-axis.
    }

    \pause\vspace{1em}

    So the number of ways to select two pairs from the two axes is
    \[\boxed{{10 \choose 2}{5 \choose 2}}\]
\end{frame}

\begin{frame}
    So the total number of intersection points is ${10 \choose 2}{5 \choose
    2}$.
\end{frame}

\subsection{One More Problem}

\begin{frame}
    \textcolor{NordOrange}{
        A triangular grid is obtained by tiling
        an equilateral triangle of side length $n$ by $n^{2}$ equilateral
        triangles of side length $1 .$ Determine the number of parallelograms
        bounded by line segments of the grid.
    }

    \begin{figure}
        \begin{center}
            \includegraphics[width=.4\linewidth]{trig_grid_1.pdf}
        \end{center}
    \end{figure}
\end{frame}

\begin{frame}
    First we have to see what the parallelograms might look like:

   \begin{figure}
        \begin{center}
            \includegraphics[width=.4\linewidth]{trig_grid_3.pdf}
        \end{center}
    \end{figure}

    \pause\vspace{1em}

    If I told you that there were three different orientations of these
    parallelograms, would you buy it?

    \pause\vspace{1em}

    That's because if you extend those parallelograms' sides, they become
    parallel to two different sides of the triangle.
\end{frame}

\begin{frame}
    Now what we do is, we work with only one orientation. Because
    \textcolor{NordBrightBlue}{if we can count how many parallelograms there
    are of the first orientation, then we can apply symmetry to count the
    other orientations.}

    \pause\vspace{1em}

    Do you see why?

    \pause\vspace{1em}

    Now, a parallelogram is defined by its parallel sides, right? What if we
    extend those sides?
\end{frame}

\begin{frame}
    If we extend the sides of the parallelogram, and add one more layer at the
    bottom of the grid, we end up with a picture like this

    \pause\vspace{1em}

    \begin{minipage}{.4\linewidth}

        \begin{figure}
            \begin{center}
                \includegraphics[width=\linewidth]{trig_grid_2.pdf}
            \end{center}
        \end{figure}
    \end{minipage}\hfill%
    \begin{minipage}{.55\linewidth}
        \pause\vspace{1em}

        What's special about this picture?

        \pause\vspace{1em}

        The extended lines intersect the edge in $4$ different points.
        \textcolor{NordRed}{And those $4$ different points define one unique
        parallelogram!}
    \end{minipage}

    \pause\vspace{1em}

    \textcolor{NordGreen}{That's a lot to take in, so I will give you 2
    minutes to think about why this happens.}

\end{frame}

\begin{frame}
    As you have seen, $4$ points on the side of the triangle defines one
    parallelogram.

    \pause\vspace{1em}

    And how many ``quadruple'' of points are there on the extended side?

    \pause\vspace{1em}

    \textcolor{NordRed}{
        \[\boxed{{n+1 \choose 4}}\]
    }

    \pause\vspace{1em}

    So there are a total of ${n+1 \choose 4}$ parallelograms of this
    orientation.

    \pause\vspace{1em}

    The same goes for the other orientations as well!
\end{frame}

\begin{frame}
    So there are a total of $3\times {n+1 \choose 4}$ parallelograms!

    \pause\vspace{1em}

    Can you explain where we used bijection?

    \pause\vspace{1em}

    Yes \texttt{we used bijection to move from the set of parallelograms to the set of
    quadruples of points on the extended edge}, and it became very easy to
    count.
\end{frame}

\subsection{Conclusion}

\begin{frame}{Further Reader}
    \textcolor{NordOrange}{The Path to Combinatorics for Undergraduate} is a
    really nice boof for cominatorics and Bijection in specific.

    \pause\vspace{1em}

    \textcolor{NordOrange}{Yufei Zhao's Note}
    \url{http://yufeizhao.com/olympiad/bijections.pdf} is a really nice
    resource for bijection related problems.
\end{frame}

\begin{frame}
    In short, \textcolor{NordBrightBlue}{the technique to move from one hard
    to count set to an easy to count set is called Bijection}, it makes your
    life easier.

    \pause\vspace{1em}

    So whenever possible, think about applying bijection to problems
    \textcolor{NordBlack!30!NordWhite}{(after induction though, always apply
    induction at the very beginning)} and see if you can get anything nice :D

    % \pause\vspace{1em}

    % \textcolor{NordRed}{Happy Problem Solving and Good Luck for TST}

    % \textcolor{NordBlack!30!NordWhite}{(you will need it, a lot)}
\end{frame}

\end{document}