\documentclass[12pt]{article}

\include{preamble}

\newtoggle{spacingmode}
\toggletrue{spacingmode}  %STUDENTS: DELETE or COMMENT this line

\newtoggle{professormode}
\toggletrue{professormode} %STUDENTS: DELETE or COMMENT this line

\newcommand{\spc}[1]{\iftoggle{spacingmode}{\\ \vspace{#1cm}}}

\title{MATH 241 Fall \the\year~Homework \#3}

\author{Professor Adam Kapelner} % STUDENTS: DELETE my name and put your name and section here e.g. \author{John Doe, Section A}. MAKE SURE YOU PUT YOUR SECTION HERE!!!!!!!!

\iftoggle{professormode}{
\date{Due by email 11:59PM October 27, \the\year \\ \vspace{0.5cm} \small (this document last updated \today ~at \currenttime)}
}

\renewcommand{\abstractname}{Instructions and Philosophy}

\begin{document}
\maketitle

\iftoggle{professormode}{
\begin{abstract}
The path to success in this class is to do many problems. Unlike other courses, exclusively doing reading(s) will not help. Coming to lecture is akin to watching workout videos; thinking about and solving problems on your own is the actual ``working out''.  Feel free to \qu{work out} with others; \textbf{I want you to work on this in groups.}

Reading is still \textit{required}. For this homework set, read the section about foundations of probability in Chapter 2 and the section about conditional probability and in/dependence in Chapter 3. Chapter references are from Ross’s 7th edition. Also read the section about discrete random variables especially the Bernoulli, the Binomial and the Hypergeometric.

The problems below are color coded: \ingreen{green} problems are considered \textit{easy} and marked \qu{[easy]}; \inorange{yellow} problems are considered \textit{intermediate} and marked \qu{[harder]}, \inred{red} problems are considered \textit{difficult} and marked \qu{[difficult]} and \inpurple{purple} problems are extra credit. The \textit{easy} problems are intended to be ``giveaways'' if you went to class. Do as much as you can of the others; I expect you to at least attempt the \textit{difficult} problems.

This homework is worth 100 points but the point distribution will not be determined until after the due date. See syllabus for the policy on late homework.

Up to [see syllabus] points are given as a bonus if the homework is typed using \LaTeX. Links to instaling \LaTeX~and program for compiling \LaTeX~is found on the syllabus. You are encouraged to use \url{overleaf.com}. If you are handing in homework this way, read the comments in the code; there are two lines to comment out and you should replace my name with yours and write your section. If you are asked to make drawings, you can take a picture of your handwritten drawing and insert them as figures or leave space using the \qu{$\backslash$vspace} command and draw them in after printing or attach them stapled.

The document is available with spaces for you to write your answers. If not using \LaTeX, print this document and write in your answers. I do not accept homeworks not on this printout. Keep this first page printed for your records. Write your name below.

\end{abstract}

\thispagestyle{empty}
\vspace{1cm}
NAME: \line(1,0){240} %~~SECTION (A or B): \line(1,0){35}
\pagebreak
}

%\problem{We will be looking into the long term frequency definition here. For this problem, you must have \texttt{R} installed. Please download it from \url{http://cran.r-project.org/} (there are links for Windows, MAC and Linux) and then double-click to open an \texttt{R} console.}
%
%\begin{enumerate}
%
%\easysubproblem{To calculate combinations, use the \texttt{choose(n,k)} function. Calculate the number of five-card hands from a standard deck by copying the following code into \texttt{R} and then pressing enter:}
%
%\begin{knitrout}
%\begin{kframe}
%\begin{verbatim}
%choose(52, 5)
%\end{verbatim}
%\end{kframe}
%\end{knitrout}
%
%Please write down the answer. Is the answer the same as we computed in class? \spc{2.5}
%
%\easysubproblem{Verify the probability in class of a ``full house'' by copying the following code into \texttt{R} and then pressing enter:}
%
%\begin{knitrout}
%\begin{kframe}
%\begin{verbatim}
%choose(13, 1) * choose(4, 3) * choose(12, 1) * choose(4, 2) /
%  choose(52, 5)
%\end{verbatim}
%\end{kframe}
%\end{knitrout}
%
%Write down the answer as a \textit{percentage}. \spc{3.5}
%
%\intermediatesubproblem{\inred{This PDF must be downloaded to do this problem.} We are going to do a little experiment to explore the definition of probability as a limiting frequency. We will be looking at the context of flipping a coin and getting heads. Remember the definition was
%
%\beqn
%\prob{\braces{H}} = \limitn \frac{\displaystyle \sum_{i=1}^n \indic{\omega_i \in \braces{H}}}{n}
%\eeqn
%
%(where $\indic{T}$ is the \qu{indicator function} which equals 1 when the expression $T$ is true and 0 if the expression $T$ is false). We will run a simulation with large values of $n$. Copy and paste the following code into your \texttt{R} terminal:}
%
%\begin{knitrout}
%\begin{kframe}
%\begin{verbatim}
%N = 30000
%sims = sample(0:1, N, replace = T)
%freqs_by_n = array(NA, N)
%for (n in 1 : N){
%  freqs_by_n[n] = sum(sims[1:n]) / n
%}
%plot(10:N,
%  freqs_by_n[10:N],
%  xlim = c(10, N),
%  ylim = c(0.40, 0.60),
%  pch = ".",
%  xlab = "number of samples",
%  ylab = "frequency of heads",
%  main = "P(H) as a limiting frequency: 30,000 samples")
%abline(h = 0.5, col = "blue")
%freqs_by_n[N]
%#last line placeholder
%\end{verbatim}
%\end{kframe}
%\end{knitrout}
%
%\inred{If the code throws an error, download the PDF to your computer and try copying and pasting again. It does NOT work from a browser window. The PDF must be downloaded.}
%
%The console should have popped up a plot illustrating a \textit{real} statistical simulation. Each time you run this code it will be different. You can compare plots with your friends but take note that they will not look exactly the same. Print this out and attach it to your homework. If you are using \LaTeX, you can include the figure into the PDF.
%
%From the title of the plot and the x and y axes, tell a story about what is going on here \textit{in English}. \spc{2.5}
%
%\easysubproblem{What is the limiting frequency of heads after 30,000 coin flips to 3 decimals based on the simulation in the previous problem? (that is the number that appears in the console directly after ``\texttt{$>$ freqs\_by\_n[N]}''). Write it below and comment on its value.}\spc{1.5}
%
%\end{enumerate}

\problem{We will follow up here with questions on the Monte Hall game.

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=1.9in]{montehall.jpg}
\end{figure}
%\FloatBarrier
%\vspace{-0.7cm}
}

\begin{enumerate}

%\easysubproblem{Explain clearly how the gameshow is played --- all the rules of the host and the contestant.}\spc{4}

\intermediatesubproblem{In class, we used a tree to show the probability of winning is 2/3 if you switch and the car was in door 1. Complete the tree for if the car was in all doors initially and show the probability of winning is 2/3. It will take the whole page.}\spc{18}

%\hardsubproblem{Now imagine a variant of the game is played in the following way: there are four doors, you pick one and the game show host opens up two doors to reveal two goats. You now have the option to keep the door you selected initially or switch to the other door that remains closed. Use Bayes Theorem (not a tree). We will do this in class next Thursday.}\spc{10}

\extracreditsubproblem{Imagine the variant where there are now $n$ doors. You choose 1 and the game show host opens up $n-2$ doors to reveal $n-2$ goats. You have the option to keep the door you selected initially or switch to the other closed door. What is the probability of winning if you switch?}\spc{10}

\end{enumerate}

\problem{New York is a \qu{concrete jungle where dreams are made of.} To this extent, a young upstart tries to drum up business in three of the tallest buildings in the city. Below from left to right are pictured One World Trade Center (104 floors), the Empire State Building (82 occupied floors) and the Bank of America Tower (55 floors).

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=3in]{buildings.png}
\end{figure}
\FloatBarrier
}

\noindent Consider the case where this person enters one of the three buildings randomly and goes to a random floor.}

\begin{enumerate}
\easysubproblem{Draw a probability tree of this random event. Use ``...'' notation so you don't need to draw hundreds of lines.}\spc{14}

\easysubproblem{Are the building selection and floor selection \textit{independent} (\ie \textit{informationally irrelevant})? Justify your answer using the definition of statistical independence.}\spc{0.5}

\easysubproblem{What is the probability of the businessman winding up on floor 23 of One World Trade Center on a given day? }\spc{5}

\intermediatesubproblem{What is the probability of the businessman winding up on floor 23 of any building on a given day? }\spc{3}

\hardsubproblem{If the businessman is on floor 50, what is the probability he is in the Bank of America Tower? }\spc{3}

\extracreditsubproblem{In one week, the businessman was on floor 12, 15 18, 32 and 59. What is the probability he visited One World Trade Center for more than one of those days? }\spc{6}

\end{enumerate}

\problem{Probability is rooted in gambling and thus we will be analyzing some gambling games. We will introduce the game of Roulette here. Basically, there's a ball that is dropped onto a spinning wheel with pockets for the ball to fall once the wheel and ball run out of momentum. There are 18 red pockets and 18 black pockets. There are two flavors of the game:

\begin{itemize}
\item European: There is one additional pocket colored green and labeled 0 (for a total of 18+18+1=37 pockets). An example of this wheel is pictured below on the left.
\item American: There are two additional pockets colored green labeled 0 and 00 (for a total of 18+18+2=38 pockets). An example of this wheel is pictured below on the right.
\end{itemize}

The gambler can make bets on any of the spaces as well as red (R), black (B), green (G), an odd number, an even number and a slew of other exotic type bets which we won't enumerate. We will be analyzing payoffs when we get to random variables next week but we will not be discussing them now.}

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=4in]{roulette.png}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem{What is the probability of the ball landing in a black pocket? Calculate for both European and American roulette.}\spc{2}

\easysubproblem{What is the probability of the ball landing in a green pocket? Calculate for both European and American roulette.}\spc{2}

\easysubproblem{What is the probability you see RRBBBRGRBB in 10 spins in Las Vegas?}\spc{2}

\easysubproblem{In the 18 red pockets there are 9 even numbered pockets and 9 odd numbered pockets. What is the probability of getting a pocket wihich is both Red and Odd in Las Vegas?}\spc{1}

\easysubproblem{What is the probability you see a spin that is both Red and Green in Las Vegas?}\spc{0.5}

\easysubproblem{What is the probability you see a spin that is Red or Green in Amsterdam?}\spc{0.5}

\easysubproblem{In Las Vegas, you play the game 10 times and always bet on black. What is the probability you win all 10 times?}\spc{2}

\intermediatesubproblem{In Las Vegas, you see BBBBBBBBBB. Conditional on seeing this event, what is the probability the next spin is R?}\spc{2}

\intermediatesubproblem{In Las Vegas, what is the probability of BBBBBBBBBBR? This is the same situation as in the previous question, but framed differently (in order to confuse you).}\spc{2}

%\hardsubproblem{In Las Vegas, you play the game 10 times and always bet on black. What is the probability you win \textit{at least} once?}\spc{10}

\end{enumerate}

%\problem{We will review the hats problem.
%
%\iftoggle{professormode}{
%\begin{figure}[htp]
%\centering
%\includegraphics[width=2in]{hats.jpg}
%\end{figure}
%\FloatBarrier
%}
%
%
%
%\begin{enumerate}
%\easysubproblem{37 people enter a room wearing a hat. They all throw their hat into the center (like picture above) and everyone puts on a random hat. What is the approximate probability no one gets their hat? No need to derive it.}\spc{1}
%
%
%\easysubproblem{42 people enter a room wearing a hat. They all throw their hat into the center (like picture above) and everyone puts on a random hat. What is the approximate probability at least one gets their hat? No need to derive it.}\spc{1}
%
%\end{enumerate}

\iftoggle{professormode}{
\paragraph{Random Variables} The bulk of this class will be spent on random variables (and almost all of Math 368 is spent on rv's as well). \\ \\
}

\problem{In class we spoke about how random variables map outcomes from the sample space to a number \ie $X: \Omega \rightarrow \reals$. That is they are set functions, just like the probability function which is $\mathbb{P}: 2^\Omega \rightarrow \zeroonecl$. We will be investigating this concept here.

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=2.5in]{rv.jpg}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem{Here is a way to produce $X \sim \bernoulli{\half}$ using the $\Omega$ from a roll of a die. Map outcomes 1,2,3 to 0 and outcomes 4,5,6 to 1. This works because

\beqn
&&\prob{X=0} = \prob{\braces{\omega : X(\omega) = 0}} = \prob{\braces{1, 2, 3}} = 1/2 ~~\text{and} \\
&&\prob{X=1} = \prob{\braces{\omega : X(\omega) = 1}} = \prob{\braces{4, 5, 6}} = 1/2.
\eeqn

Describe two other scenarios or devices that produce their own $\Omega$'s that also result in $X \sim \bernoulli{\half}.$ Be creative (i.e. not boring).} \spc{4}

\intermediatesubproblem{We talked about in class how the sample space no longer needs to be considered once the random variable is described. Why? Use your answer to (a) to inspire this answer. Write it \textit{in English} below.} \spc{3}

\hardsubproblem{Back to philosophy... Let's say $X$ models the price difference that IBM stock moves in one day of trading. For instance, if the stock closed yesterday at \$56.24 and today it closed at \$57.24, the random variable would be \$1 for today. According to our definition of a random variable, there is a sample space with outcomes being drawn ($\omega \in \Omega$) that is \qu{controlling} the value of $X$. Describe it the best you can \textit{in English}. There are no right or wrong answers here, but your answer must be coherent and demonstrate you understand the question.} \spc{6}

\end{enumerate}

\problem{We will now study probability mass functions (PMF's) denoted as $p(x)$ and cumulative distribution functions (CDF's) denoted as $F(X)$ and review the r.v.'s we did in class.}

\begin{enumerate}

\easysubproblem{Draw the PMF for $X \sim \bernoulli{p}$.}  \spc{3}

\easysubproblem{Draw the CDF for $X \sim \uniformdiscrete{1,3,4,9}$.}  \spc{3}

\intermediatesubproblem{Using the r.v. from the previous question, what is $\prob{X \in (3,9)}$? I am trying to trick you here.} \spc{3}

\hardsubproblem{In class we defined the Bernoulli r.v. as:

\beqn
X \sim \begin{cases}
1 \withprob p \\
0 \withprob 1-p
\end{cases}
\eeqn

and put its PMF on the board. Write $p(x)$ for $X \sim \bernoulli{p}$ that is only valid for not only all values in the $\support{X}$ but all values in $\reals$. Hint: use the indicator function.} \spc{1}

\easysubproblem{What is the parameter space of $X$ where $X \sim \bernoulli{p}$ and why?}  \spc{1.5}

%\hardsubproblem{Sometimes knowing the $\Omega$ matters a little bit. Let's say $X_1 \sim \bernoulli{\half}$ is generated from one coin and $X_2 \sim \bernoulli{\half}$ is generated from another coin independently tossed. Create a new r.v. $T = X_1 + X_2$. Describe its PMF using the $\sim$ notation like in the previous problem. Thus write \qu{$T \sim$} something.} \spc{13}

\hardsubproblem{Consider the PMF we discussed for $X \sim \bernoulli{\half}$. Does $\myint{x}{}{}{p(x)} = F(x) + C$ where the constant $C \in \reals$? Explain. Think carefully about what integration really means.} \spc{5}

\hardsubproblem{How about the opposite? Consider the CDF we discussed for $X \sim \bernoulli{\half}$. Does $\frac{\text{d}}{\text{d}x}[F(x)] = p(x)$? Explain. Think carefully about what differentiation really means.} \spc{3}

\end{enumerate}

\iftoggle{professormode}{
\paragraph{Hypergeometric Distribution} This is a very interesting random variable and we will cover it thoroughly between this homework and the next one.\\ \\
}

\problem{The hypergeometric is sampling \qu{without replacement.} Imagine you have this bag of marbles with 37 marbles and 17 of them are black. We will define a \qu{success} as drawing a black marble.

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=2in]{marble.jpg}
\end{figure}
\FloatBarrier
}

\begin{enumerate}

\easysubproblem{Let's say you draw one marble. Call this r.v. $X$. Is it hypergeometric?} \spc{0.2}

\easysubproblem{The hypergeometric distribution has three parameters. What are the parameters for $X$?} \spc{2}

\easysubproblem{Write, but do not draw, the PDF, $p(x)$ for the r.v. $X$ where $x$ is the number of successes.} \spc{2}

\easysubproblem{What is the support of this r.v.?} \spc{2}

\intermediatesubproblem{There is another variable we learned about in class with this same support. Show that $X$ is distributed as this type of r.v. and find its parameter(s).} \spc{2}

\easysubproblem{Now imagine you draw 4 marbles without replacement. Call this r.v. $X$ (and forget about the previous r.v. $X$ from this question, parts a-e). How is $X$ distributed? Use the notation in class and find its parameters.} \spc{2}

\easysubproblem{What is the support of $X$?} \spc{2}

\easysubproblem{Write, but do not draw, the PMF of $X$.} \spc{3}

\easysubproblem{Draw the PMF of $X$.} \spc{6}

\easysubproblem{Draw the CDF of $X$.} \spc{6}

\easysubproblem{What is the probability of getting 4 successes in a row? Use the PMF.} \spc{3}

%\easysubproblem{What is the probability of getting 4 successes in a row? Use conditional probability. This should yield the same answer.} \spc{3}

\easysubproblem{Now imagine you draw 27 marbles without replacement. Call this r.v. $X$ (and forget about the previous r.v. $X$). How is $X$ distributed? Use the notation in class and find its parameters.} \spc{2}

\easysubproblem{What is the support of $X$?} \spc{1}

\easysubproblem{Why is $0 \notin \support{X}$?} \spc{1}

\easysubproblem{Write, but do not draw, the PMF of $X$.} \spc{1}

\hardsubproblem{Find the mode of this distribution. \qu{Mode} is defined as the most likely outcome result.} \spc{6}

\end{enumerate}

\problem{Generally, the hypergeometric has three parameters. We will solve for its support here under several disjoint conditions and then in class we will generalize it. Call $X$ a hypergeometric r.v. with all its parameters free - meaning they can take on any value, so please use the notation $n,~K,~N$ in your answers as we did in class. This problem is just copying from your class notes.}

\begin{enumerate}
\easysubproblem{Using the usual parameterization of the hypergeometric, describe the parameter space. You need to say what sets each of the parameters \qu{lives} in. }\spc{4}

\easysubproblem{Write, but do not draw, the PMF of $X$. }\spc{3}

%\intermediatesubproblem{ $x$ is the free variable in $p(x)$ which you wrote in (b) and it designates the number of successes. Show that successes and failure are essentiall the same thing by finding $p(n-x)$ and replacing $K$ with $N-K$. What does this teach you? }\spc{2.5}

\easysubproblem{ Let's say $n < K$ and $n < N-K$. What is the support of $X$ in this situation? }\spc{3}

\easysubproblem{ Let's say $n < K$ and $n \geq N-K$. What is the support of $X$ in this situation? }\spc{3}

\easysubproblem{ Let's say $n \geq K$ and $n < N-K$. What is the support of $X$ in this situation? }\spc{3}

\easysubproblem{ Let's say $n \geq K$ and $n \geq N-K$. What is the support of $X$ in this situation? }\spc{2.5}

\intermediatesubproblem{Find a formula using a sum for the CDF of the general hypergeometric r.v. }\spc{6}

\extracreditsubproblem{Demonstrate that the sum of the PMF over the support is 1 (on a separate piece of paper).}\spc{0}

\end{enumerate}

%
%\problem{We will look at the general hypergeometric distribution with large $N$.}
%
%\begin{enumerate}
%
%\easysubproblem{We will now begin deriving the binomial in pieces. Parameterize a hypergeometric by setting $K = pN$. What is the parameter space for $p$? }\spc{2.5}
%
%\easysubproblem{Write the PMF $p(x)$ for this r.v. using the $p$ parameterization using $x$ as the free variable. }\spc{2.5}
%
%\easysubproblem{What limit do we take and why are we taking this limit? }\spc{2.5}
%
%\easysubproblem{Rewrite the PMF without choose notation using only factorials and simplify the fraction by moving the factorial terms from denominator, $\binom{N}{n}$, to the numerator.}
%
%\beqn
%p(x) = \lim_{N \rightarrow \infty} \frac{(pN)!}{\inpurple{x!} (pN - x)!} \frac{((1-p)N)!}{\inpurple{(n-x)!} ((1-p)N - n + x)!} \frac{\inpurple{n!} (N-n)!}{N!}
%\eeqn
%
%\easysubproblem{Which three terms can you factor out from the limit expression? Show that they are equivalent to $\binom{n}{x}$. }\spc{5}
%
%
%\hardsubproblem{ Within the limit, you now have three ratios. Write these ratios by canceling out the common terms. For instance $10!/6! = 10 \times 9 \times 8 \times 7$ and $6!/10! = 1 / (10 \times 9 \times 8 \times 7)$. You have to get the indexing right.  }\spc{6}
%
%\intermediatesubproblem{ How many terms are in the numerator? How many terms are in the denominator.}
%
%There are $x$ terms in the first piece of the numerator and $n-x$ terms in the second piece of the denominator for a total of $n$ terms. Then there are $n$ terms in the denominator.
%
%\intermediatesubproblem{ Reason in English that the denominator looks like a bunch of $N - c_i$ where the $c_i$'s are all constants which are negligible as $N \rightarrow \infty$. }\spc{3}
%
%\intermediatesubproblem{ Reason in English that the numerator looks like a bunch of $Np - c_i$ where the $c_i$'s are all constants which are negligible as $N \rightarrow \infty$ as well as a bunch of $(1-p)N - c_i$ where the $c_i$'s are all constants which are negligible as $N \rightarrow \infty$.  }\spc{5}
%
%\intermediatesubproblem{ Match each $Np - c_i$ term in the numerator to one $N - c_i$ term in the denominator and take the limit of each one individually. Show that you wind up with $p \times p \times \ldots$ for a total of $x$ times, i.e. $p^x$. }\spc{4}
%
%\intermediatesubproblem{ Match each $(1-p)N - c_i$ term in the numerator to one $N - c_i$ term in the denominator and take the limit of each one individually. Show that you wind up with  $(1-p) \times (1-p) \times \ldots$ for a total of $n-x$ times, i.e. $ \tothepow{1-p}{n-x}$. }\spc{4}
%
%\easysubproblem{Using your answers from the previous parts to write the binomial r.v.'s PMF. }\spc{2}
%
%\extracreditsubproblem{ Imagine you are drawing $n$ successes with and without replacement. Derive a function of $N$ and $n$ which gives this percentage difference for $N$ and $n$ generally when the number of successes $K = \half N$. }\spc{7}
%
%
%\end{enumerate}

\problem{Imagine two Bernoulli r.v.'s $X_1$ and $X_2$ which model two fair coin flips where Heads is mapped to 1 and tails is mapped to 0. The probability of heads is 1/2.}

\begin{enumerate}

\easysubproblem{Given no other information, explain using the definition of r.v. independence why these two r.v.'s are independent. }\spc{2}

\easysubproblem{Given no other information, explain using the definition of equality in distribution why $X_1 \equalsindist X_2$. }\spc{2}

\easysubproblem{Are $X_1, X_2 \iid \bernoulli{p}$? Yes / no.}\spc{-0.5}

\intermediatesubproblem{ Now imagine these two coins were linked using some sort of sorcery. They are flipped at the same time but are guaranteed to flip the same way. That is, if the first coin goes heads, the second coin must go heads (and if the first coin goes tails, the second coin must go tails).

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=2.0in]{magiccoins.png}
\end{figure}
\FloatBarrier
}

Explain using the definition of r.v. independence why these two r.v.'s are \textit{dependent}.}\spc{4}

\hardsubproblem{ Using the same two sorcery-controlled coins, explain using the definition of equality in distribution why or why not $X_1 \equalsindist X_2$. }\spc{3}

%\easysubproblem{Let $T_2 = X_1 + X_2$. Is $T_2 \sim \binomial{2}{\half}$? Why or why not?  }\spc{4}

\end{enumerate}

\problem{We will now look at the binomial in general.}

\begin{enumerate}

\intermediatesubproblem{ Show using the definition of equals in distribution that $X_1 \equalsindist X_2$ if $X_1 \sim \bernoulli{p}$ and $X_2 \sim \binomial{1}{p}$.  }\spc{3}

\intermediatesubproblem{Imagine an infinite bag where 47\% of the balls are \qu{successes}. If I draw 87 balls, what is the probability I get 29 success balls?}\spc{3}

\intermediatesubproblem{Imagine I have a bag with 300 balls where $141$ of the balls are \qu{successes}. If I draw 87 balls \textit{with replacement}, what is the probability I get 29 success balls?}\spc{3}

\intermediatesubproblem{Why is your answers to (c) and (d) the same?}\spc{3}

\easysubproblem{Let $\Xoneton \iid \bernoulli{p}$. Give a real-life example of this situation}\spc{4}

\easysubproblem{Let $T_n = X_1 + \ldots + X_n$ where $\Xoneton \iid \bernoulli{p}$. How is $T_n$ distributed? }\spc{1}

%\hardsubproblem{Let $X_1, \ldots, X_n \iid \bernoulli{p}$ and $T_n = \sum_{i=1}^n X_i$. Derive the distribution of $T_n$ from first principles just like we did in the notes.}\spc{11}

\end{enumerate}

\end{document}


\section{Function Contracts}
\label{sect:functions}

Next we turn to the specification of functions. We'll take the example
from the previous section, and pull the computation of the minimum of
two numbers out into a separate function:

\vccInput[linerange={begin-}]{c/3.1.min2.c}

(The listing above presents both the source code and the output
of VCC, typeset in a different fonts, and
the actual file name of the example is replaced with \vcc{/*`testcase`*/}.)
VCC failed to prove our assertion, even though it's easy to see that
it always holds. This is because verification in VCC is \Def{modular}:
VCC doesn't look inside the body of a function (such as the definition of \vcc{min()})
when understanding the effect of a call to the function (such as
the call from \vcc{main()});
all it knows about the effect of calling \vcc{min()} is that the call
satisfies the specification of \vcc{min()}.
Since the correctness of \vcc{main()} clearly depends on what \vcc{min()}
does, we need to specify \vcc{min()} in order to verify \vcc{main()}.

The specification of a function is sometimes called its \Def{contract},
because it gives obligations on both the function and its callers. It
is provided by four types of annotations:
\begin{itemize}
\item A requirement on the caller (sometimes called a
  \Def{precondition} of the function) takes the form \vcc{_(requires E)},
  where \vcc{E} is an expression. It says that callers of the
  function promise that \vcc{E} will hold on function entry.

\item A requirement on the function (sometimes called a
  \Def{postcondition} of the function) takes the form \vcc{_(ensures E)},
  where \vcc{E} is an expression. It says that the function
  promises that \vcc{E} holds just before control exits the
  function.

\item The third type of contract annotation, a \Def{writes clause}, is described
  in the next section. In this example, the lack of writes clauses says that \vcc{min()}
  has no side effects that are visible to its caller.

\item The last type of contract annotation, a \Def{termination clause},
  is described in section \secref{termination}. For now, we won't bother
  proving that our functions terminate.
\end{itemize}

For example, we can provide a suitable specification for \vcc{min()} as
follows:
\vccInput[linerange={min-endmin,out-}]{c/3.2.min3.c}
\noindent
(Note that the specification of the function comes after the header and
before the function body; you can also put specifications on function
declarations (\eg in header files).)
The precondition \vcc{_(requires \true)} of \vcc{min()} doesn't
really say anything (since \vcc{\true} holds in every state), but is included
to emphasize that the function can be called from any state and
with arbitrary parameter values.
The postcondition states that the value returned from \vcc{min()}
is no bigger than either of the inputs.
Note that \vcc{\true} and \vcc{\result} are spelled with a backslash
to avoid name clashes with C identifiers.%
\footnote{
  All VCC keywords start with a backslash;
  this contrasts with annotation tags (like \vcc{requires}),
  which are only used at the beginning of annotation
  and therefore cannot be confused with C identifiers
  (and thus you are still free to have, \eg
  a function called \lstinline{requires} or \lstinline{assert}).}

VCC uses function specifications as follows. When verifying the body of a
function, VCC implicitly assumes each precondition of the function on
function entry, and implicitly asserts each postcondition of the
function (with \vcc{\result} bound to the return value and each
parameter bound to its value on function entry) just before the
function returns. For every call to the function, VCC replaces the
call with an assertion of the preconditions of the function, sets the
return value to an arbitrary value, and finally assumes each
postcondition of the function.
For example, VCC translates the program above roughly as follows:
\vccInput[linerange={begin-end}]{c/3.3.min_assert.c}

Note that the assumptions above are ``harmless'', that is in a fully
verified program they will be never violated, as each follows
from the  assertion that proceeds it in an execution%
\footnote{
A more detailed explanation of why this translation is sound is given in section \secref{soundness}.
}%
For example, the  assumption generated by a precondition could fail only if the
assertion generated from that same precondition  before it fails.

\begin{note}
\notehd{Why modular verification?}
Modular verification brings several benefits.
First, it allows verification to more easily scale to
large programs. Second, by providing a precise interface between
caller and callee, it allows you to modify the implementation of
a function like \vcc{min()} without having to worry about breaking the
verifications of functions that call it (as long as you don't change
the specification of \vcc{min()}). This is especially important
because these callers normally aren't in scope, and the person
maintaining \vcc{min()} might not even know about them (e.g., if
\vcc{min()} is in a library). Third, you can verify a function like
\vcc{main()} even if the implementation of \vcc{min()} is unavailable
(\eg if it hasn't been written yet).
\end{note}

\subsection*{Exercises}
\begin{enumerate}
\item
What is the effect of giving a function the specification
\vcc{_(requires \false)}? How does it effect verification of
the function itself? What about its callers? Can you think of a good
reason to use such a specification?

\item
Can you see anything wrong with the above specification of \vcc{min()}?
Can you give a simpler implementation than the one presented? Is this
specification strong enough to be useful? If not, how might it be
strengthened to make it more useful?

\item
Specify a function that returns the (\vcc{int}) square root of its (\vcc{int})
argument. (You can try writing an implementation for the function, but
won't be able to verify it until you've learned about loop
invariants.)

\item
Can you think of useful functions in a real program that might
legitimately guarantee only the trivial postcondition \vcc{_(ensures \true)}?
\end{enumerate}

\subsection*{Solutions}
\begin{enumerate}

\item
Any function with a spec that includes \vcc{_(requires \false)} should
verify. However, a call to such a function will only verify if the
call itself is dead code (and VCC can prove it is dead code). Putting
\vcc{_(requires  \false)} on a function is one way to document that nothing has
been proven about it, and that it should not be called.

\item
The spec guarantees that \vcc{min} returns a result that is small
enough, but nothing prevents it from always returning
\vcc{INT_MIN}. This might be strong enough for some applications, but
for most, you probably want the additional postcondition
\vcc{_(ensures \result == a || \result == b)}.

\item
\begin{VCC}
int sqrt(int x)
_(requires x >= 0)
_(ensures \result * \result <= x && (result + 1) * (\result + 1) > x)
;
\end{VCC}

\item
Many important system functions have no nontrivial guaranteed
postcondition, save those that come from the omission of writes clauses.
For example, a system call that tries to collect garbage might
very well have an empty specification.
\end{enumerate}

\subsection{Reading and Writing Memory}
\label{sect:writes}

When programming in C, it is important to distinguish two kinds of
memory access. \Def{Sequential} access, which is the default, is appropriate when
interference from other threads (or the outside world) is not an
issue, \eg when accessing unshared memory. Sequential accesses can be
safely optimized by the compiler by breaking it up into multiple
operations, caching reads, reordering operations, and so on.
\Def{Atomic} access is required when the access might race with other
threads, \ie write to memory that is concurrently read or written,
or a read to memory that is concurrently written. Atomic access is typically
indicated in C by accessing memory through a volatile type (or through atomic
compiler intrinsics). We consider only sequential
access for now; we consider atomic access in section \secref{concurrency}.

To access a memory object pointed to by a pointer \vcc{p}, \vcc{p}
must point to a valid chunk of memory%
\footnote{VCC actually enforces a stronger condition, that the memory
  is ``typed'' according to \vcc{p}.
}%
. (For example, on typical hardware, its virtual address must be suitably aligned,
must be mapped to existing physical memory, and so on.)
In addition, to safely access memory sequentially, the memory must not be concurrently
written by other threads (including hardware and devices). Most of the
time\footnote{
  It is also possible to read memory sequentially if it is a
  nonvolatile field of an object that is known to be closed, even if
  it is not owned by the thread; this allows multiple threads to
  sequentially access shared read-only memory.
}, this is because the memory object is ``part of'' something that
is ``owned'' by the thread (concepts that will be discussed later);
we express this with the predicate
\vcc{\thread_local(p)}\footnote{Note that thread locality only makes sense in
the context of a particular thread, and so cannot appear, for example,
in type invariants.} VCC asserts this
before any sequential memory access to (the memory pointed to by) \vcc{p}.

To write sequentially through \vcc{p}, you need to know \vcc{\thread_local(p)},
and that no other thread is trying to read (sequentially or
concurrently) through \vcc{p} at the same time\footnote{
  You also need to know that no object invariants depend on \vcc{*p};
  this is why object invariants are in effect only for closed objects, and only
  (parts of) open objects are mutable.
}. We write this as \vcc{\mutable(p)}. Like thread-locality,
mutability makes sense only in the context of a particular thread.
%%Writing via pointers other than \vcc{p} cannot make \vcc{p} non-mutable.

\begin{note}
If VCC doesn't know why an object is thread local, then it has
hard time proving that the object stays thread local after an operation
with side effects (\eg a function call).
Thus, in preconditions you will sometimes want to use
\vcc{\mutable(p)} instead of \vcc{\thread_local(p)}.
The precise definitions of mutability and thread locality
is given in \secref{ownership},
where we also describe another form of guaranteeing thread locality
through so called ownership domains.
\end{note}

The \vcc{NULL} pointer, pointers outside bounds of arrays,
the memory protected by the operating system, or outside
the address space are never thread local (and thus also never mutable
nor writable).

There is one further restriction on sequential writes, motivated by
the desire to limit the possible side effects of a function call to a
specific set of mutable objects. We could do this by adding a
postcondition that all other parts of the state are unmodified, but
VCC provides some sugar to make specification (and reasoning) about
such properties more convenient and efficient. The idea is that when
you call a function, you give it permission to write certain objects,
but not others; \vcc{\writable(p)} expresses the condition that
the function has the right to write to
\vcc{p}. Thus, when writing through \vcc{p}, VCC asserts
\vcc{\mutable(p) && \writable(p)}.

While mutability is a thread-level concept, writability is a property
of a particular instance of an executing function. (That is, just
because something is writable for you doesn't mean it will be writable
for a function you call.) Therefore, you can't express that a function
needs ``permission'' to write \vcc{p} by
\vcc{_(requires \writable(p))},
because preconditions are evaluated in the context
of the caller. Instead, you specify that a function needs writability
of \vcc{p} at function entry with the annotation \vcc{_(writes p)},
called a ``writes clause''. When you call a function, VCC assumes that
all of the objects listed in the writes clauses are writable on
function entry.  Moreover, if an object becomes mutable (for a thread)
after entry to a function call, it is considered writable within that
call (as long as it remains mutable).

%Moved this up -E
%Access to thread local memory will never crash the program.
%The memory that is not thread-local is covered in
%\secref{concurrency}.

Let's have a look at an example:
\vccInput[linerange={begin-}]{c/3.4.rw.c}
\noindent
The function \vcc{write_wrong} fails because \vcc{p} is only
mutable, and not writable.
In \vcc{read_wrong} VCC complains that it does not know
anything about \vcc{p} (maybe it's \vcc{NULL}, who knows),
in particular it doesn't know it's thread-local.
\vcc{read2} is fine because \vcc{\mutable} is stronger
than \vcc{\thread_local}.
Finally, in \vcc{test_them} the first three assertions succeed
because if something is not listed in the writes clause
of the called function it cannot change.
The last assertion fails, because \vcc{write()} listed \vcc{&a}
in its writes clause.

%\noindent Without the \vcc{\thread_local()} annotation you would
%get the following:
%\vccInput[linerange={begin-}]{c/01_read_wrong.c}

% the concept of validity (typed pointers) is gone from VCC3 --MM
%For each memory access within a program, VCC checks that the access is accessing a
%\Def{valid} memory object. Validity implies that the object address
%points to memory that is actually in the address space of the program
%(i.e., it has been allocated, either on the stack or on the heap, and
%has not been freed). (Validity in VCC additionally requires that the
%access is appropriately typed; this aspect is
%described in more detail in \secref{type-safety}).

Intuitively, the clause \vcc{_(writes p, q)} says that,
of the memory objects that are thread-local to the caller before the call,
the function is going to modify only the object pointed to by \vcc{p}
and the object pointed to by \vcc{q}.
%\todo{I think we should get rid of this footnote. For writes claiuses, the ownership domains
%are logically irrelevant; what matters is when the object became mutable.
%\footnote{
%And their ``ownership domains'',
%but until \secref{ownership} we consider these to be empty.
%}
In other words, it is roughly equivalent to a postcondition that ensures
that all other objects thread-local to the caller prior
to the call remain unchanged.
A function can have multiple writes clauses, and implicitly
combines them into a single set. If a function spec contains no writes clauses,
it is equivalent to specifying a writes clause with empty set of
pointers.

% Do we have essentially the same paragraph later? -E
%More precisely, an object is \vcc{\writable} if it is \vcc{\mutable} and
%it is either listed in a writes clause of the function,
%or it became \vcc{\mutable} sometime after the function was entered; the
%latter condition guarantees that either \vcc{p} was listed in the
%writes clause or was not thread-local in the caller when the call to
%the function was made.
%In particular, formal function parameters and local variables are
%writable as long as they are in scope and have not been explicitly
%wrapped (\secref{invariants}) or reinterpreted to a
%different type (\secref{reint}).  VCC asserts
%\vcc{\writable(p)} on each attempt to write to \vcc{*p}, as well as on
%each call to a function that lists \vcc{p} in a writes clause.

Here is a simple example of a function that visibly reads and writes
memory; it simply copies data from one location to another.
\vccInput[linerange={begin-}]{c/3.5.copy1.c}
In the postcondition the expression \vcc{\old(E)} returns the value
the expression \vcc{E} had on function entry.
Thus, our postcondition states that the new value of \vcc{*to}
equals the value \vcc{*from} had on call entry.
VCC translates the function call \vcc{copy(&x,&y)} approximately as
follows:
\begin{VCC}
_(assert \thread_local(&x))
_(assert \mutable(&y))
// record the value of x
int _old_x = x;
// havoc the written variables
havoc(y);
// assume the postcondition
_(assume y == _old_x)
\end{VCC}

%% Shouldn't \array_range be allowed for arrays of non-primitives also?
% Similar to \vcc{\thread_local_array},
%\vcc{\mutable_array(ar,sz)} is defined as
%\vcc{\forall unsigned i; i < sz ==> \mutable(&ar[i])}.
%
%The expression \vcc{\thread_local_array(ar, sz)} is
%syntactic sugar for \vcc{\forall unsigned i; i < sz ==> \thread_local(&ar[i])}.
%
%Programs that manipulate arrays often write to multiple array
%locations. Writes clauses actually allow sets of pointers, rather than
%individual pointers. We'll introduce sets in full generality later, but
%note one special case: the expression \vcc{\array_range(ar, len)}
%denotes the set of pointers \vcc@{&ar[0], &ar[1], ..., &ar[len-1]}@.
%Thus, a writes clause of the form
%\vcc{_(writes \array_range(ar,len))}
%allows writing to all elements of the array.

\subsubsection{Local Variables}
Unlike most block-structured languages, C allows you to take the
addresses of local variables (with the \vcc{&} operator). If you take
the address of a local, nothing prevents you from storing that address
in a data structure, and trying to dereference the address after the
lifetime of the variable has ended. (The result is not pretty.) Even
if you are careful about its lifetime, once you take the address of a
variable, you have to worry that writing through some seemingly
unrelated pointer might change the value of the variable, which is a
pain.

Because of this, VCC distinguishes between local variables whose
addresses are never taken and those whose addresses are taken; the
former are called \Def{purely local} variables. A purely local
variable is much, much easier to reason about; you know that its value
can be changed only by a an update through its name
(In particular, it cannot be modified by function calls, by
assignments to other variables, or assignments through pointers.)
Purely local variables are always considered thread-local (so there is
no thread-locality check when reading them) and writable (so you never
have to mention them in writes clauses of loops or blocks). Also, if
you have a loop that doesn't modify a purely local variable in scope,
VCC will automatically infer that the value of that variable is not
changed in the loop. So you should definitely keep variables purely
local whenever possible.

A local variable that is not purely local is treated as if it was
allocated on the heap when its lifetime starts (but without the
possibility of allocation failure), and is freed when the lifetime
ends.

The treatment of pure locality sometimes results in the strange
phenomenon that changing some code near the end of a function body can
cause verification of someting earlier in the function body to
fail. This is because if you take the address of a local near the
bottom, it is treated as impure for the whole function body. The
simplest workaround in such cases is just to introduce a new local for
the bottom part of the function\footnote{This won't effect the final
binary produced by a decent optimizing compiler.}.

\subsection{Arrays}
\label{sect:arrays}

Array accesses are a kind of pointer accesses.
Thus, before allowing you to read an element of an array VCC checks if it's thread-local.
Usually you want to specify that all elements of an array are thread-local,
which is done using the expression \vcc{\thread_local_array(ar, sz)}.
It is essentially a syntactic sugar for
\vcc{\forall unsigned i; i < sz ==> \thread_local(&ar[i])}.
The annotation \vcc{\mutable_array()} is analogous.
To specify that an array is writable use:
\begin{VCC}
_(writes \array_range(ar, sz))
\end{VCC}
which is roughly a syntactic sugar for:
\begin{VCC}
_(writes &ar[0], &ar[1], ..., &ar[sz-1])
\end{VCC}

For example, the function below is recursive implementation of the
C standard library \vcc{memcpy()} function:
\vccInput[linerange={begin-end}]{c/3.6.copy_array.c}
It requires that array \vcc{src} is thread-local,
\vcc{dst} is writable, and they do not overlap.
It ensures that, at all indices, \vcc{dst} has the
value \vcc{src}. The next section presents a more conventional implementation using a loop.

\subsection{Termination}
\label{sect:termination}
A function terminates if it is guaranteed to return to its
caller. You can specify termination for simple functions (like the
ones we've seen so far) by simply adding to the specification
\vcc{_(decreases 0)}; this will do the job as long as your functions
are not recursive. For functions that are recursive (or which look
potentially recursive to VCC, because of potential callbacks from
functions whose bodies are hidden from VCC), the termination clause of
a function gives a measure that decreases for each call that might start
a chain of calls back the function. For example, to verify the
termination of \vcc{my_memcpy} above, you need only add to its
specification the additional annotation \vcc{_(decreases len)}. This
annotation defines a ``measure'' on calls to \vcc{my_memcpy} (namely,
the value passed as the last parameter). VCC checks termination
by checking that (1) all loops in the body terminate
(\secref{loopTermination}), and
(2) for every function call within the body of
\vcc{my_memcpy} that is potentially the first of a chain of calls
leading to a call back to \vcc{my_memcpy}, the called function has a
\vcc{_(decreases)} specification and the measure of the call to that
function is strictly less than the measure of the calling function
instance.

It is usually a good idea to prove termination for sequential code
when you can\footnote{ You should also consider doing it for your
  concurrent code, but here VCC is much more limited in its
  capabilities. The reason for this is that proving termination for a
  racy function (e.g., one that has to compete for locks) typically
  depends on fairness assumptions (e.g., that a function trying to
  grab a spinlock will eventually get lucky and get it, if the
  spinlock is infinitely often free) and/or global termination
  measures (e.g., to make sure that other threads will release
  spinlocks once they acquire them). VCC does not currently support
  either of these.}.
More details on termination measures can be found in the VCC manual.

\subsection{Pure functions}
\label{sect:pureFunctions}
A \Def{pure function} is one that has no side effects on the program
state. In VCC, pure functions are not allowed to allocate memory, and can
write only to local variables. Only pure functions can be called within VCC
annotations; such functions are required to terminate\footnote{
  This is to guarantee that there is indeed a mathematical function
  satisfying the specification of the function.
}.

The function \vcc{min()} above
example of a function that can be declared to be pure; this is done by
adding the modifier \vcc{_(pure)} to the beginning of the function specification,
\eg
\begin{VCC}
_(pure) min(int x, int y) ...
\end{VCC}

Being pure is a stronger condition that simply having an empty writes
clause. This is because a writes clause has only to mention those side
effects that might cause the caller to lose information (\ie
knowledge) about the state, and as we have seen, VCC takes advantage
of the kind of information callers maintain to limit the kinds of side
effects that have to be reported.

A pure function that you don't want to be executed can be defined
using the \vcc{_(def)} tag, which is essentially a pure ghost function
(one that can be used only in specifications) that is inlined, and
uses the following streamlined syntax:
\vccInput[linerange={beginsp-endsp}]{c/3.7.issorted.c}
\noindent
A partial spec for a sorting routine could look like the following:%
\footnote{We will take care about input being permutation of the output in \secref{ghosts}.}
\vccInput[linerange={beginso-endso}]{c/3.7.issorted.c}

\subsection{Contracts on Blocks}

Sometimes, a large function will contain an inner block that
implements some simple functionality, but you don't want to refactor
it into a separate function (\eg because you don't want to bother with
having to pass in a bunch of parameters, or because you want to verify
code without rewriting it). VCC lets you conduct your verification as
if you had done so, by putting a function-like specification on the
block. This is done by simply writing function specifications
preceding the block, \eg
\begin{VCC}
...
x = 5;
_(requires x == 5)
_(writes &x)
_(ensures x == 6)
{
  x++;
}
...
\end{VCC}
VCC translates this by (internally) refactoring the block into a
function, the parameters of which are the variables from the
surrounding scope that are mentioned within the block (or the block
specifications), so blocks with contracts cannot have statements that
transfer control outside of the block. The advantages of this
translation is that within the block, VCC can ignore what it knows
about the preceding context, and following the block, VCC can
``forget'' what it knew inside the block (other than what escapes
through the \vcc{ensures} clauses); in each case, this results in less
distracting irrelevant detail for the theorem prover.

Sometimes, you don't care about information flowing into the block,
but only care about the information flowing out of the block. In this
case, you can use the precondition \vcc{_(requires \full_context())},
which tells VCC to verify the block using all of the information about
what came before, but using the postconditions and writes clauses to
hide information about what went on inside the block to the code that
follows the block.

%% \vccInput[linerange={swap-partition}]{c/04_partition.c}
%% \vccInput[linerange={foo-}]{c/01_swap1.c}

%% Because global variables (like \vcc{z}) might be visible to callers
%% of \vcc{copy()}, \vcc{copy()} needs to report that they might change.
%% Note that the writes clause lists pointers to memory locations, not lvalues
%% (\ie \vcc{_(writes &x, &y, &z)} and not \vcc{_(writes x, y, z)}).
%% Note also that because \vcc{&z} aliases neither \vcc{&x} nor \vcc{&y}, VCC can
%% deduce that \vcc{swap(&x, &y)} does not change \vcc{z}.

%% Had we left out the writes clause from the specification of
%% \vcc{swap()}, VCC would report several errors:
%% \vccInput[linerange={out-}]{c/01_swap2.c}
%% \noindent

%% Whenever a memory object is read, VCC asserts that it is
%% thread-local; whenever a memory object is written or is
%% mentioned in the writes clause of a function call, VCC asserts that it
%% is writable. Try removing each of the function annotations in
%% this example (one at a time) to see what error messages you get.

%% For example, the assert/assume translation of \vcc{swap()} is%
%% \footnote{
%%   VCC does not generate the read and writes checks for the local variable
%%   \vcc{tmp}. Because \vcc{tmp} is a local that never has its address
%%   taken, it can be thought of as remaining in a register, where it is
%%   guaranteed to remain writable.
%% }:

%% \vccInput[linerange={swap-}]{c/01_swap3.c}

%% \noindent
%% As we can see, one effect of \vcc{writes p, q} is the implicit
%% precondition \vcc{requires \writable(p) && \writable(q)}. Such precondition
%% needs to be checked in \vcc{foo()}, at the place where it calls \vcc{swap()}.
%% In other words, the called function can write at most what the caller can write.
%% In particular if we forget to list \vcc{&x} in the writes
%% clause of \vcc{foo()} we would get an error when it tries to call a
%% function that possibly writes \vcc{&x}:

%% \vccInput[linerange={out-}]{c/01_swap4.c}

%% You might wonder why cannot we just have
%% \vcc{requires \writable(p)} and instead have the specialized writes clause.
%% The reason is that the writes clause also specifies that nothing
%% outside of the writes clause will be changed.
%% This is why we can prove that \vcc{y} is still \vcc{42} after the call
%% to \vcc{boundedIncr(&x)}.

%A predicate \vcc{\mutable(p)} states that the object pointed to by \vcc{p}
%is allocated, ``belongs'' to the current thread, and is in a ``phase of life''
%that allows for modification.
%We will get into details all of these later.
%For now we just need to know that in order to be able to write to \vcc{*p}
%one needs to know that \vcc{p} was listed in the writes clause \emph{and} \vcc{\mutable(p)}.
%For example, if we remove the \vcc{_(writes ...)} clause from the
%\vcc{boundedIncr()} we get the following output:


%&latex
\documentclass[a4paper]{report}
\usepackage{commons/commons}
\usepackage{commons/note_style}
\usepackage{commons/code_style}
\usepackage[normalem]{ulem}
\usepackage{comment}
\usepackage{titlesec}
\titleformat{\chapter}[display]
  {\normalfont\sffamily\big\bfseries\color{black}}
  {\chaptertitlename\ \thechapter}{5pt}{\huge}
%\excludecomment{figure}
\usepackage{amsbsy}
\usepackage{amstext}

\graphicspath{{./fig/}}
\begin{document}

%+Title
\title{Machine Learning Quicksheet}
\author{Daniel D. Zhang}
\date{Winter 2015. \today}
\maketitle
%-Title

%+Abstract
%\begin{abstract}
%This is the notes for CSE 232A.
%\end{abstract}
%-Abstract

%+Contents
\tableofcontents
%-Contents
\chapter*{Preface}
\section*{Acronyms}
\begin{obeylines}
\rih{distr}. Distribution
\rih{Pr}. Probability
\end{obeylines}
\chapter{Introduction}
Mapping
$$
f: \mat{X} \ra \mat{Y}.
$$
Outline
\begin{enumerate}
\item Nonparametric methods
\begin{enumerate}
\item NN (nearest neighbor)
\item Decision tree

$\ra$ benefits: 1) unbounded input size; 2) arbitrary complex model
\end{enumerate}
\item Classification with parameterized models

General ways to classify
\begin{enumerate}
\item Generative, generative probability - Bayes, Fisher
\item Discriminative, boundary - SVM, logistic regression
\end{enumerate}
\item Combining classifiers
\item Representation learning
\end{enumerate}

\chapter{Nearest Neighbor}
\section{kNN}
\subsection{Parameter Selection $k$}
Let $\Gamma_k(S, x)$ be the prediction made on $x$ using its $k$-NN in $S.$
\begin{enumerate}
\item Hold-out set. Choose a subset $V\subset S$ as a validation set
$$
k = \arg\min_k \sum_{(x, y)\in V} \textbf{1}(\Gamma_k(S\backslash V, x)\neq y)
$$
\item Leave-one-out cross-validation
$$
k = \arg\min_k \sum_{(x, y)\in S} \textbf{1}(\Gamma_k(S\backslash\{(x, y)\}, x)\neq y)
$$
\end{enumerate}
\subsection{Distance function}
\subsubsection{$l_p$ norms}

\subsubsection{Metric space}
distance function $d: \mat{X}\times\mat{X}\ra \R$ is a metric iff:
\begin{enumerate}
\item $\geq0$
\item $=0$ iff equal
\item symmetry
\item triangle inequality
\end{enumerate}

\section{Statistical Learning Theory}
\begin{enumerate}
\item distribution over $x$, pdf: $\mu(x)$
\item distribution over $P(y=1|x)$: $\eta(x)$
\item classification rule $h: \mat{X}\ra\{0, 1\}$
\item bayes risk (general form): $R(h)=Pr\big(h(X)\neq Y\big)$
\begin{align*}
R(h) &= \E_x Pr\big(h(x)\neq y)\big) \\
&= \int pdf(x)\cdot Pr\big(h(x)\neq y)\big) \diff x
\end{align*}
\item Binary bayes-optimal classifier
\begin{eqnarray*}
h^*(x) = \left\{ \begin{array}{rl}
  1 &\mbox{ if }\eta(x)>\frac{1}{2}\\
  0 &\mbox{ ow}
       \end{array} \right.
\end{eqnarray*}
with mim risk:
\begin{align*}
R(h^*) &= \E_x \min\Big(\eta(X), 1-\eta(X)\Big) \\
&= \int pdf(x)\cdot \min\Big(\eta(X), 1-\eta(X)\Big) \diff x
\end{align*}

\end{enumerate}

\chapter{Decision Tree}
\section{Construction}
Greedy algorithm to split one of $\{leaves\}$ s.t. most decrease the uncertainty in tree. \subsection{Uncertainty measure}
\begin{enumerate}
\item Misclassification rate: $1-\max_i p_i$
\item Gini index: $\sum_{i\neq j} p_i p_j$
\item Entropy: $\sum_i p_i \log\frac{1}{p_i}$
\end{enumerate}
Reduction in uncertainty measure $u(\cdot)$.
$\Big(u(S)-(p_Lu(S_L)+p_Ru(S_R))\Big)|S|$
\subsection{Overfitting \& Pruning}
\begin{enumerate}
\item Training error
$$\hat\varepsilon(h) = \frac{1}{n}\sum_i \textbf{1}\Big(h(X^{(i)})\neq Y^{(i)}\Big)$$
\item True error, underlying error
$$\varepsilon(h)=Pr_{(x,y)\sim D}\Big(h(x)\neq y \Big)$$
, where $D$ is the underlying distribution.
\item Validation error, as an indicator for true error. Validation set $V$.
$$\varepsilon(h,V) = \frac{1}{|V|}\sum_{(x, y)\in V} \textbf{1}\Big(h(x)\neq y\Big)$$
If $V$ is chosen iid from $D$, then:
\begin{align*}
\E[\varepsilon(h, V)] &=\varepsilon(h)\\
\operatorname{Var}[\varepsilon(h, V)] &= \frac{1}{|V|}
\end{align*}
\end{enumerate}

\chapter{Generative Model}
\section{Bayes Generative Model}
\subsection{General}
Underlying distribution $D$ over $\mat{X}\times \mat{Y}$

The overall density is a mixture of the individual densities,
$$
Pr(x) = \sum_i \pi_i P_i(x)
$$

Then bayes predictions:
$$
Pr(y=j|x) = \frac{Pr(y=j)Pr(x|y=j)}{Pr(x)} = \frac{\pi_jP_j(x)}{\sum_i \pi_i P_i(x)}
$$
, where $\pi_j=Pr(y=j), P_j(x)=Pr(x|y=j)$, $y$ here is for one data point.

Notation-wise, $i$ is for data point index, $j$ is for class label index, $d$ for dim index.
\subsection{Model $P_i(x)$}
\subsubsection{Naive Bayes}
Assume indpt among the dimensions:
\begin{align*}
P_j(x) &= \prod_d P_{jd}(x_d) \\
P_{jd}(x_d) &= Pr(x_d|y=j)
\end{align*}
For binary input $\mat{X}=\{0, 1\}^p$
\begin{align*}
p_{jd} &= Pr(x_d=1|y=j)\\
\hat p_{jd} &= \frac{n_{jd}}{n_{j}} \text{ by ML}\\
\hat p_{jd} &= \frac{n_{jd}+\alpha}{n_{j}+D\alpha} \text{ smoothing, where } d= 0,...,D-1
\end{align*}
Binary-class classification, in \textit{power selection} notation:
$$
\hat y = \arg\max_j \quad \pi_j \prod_d p_{jd}^{x_d}(1-p_{jd})^{(1-x_d)}
$$
\subsubsection{Multinomial naive Bayes}
The example of application of multinomial naive Bayes is for document data.

Ignore the order of dimension (\textbf{bag of words}); each dimension can take multiple values.

Per value over value set $V$ per toplic:
$$
p = (p_1,..., p_{|V|})
$$
, where $\forall i, p_i\geq 0, \sum_i p_i=1$.

For input data $x=(x_1, ..., x_{|V|})$, where $x_i$ means count of value $V_i$ (term frequency), for each topic $j$:
\begin{align*}
Pr_j(x) = \prod_{i=1}^{|V|}p_{ji}^{x_i}
\end{align*}
Then the classification rule is:
$$
\hat y = \arg \max_j\quad \pi_j \prod_{i=1}^{|V|}p_{ji}^{x_i}
$$

In verbose mathematical expression:
\begin{align*}
P(y|x)\propto P(x|y)P(y)&=P(y)\prod_{d=1}^{D}P(v_d|y)^{|c_d|}\\
&=P(y)\prod_{i=1}^{N}P(x_i|y)
\end{align*}
\section{Gaussian}
\subsection{Basics}
\begin{align*}
& var(X) = \E(X-\mu)^2 = \E(X^2)-\mu^2 \\
& cov(X, Y) = \E[(X-\mu_X)(Y-\mu_Y)] = \E[XY]-\mu_X\mu_Y \\
& corr(X, Y) = \frac{cov(X, Y)}{\sigma(X)\sigma(Y)}
\end{align*}

\subsection{Multivariate Gaussian}
\subsubsection{Bivariate}
\begin{align*}
\mu &= [\mu_X, \mu_Y]^\top \\
\Sigma &= \begin{bmatrix}
\Sigma_{XX} & \Sigma_{XY} \\
\Sigma_{YX} & \Sigma_{YY}
\end{bmatrix}\\
&= \begin{bmatrix}
\operatorname{var}(X) & \operatorname{cov}(X,Y) \\
\operatorname{cov}(X,Y) & \operatorname{var}(Y)
\end{bmatrix}
\end{align*}
, the the values in $\Sigma$ can describe the shape of the bivariate gaussian distribution.

\subsubsection{Multivariate}
pdf:
$$
p(x) = \frac{1}{\sqrt{(2\pi)^p|\Sigma|}}\exp\Big(-\frac{1}{2}(\vec x-\vec\mu)^\top\Sigma^{-1}(\vec x-\vec\mu)\Big)
$$
, where $\vec\mu=\E \vec x, \Sigma=\E(\vec x-\vec\mu)(\vec x-\vec\mu)^\top=(X-\vec\mu)(X-\vec\mu)^\top$. Algebraically, $\Sigma_{ij}=\operatorname{cov}(X_i, X_j)$.

The $(\vec x-\vec\mu)^\top\Sigma^{-1}(\vec x-\vec\mu)$ can be rewrite as:
$$
(\vec x-\vec\mu)^\top\Sigma^{-1}(\vec x-\vec\mu) = \sum_{i, j} \Sigma^{-1}_{ij} (\vec x_i-\vec \mu_i)(\vec x_j-\vec \mu_j)
$$

\subsubsection{Classification}
\begin{align*}
\pi_1 P_1(x) > \pi_2 P_2(x) &\equiv x^\top Mx+2w^\top x \geq \theta \\
&\equiv x^\top\Big(-\frac{1}{2}(\Sigma_1^{-1}-\Sigma_2^{-1})\Big)x+2\Big(\Sigma_1^{-1}\mu_1-\Sigma_2^{-1}\mu_2\Big)^\top x \geq \theta
\end{align*}
Three cases of multivariate Gaussian generative model classification:
\begin{enumerate}
\item $\Sigma_i = \sigma_i I$
\item $\Sigma_1=\Sigma_2=\Sigma$
\item $\Sigma_1 \neq \Sigma_2$
\end{enumerate}
\section{Linear Algebra}
\subsection{PSD}
$$
A \text{ is real \textbf{sym}} \wedge\forall z\in \R^p\cdot z^\top Az \geq 0
$$
\subsection{eigen}
\begin{align*}
A \vec\mu_i &=\lambda_i \vec\mu_i\\
\text{Solving:} \\
\det(A-\lambda I) &= 0
\end{align*}
Spectral decomposition
\begin{align*}
A &= Q\Lambda Q^\top \\
A^{-1} &=Q\Lambda^{-1}Q^\top
\end{align*}

\subsection{SVD}
\begin{align*}
A &= U\Lambda V^\top\\
AA^\top &= U\Lambda^2U^\top \\
A^\top A &= V\Lambda^2V^\top
\end{align*}

\chapter{Discriminative Models}
Model $P(y|x)$ directly.
\section{Logistic Regression model}
Logistic regression is the discriminative model corresponding to Guassian gen model.

Loss function:
$$
\mat{L}(w) = -\sum_{i} \ln {P_w(y^{(i)}|x^{(i)})} = (-)(-) \sum_{i} \ln {\Big(1+\exp(-y^{(i)}\big(w^\top x^{(i)})\big)\Big)}
$$

Min $\mat{L}(w)$ by:
\begin{enumerate}
\item Gradient descent
\item Newton-Raphson
$$
w = w -\mat{H}^{-1} \frac{\partial}{\partial w} \mat{L}(w)
$$

, where $\mat H_{jk} = \frac{\partial^2}{\partial w_j \partial w_k} \mat L(w)$.
\end{enumerate}

\chapter{Linear Classification}
\section{Hyperplane}
Homogeneous linear separators. Adding extra feature (bias) to $\tilde x=(x,1)\in \R^{p+1}$.
Target linear classifier:
$$
y^{(i)}(w^\top x^{(i)}) > 0
$$
\section{Perceptron}
Core algorithm:
$$
w = w+yx
$$

, where $(x, y)$\footnote{More formally, $(\vec x, y)$} is \textbf{misclassified}.

Converge after this number of updates:
$$
\frac{R^2}{\gamma^2}
$$

, where $R=\max ||x^{(i)}||, \gamma $ is the margin s.t. $y^{(i)}(w^\top x^{(i)}) \geq \gamma$. If margin is large, converge fast.

Derivations:
\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.30]{perceptronTheory}}
\caption{Perceptron Proof, Theory}
\label{fig:perceptronTheory}
\end{figure}

\section{SVM}
\textbf{Target} linear classifier:
$$
y^{(i)} (w^\top x^{(i)}+b) \geq 1
$$

Solve \textbf{margin} $\gamma$:
\begin{align*}
w^\top x + b &=1 \text{ // on boundary}\\
w^\top(x-\gamma \hat w)+b &= 0 \text{ // on separator}
\end{align*}

Notice that $w \cdot \hat w=||w||$.

\textbf{Primal}
\begin{align*}
& \min_{w\in \R^p, b\in \R} \frac{1}{2}||w||^2\\
\text{s.t. } & \forall i,~ y^{(i)}(w \cdot x^{(i)}+b)\geq 1
\end{align*}

\textbf{Dual}
\begin{align*}
& \max_{\alpha \in \R^n} \sum_i \alpha_i -\sum_i\sum_j \alpha_i \alpha_j y^{(i)}y^{(j)}(x^{(i)}\cdot x^{(j)}) \\
\text{s.t. } & \sum_i \alpha_i y^{(i)} = 0\\
& \forall i,~ 0\leq\alpha_i
\end{align*}

Optimality and support vectors.
\begin{enumerate}
\item $w=\sum_i \alpha_i y^{(i)} x^{(i)}$
\item $\alpha_i >0 \ra y^{(i)}(w^\top x^{(i)}+b)=1$, and $x^{(i)}$ is support vector.
\end{enumerate}

With regularization (slack $\xi_i$)

\textbf{Primal}
\begin{align*}
& \min_{w\in \R^p, b\in \R} \frac{1}{2}||w||^2+C\sum_{i=1}^n\xi_i\\
\text{s.t. } & \forall i,~ y^{(i)}(w \cdot x^{(i)}+b)\geq 1 -\xi_i \\
& \forall i,~\xi_i \geq 0
\end{align*}

\textbf{Dual}
\begin{align*}
& \max_{\alpha \in \R^n} \sum_i \alpha_i -\sum_i\sum_j \alpha_i \alpha_j y^{(i)}y^{(j)}(x^{(i)}\cdot
x^{(j)}) \\
\text{s.t. } & \sum_i \alpha_i y^{(i)} = 0\\
& \forall i,~0\leq \alpha_i \leq C
\end{align*}

Decision rule with kernel $\mat{K}$:
\begin{align*}
h(x) &= \sign(w \cdot \phi(x))  \\
&= \sign\Big(\sum_i \alpha_iy^{(i)} \phi(x^{(i)}) \cdot \phi(x)\Big) \\
&= \sign\Big(\sum_i \alpha_i y^{(i)} \mat{K}(x^{(i)}, x)\Big)
\end{align*}

\section{Convex Surrogates for 0-1 Loss}
Convex loss functions to approximate 0-1 loss functions. 0-1 loss function is the ideal, but we need to use convex func as surrogate.

\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.00]{convexSurrogates}}
\caption{Convex Surrogate}
\label{fig:convexSurrogates}
\end{figure}

\chapter{Kernel}
\section{Kernel}
Mapping function $\phi(x)$.
\section{Kernel Trick}
Weights vector $w$ is a linear combination of $\phi(X): w=A^\top\phi(X)$.

Compute $\phi(x)\cdot \phi(x')$, or $<\phi(x), \phi(x')>$, without explicitly constructing $\phi(.)$.

\section{Kernel Functions}
General Form
\begin{align*}
k(x, x') = \phi(x)\cdot\phi(x')
\end{align*}

Polynomial kernel (high-dim space)
$$
k(x, x')=(1+x\cdot x')^d
$$

String kernel ($+\infty$ dim space: Hilbert space)

\begin{align*}
\phi_s(x)&= s.\text{tf in } x\\
\phi(x) &= (\phi_s(x)|\forall \text{ substring } s). \\
\end{align*}

Gaussian kernel
$$
k(x, x') = \exp\Bigg(\frac{-||x-x'||^2}{2\sigma^2}\Bigg)
$$

Kernel matrix: $\{x^{(1)},...,x^{(T)}\}\subset \mat{X}$.
$$
\mat{K}_{ij} = k(x^{(i)}, x^{(j)})
$$

, where $\mat{K}\in \R^{T\times T}$.

\textbf{Mercer's} condition: $\mat{K}$ is PSD.
\chapter{Regression}
\section{Optimization problem}
In \textbf{over-determined} system, no solution in closed form equation; thus need to transform the closed form eqn to min problem
$$
\mat{A}x=b \mapsto \min \|\mat{A}x-b\|_2^2
$$

To solve the optimization problem:
\begin{align*}
\nabla_x \|\mat{A}x-b\|_2^2 &= 0 \\
2\mat{A}^\T(\mat{A}x-b) &= 0
\end{align*}

Compare:
\begin{align*}
x &= \mat{A}^{-1} b  \\
x &= (\mat{A}^\T A)^{-1}\mat{A}^\T b
\end{align*}
$\mat{A}$ is $T\times dim$, thus too large to pseudo-inverse, thus Gradient Descent.

\chapter{Richer Output Space}
\section{Multiclass classification}
One weight vector $w_i$ per class.

\textbf{Predicting.}
$$
\arg\max_j w_j \cdot x
$$

\textbf{Learning.}
\begin{enumerate}
\item Logistic regression: convex, max data likelihood
$$
\max \prod_i Pr(y^{(i)}|x^{(i)})
$$
\item Perceptron: iterative improvement.

Given a misclassified point $(x, y)$, with misclassified label $\hat y$.
\begin{align*}
w_y &= w_y + x \\
w_{\hat y} &= w_{\hat y} - x
\end{align*}
\item SVM: convex, min sum of margin with constraints
\begin{align*}
&\min_{w_1, ..., w_k \in \R^p, \xi\in \R^n} \frac{1}{2}\sum_{j=1}^k ||w_j||^2+C\sum_i \xi_i \\
\text{s.t. } &\forall i, \forall y \neq y^{(i)},~ w_{y^{(i)}}\cdot x^{(i)} - w_y \cdot x^{(i)} \geq 1-\xi_i \\
& \forall i,~ \xi_i\geq0
\end{align*}
\end{enumerate}

\section{Structured output spaces}
Single weight vector $w$.
\begin{enumerate}
\item Feature Vector
$$
\Phi(x, y) = \big(\phi_1(x,y),...,\phi_k(x, y)\big)
$$
\item Predict using linear function of features
$$
\hat y = \arg\max_y w\cdot \Phi(x, y)
$$
\item Training
\begin{enumerate}
\item Perceptron
$$
w = w + \Phi(x^{(i)}, y^{(i)})-\Phi(x^{(i)}, \hat y)
$$

\item SVM
\begin{align*}
&\min_{w \in \R^k, \xi\in \R^n} \frac{1}{2}\sum_{j=1}^k ||w||^2+C\sum_i
\xi_i \\
\text{s.t. } &\forall i, \forall y \neq y^{(i)},~  w\cdot \Phi(x^{(i)}, y^{(i)})-w\cdot \Phi(x^{(i)}, y)\geq \Delta(y^{(i)}, y)-\xi_i \\
& \forall i,~ \xi_i\geq0
\end{align*}
\end{enumerate}
\end{enumerate}

\chapter{Boosting, Bagging, Random Forest}
Weighted weak classifier $h_t$

\section{Boosting}
\subsection{AdaBoost Algorithm}
\begin{python}[mathescape]
$D_0(i)=\frac{1}{n}, \forall i$
for t in xrange(T):
  train $h_t: \mat{X}\ra [-1, 1]$ using $D_t$
  calculate margin of success: $r_t=\sum_i D_t(i)y^{(i)}h_t(x^{(i)})\in [-1, 1]$
  calculate strength of update: $\alpha_t = \frac{1}{2}\ln {\frac{1+r_t}{1-r_t}}\in \R^+$
  reweight distribution: $D_{t+1}(i)\propto D_t(i)\exp\big(-\alpha_t y^{(i)}h_t(x^{(i)})\big)$

return $H(x)=\sign\Big(\sum_t \alpha_t h_t(x)\Big)$
\end{python}

The margin of success is also known as correlation. Note the label domain is $[-1, +1]$.
\subsection{Error Rate}
\subsubsection{Bound of Error Rate}
Training error drop off:
$$
e^{-\gamma^2T/2}
$$
, where $|\sum_i D_t(i)y^{(i)}h_t(x^{(i)})|\geq \gamma$ differs from random by \textbf{margin} $\gamma$.

\subsubsection{Analyzing the Training Error}
\begin{align*}
\varepsilon_t &= \hat{err}_{w_t}(h_t) \\
&= \sum_{i=1}^{N} w_t (x_i) \boldsymbol{1}(h_t(x_i)\neq y) \\
\end{align*}

, where $w_t(x_i)$ is the $D_t(i)$.

If $h_t$ is a random rule, then $\varepsilon_t = 0.5\sum_i w_t(x_i)=0.5$.

If $h_t$ is a weak rule, then $\varepsilon_t=\frac{1}{2}-\gamma_t$.

\subsection{Enhanced Sapce}
Enhanced space: $\forall h \in \mat{H}$ corresponds to a feature
$$
\phi(x) = \Big(h(x):h\in \mat{H}\Big)
$$

$\phi(x)$ represents the new space based.

Alternatively notation:
\begin{align*}
\vec{h}(x) = \Big(h_1(x), h_2(x), ..., h_{T}(x)\Big)
\end{align*}

Thus,
$$
H(x) = \sign\Big(\sum_t \alpha_t h_t(x)\Big)
$$

is a linear classifier in the enhanced space $\phi$.

Boosting is a \textbf{coordinate descent} procedure for minimizing the loss.

\subsection{Variational optimization}
Variational analysis, like gradient descent. think of the function as vec in $+\infty$ dim. Adaboost gd on the func vector.

\section{Bagging}
Sample with replacement, majority vote.

Note, if without replacement, when sample size = $T$, then all sample sets are the same.

\section{Random Forest}
Sample $n'$ with replacement, sample $k$ features, majority vote.

\chapter{Clustering}
Representation learning.

Learn true dof.

\section{$k$-means}
\subsection{Cost function}
$$
cost(\mu_1, ..., \mu_k) = \sum_{i=1}^n \min_j ||x_i-\mu_j||^2
$$

Sum of distance to the nearest center.

\subsection{Algorithm}
Lloyd’s $k$-means algorithm - EM\ algorithm
\begin{align*}
C_i^{(t)}& = \big \{ x_p : \| x_p - \mu^{(t)}_i \|^2 \le \big \| x_p - \mu^{(t)}_j \|^2 \ \forall j, 0 \le j < k \big\}  \\
\mu^{(t+1)}_i &= \frac{1}{|C^{(t)}_i|} \sum_{x_j \in C^{(t)}_i} x_j
\end{align*}

\subsection{Initialization}
\begin{enumerate}
\item Random
\item Extra centers, prune
\item $k$-mean++: iteratively add centers to $C$ from data points according to the following distribution
$$
Pr(x) \propto \big(\min_{\mu \in C}||x-\mu||\big)^2
$$

\end{enumerate}
\section{EM mixture of Gaussians}
\subsection{Predicting}
$$
Pr(x) = \sum_k \pi_k N(\mu_k, \Sigma_k)
$$

\subsection{Learning}
Learning the parameters of $Pr(x)$ (i.e. $\pi, \mu, \Sigma$) s.t.

$$
\max Pr(data|\sum_k \pi_k P_k)
$$

E-step:

calculate the probability of data point $x_i \in cluster~j$.
\begin{align*}
w_{ij} = Pr(cluster~j|x_i) = \frac{\pi_j P_j(x_i)}{\sum_j \pi_j P_j(x_i)}
\end{align*}

M-step:

M-step for parametric learning
\begin{align*}
\pi_j &= \frac{1}{n} \sum_{i=1}^n w_{ij} \\
\mu_j &= \frac{1}{n\pi_j} \sum_{i=1}^n w_{ij} x_i \\
\Sigma_j &= \frac{1}{n\pi_j} \sum_{i=1}^n w_{ij}(x_i-\mu_j)(x_i-\mu_j)^\top
\end{align*}
\begin{align*}

\end{align*}
\section{Agglomerative hierarchical clustering}
Linkage
\begin{enumerate}
\item Single link
\item Complete link
\item Average link
\end{enumerate}

\chapter{Linear Projection}
\section{Projection}
\begin{align*}
x &\in \R^p\\
U^\top x &\in \R^k \\
UU^Tx &\in \R^p
\end{align*}

\section{PCA}
$x \mapsto$ a single direction $u$:

The mean is $u^\top\mu$
\begin{align*}
\E(u^\top X) &= u^\top \E(X) \\
&= u^\top \mu
\end{align*}

The variance is $u^\top \Sigma u$:
\begin{align*}
Var(u^\top X) &= \E(u^\top X-u^\top \mu)^2\\
&= \E(u^\top(X-\mu))^2\\
&= \E(u^\top(X-\mu)(X-\mu)^\top u)\\
&= u^\top \E(X-\mu)(X-\mu)^\top u  \\
&= u^\top \Sigma u
\end{align*}

, where $X-\mu$ is a mat. Notice the order of mat multiplication when transforming $a^2$ to $aa^\top$ where $a$ is a scalar.

$x \mapsto$ multiple directions:

$$
\text{eigen}(\Sigma)
$$

$$
Au = \lambda u
$$

Spectral decomposition.
$$
M = U\Lambda U^\top
$$

, where $U^\top$ projects $\vec x$ to M's coordinates, $\Lambda$ scales $\vec x$, and then  $U$ projects back to original coordinates.
\section{Low-Rank Approximation}
\subsection{SVD}
\begin{align*}
M &= U\Lambda V^\top  \\
&= \sum_i \sigma_i u_i v_i^\top
\end{align*}
$M$ is not sym. $M\in \R^{p\times q}$
\begin{align*}
M &= U \Lambda V^\top \\
M &=UW^\top \\
\hat M &=\hat U \hat W^\top
\end{align*}

, where $\hat U$ is the first $k$ cols of $U$, $\hat W^\top$ is the first $k$ rows of $\hat W^\top$. For low-rank approximation.

\subsection{Applications}
\subsubsection{Latent semantic indexing (LSI)}
$k$ topics. Topic is latent.

\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=.80]{lsi}}
\caption{Latent semantic indexing. Notice that mat $\Psi$ is col-wise stacked by latent vectors rather than the data points. }
\label{fig:lsi}
\end{figure}

What the meanings of $n, V, k$ are.
\subsection{Collaborative filtering}
$k$ latent directions.

\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=0.98]{collaborativeFiltering}}
\caption{Collaborative Filtering. Notice that mat $W^\top$ is row-wise stacked by data points rather than latent vectors. }
\label{fig:collaborativeFiltering}
\end{figure}

What the meanings of $n, p, k$ are.

\chapter{Non-linear Projections}
\section{Manifold learning}
\subsection{ISOMAP}
ISOMAP finds \textit{geodesic distances} among data points.

Euclidian distance approximates geodesic distance in neighboring point

Steps:
\begin{enumerate}
\item Construct neighborhood graph: threshold $\tau$
\item Compute geodesic distance: Floyd Algorithm for shortest path
\end{enumerate}

\subsection{Distance-preserving Embedding MDS}
Distance matrix:
$$
D_{ij} = ||z^{(i)}-z^{(j)}||^2
$$

Gram matrix:
$$
B_{ij} = z^{(i)}\cdot z^{(j)}
$$

Relationship:
$$
B = -\frac{1}{2} HDH
$$

, where $H=I_n - \frac{1}{n}\boldsymbol1\boldsymbol1^\top$.

Solve $z^{(i)}$

\begin{align*}
B &= U\Lambda U^\top \\
&= ZZ^\top \\
Z &= U\Lambda^{1/2} \\
\end{align*}

In case of taking sqrt of negative, set negative numbers to 0. $Z=U\Lambda_+^{1/2}$
\section{Dictionary learning}
\subsection{Dictionary}
\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=.70]{dicLearn}}
\caption{Dictionary Learning}
\label{fig:dicLearn}
\end{figure}

Generalization of PCA
\begin{enumerate}
\item PCA
\item Indpt component analysis
\item Spare encoding: $\min L(\phi, S) $
\begin{align*}
L(\phi, S) &= ||X-\PhiS||_F^2 - \lambda\cdot sparsity(S) \\
&= \sum_{i=1}^n \Big(||x^{(i)}-\phi s^{(i)}||^2-\lambda\cdot sparsity(s^{(i)})\Big)
\end{align*}
\end{enumerate}

\subsection{Autoencoder}
Use fewer then $T$ hidden notes to represent the $T$ data points.

$$
x \approx D(E(x))
$$

\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.20]{autoencoder}}
\caption{Autoencoder}
\label{fig:autoencoder}
\end{figure}

\chapter{Prediction with Expert Advice}
\begin{itemize}
\item Statistical model: ML and distribution P
\item Non-statistical model: no assuming dist P
\end{itemize}

\section{Objective}
Notations:
\begin{enumerate}
\item Expert advices: collections of classifiers $\mat H$.
\item data: $x_t \in \mat X, y_t \in \mat Y$.
\end{enumerate}

\textbf{Objective}: Do as well as the best classifier from $\mat H$ in hindsight. i.e. min the regret.

\textbf{Binary prediction}:
$$
regret(T) = \sum_t \ell(y_t, \hat y_t) - \min_{h\in \mat H} \sum_t \ell(y_t, h(x_t))
$$

, where $\ell$ is the loss function.

\textbf{Randomized prediction}: choose a distribution over the expert predictions: $$
p_t = (p_t^{(1)},...,p_t^{(N)})
$$

Incur expected loss $\E_{i\sim p_t}[\ell_t^{(i)}]$, where $\ell_t^{(i)}=\ell(y_t, h_i(x_t))$.

regret = expected loss - loss of best learner:
$$
regret(T) = \sum_{t=1}^T \sum_{i=1}^N p_t^{(i)}\ell_t^{(i)} - \min_{1\leq i \leq N}\sum_{t=1}^\top\ell_t^{(i)}
$$

\section{Algorithm}
The hedge algorithm

At iteration $t$, for $i$-th expert:
\begin{align*}
p_t^{(i)} &\propto p_{t-1}^{(i)} e^{-\eta \ell_{t-1}^{(i)}} \\
p_t^{(i)} &\propto e^{\eta L_{t-1}^{(i)}}
\end{align*}

, where $L^{(i)}_t = \sum_{t'\leq t} l^{(i)}_{t'}$, the cumulative expected loss.

\textbf{Theorem}. Bound the cumulative expected loss of hedge algorithm:
$$
L^\text{Hedge}_T\leq \min_{1\leq i \leq N} \frac{\eta L^{(u)}_T+\ln N}{1-e^{-\eta}}
$$

Tuning $\eta$ to find the lower bound of $L^\text{Hedge}_T$:

Setting
$$
\eta = \ln \Bigg(1+\sqrt{\frac{2\ln N}{\tilde L}}\Bigg)
$$

, where $\min_i L_i^\top \leq \tilde L $.

Then $L_T^\text{Hedge}=O(\sqrt{2T\ln N})$

\chapter{Neural Networks}

\section{Supervised Neural Networks}

\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.20]{nnBasic}}
\caption{Supervised Neural Network}
\label{fig:nnBasic}
\end{figure}

\subsection{Forward Propagation}

$$
a^{(l+1)} = \sigma\Big(W^{(l)}a^{(l)}+b^{(l)}\Big)
$$

, where $W_{ij}^{l}$ denotes the weight from node $j$ at layer $l$ to node $i$ at layer $l+1$. $b_i^{(l)}$ is the bias for node $i$ at layer $l+1$. $a_i^{(l)}$ is the node $i$ at layer $l$. $\sigma(.)$ is the sigmoid function.

Define input value $z^{l+1} = W^{(l)}a^{(l)}+b^{(l)}$.

\subsection{Backward Propagation}
Loss function:
\begin{align*}
\mat{J}(W,b) = \frac{1}{T} \sum_{t} \frac{1}{2} \| h_{W,b}(x^{(i)})-y^{(i)}\|^2+\frac{\lambda}{2} \sum_{l=1}^{n_l-1} \sum_i \sum_j \big(W_{ij}^{(l)}\big)^2
\end{align*}
Batch gradient descent. For node $j$ at layer $l$.

Output layer:
$$
\delta_j^{(n_l)} = \frac{\partial}{\partial z_j^{(n_l)}} \frac{1}{2} \| y-h_{W, b}(x)\|^2 = -(y_j - a_j^{(n_l)}) \cdot \sigma'(z_j^{(n_l)})
$$

Hidden layer:
$$
\delta_j^{(l)} = \Big(\sum_i W_{ij}^{(l)} \delta_j^{(l+1)}\Big) \cdot \sigma'(z_j^{l})
$$

After vectorization:
\begin{align*}
\delta^{(n_l)} &= -(y - a^{(n_l)}) \cdot  \sigma'(z^{(n_l)}) \\
\delta^{(l)} &= \Big( W^{(l)}^\top \delta^{(l+1)}\Big) \cdot \sigma'(z^{l})
\end{align*}
$\cdot$ for element-wise multiplication.

Then for partial derivative
\begin{align*}
\nabla_{W^{(l)}} \mat{J}(W, b; x, y) &= \delta^{(l+1)} a^{(l)}^\top \\
\nabla_{b^{(l)}} \mat{J}(W, b; x, y) &= \delta^{(l+1)}
\end{align*}

Then for batch gradient descent.
\begin{align*}
W^{(l)} &= W^{(l)} - \eta\Big[\frac{1}{T}\sum_t \nabla_{W_{(l)}} \mat{J}(W, b; x^{(t)}, y^{(t)}) + \lambda W^{(l)}\Big] \\
b^{(l)} &= b^{(l)} - \eta \Big[\frac{1}{T}\sum_t \nabla_{b_{(l)}} \mat{J}(W, b; x^{(t)}, y^{(t)})\Big]
\end{align*}
\section{Activation Functions}
\subsection{Sigmoid function}
\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.20]{sigmoid}}
\caption{Sigmoid Function}
\label{fig:sigmoid}
\end{figure}
$$
\sigma(z) = \frac{1}{1+\exp(-z)}
$$

Properties:
\begin{align*}
1-\sigma(z) &= \sigma(-z) \\
\sigma'(z) &= \sigma(z)\sigma(-z)
\end{align*}

Can be derived from the sigmoid function graph.

\subsection{Rectified Linear Unites}
\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=2.30]{rectifier}}
\caption{Rectifier}
\label{fig:rectifier}
\end{figure}
$$
f(x) = \max(0, x)
$$

A smooth approximation to the rectifier is the analytic function - softplus function.
$$
f(x) = \ln(1 + e^x)
$$

$$
\frac{\diff}{\diff x} \ln(1 + e^x) = \sigma(x)
$$

The derivative of softplus is the logistic function.

\section{Convolution Neural Networks}
\subsection{CNN Architecture}
\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.30]{cnn}}
\caption{CNN}
\label{fig:cnn}
\end{figure}

Transform inoupt 3D volume to output 3D volume
\subsection{Convolving Layer}
$$
sz'= (sz-fieldSz+2padSz)/strideSz+1
$$

\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.30]{convolve}}
\caption{Convolution}
\label{fig:convolve}
\end{figure}
\subsection{Pooling Layer}
\begin{figure}[!htp]
\centering
\subfloat{\includegraphics[scale=1.20]{pooling}}
\caption{Pooling}
\label{fig:pooling}
\end{figure}
\end{document}