\documentclass{article}
\usepackage{amssymb}
\usepackage{comment}
\usepackage{courier}
\usepackage{fancyhdr}
\usepackage{fancyvrb}
\usepackage[T1]{fontenc}
\usepackage[top=.75in, bottom=.75in, left=.75in,right=.75in]{geometry}
\usepackage{graphicx}
\usepackage{lastpage}
\usepackage{listings}
\lstset{basicstyle=\small\ttfamily}
\usepackage{mdframed}
\usepackage{parskip}
\usepackage{ragged2e}
\usepackage{soul}
\usepackage{upquote}
\usepackage{xcolor}
\usepackage[ampersand]{easylist}

% http://www.monperrus.net/martin/copy-pastable-ascii-characters-with-pdftex-pdflatex
\lstset{
upquote=true,
columns=fullflexible,
literate={*}{{\char42}}1
         {-}{{\char45}}1
         {^}{{\char94}}1
}
\lstset{
  moredelim=**[is][\color{blue}\bf\small\ttfamily]{@}{@},
}

% http://tex.stackexchange.com/questions/40863/parskip-inserts-extra-space-after-floats-and-listings
\lstset{aboveskip=6pt plus 2pt minus 2pt, belowskip=-4pt plus 2pt minus 2pt}

\usepackage[colorlinks,urlcolor={blue}]{hyperref}

\begin{document}

\fancyfoot[L]{\color{gray} C4CS -- F'18}
\fancyfoot[R]{\color{gray} Revision 1.0}
\fancyfoot[C]{\color{gray} \thepage~/~\pageref*{LastPage}}
\pagestyle{fancyplain}

\title{\textbf{Office Hours ++ (Unit Testing and Python)\\}}
\author{\textbf{\color{red}{Due: Sunday, November 11th, 11:59PM (Hard Deadline)}}}
\date{}
\maketitle

\section*{Submission Instructions}
Submit this assignment on \href{https://gradescope.com/courses/24368}{Gradescope}.
You may find the free online tool \href{https://www.pdfescape.com}{PDFescape}
helpful to edit and fill out this PDF.
You may also print, handwrite, and scan this assignment.

\section*{Adding Features to Your Python RPN Calculator}

Building off of what was covered in lecture, work on using the test-driven development methodology and implement \textbf{three (3)} of the features from the list below. Note that this list is not comprehensive, so if there is a different feature that you would like to build that is non-trivial (e.g. implementing a function that would just change the sign on a number would be trivial), you may choose to build that instead.

In order to receive credit, you will need to prove the test cases for your additional functions initially fail, and then later pass when the function is fully implemented.

\subsection*{Feature List:}
Choose \textbf{three (3)} of the following features to build. You may elect to build any number of custom features and each will count as a separate feature - you are not restricted to just one custom feature.

\begin{itemize}
  \item[$\square$] \textbf{Implement additional calculator keys}

  For this feature, implement the following subfeatures:
	\begin{itemize}
	\item[$\square$] Calculate percentages using the \texttt{\%}. You may find \href{https://blogs.msdn.microsoft.com/oldnewthing/20080110-00/?p=23853}{this article} helpful.
	\end{itemize}
	\begin{itemize}
	\item[$\square$] Calculate exponent using \texttt{\^}.
	\end{itemize}
	\begin{itemize}
	\item[$\square$] Perform integer division using \texttt{//}.
	\end{itemize}
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Implement bitwise operators (and, or and not)}
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Implement a basic math library}

  For this feature, implement the following subfeatures:
  \begin{itemize}
	\item[$\square$] Allow for usage of constants (\texttt{pi}, \texttt{e}, ...)
	\end{itemize}
	\begin{itemize}
	\item[$\square$] Binary functions
	\end{itemize}
	\begin{itemize}
	\item[$\square$] Unary functions (\texttt{sin}, \texttt{cos}, etc.)
	\end{itemize}
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Degrees and Radians Mode}

	This would follow from the above feature. Add a command/method which can set whether trigonometric operations use degrees or radians. For example, by entering the keyword \texttt{rad}, if the operation \texttt{3.1415 sin} is entered, the output would be \texttt{0}. Equivalently, if the mode is set using \texttt{deg}, if the operation \texttt{360 cos} is entered, the output would be \texttt{1}.\newline
\end{itemize}

\newpage

\begin{itemize}
  \item[$\square$] \textbf{Use the Results of a Previous Calculation}

	Add the ability to use the results of your previous calculation in the next calculation. For example, if we have the first input as \texttt{2 3 +} followed by the next command \texttt{:ans 7 +}, the output should be \texttt{12}. In order to implement this, you will need to define a language - in the example we used the colon(:) to denote this special variable \newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Summation}

Implement a command that find the sum of all of the elements on the stack and adds this result to the top of the stack.\newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Different Base Number Systems}

Add a command or method which will allow the user to use your calculator for calculations in different base number systems. For example, using an option \texttt{hex} and then entering the input \texttt{A A +} should result in the output \texttt{14}.\newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Factorial Operator}

Implement the factorial operator. For example, the input \texttt{4 !} should return the output \texttt{24}. \newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Error Handling for Division by Zero}

Implement an error handling method that perevents division by 0 errors and saves the user's existing state. For example, if the stack initually contained \{\texttt{1}\} and a user enters \texttt{4 0 /}, output a helpful error message and preserve the initial stack before these values were entered (i.e. \{\texttt{1}\}). \newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Session History}

Implement a command such that when it is called, it outputs the standard format of the previous operation. For example, if the previous input was \texttt{3 2 +}, calling this command will print \texttt{3 + 2 = 5}.\newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Convert Between Decimal and Fraction}

Implement a command to convert the item on the stack from decimal to fraction and vice-versa. For example, calling this command when the item at the top of the stack is \texttt{0.75}, the output should return \texttt{3/4}.\newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Repeat Operator}

Implement a repeat operator that will repeatedly carry out a provided binary operation on all items provided in the input line. For example, the input \texttt{4 2 6 * !} (where ! is the repeat operator) would result in the output \texttt{48}, regardless of whatever was previously on the stack.\newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Rotate}

Implement a command that will rotate the order of all items currently on the stack. For example, if the stack currently contains \{\texttt{2 4 6}\}, after calling this operator the stack should be \{\texttt{6 4 2}\}.\newline
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Copy}

Implement a command that will add a copy of the current top element to the stack. For example, if the stack currently contains \{\texttt{2 4 6}\}, after calling this operator the stack should be \{\texttt{2 2 4 6}\}.\newline
\end{itemize}

\newpage

\begin{itemize}
  \item[$\square$] \textbf{Allow a Persistent Stack}

Make the stack for your RPN calculator persistent. For example, the input \texttt{1 2 3 +} should not produce any errors. From this, the prompt should also be customized to display information about the stack. Using the previous example, the prompt could now be customized to be \texttt{rpn calculator [1, 5]}, or something similar.

\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Add a Memory System}

Add a basic memory system to your calculator. This would be equivalent to the \texttt{M+}, \texttt{M-}, \texttt{MR} and \texttt{MC} on a regular calculator. This could be extended to add a near infinite amount of memory registers by defining another special character (as in the "Use the Results of a Previous Calculation). For example, these registers could be called using the \texttt{\&} key: \texttt{\&myval+}, \texttt{\&c4cs-}, etc.

\end{itemize}

\begin{itemize}
	\item[$\square$] \textbf{Read data in from an external file}

Add the ability to read in data from an external file for your calculator. This external file could be formatted in a style of your choice (csv, tsv, etc.).
\end{itemize}

\begin{itemize}
  \item[$\square$] \textbf{Custom Feature!}

If there is a meaningful command not listed above and currently not already implemented by the calculator, you may choose to implement this instead. For example, you could choose to use other Python libraries (such as numbers, cmath, decimal, fractions, statistics, random, NumPy, SciPy, etc) to add new features. \newline
\end{itemize}

\newpage
\section*{Feature 1:}
\subsection*{Test Code:}
\vspace*{5cm}
\subsection*{Screenshot of Failing Test:}
\vspace*{5cm}
\subsection*{Implementation Code:}
\vspace*{5cm}
\subsection*{Screenshot of Passing Test:}
\vspace*{5cm}

\newpage
\section*{Feature 2:}
\subsection*{Test Code:}
\vspace*{5cm}
\subsection*{Screenshot of Failing Test:}
\vspace*{5cm}
\subsection*{Implementation Code:}
\vspace*{5cm}
\subsection*{Screenshot of Passing Test:}
\vspace*{5cm}

\newpage
\section*{Feature 3:}
\subsection*{Test Code:}
\vspace*{5cm}
\subsection*{Screenshot of Failing Test:}
\vspace*{5cm}
\subsection*{Implementation Code:}
\vspace*{5cm}
\subsection*{Screenshot of Passing Test:}
\vspace*{5cm}

\end{document}


\documentclass[12pt]{article}

\include{preamble}

\title{Math 241 Fall 2016 \\ Midterm Examination One}
\author{Professor Adam Kapelner}

\date{September 22, 2016}

\begin{document}
\maketitle

\noindent Full Name \line(1,0){270} ~~~ Section (A or B)~ \line(1,0){30}

\thispagestyle{empty}

\section*{Code of Academic Integrity}

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

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

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

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

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

\normalsize

\section*{Instructions}

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

\pagebreak

\problem The Canterbury Park racetrack in Shakopee, Minnesota usually is home to horse racing, but at times they host a whimsical Ostrich race called the \qu{Don't Lay an Egg Dash}.  Supposedly, ostriches are about as fast as horses. You can train a horse, but ostriches are erratic and true wild cards.

\begin{figure}[htp]
\centering
\includegraphics[width=6in]{dontlayanegg.png}
\end{figure}

In this race (see above) there are 5 ostriches named \qu{Flightless Fred}, \qu{Longneck Ned}, \qu{Zippy}, \qu{Try-n-Fly} and \qu{Birdman}. One will win the race (come in first), one will come in second, etc.

\benum
\subquestionwithpoints{3} How many possible distinct outcomes are there in this race? \spc{1}

\subquestionwithpoints{4} Would the assumption of each of these distinct outcomes being equally likely be a good assumption? Why or why not? \spc{3}

\subquestionwithpoints{2} Despite what you wrote for (b), assume each outcome is equally likely for the remainder of the problem. What is the probability \qu{Flightless Fred} wins? \spc{2}

\subquestionwithpoints{4} What is the probability \qu{Flightless Fred} wins, \qu{Birdman} comes in second and \qu{Try-n-Fly} comes in third? Note: this is called a \qu{trifecta} bet at the track. \spc{4}

\subquestionwithpoints{5} What is the probability \qu{Flightless Fred}, \qu{Birdman} and \qu{Try-n-Fly} all finish in the top 3 in an unspecified order? Note: this is called a \qu{trifecta-box} bet at the track. \spc{4}

\subquestionwithpoints{6} What is the probability \qu{Flightless Fred} finishes in the top 3 places? Note: this is called a show bet at the track. \spc{8}

\subquestionwithpoints{5} What is the probability \qu{Flightless Fred} and \qu{Birdman} finish neck-neck? They don't have to win, but they have to finish one after the other. \spc{5}

\subquestionwithpoints{5} The jockeys (the person riding the bird) on \qu{Flightless Fred}, \qu{Longneck Ned} and \qu{Zippy} are women and the jockeys on \qu{Try-n-Fly} and \qu{Birdman} are men. What is the probability first, second and third place feature 2 women and one man in no particular order? \spc{5}

\subquestionwithpoints{3} What is the number of unique probability questions can I ask about the outcome of this race? \spc{2}

\eenum

\problem This problem is about the philsophical theory of probability.

\benum
\subquestionwithpoints{2} Let $p := \prob{\text{Trump wins the election in November}}$. Posit a \textit{numeric answer} for $p$. \spc{2}

\subquestionwithpoints{5} Explain why there is no \qu{correct} answer to (a). Make sure to mention in your answer which definition of probability you are invoking. \spc{2}

\subquestionwithpoints{2} Would Laplace believe that the event \qu{Trump wins the election in November} to be \emph{truly} random? Yes/no only. \spc{2}
\eenum

\problem This problem is about sets and the mathematical theory of probability.

\benum

\subquestionwithpoints{3}Simplify $\braces{\varnothing} \cap \braces{\braces{\varnothing}}$. \spc{0.5}

\subquestionwithpoints{3} Let $A = \braces{1,2,3}$ and $B = \braces{1,2,3,4}$. Simplify $2^A \cap 2^B$. \spc{0.5}

\subquestionwithpoints{3} Compute $\cprob{A}{A^C}$. \spc{0.5}

\subquestionwithpoints{6} Consider events $A$ and $B$. Prove that $\prob{A \cup B} \leq \prob{A} + \prob{B}$. Use the \qu{axioms} of the probability set function and your knowledge of set theory and \textit{any} theorems from class that may be useful. \spc{12}

\eenum

\problem For the most part, motor vehicles are produced with one of two types of transmissions: manual and automatic. Manual is less common in cities because it's considered more burdensome for drivers, especially in traffic.

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

The percentage of new manual vehicles produced for 2017 is 46.2\%. Americans consume 21.1\% of worldwide vehicles. And 6.5\% of new vehicles in American are estimated to be manual.

\benum
\subquestionwithpoints{2} Let $A$ denote the event that a new car is in America and $M$ denote the event that a new car has a manual transmission. What is $\prob{A}$? Compute explicitly. \spc{1}

\subquestionwithpoints{2} Let $A$ denote the event that a new car is in America and $M$ denote the event that a new car has a manual transmission. What is $\prob{M}$? Compute explicitly. \spc{1}

\subquestionwithpoints{2} Let $A$ denote the event that a new car is in America and $M$ denote the event that a new car has a manual transmission. What is $\cprob{M}{A}$? Compute explicitly. \spc{1}

\subquestionwithpoints{4} What is the probability that the car is in America given that it is a manual transmission? Compute explicitly. \spc{5}

\subquestionwithpoints{7} How much more likely is a car to be manual outside of America than within America? Compute explicitly. \spc{9}

\subquestionwithpoints{3} [Extra Credit] Odds Against an event $A$ is defined as $\prob{A^C} / \prob{A}$. What are the odds against a car being American given that its automatic? Compute explicitly. \spc{6}

\subquestionwithpoints{4} You see seven new cars go by in New York. What is the probability all of them were manual? Compute explicitly. \spc{4}

\subquestionwithpoints{6} You see seven new cars go by in New York. What is the probability at least one of them was manual? Compute explicitly. \spc{6}

\subquestionwithpoints{3} You see seven new \textit{manual} cars go by in New York. What is the probability the next one (the eigth car) is automatic? Compute explicitly. \spc{1}

\subquestionwithpoints{6} List all assumption(s) you use to answer (g), (h) and (i). There should be at least two. State if each assumptions is reasonable \textit{and why}.\spc{7}

\subquestionwithpoints{3} You go to a party with 20 cars and everyone puts their keys in a bag and the keys are distributed out randomly. What is the approximate probability at least one person gets the keys to their car? Compute approximately. \spc{2}

\eenum

\end{document}

\problem This question is about \qu{combination locks.} The particular model pictured below is called a MasterLock.

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

\noindent It has 40 notches labeled $\braces{0, 1, \ldots, 39}$. The lock is opened by entering the \qu{code} which consists of turning the lock to the right to the first number in the code, then turning one full rotation to the left and then to the second number in the code and finally turning right to the third number in the code. There are no restrictions on these three code numbers. Locks are identified by these three numbers and assume the locks are identical in every other way.

\benum
\subquestionwithpoints{3} Express $\Omega$, the universe of all possible lock combinations using set notation.\spc{2}

\subquestionwithpoints{3} How many unique locks can be produced? That is to say, how many codes are there? This is also $|\Omega|$.\spc{2}

\subquestionwithpoints{3} The locks are manufactured with the codes being equally likely. You go to the store and purchase one MasterLock. What is the probability the code is 21-10-32? No need to compute. \spc{2}

\subquestionwithpoints{3} The locks are manufactured with the codes being equally likely. Your friend goes to the store and purchases one MasterLock with the code 21-10-32. You also purchase a lock. What is the probability you get the same code? \spc{2}

\subquestionwithpoints{3} In one sentence, explain why the lock should not be called a \qu{combination lock} and instead be called a \qu{permutation lock.}\spc{2}

\subquestionwithpoints{3} In one sentence, explain why the lock should not be called a \qu{permutation lock} either.\spc{2}

\subquestionwithpoints{4} Assume that no numbers in the code are duplicated (each code number is unique and cannot be repeated). What is the probability of getting a lock where each number is less than 10 (that is 0, 1, \ldots, 9)? No need to compute explicitly.\spc{3}

\subquestionwithpoints{5} Assume that no numbers in the code are duplicated (each code number is unique and cannot be repeated). What is the probability of getting two even numbers and one odd number in the code (not necessarily in that order)? Note: zero is an even number; thus, there are twenty even numbers and twenty odd numbers in a MasterLock code number. \spc{3}

\subquestionwithpoints{4} What is the probability of getting a lock whose code consists of one odd number and two non-trivial powers of two i.e. from the set $\braces{2, 4, 8, 16, 32}$? \spc{2}

\eenum

\problem Imagine you are a contestant on the Monte Hall game.

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

Assume the same rules as we dicussed in class and that was on the homework. There are three doors with a hidden car in one and two smelly goats in the others. The location of the car is known by the gameshow host. You pick a door and the gameshow host opens a different door revealing one of the two goats. You now have the option to switch.

In this problem a similar situation as we talked about in class: assume the car is in door 2 but the contestant does not know thats.

\benum
\subquestionwithpoints{6} Draw the tree for this game with the following events: contestant first door pick (with states DP1, DP2, DP3), gameshow host door open (with states DO1, DO2, DO3), contestant win assuming contestant switches doors (with states W and L). Assume the car is in door 2. When drawing the tree, include all conditional probabilities and joint probabilities.\spc{10}

\subquestionwithpoints{5} Assuming the contestant has won, what is the probability the contestant chose door 1 as their first door pick?\spc{3}

\subquestionwithpoints{6} Assuming the host chose door 3 to open up, what is the probability the contestant won?\spc{3}

\subquestionwithpoints{5} Assume that if the contestant chose door 2 on the first pick, the game is repeated with 60\% probability but otherwise the normal rules of the game are in effect. What is the probability the contestant wins now?\spc{6}

\eenum

\problem This problem has theoretical questions that are not related to one another.

\benum

\subquestionwithpoints{3} What is the set of $\integers \backslash \rationals$?\spc{2}

\subquestionwithpoints{3} What is $\bigcup_{i=1}^{10} (-i,~i)$?\spc{1}

\subquestionwithpoints{4} Assume $\Omega = \braces{\omega_1, \omega_2, \omega_3}$ where each outcome is equally likely. Compute:

\beqn
\sum_{A \in 2^\Omega} \prob{A} = \hspace{400px}
\eeqn\spc{0.5}

\subquestionwithpoints{4} [Extra Credit] Repeat the calculation in the previous question except with $\Omega$ now having an arbitrary number of outcomes, $n$. Answer as succinctly as possible (use the most compact notation). \spc{4}

\subquestionwithpoints{5} Assume that $\prob{A} > 0$ if $A$ is non-empty. If $A \subset D$, prove that $\prob{A} < \prob{D}$. Prove it from the definition of probability as well as what you know from set theory.\spc{9}

\subquestionwithpoints{4} Using all theorems and facts from class, prove that:

\beqn
\prob{A^C \cap B^C} = 1 - \prob{A} - \prob{B} + \prob{A \cap B}
\eeqn\spc{4}

\subquestionwithpoints{3} In the multinomial expansion of $(a+b+c+d+e+f)^{600}$, how many terms are $a^{100} b^{100} c^{100} d^{100}e^{100}f^{100}$? No need to compute explicitly.\spc{2}

\subquestionwithpoints{4}  Approximate 1000! as 10 to the power of some number. Compute that number to one decimal place.\spc{3}

\subquestionwithpoints{3}  If $a = \binom{999}{500}$ and $b = \binom{999}{499}$, express $\binom{1000}{500}$ as a function of $a$ and $b$.\spc{2}

\subquestionwithpoints{3} What is the probability that OJ Simpson was guilty of the homicides he was accused of?\spc{2}

\subquestionwithpoints{4} Why is the long run frequency definition of probability not adequate to define the probability you came up with in the previous question? \spc{3}

\subquestionwithpoints{3} Laplace believed in universal determinism. If he thought everything was predetermined, how would he explain how a coin flips randomly to heads or tails? \spc{5}

%\subquestionwithpoints{3} Explain using estimates of conditional and unconditional probabilities why the events \qu{having lung cancer} (denoted by B) and \qu{being a smoker} (denoted by A) are independent or dependent. Use the same reasoning we used in class. \spc{3}

\subquestionwithpoints{4} A company has 20 trucks and 10 grocery stores. If each truck carries the same load (i.e. they can be considered indistinct) but each grocery store is different, how many ways is there to send the 20 trucks to the 10 grocery stores such that each store gets at least one shipment? No need to compute explicitly. \spc{3}

\subquestionwithpoints{4} 20 couples enter a ballroom. Each man chooses a random girl to dance with. What is the approximate probability no man dances with his girlfriend?
\eenum

\end{document}


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

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

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

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

\usepackage{alltt}

\usepackage{lscape,fancyhdr}

\usepackage{hyperref}

\pagestyle{fancy}

\usepackage{amsmath,epsfig,subfigure,amsthm,amsfonts,epsf,psfrag,rotating,setspace,bm}

\usepackage{verbatim,color} % Allow text colors}

\setlength{\oddsidemargin}{-0.4in}		% default=0in
\setlength\evensidemargin{-0.4in}

\setlength{\textwidth}{9.8in}		% default=9in

\setlength{\columnsep}{0.5in}		% default=10pt

\setlength{\columnseprule}{0pt}		% default=0pt (no line)

\setlength{\textheight}{7.0in}		% default=5.15in

\setlength{\topmargin}{-0.75in}		% default=0.20in

\setlength{\headsep}{0.25in}		% default=0.35in

\setlength{\parskip}{1.2ex}

\setlength{\parindent}{0mm}

\lhead{Course EPIB607: Regression handout 005 (Logistic regression)}
\rhead{jh,sb \ \ \ v. 2018.11.19}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

\section{Kidney stone removal procedures 1}
The 1986 BMJ article \textit{Comparison of treatment of renal calculi by open surgery, percutaneous nephrolithotomy, and extracorporeal shockwave lithotripsy} by Charig et. al, was a study designed to compare different methods of treating kidney stones in order to establish which was the most cost effective and successful. The procedure, either open surgery, or percutaneous nephrolithotomy (PN, a keyhole surgery procedure), was defined to be successful if stones were eliminated or reduced to less than 2 mm after three months. The study collected cases of kidney stones treated at a particular UK hospital during 1972-1985. The counts of successes for the two surgical procedures were:

\begin{table}[h]
\centering
\begin{tabular}{lcc|c}
	& Unsuccessful &  Successful & Total\\
Open surgery & 77 & 273 & 350 \\
PN & 61 & 289 & 350 \\
\hline
Total & 138 & 562 & 700
\end{tabular}
\end{table}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\begin{verbatim}
##
## Call:
## glm(formula = cbind(cases, controls) ~ open, family = binomial(link = "logit"),
##     data = df)
##
## Deviance Residuals:
## [1]  0  0
##
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -1.5556     0.1409 -11.040   <2e-16 ***
## open          0.2899     0.1911   1.517    0.129
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 2.3148e+00  on 1  degrees of freedom
## Residual deviance: 9.9920e-15  on 0  degrees of freedom
## AIC: 15.696
##
## Number of Fisher Scoring iterations: 3
\end{verbatim}

\end{knitrout}

\clearpage

\section{Kidney stone removal procedures 2}
Below are the same outcomes tabulated by the size of the kidney stone (smaller than 2cm/at least 2cm in diameter):

\begin{table}[h]
	\centering
	\begin{tabular}{lcc|c}
		$<$ 2cm & Unsuccessful &  Successful & Total\\
		Open surgery & 6 & 81 & 87 \\
		PN 			 & 36 & 234 & 270 \\
		\hline
		Total 	& 42 & 315 & 357 \\
		& & &  \\
				$\geq$ 2cm & Unsuccessful &  Successful & Total\\
		Open surgery & 71 & 192 & 263 \\
		PN 			 & 25 & 55 & 80 \\
		\hline
		Total 		& 96 & 247 & 343
	\end{tabular}
\end{table}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\begin{verbatim}
##
## Call:
## glm(formula = cbind(cases, controls) ~ open + size, family = binomial(link = "logit"),
##     data = df)
##
## Deviance Residuals:
##       1        2        3        4
## -0.7636   0.3588   0.2756  -0.4695
##
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -1.9366     0.1704 -11.361  < 2e-16 ***
## open         -0.3572     0.2291  -1.559    0.119
## size          1.2606     0.2390   5.274 1.33e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 33.1239  on 3  degrees of freedom
## Residual deviance:  1.0082  on 1  degrees of freedom
## AIC: 26.355
##
## Number of Fisher Scoring iterations: 3
\end{verbatim}

\end{knitrout}

\clearpage

\section{Diabetes cohort data 1}

\begin{table}[h]
	\centering
	\begin{tabular}{lcc|c}
		& Dead &  Censored & Total\\
		Type II & 218 & 326 & 544 \\
		Type I & 105 & 253 & 323 \\
		\hline
		Total & 323 & 579 & 902
	\end{tabular}
\end{table}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\begin{verbatim}
##
## Call:
## glm(formula = cbind(cases, controls) ~ type, family = binomial(link = "logit"),
##     data = df)
##
## Deviance Residuals:
## [1]  0  0
##
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -0.8794     0.1161  -7.576 3.58e-14 ***
## type          0.4770     0.1454   3.282  0.00103 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 1.0978e+01  on 1  degrees of freedom
## Residual deviance: 1.4033e-13  on 0  degrees of freedom
## AIC: 16.858
##
## Number of Fisher Scoring iterations: 2
\end{verbatim}

\end{knitrout}

\clearpage

\section{Diabetes cohort data 2}

Below are the same outcomes tabulated by age:

\begin{table}[h]
	\centering
	\begin{tabular}{lcc|c}
		$\leq 40$ & Dead &  Censored & Total\\
		Type II & 0 & 15 & 15 \\
		Type I & 1 & 129 & 130 \\
		\hline
		Total & 1 & 144 & 145 \\
		& & & \\
		$> 40$ & Dead &  Censored & Total\\
		Type II & 218 & 311 & 529 \\
		Type I & 104 & 124 & 228 \\
				\hline
		Total & 322 & 435 & 757 \\
	\end{tabular}
\end{table}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\begin{verbatim}
##
## Call:
## glm(formula = cbind(dead, censored) ~ type + age, family = binomial(link = "logit"),
##     data = df)
##
## Deviance Residuals:
##        1         2         3         4
## -0.41982   0.09091   0.00776  -0.01168
##
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -4.9525     1.0036  -4.935 8.02e-07 ***
## type         -0.1816     0.1595  -1.139    0.255
## age           4.7781     1.0108   4.727 2.28e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 133.81237  on 3  degrees of freedom
## Residual deviance:   0.18471  on 1  degrees of freedom
## AIC: 20.745
##
## Number of Fisher Scoring iterations: 5
\end{verbatim}

\end{knitrout}

\end{document}


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

\title{\large{\textsc{\vspace{-1cm}Dynamic Programming Implementation Guide}}}
\date{}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage[a4paper, portrait, margin=0.5in]{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

\subsection*{Bottom-up}

\begin{enumerate}
    \item \textbf{initialize memo}

If using an array, make sure you initialize it to the correct size (usually size \texttt{N + 1}, to store all \texttt{N} subproblems plus the base case). If using a hashmap, you don't have to worry about sizing.

    \item \textbf{store base case in memo}

    \item \textbf{iterate over subproblems}

Using a loop, iterate the indices into your memo over the subproblems. Pay attention to the direction of the dependencies in the recurrence relation. E.g., for \texttt{DP[i] = min(DP[i+1], DP[i+2])}, you must solve the problems in \textit{descending} order for \texttt{i}. Generally speaking, you should solve the smallest problems first.

    \item \textbf{solving subproblems}

    In your loop, calculate \texttt{DP[key]} (\texttt{key} may be one or more indices, potentially something else) using your recurrence relation. Often you will take the \texttt{min} or \texttt{max} of all possible guesses.

    \item \textbf{store answer to subproblems}

    Store the result in \texttt{DP[key]}.

    \item \textbf{return answer to original problem}

    This should be a value in your memo.

\end{enumerate}

\subsection*{Top-down}

\begin{enumerate}

    \item \textbf{initialize memo}

In a public facing function, initialize your memo. If using an array, make sure you initialize it to the correct size (usually size \texttt{N + 1}, to store all \texttt{N} subproblems plus the base case). If using a hashmap, you don't have to worry about sizing. If you are using an array, you might want to fill it with a special value to indicate that the subproblems are currently unsolved.

    \item \textbf{store base case in memo}

    \item \textbf{create private recursive function}

This function's purpose is to return the solution to subproblems (i.e., it returns \texttt{DP[key]}). This function's inputs should contain the memo, the key value(s) for the memo (e.g., \texttt{i} and \texttt{j}), and any other data needed (often the input to your public function is also an input to your private function).

    \item \textbf{check if memo has solution}

In the recursive function, check if your memo already contains a solution for the given input key value. If it does, return the value in the memo.

    \item \textbf{solving subproblems}

In your recursive function, use your recurrence relation to calculate \texttt{DP[key]}. \textbf{Don't explicitly access values in your memo. Instead, call the recursive function.} For example, for Fibonacci:

\begin{lstlisting}[language=Java]

private int fib_recurs(int i, int[] DP) {
    if (DP[i] != -1) return DP[i];
    DP[i] = DP[i-1] + DP[i-2]; // don't do this
    DP[i] = fib_recurs(i-1, DP) + fib_recurs(i-2, DP); // do this instead
    return DP[i];
}

\end{lstlisting}

    \item \textbf{store and return answer to subproblem}

Store the answer to the subproblem in your memo and return it (this is shown above)

    \item \textbf{return answer to original problem}

    In your public function, use your recursive function to solve the problem and return it, e.g., \texttt{return fib\_recurs(10, DP)}.

\end{enumerate}

\end{document}


\documentclass{article}

\title{\sc\LARGE CSCA67 Tutorial, Week 3\\
{\Large Sept. 28th-Oct. 2nd, 2015}}
\date{}
\author{\sc Compiled by {\em G. Singh Cadieux}\\[1ex]
\sc Adapted from\\
A. Bretscher, \href{http://www.utsc.utoronto.ca/~bretscher/a67/lectures/w4.pdf}{\em CSCA67 Week 4 Lecture Notes},\\
\href{http://www.intmath.com/counting-probability/3-permutations.php}{\em Interactive Mathematics: Permutations (Ordered Arrangements)} \&\\
Lov\'{a}sz, et al. \textit{Discrete Mathematics: Elementary and Beyond.} Springer, 2003.}

\usepackage{fullpage}
\usepackage{amsmath,amssymb}
%\usepackage{color}
%\usepackage{multicol}
\usepackage{tikz}
\usepackage{hyperref}

\setlength{\parindent}{0pt}

\begin{document}
\maketitle

\section{\sc More counting problems}

\subsection*{{\normalsize A class consists of 15 students of whom 5 are prefects.}\\
Q: {\em How many committees of 8 can be formed if each consists of}}
\subsubsection*{a) {\em exactly 2 prefects?}}
Let us consider the students on the committee who are prefects as 1 entity $P$, and those who are not prefects as 1 entity $NP$. Then, using the product rule, we determine that
\begin{equation*}
\text{total \# of possible committees with 2 prefects}=\text{\# of possibilities for }P\times \text{\# of possibilities for }NP
\end{equation*}
The total number of possible $P$s is the number of ways we can choose 2 prefects for the committee from the 5 prefects in the class.\\[1ex]
Similarly, the total number of possible $NP$s is the number of ways we can choose $8-2=6$ non-prefects for the committee from the $15-5=10$ non-prefects in the class.
\begin{align*}
\text{total \# of possible committees with 2 prefects}& =\dfrac{5!}{(5-2)!\,2!}\times\dfrac{10!}{(10-6)!\,6!}\\
& =10\times 120\\
& =2100
\end{align*}

\subsubsection*{b) {\em at least 2 prefects?}}
The total number of committees consisting of at least 2 prefects is the sum of the number of committees containing 2 prefects, and the number of committees containing 3 prefects, and so on.\\[1ex]
Rather than calculating each of these combinations and summing them, it is simpler to consider the total number of committees consisting of fewer than 2 prefects, since this is only the sum of the number of committees containing 1 prefect and the number of committees containing no prefects. As there are only 2 possibilities - either a committee has fewer than 2 prefects or at least 2 prefects - and both possibilities cannot be true simultaneously - a committee cannot simultaneously have fewer than 2 prefects and at least 2 prefects - the total number of committees is the sum of these 2 possibilities.
\begin{align*}
\text{total \# of committees}& =\left(\text{\# of committees cont.}<\text{2 prefects}\right)+
\left(\text{\# of committees cont.}\geq\text{2 prefects}\right)\\
\text{\# of committees cont.}\geq\text{2 prefects}& =\text{total \# of committees}-\left(\text{\# of committees cont.}<\text{2 prefects}\right)\\
& =\text{total \# of committees}-(\text{\# of committees cont. no prefects}\\
& \quad+\text{\# of committees cont. 1 prefect})
\end{align*}
By the same reasoning as in \textbf{(a)},
\begin{align*}
\text{\# of committees with no prefects}& =\dfrac{5!}{(5-0)!\,0!}\times\dfrac{10!}{(10-8)!\,8!}\\
& =1\times 45\\
& =45\\
\text{\# of committees with 1 prefect}& =\dfrac{5!}{(5-1)!\,1!}\times\dfrac{10!}{(10-7)!\,7!}\\
& =5\times 120\\
& =600
\end{align*}
And the total number of all possible committees is simply the number of ways that 8 students can be chosen, without restriction, from the class of 15. Thus,
\begin{align*}
\text{\# of committees cont.}\geq\text{2 prefects}& =\dfrac{15!}{(15-8)!\,8!}-\left(45+600\right)\\
& =6435-645\\
& =5790
\end{align*}

\subsection*{{\normalsize Out of 5 mathematicians and 7 engineers, a committee consisting of 2 mathematicians and 3 engineers is formed.}\\
Q: {\em How many ways can this be done if}}
\subsubsection*{a) {\em any mathematician and any engineer can be included?}}
Let us consider the mathematicians on the committee who are prefects as 1 entity $M$, and the engineers on the committee as 1 entity $E$. Then, using the product rule, we determine that
\begin{equation*}
\text{total \# of possible committees}=\text{\# of possibilities for }M\times \text{\# of possibilities for }E
\end{equation*}
The total number of possibilities for $M$ is the number of ways we can choose 2 mathematicians from 5, and the total number of possibilities for $E$ is the number of ways we can choose 3 engineers from 7.
\begin{align*}
\text{total \# of possible committees}& =\dfrac{5!}{(5-2)!\,2!}\times\dfrac{7!}{(7-3)!\,3!}\\
& =10\times 35\\
& =350
\end{align*}

\subsubsection*{b) {\em one particular engineer must be on the committee?}}
If one particular engineer must be on the committee, we use the same method as above, except that we are choosing 2 engineers from 6 rather than 3 from 7, since we have already selected 1 engineer and must now choose from those remaining.
\begin{align*}
\text{total \# of possible committees}& =\dfrac{5!}{(5-2)!\,2!}\times\dfrac{6!}{(6-2)!\,2!}\\
& =10\times 15\\
& =150
\end{align*}

\subsubsection*{c) {\em two particular mathematicians cannot be on the committee?}}
If two particular engineers cannot be on the committee, then we continue to use the same method as above, but we are choosing 2 mathematicians from 3 rather than from 5, since we have excluded 2 mathematicians as choices.
\begin{align*}
\text{total \# of possible committees}& =\dfrac{3!}{(3-2)!\,2!}\times\dfrac{7!}{(7-3)!\,3!}\\
& =3\times 35\\
& =105
\end{align*}

\subsection*{{\normalsize You have 8 colours of paint and 14 rooms in your house, each of which you want to paint in a single colour.\\}
Q: {\em How many ways can you paint your house?}}
If there are 8 ways to paint each room, and 14 rooms to be painted, then by the product rule,
\begin{align*}
\text{\# of ways to paint 14 rooms}& =\text{\# of ways to paint room1}\times\text{\# of ways to paint room2}\times\ldots\\
& \quad\times\text{\# of ways to paint room14}\\
& =8^{14}
\end{align*}

\subsubsection*{Q: {\em How many combinations of coloured rooms can you make?}}
Because we are asked for the number of combinations, we do not consider the order in which the rooms are painted, but simply the number of rooms in the house that are painted each colour.\\[1ex]
Specifically, since we can paint multiple rooms the same colour, we are asked for the number of combinations of colours for $r=14$ rooms, with $n=8$ colours from which to choose.
\begin{equation*}
\binom{r+(n-1)}{r}\Rightarrow\binom{14+(8-1)}{14}=116\,280\text{ colour combinations for 14 rooms}
\end{equation*}

\section{\sc Review of week 4's lecture}
\subsection*{\em Introduction to probability}

\subsection{\em Basic definitions}
\begin{description}
\item[\sc Experiment:] \hfill\\
a clearly-defined procedure that results in 1 of a set of \textit{outcomes}/\textit{elementary events}\\
\textbf{eg.} tossing a coin and recording the outcome
\item[\sc Sample space:] \hfill\\
a set \textsl{S} that includes all possible outcomes of an experiment\\
\textbf{eg.} the experiment of tossing a coin and recording its outcome has sample space \textsl{S} = \{heads, tails\}
\item[\sc Event:] \hfill\\
a subset of the sample space \textsl{S}\\
\textbf{eg.} for the experiment of tossing a coin and recording its outcome, the events are $\emptyset$ (empty set), \{heads\}, \{tails\}, \{heads, tails\}
	\begin{description}
	\item[\sc Compound event:] an event consisting of multiple outcomes/elementary events\\
	\textbf{eg.} for the experiment of tossing a coin and recording its outcome, the only compound event is \{heads, tails\}\\[1ex]
	\textbf{eg.} for the experiment of throwing a standard die, the compound events include \{1, 2\}, \{at least 3\} = \{3, 4, 5, 6\}, \{even numbers\} = \{2, 4, 6\}
	\end{description}
\item[\sc Probability:]\hfill\\
the chance of the occurrence of an event, between 0 (it is impossible for the event to occur) and 1 (it is absolutely certain that the event will occur)
\end{description}

\subsubsection*{\em Choosing an appropriate sample space}
Generally, the sample space for an experiment should be \textit{collectively exhaustive} - that is, no matter what happens in the experiment, the result is an outcome that is included in the sample space. Thus (as described in the definition below), there should be a 100\% probability of any outcome in the sample space occurring; and by the same token, there should be a 0\% probability of no outcome in the sample space occurring.\\[1ex]
For example, the experiment of throwing a standard die could not have sample space \textsl{S} = \{1, 2, 3, 4\}, since it is also possible for us to roll a 5 or 6.\\[1em]
Within the sample space, all elements should be \textit{distinct} and \textit{mutually exclusive} - that is, it should not be possible for the experiment to have more than 1 outcome (as described in the definition above).\\[1ex]
For example, the experiment of throwing a standard die could have sample space \textsl{S} = \{1 or 2, 3, 4 or 5 or 6\}, but not \textsl{S} = \{1 or 2, 2 or 3, 4 or 5 or 6\}, since rolling a 2 results in both ``1 or 2" and ``2 or 3" as outcomes.\\[1em]
The sample space should have enough detail to distinguish between all outcomes of interest, without including irrelevant outcomes.\\[1ex]
For example, the experiment of throwing a standard die should generally not have sample space \textsl{S} = \{1 or 2 or 3 or 4 or 5 or 6\}, since this does not allow us to distinguish between each face of the die.\\
Nor should it have sample space \textsl{S} = \{1, 2, 3, 4, 5, 6, 7, 9, 100\}, since 7, 9, and 100 are not faces of a standard die and so will never occur.\\[1em]
Finally, it may be useful to design a sample space in which all outcomes are \textit{equally likely}. If this is the case, then we can use the definition of probability below to calculate the probabilities of various events. Otherwise, we need to assign probability values to all relevant outcomes and/or events.\\[1ex]
For example, the experiment of throwing two standard dice and recording their sum could have an equally-likely sample space \textsl{S} = \{(1,1), (1,2), \ldots, (6,6)\}, or sample space \textsl{S}$^*$ = \{2, 3, \ldots, 12\}. For \textsl{S}$^*$, the probability of each of 3, 4, \ldots, 11 would increase with the number of ways in which these sums could occur: 3 can be formed in 2 ways ((1,2), (2,1), while 4 can be formed in 3 ways ((1,3), (2,2), (3,1)), making 4 a more likely outcome than 3.

\subsection{\em Probability \text{(Laplace's classical definition)}}
The probability $P(E)$ of an event $E$ in an \textit{equally-likely} sample space \textsl{S} is
\begin{equation*}
P(E)=\dfrac{|E|}{|\textsl{S}|}
\end{equation*}
(That is, the number of outcomes in $E$ divided by the number of outcomes in \textsl{S}.)\\[1ex]
\textbf{eg.} for the experiment of throwing a standard die, with \textsl{S} = \{1, 2, 3, 4, 5, 6\}, the probability of rolling an even number is
\begin{equation*}
\dfrac{|\{\text{even numbers}\}|}{|\textsl{S}|}=\dfrac{|\{2,4,6\}|}{|\{1,2,3,4,5,6\}|}=\dfrac{3}{6}=50\%
\end{equation*}
\textbf{eg.} for the experiment of throwing a standard die, with \textsl{S} = \{1, 2, 3, 4, 5, 6\}, the probability of rolling a power of 2 is
\begin{equation*}
\dfrac{|\{\text{powers of 2}\}|}{|\textsl{S}|}=\dfrac{|\{1,2,4\}|}{|\{1,2,3,4,5,6\}|}=\dfrac{3}{6}=50\%
\end{equation*}

\section{\sc Probability problems}
\textsc{N.B. There are} several possible, equally valid ways to arrive at these answers, including different methods that may have been demonstrated in lecture.

\subsection*{Q: {\em What is the probability of}}
\subsubsection*{a) {\em getting an ace if we choose a card at random from a standard pack of 52 playing cards?}}
Let us select \textsl{S} = \{52-card deck\} = \{\textcolor{red}{2$\heartsuit$}, \ldots, \textcolor{red}{2$\diamondsuit$}, \ldots, 2$\clubsuit$, \ldots, 2$\spadesuit$, \ldots\}. The probability of choosing any one card is the same for every card in the deck, so all outcomes in \textsl{S} are equally likely, and we can use the above classical definition of probability:
\begin{equation*}
P(E)=\dfrac{|E|}{|\textsl{S}|}\Rightarrow P(\{\text{aces}\})=\dfrac{|\{\text{aces}\}|}{|\{\text{52-card deck}\}|} =\dfrac{|\{\textcolor{red}{A\heartsuit},\textcolor{red}{A\diamondsuit},A\clubsuit,A\spadesuit\}|}{52}
=\dfrac{4}{52}=\dfrac{1}{13}
\end{equation*}

\subsubsection*{b) {\em getting a 5 if we roll a die?}}
Let us select \textsl{S} = \{1, 2, 3, 4, 5, 6\}. The probability of rolling any number on the die is the same (assuming that it is a standard die), so all outcomes in \textsl{S} are equally likely, and we can again use the classical definition of probability:
\begin{equation*}
P(\{5\})=\dfrac{|\{5\}|}{|\{1, 2, 3, 4, 5, 6\}|}=\dfrac{1}{6}
\end{equation*}

\subsubsection*{c) {\em getting an even number if we roll a die?}}
As in \textbf{(b)}, we select \textsl{S} = \{1, 2, 3, 4, 5, 6\}, and again use the definition of probability:
\begin{equation*}
P(\{\text{even numbers}\})=\dfrac{|\{\text{even numbers}\}|}{|\{1, 2, 3, 4, 5, 6\}|}=\dfrac{|\{2,4,6\}|}{6}=\dfrac{3}{6}=\dfrac{1}{2}
\end{equation*}

\subsubsection*{d) {\em having one Tuesday in this week?}}
Every week must have a Tuesday, so $P(\{\text{Tuesday}\})=1$.

\subsection*{{\normalsize There are 15 balls numbered 1 to 15, in a bag.}\\
Q: {\em If we select one at random, what is the probability that the number printed on the ball will be a prime number greater than 5?}}
Let us select \textsl{S} = \{ball 1, ball 2, \ldots, ball 15\}. Then, assuming that it is equally likely that we will select any one ball, all outcomes in \textsl{S} are equally likely, and we can use the above classical definition of probability:
\begin{align*}
P(E)=\dfrac{|E|}{|\textsl{S}|}\Rightarrow P(\{\text{prime-numbered balls greater than 5}\})& =\dfrac{|\{\text{prime-numbered balls greater than 5}\}|}{|\{\text{ball 1, ball 2, \ldots, ball 15}\}|}\\
& =\dfrac{|\{\text{ball 7, ball 11, ball 13}\}|}{15}\\
& =\dfrac{3}{15}=\dfrac{1}{5}
\end{align*}

\subsection*{{\normalsize The names of 4 directors of a company will be placed in a hat and a 2-member delegation will be selected at random to represent the company at an international meeting. Let A, B, C and D denote the directors of the company.}\\
Q: {\em What is the probability that}}
\subsubsection*{a) {\em A is selected?}}
Let us select \textsl{S} = \{delegations\} = \{AB, AC, AD, BC, BD, CD\}. The outcomes in which A is selected are AB, AC, and AD. Assuming that it is equally likely that any one director's name will be drawn from the hat, all outcomes in \textsl{S} are equally likely, so we can use the above classical definition of probability:
\begin{equation*}
P(E)=\dfrac{|E|}{|\textsl{S}|}\Rightarrow P(\{\text{A is selected}\})=\dfrac{|\{\text{A is selected}\}|}{|\{\text{AB, AC, AD, BC, BD, CD}\}|}=\dfrac{|\{\text{AB, AC, AD}\}|}{6}=\dfrac{3}{6}=\dfrac{1}{2}
\end{equation*}

\textsc{Alternatively}, we can simply select \textsl{S} = \{delegations\} without describing the possible delegations. We instead use combinatorics, as we have seen before, to count the total number of possible delegations, and the number of possible delegations which include A.\\[1ex]
The total number of possible delegations is the number of ways in which we can select 2 members from the 4 directors.\\[1ex]
The number of possible delegations which include A is the number of ways in which we can select the second member from the 3 remaining directors, after having selected A.
\begin{equation*}
P(\{\text{A is selected}\})=\dfrac{|\{\text{A is selected}\}|}{|\{\text{delegations}\}|}=\dfrac{\binom{3}{1}}{\binom{4}{2}}=\dfrac{3}{6}=\dfrac{1}{2}
\end{equation*}

\subsubsection*{b) {\em A or B is selected?}}
Let us select \textsl{S} = \{delegations\} = \{AB, AC, AD, BC, BD, CD\}. The outcomes in which A or B is selected are AB, AC, AD, BC, and BD. Assuming that it is equally likely that any one director's name will be drawn from the hat, all outcomes in \textsl{S} are equally likely, so we can again use the definition of probability:
\begin{equation*}
P(\{\text{A or B is selected}\})=\dfrac{|\{\text{A or B is selected}\}|}{|\{\text{AB, AC, AD, BC, BD, CD}\}|}=\dfrac{|\{\text{AB, AC, AD, BC, BD}\}|}{6}=\dfrac{5}{6}
\end{equation*}

\textsc{Alternatively}, we can again select \textsl{S} = \{delegations\}, and use combinatorics to count the total number of possible delegations, and the number of possible delegations which include A or B.\\[1ex]
To form a delegation that includes A or B, we can first choose A, and then a second member from the 3 remaining directors (including B); or we can first choose B, and then a second member from the 2 remaining directors (we exclude A so as not to count AB again).
\begin{equation*}
P(\{\text{A or B is selected}\})=\dfrac{|\{\text{A or B is selected}\}|}{|\{\text{delegations}\}|}=\dfrac{\binom{3}{1}+\binom{2}{1}}{\binom{4}{2}}=\dfrac{5}{6}
\end{equation*}

\textsc{Another alternative} is to use the complement rule above. Rather than counting the number of outcomes in which A or B is selected, we can count the number of outcomes in which \textit{not} ``A or B is selected" - that is, in which it is not true that A or B is selected. From \textsl{S} = \{delegations\} = \{AB, AC, AD, BC, BD, CD\}, CD is the only outcome in which this is the case.\\[1ex]
We can also determine logically that, if it is not true that A or B is selected, it must be true that neither A nor B was selected. This leaves only 2 directors, C and D, from which to select, meaning that the only remaining delegation is CD.
\begin{align*}
P(E)=1-P(\bar{E})\Rightarrow P(\{\text{A or B is selected}\})& =1-P(\{\overline{\text{A or B is selected}}\})\\
& =1-P(\{\text{CD}\})\\
& =1-\dfrac{|\{\text{CD}\}|}{|\{\text{AB, AC, AD, BC, BD, CD}\}|}\\
& =1-\dfrac{1}{6}=\dfrac{5}{6}
\end{align*}
\textsc{Generally}, we use the complement rule in cases in which there are fewer outcomes in $\bar{E}$ than in $E$, or in which $\bar{E}$ is easier to compute than $E$.

\subsubsection*{c) {\em A is not selected?}}
Let us select \textsl{S} = \{delegations\} = \{AB, AC, AD, BC, BD, CD\}. The outcomes in which A is not selected are BC, BD, and CD. Assuming that it is equally likely that any one director's name will be drawn from the hat, all outcomes in \textsl{S} are equally likely, so we can again use the definition of probability:
\begin{equation*}
P(\{\text{A is not selected}\})=\dfrac{|\{\text{A is not selected}\}|}{|\{\text{AB, AC, AD, BC, BD, CD}\}|}=\dfrac{|\{\text{BC, BD, CD}\}|}{6}=\dfrac{3}{6}=\dfrac{1}{2}
\end{equation*}

\textsc{Alternatively}, we can again select \textsl{S} = \{delegations\}, and use combinatorics to count the total number of possible delegations, and the number of possible delegations which do not include A.\\[1ex]
The number of possible delegations which do not include A is the number of ways in which we can select 2 members from the remaining 3 directors, after having excluded A.
\begin{equation*}
P(\{\text{A is not selected}\})=\dfrac{|\{\text{A is not selected}\}|}{|\{\text{delegations}\}|}=\dfrac{\binom{3}{2}}{\binom{4}{2}}=\dfrac{5}{6}=\dfrac{1}{2}
\end{equation*}

\textsc{Another alternative} is to again use the complement rule. We have already determined the probability of the event that A is selected, which is the complement of the event that A is not selected. Thus
\begin{equation*}
P(\{\text{A is not selected}\})=1-P(\{\text{A is selected}\})=1-\dfrac{1}{2}=\dfrac{1}{2}
\end{equation*}

\section{\sc Additional practice problems}

The names of 5 directors of a company will be placed in a hat and a 2-member delegation will be selected at random to represent the company at an international meeting. Let A, B, C, D, and E denote the directors of the company.\\[1ex]
{\bf Q: What is the probability that\\[1ex]
a) A is selected?\\[1ex]
b) A or B is selected?\\[1ex]
c) A is not selected?}\\[1em]
{\bf Q: What is the probability of drawing a diamond from a standard pack of 52 playing cards?}\\[1em]
{\bf Q: If a die is rolled and a coin is tossed, what is the probability that the die shows an odd number and the coin shows a head?}\\[1em]
{\bf Q: If 2 standard dice are rolled, what is the probability that the sum is\\
a) equal to 1?\\[1ex]
b) equal to 4?\\[1ex]
c) less than 13?}

\end{document}


\documentclass[12pt]{article}
\usepackage[noanswer]{exercise}
\usepackage[]{amsthm}
\usepackage[]{amsmath}
\usepackage[]{enumitem}
\usepackage[]{hyperref}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{proposition}{Proposition}
\newtheorem{corollary}{Corollary}

\theoremstyle{definition}
\newtheorem{definition}{Definition}

\let\set\mathbf

% I don't like how the "exercise" package normally lays things out.
% Feel free to copy this if you agree.
\renewcommand{\ExerciseHeader}{\noindent\textbf{\ExerciseName\ \ExerciseHeaderNB}\ \ExerciseHeaderTitle\ \ExerciseHeaderOrigin\smallskip}
\renewcommand{\ExerciseHeaderOrigin}{(\ExerciseOrigin)}
\renewcommand{\AnswerHeader}{\bigskip\noindent\textbf{Solution \ExerciseHeaderNB}\newline}

\title{Workshop Notes}
\author{Robert Dougherty-Bliss}
\date{10 February 2021}

\begin{document}

\maketitle

% Adding the * gets rid of the section numbering.
\section*{Logistics}%
\label{sec:logistics}

Nothing to say here.

\section*{New stuff}%
\label{sec:new_stuff}

Since I last saw you, you learned more about sequences and got some new
homework. We're going to talk about some sequence topics and a problem from the
new homework, as well as any other questions you might have.

\subsection*{Ces\`aro means}%
\label{sub:cesaro_means}

If we have a sequence $x(n)$, then we can take \emph{averages} of it by
defining
\begin{equation*}
    y(n) = \frac{1}{n} \sum_{k = 1}^n x(k).
\end{equation*}

\textbf{Plausible statement:} If $x(n)$ converges, then the averages also
converge to the same thing.

This is true. It's called Ces\`aro summation. The proof illustrates one of the
main techniques of analysis: Splitting a problem into two managable parts.

\begin{theorem}
    If $x(n) \to x$, then
    \begin{equation*}
        y(n) = \frac{1}{n} \sum_{k = 1}^n x(k) \to x.
    \end{equation*}
\end{theorem}

\begin{proof}
    We want to show that, given any $\epsilon > 0$, there exists some integer
    $N$ such that
    \begin{equation*}
        |y(n) - x| < \epsilon
    \end{equation*}
    for $n > N$.

    The trick about averaging is that we can break up a single $x$ into many
    terms:
    \begin{equation*}
        y(n) - x = \frac{1}{n} \sum_{k = 1}^n x(k) - x = \frac{1}{n} \sum_{k = 1}^n (x(k) - x).
    \end{equation*}
    Therefore, by the triangle inequality,
    \begin{equation*}
        |y(n) - x| \leq \frac{1}{n} \sum_{k = 1}^n |x(k) - x|.
    \end{equation*}
    For ``large'' $k$, we have the ``sharp'' bound $|x(k) - k| < \epsilon$. For
    ``small'' $k$, we only have the ``coarse'' fact that $|x(k) - k|$ is
    bounded, since $x(k) - k \to 0$.

    To make this idea rigorous, pick $M$ such that $|x(k) - x| \leq M$ for all
    $k$ (by boundedness), and $N$ such that $|x(k) - x| < \epsilon$ for $k > N$
    (by convergence). Then, for $n > N$,
    \begin{align*}
        |y(n) - x|
            &\leq \frac{1}{n} \sum_{k = 1}^N |x(k) - x| + \frac{1}{n} \sum_{N < k \leq n} |x(k) - x| \\
            &\leq \frac{NM}{n} + \frac{\epsilon (n - N)}{n} \\
            &\leq \frac{NM}{n} + \epsilon.
    \end{align*}
    To get the right bound here, we need to turn that $NM / n$ into something
    involving $\epsilon$. But $N$ and $M$ are both fixed relative to
    $\epsilon$, so we can just require $n$ to be really big in comparison. That
    is, let $N'$ be such that $N' > N$ and $NM / n < \epsilon$ for $n > N'$.
    Then, for $n > N'$, the bound becomes
    \begin{equation*}
        |y(n) - x| \leq \epsilon + \epsilon = 2\epsilon.
    \end{equation*}
    Since $\epsilon$ was arbitrary, this is equivalent and shows that $y(n) \to
    x$.
\end{proof}

\textbf{Interesting question:} If the averages converge, does $x(n)$ converge?

\subsection*{Limits superior and inferior}%
\label{sub:limits_superior_and_inferior}

Not every sequence has a limit, but sometimes we want to talk about ``limits''
even when they don't exist. For example, $a(n) = (-1)^n$ does not ``converge,''
but it kind of has two ``pseudo-limits,'' $1$ and $-1$. These ``peusdo-limits''
are made precise by the \emph{limits superior and inferior}.

Given any sequence $a(n)$, there exists a number $\beta$ (possibly $\infty$)
such that:
\begin{enumerate}
    \item For every $\epsilon > 0$, we have $a(n) < \beta + \epsilon$ eventually.

    \item For every $\epsilon > 0$, there are infinitely many $n$ such that
        $a(n)$ is in $(\beta - \epsilon, \beta]$.
\end{enumerate}
The number $\beta$ is called the \emph{limit superior} of $a(n)$, and we denote
it by $\beta = \limsup_n a(n)$.

The above properties actually uniquely define the limit superior, but they
don't prove that it exists. Let's go through how Abbott does it. For now,
suppose that $a(n)$ is a bounded sequence.

\begin{enumerate}[label=(\alph*)]
    \item Prove that $y(n) = \sup \{a(k) \mid k \geq n\}$ converges. Let
        $\limsup_n a(n) = \lim_n y(n)$.

        % The sets are decreasing.

    \item Give the analogous definition for $\liminf_n a(n)$.

        % Switch to infs. Now the sets are increasing.

    \item Show that $\liminf_n a(n) \leq \limsup_n a(n)$. [Hint: If $x(n)$ and
        $y(n)$ converge and $x(n) \leq y(n)$, then $\lim_n x(n) \leq \lim_n
        y(n)$. (You should prove this!)]

        % The hint gives it away.

    \item Show that $\lim_n a(n) = x$ iff $\liminf_n a(n) = x = \limsup_n
        a(n)$. (You can't use the above properties I mentioned unless you prove
        them!)

        % Say that a(n) -> x. For every eps > 0, we have a(n) in (x - eps, x + eps)
        % for large n. Thus sup {a(k) | k >= n} <= x + eps for large n, which gives
        % limsup a(n) <= x + eps for every eps > 0. Thus limsup a(n) <= x.
        % Same argument with flipped signs works for liminf.

        % Conversely, say that limsup = liminf = x. For every eps > 0, we have
        % sup {a(k) | k >= n} < x + eps eventually, so a(k) <= x + eps eventually.
        % By an analogous argument, a(k) >= x - eps eventually, so a(k) is in
        % [x - epsilon, x + epsilon] eventually.
\end{enumerate}

\begin{Exercise}
    If $a(n) \leq b(n)$, we can't always say that $\lim_n a(n) \leq \lim_n
    b(n)$, because the limits might not even exist. Show that
    \begin{equation*}
        \limsup_n a(n) \leq \limsup_n b(n)
    \end{equation*}
    and
    \begin{equation*}
        \liminf_n a(n) \leq \liminf_n b(n)
    \end{equation*}
    whenever $a(n) \leq b(n)$ for all $n$.
\end{Exercise}

\end{document}


\documentclass[]{article}

\usepackage{graphicx}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{color}
\usepackage{soul}
\usepackage{amsmath}

\DeclareRobustCommand{\hlcyan}[1]{{\sethlcolor{cyan}\hl{#1}}}
\DeclareRobustCommand{\hlgreen}[1]{{\sethlcolor{green}\hl{#1}}}
\DeclareRobustCommand{\hlred}[1]{{\sethlcolor{red}\hl{#1}}}
\DeclareRobustCommand{\hlyellow}[1]{{\sethlcolor{yellow}\hl{#1}}}
\DeclareRobustCommand{\hlorange}[1]{{\sethlcolor{orange}\hl{#1}}}

%opening
\title{Session 5}
\author{Fakhir}

\begin{document}

\maketitle

\section*{Solutions}

\begin{enumerate}
	\item Plot $\sqrt{x}$. Observing it gives us a one sided limit i.e limit from the right. It is a continuous function (how?) but it is not defined for $x<0$:
	$$\lim_{x\to0^{+}}\sqrt{x} = 0$$
	$$\lim_{x\to0^{-}}\sqrt{x} = invalid$$

	\item Visualizing the graph gives us two sided limit. This function is not defined at $x=-1$:
	$$\lim_{x\to-1^{+}}\frac{1}{x+1} = +\infty$$
	$$\lim_{x\to-1^{-}}\frac{1}{x+1} = -\infty$$

	\item First by visualizing the graph (and then bit cheating by plotting it in maxima) gives us two sided limit. This function is not defined at $x=1$:
	$$\lim_{x\to1^{+}}\frac{1}{(x-1)^{4}} = +\infty$$
	$$\lim_{x\to1^{-}}\frac{1}{(x-1)^{4}} = +\infty$$

	Since in this case $\lim_{x\to1^{-}}\frac{1}{(x-1)^{4}} = \lim_{x\to1^{+}}\frac{1}{(x-1)^{4}} $ \\

	We can simply say:

	$$\lim_{x\to1}\frac{1}{(x-1)^{4}} = +\infty$$

	\item First by visualizing the graph (couldn't draw on maxima) gives us two sided limit. This function appears to be continuous at all points:
	$$\lim_{x\to0^{+}}|\sin{x}| = 0$$
	$$\lim_{x\to0^{-}}|\sin{x}| = 0$$

	Again we can say:

	$$\lim_{x\to0}|\sin{x}| = 0$$

	\item Couldn't plot it in maxima. In my opinion it is a piece wise graph given by:
	\[
	f(x)=
	\begin{cases}
	1, & \text{if } x>0 \\
	-1,& \text{if } x<0 \\
	undefined, & \text{if } x=0
	\end{cases}
	\]

	So this implies that it has a two sided limit as follows:
	$$\lim_{x\to0^{+}}\frac{|x|}{x} = +1$$
	$$\lim_{x\to0^{-}}\frac{|x|}{x} = -1$$ \\~\\

\end{enumerate}

\hlred{Extra questions that need to be answered:}
\begin{enumerate}
	\item I didn't quite understand what he means by ``one sided limit'' in the MiT solutions file. In some places (Q4 and Q5) he has used ``no need to use one sided limit here because..'', not too sure what he is saying. I thought one sided limit means the special case when either $\lim_{x\to k^{+}}$ exists or $\lim_{x\to k^{-}}$ but not both. Am I missing something here?

	\item Show that $\sqrt{x}$ is a continuous function specially at $x=0$ (beware: it has one sided limit).

	\item Show that $|\sin{x}|$ is a continuous function specially at $x=0$

\end{enumerate}

\end{document}

mit-ocw/[in progress] Single Variable Calculus, 2010/my solutions/1. Differentiation/Session5.tex