% Copyright 2021 Edoardo Riggio

% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at

% 	http://www.apache.org/licenses/LICENSE-2.0

% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.

\documentclass{article}

\usepackage{amsmath, graphicx, hyperref}
\usepackage{fancyvrb, newverbs, xcolor}

\definecolor{cverbbg}{gray}{0.93}

\newenvironment{cverbatim}
 {\SaveVerbatim{cverb}}
 {\endSaveVerbatim
  \flushleft\fboxrule=0pt\fboxsep=.5em
  \colorbox{cverbbg}{\BUseVerbatim{cverb}}%
  \endflushleft
}

\newenvironment{lcverbatim}
 {\SaveVerbatim{cverb}}
 {\endSaveVerbatim
  \flushleft\fboxrule=0pt\fboxsep=.5em
  \colorbox{cverbbg}{%
    \makebox[\dimexpr\linewidth-2\fboxsep][l]{\BUseVerbatim{cverb}}%
  }
  \endflushleft
}

\newcommand{\ctexttt}[1]{\colorbox{cverbbg}{\texttt{#1}}}
\newverbcommand{\cverb}
  {\setbox\verbbox\hbox\bgroup}
  {\egroup\colorbox{cverbbg}{\box\verbbox}}

\begin{document}
\begin{titlepage}
    \begin{center}
        \vspace*{1cm}

        \Huge
        \textbf{Computer Networking Formulary}

        \vspace{0.5cm}
        \LARGE

        \vspace{.5cm}

        Edoardo Riggio
   		  \vspace{1.5cm}

        \vfill

        \today

        \vspace{.8cm}
          \Large
          Computer Networking - SP. 2021 \\
        Computer Science\\
        Universit\`{a} della Svizzera Italiana, Lugano\\

    \end{center}
\end{titlepage}

\tableofcontents

\newpage

\section{Computer Networking and the Internet}
\subsection{Queuing Delay}
\vspace{.3cm}
\[ d_{queue} = \frac{L \cdot a}{R} \cdot \frac{L}{R} \cdot \left( 1 - \frac{L \cdot a}{R} \right) \] \\
Where \textbf{L} is the length of the packet, \textbf{a} is the average rate of packets per second, and \textbf{R} is the transmission rate of the link.

\subsection{Transmission Delay}
\vspace{.3cm}
\[ d_{transmission} = \frac{L}{R} \] \\
Where \textbf{L} is the length of the packet, and \textbf{R} is the transmission rate of the link.

\subsection{Propagation Delay}
\vspace{.3cm}
\[ d_{propagation} = \frac{d}{s} \] \\
Where \textbf{d} is the length of the link, and \textbf{s} is the propagation speed of the link.

\subsection{Nodal Delay}
\vspace{.3cm}
\[ d_{nodal} = d_{processing} + d_{queue} + d_{transmission} + d_{propagation} \]

\subsection{End-to-End Delay}
\vspace{.3cm}
\[ d_{end-to-end} = N \cdot (d_{processing} + d_{queue} + d_{transmission} + d_{propagation}) \] \\
Where \textbf{N} is the number of nodes. This can be used if all nodes have the same nodal delay.
\[ d_{end-to-end} = \sum^N_1 d_{nodal~i} \] \\
Where \textbf{N} is the number of nodes, and $d_{nodal~i}$ is the nodal delay of the $i$th node.

\subsection{End-to-End Throughput}
\vspace{.3cm}
\[ t_{end-to-end} = \frac{F}{T} \] \\
Where \textbf{F} is the length of the packet, and \textbf{T} is the transfer rate of the link.

\section{Application Layer}
\subsection{Client-Server File Distribution}
\vspace{.3cm}
\[ D_{CS} = \max \left\{ \frac{NF}{u_s}, \frac{F}{d_{min}} \right\} \] \\
Where \textbf{F} is the length of the packet, \textbf{d} is the length of a link, \textbf{N} is the number of clients, $u_s$ is the server's upload rate.

\subsection{P2P File Distribution}
\vspace{.3cm}
\[ D_{P2P} = \max \left\{ \frac{F}{u_s}, \frac{F}{d_{min}}, \frac{NF}{u_s + \sum^N_{i=1}u_i} \right\} \] \\
Where \textbf{F} is the length of the packet, \textbf{d} is the length of a link, \textbf{N} is the number of peers, $u_s$ is the server's upload rate, and $u$ is the peer's upload rate.

\section{Transport Layer}
\subsection{Estimating the RTT}
\vspace{.3cm}
\[ RTT_{est} = (1 - \alpha) \cdot RTT_{est} + \alpha \cdot RTT_{sample} \] \\
Where $RTT_{est}$ is the currently computed estimate, and $RTT_{sample}$ is the RTT measurement.

\subsection{Estimating the Timeout}
\vspace{.3cm}
\[ T = RTT_{est} + 4 \cdot ((1-\beta) \cdot RTT_{dev} + \beta \cdot | RTT_{sample} - RTT_{est} |) \] \\
Where $RTT_{est}$ is the currently computed estimate, and $RTT_{sample}$ is the RTT measurement.

\section{Network Layer}
\subsection{Output Port Queuing}
\vspace{.3cm}
\[ Buff = RTT \cdot \frac{C}{N} \] \\
Where \textbf{C} is the link capacity, and \textbf{N} is the number of packets.

\subsection{Weighted Fair Queuing}
\vspace{.3cm}
\[ W_a = \frac{W_i}{\sum_i i} \] \\
Where \textbf{W} is the weight of the $i$th packet, and \textbf{i} is the number of the packet.

\section{Link Layer}
\subsection{Successful Transmission in ALOHA}
\vspace{.3cm}
\[ S = p(1-p)^{2(N-1)} \] \\
Where \textbf{W} is the weight of the $i$th packet, and \textbf{i} is the number of the packet.

\subsection{Successful Transmission in Slotted ALOHA}
\vspace{.3cm}
\[ S = Np(1-p)^{N-1} \] \\
Where \textbf{N} is the number of active nodes, and \textbf{p} is the probability.

\subsection{Bit Errors}
\vspace{.3cm}
\[ BER = \frac{N_{err}}{N_{tot}} \] \\
Where $N_{err}$ is the number of bit errors, and $N_{tot}$ is the number of bit errors.

\subsection{Cyclic Redundancy Check}
\vspace{.3cm}
\[ R = (D \cdot 2^r) \% (G) \] \\
Where \textbf{D} is a piece of data where a CRC field is added, \textbf{r} is the length of the CRC field, and \textbf{G} is the generator -- bit pattern of $r + 1$ bits.

\section{Wireless Networks}
\subsection{Power Difference}
\vspace{.3cm}
\[ P_{rx}(d) \approx \beta \cdot P_{tx} \cdot \frac{1}{d^\alpha} \] \\
Where $P_{rx}$ is the received power, $\beta$ is the multiplicative attenuation factor, $P_{tx}$ is the transmitted power, \textbf{d} is the distance between transmitter and receiver, and $\alpha$ is the path loss exponent.

\subsection{Received Signal Strength Indicator}
\vspace{.3cm}
\[ RSSI = 10~log_{10}\left(\frac{P_{rx}}{1~mW}\right) \] \\
Where $P_{rx}$ is the received power

\end{document}


\input{/path/to/preamble.tex}
\title{Math 101 - Homework 1}
\date{January 14, 2022}
\author{First Name Last Name}

\begin{document}
\maketitle

\section*{Section 2.4}
\problem{2.4}{1}
\problem{2.4}{2}
\problem{2.4}{14}
\problem{2.4}{15}
\problem{2.4}{17}
\problem{2.4}{36}
\problem{2.4}{37}
\problem{2.4}{38}
\problem{2.4}{40}
\section*{Section 2.5}
\problem{2.5}{1}
\problem{2.5}{2}
\problem{2.5}{5}
\problem{2.5}{9}

\end{document}


% --- [ Value Set Analysis ] ---------------------------------------------------

% Results: objective communication of data

\subsection{Value Set Analysis}

To recovery variables stored in memory regions (e.g. global, heap, stack), the Value Set Analysis (VSA) algorithm is key; as it determines the possible ranges of values that registers may hold at every point of execution (i.e. registers holding addresses to memory locations). The value ranges (upper and lower bounds, and stride) give a conservative estimate of possible memory locations for variables \cite{wysinwyx}. While few of the reviewed papers have mentioned it, symbolic execution should be able to improve on this conservative estimate.


\documentclass{beamer}
\mode<presentation>

\usetheme{Madrid}

% \usepackage[margin=.9in]{geometry}
\usepackage{pgfplots}
\usepackage{listings}
% \pgfplotsset{compat=default}
\pgfplotsset{compat=1.10}
\newcommand{\cuckoo}{{\rm cuckoo}}
\newcommand{\hash}{{\rm siphash}}
% \usepackage{hyperref}

\title{Cuckoo Cycle}
\subtitle{a memory bound graph-theoretic proof-of-work}
\author{John Tromp}
\date{\today}
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

% \section*{Outline}
% \begin{frame}
% \frametitle{Outline}
  % \tableofcontents
% \end{frame}

\begin{frame}
\frametitle{What is a proof-of-work system?}
\begin{itemize}
\item
Allows a verifier to check, with negligible effort, that a prover has expended a large amount of computational effort.
% \pause
\item {Applications}
\begin{itemize}
\item Spam
% \pause
\item Denial-of-Service
% \pause
\item Crypto Currencies
\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Example PoW: Hashcash, by Adam Back (1997)}
A \alert{proof} for a message $m$ is a {\em nonce} $n$ such that
\begin{equation*}
H(m|n)
\end{equation*} starts with many zero bits,
for some fixed hash function $H$.
%  and notion of many.
% \pause
\begin{itemize}
\item
Hashcash adopted by most crypto-currencies
with various choices of hash function
\item
double-SHA256 in Bitcoin
\item scrypt in Tenebrix, copied by Litecoin
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Memory Bound PoW}
\begin{itemize}
\item main memory latency the bottleneck
\end{itemize}
% \pause
\begin{description}
\item[MB1] a target memory footprint that exceeds a single memory chip
% \pause
\item[MB2] a pattern of necessarily random memory accesses
% \pause
\item[MB3] minimal computation per random memory access
% \pause
\item[MB4] no feasible trade-off of memory for time
% \pause
\end{description}
\begin{itemize}
\item DRAM is like an ASIC for all memory bound applications
\item why not SRAM?
%\item huge economies of scale of commodity DRAM production
%\pause
\end{itemize}
\end{frame}

% \begin{frame}
% \frametitle{What about SRAM?}
% \begin{itemize}
% \item order of magnitude faster
% % \pause
% \item two orders more expensive
% % \pause
% \item not cost effective
% % \pause
% \item maximize memory accesses per second per dollar
% \end{itemize}
% \end{frame}

\begin{frame}
\frametitle{Graph Theoretic PoW}
\begin{itemize}
\item Erd\H{o}s-R\'{e}nyi model, denoted $G(N,M)$
% \pause
\item picks among all graphs with $N$ nodes and $M$ edges
% \pause
\item instead pick edges with a {\em keyed} hash function $h$
mapping nonces to edges
% \pause
\item graph has \alert{solution} if some $H$ occurs as subgraph
% \pause
\item \alert{proof} is set nonces that generate edges of $H$'s occurance
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Graph Theoretic PoW Properties}
\begin{itemize}
\item verifiable in time depending only on $H$,
independent of $N$ and $M$.
\item expected \#occurrences of $H$ is a function of both $N$ and $M$.
\item monotonically increasing in $M$.
\item in many cases, roughly a function of $\frac{M}{N}$
\item less than one expected solution makes PoW challenging
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Cuckoo Cycle}
\begin{itemize}
\item $H$ is length $L$ cycle
% \pause
\item in bipartite graph
% \pause
\item $M=N/2$
% \pause
\item $h$ is siphash2-4, a lightweight 6 round hash function
with 128-bit key and 64-bit output.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Cuckoo Cycle Solution Probability}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[xlabel={$\frac{M}{N}$ in \%}, ylabel={probability}, legend pos=north west]
\addplot[color=blue] coordinates {
(40,0) (41,0.00001) (42,0.00003) (43,0.0001) (44,0.00031) (45,0.00067)
(46,0.00141) (47,0.00385) (48,0.00967) (49,0.02222) (50,0.05076)
(51,0.10117) (52,0.17953) (53,0.27993) (54,0.39581) (55,0.51873)
(56,0.63614) (57,0.73955) (58,0.82352) (59,0.88719) (60,0.93182)
(61,0.9616) (62,0.97949) (63,0.98956) (64,0.99503) (65,0.99799)
(66,0.99907) (67,0.9996) (68,0.99989) (69,0.99997) (70,0.99998) (71,1) };
\addlegendentry{has 42-cycle}
\end{axis}
\end{tikzpicture}
\end{frame}

\begin{frame}
\frametitle{Cuckoo hashing (2004)}
\begin{itemize}
\item two same size tables, each with its own hash function
% \pause
\item two possible locations for each key
% \pause
\item upon insertion: if both occupied
% \pause
\item kick one out and insert in alternate location
% \pause
\item repeat until vacancy or some max iteration reached
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Cycle detection in Cuckoo Cycle}
\begin{itemize}
\item enumerate nonces one by one
\item insert nonce in Cuckoo hashtable, but store alternate
location, and forget nonce
% \pause
\item maintain {\em forest}, a disjoint union of trees.
\item In each tree, all edges are directed toward its {\em root}
% \pause
\item if edge endpoints have different roots:
% \pause
\item reverse edges on shorter path
% \pause
\item join trees
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Example, adding first 7 edges}
% \item graph on $N=8+8$ nodes after adding 7 edges
% In order to add the 8th edge $(10,11)$, we follow the paths $10 \rightarrow 5
% \rightarrow 8$ and $11 \rightarrow 12$ to find different roots $8$ and $12$.
% Since the latter path is shorter, we reverse it to $12 \rightarrow 11$ so we
% can add the new edge as $(11 \rightarrow 10)$, resulting in the middle diagram.
% In order to add to 9th edge
% $(10,13)$ we now find the path from $10$ to be the shorter one, so we reverse
% that and add the new edge as $(10 \rightarrow 13)$, resulting in the right diagram.
\begin{tikzpicture}[scale=2,>=stealth]
\node  (2) at (1, 2) [shape=circle,draw] {2};
\node  (4) at (2, 2) [shape=circle,draw] {4};
\node  (8) at (3, 2) [shape=circle,draw] {8};
\node (10) at (4, 2) [shape=circle,draw] {10};
\node (12) at (5, 2) [shape=circle,draw] {12};
\node  (5) at (4,-1) [shape=circle,draw] {5};
\node  (9) at (2,-1) [shape=circle,draw] {9};
\node (11) at (5,-1) [shape=circle,draw] {11};
\node (13) at (3,-1) [shape=circle,draw] {13};
\node (15) at (1,-1) [shape=circle,draw] {15};
\draw [<-]  (2) -- (15);
% \pause
\draw [<-]  (4) -- (9);
% \pause
\draw [<-]  (8) -- (5);
% \pause
\draw [->]  (4) -- (15);
% \pause
\draw [<-] (12) -- (11);
% \pause
\draw [->] (10) -- (5);
% \pause
\draw [<-]  (4) -- (13);
\end{tikzpicture}
\end{frame}

\begin{frame}
\frametitle{Example, adding edge $(10,13)$}
\begin{tikzpicture}[scale=2,>=stealth]
\node  (2) at (1, 2) [shape=circle,draw] {2};
\node  (4) at (2, 2) [shape=circle,draw] {4};
\node  (8) at (3, 2) [shape=circle,draw] {8};
\node (10) at (4, 2) [shape=circle,draw] {10};
\node (12) at (5, 2) [shape=circle,draw] {12};
\node  (5) at (4,-1) [shape=circle,draw] {5};
\node  (9) at (2,-1) [shape=circle,draw] {9};
\node (11) at (5,-1) [shape=circle,draw] {11};
\node (13) at (3,-1) [shape=circle,draw] {13};
\node (15) at (1,-1) [shape=circle,draw] {15};
\draw [<-]  (2) -- (15);
\draw [<-]  (4) -- (9);
\draw [<-]  (8) -- (5);
\draw [->]  (4) -- (15);
\draw [->] (12) -- (11);
\draw [->] (10) -- (5);
\draw [<-]  (4) -- (13);
\draw [<-] (10) -- (11);
\end{tikzpicture}
\end{frame}

\begin{frame}
\frametitle{Example, adding edge $(8,9)$}
\begin{tikzpicture}[scale=2,>=stealth]
\node  (2) at (1, 2) [shape=circle,draw] {2};
\node  (4) at (2, 2) [shape=circle,draw] {4};
\node  (8) at (3, 2) [shape=circle,draw] {8};
\node (10) at (4, 2) [shape=circle,draw] {10};
\node (12) at (5, 2) [shape=circle,draw] {12};
\node  (5) at (4,-1) [shape=circle,draw] {5};
\node  (9) at (2,-1) [shape=circle,draw] {9};
\node (11) at (5,-1) [shape=circle,draw] {11};
\node (13) at (3,-1) [shape=circle,draw] {13};
\node (15) at (1,-1) [shape=circle,draw] {15};
\draw [<-]  (2) -- (15);
\draw [<-]  (4) -- (9);
\draw [->]  (8) -- (5);
\draw [->]  (4) -- (15);
\draw [->] (12) -- (11);
\draw [<-] (10) -- (5);
\draw [<-]  (4) -- (13);
\draw [<-] (10) -- (11);
\draw [->] (10) -- (13);
\end{tikzpicture}
\end{frame}

% The left plot in Figure~\ref{runtimes} shows both the total runtime in seconds and the runtime of just
% the hash computation, as a function of (log)size. The latter is purely
% linear, while the former is superlinear due to increasing memory latency
% as the nodes no longer fit in cache. The right plot show this more clearly
% as the percentage of hashing to total runtime, ending up around 5\%.

% \begin{frame}
% \frametitle{Runtime}
% \begin{tikzpicture}
% \begin{axis}[xlabel={$\mbox{log}_2(N)$}, ymode=log, ylabel={seconds}, legend pos=north west]
% \addplot[color=red] coordinates {
% % (10,0.0000) (11,0.0000) (12,0.0000) (13,0.0001) (14,0.0001)
% (15,0.0002) (16,0.0004) (17,0.0008) (18,0.0017) (19,0.0034)
% (20,0.0068) (21,0.0139) (22,0.0271) (23,0.0542) (24,0.1084)
% (25,0.2166) (26,0.4336) (27,0.8658) (28,1.7322) (29,3.4719)
% (30,7.0389) };
% \addlegendentry{hashing runtime}
% \addplot[color=green] coordinates {
% % (10,0.0000) (11,0.0000) (12,0.0001) (13,0.0001) (14,0.0003)
% (15,0.0002) (16,0.0010) (17,0.0022) (18,0.0049) (19,0.0104)
% (20,0.0250) (21,0.0986) (22,0.2465) (23,0.5332) (24,1.1922)
% (25,2.5505) (26,5.3394) (27,11.0793) (28,23.1984) (29,54.6811)
% (30,128.1682) };
% \addlegendentry{total runtime}
% \end{axis}
% \end{tikzpicture}
% \end{frame}

\begin{frame}
\frametitle{Compute Intensity}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[xlabel={$\mbox{log}_2(N)$}, ylabel={\% runtime}, legend pos=north east]
\addplot[color=blue] coordinates {
% (10,38.8889) (11,33.3333) (12,45.1613) (13,39.8496) (14,44.6154)
(15,96.5217) (16,45.9119) (17,38.2180) (18,35.0988) (19,32.6724)
(20,27.2076) (21,14.0874) (22,11.0014) (23,10.1635) (24,9.0964)
(25,8.4921) (26,8.1215) (27,7.8144) (28,7.4670) (29,6.3494)
(30,5.4919) };
\addlegendentry{hashing percentage}
\end{axis}
\end{tikzpicture}
\end{frame}

%The left plot in Figure~\ref{accesses} shows the probability of finding a 42-cycle as a function
%of the percentage edges/nodes, while the right plot shows the average number of
%memory reads and writes per edge as a function of the percentage
%of processed nonces (progress through main loop).
%Both were determined from 10000 runs at size $2^{20}$;
%results at size $2^{25}$ look almost identical.
%In total the basic algorithm averages 3.3 reads and 1.1 writes per edge.

\begin{frame}
\frametitle{Memory Reads and Writes}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[xlabel={\% nonces processed}, ylabel={\# memory accesses per nonce}, ymin=0, legend pos=north west]
\addplot[color=green] coordinates {
(0,2.01) (1,2.02) (2,2.03) (3,2.04) (4,2.05) (5,2.06) (6,2.07) (7,2.08) (8,2.09) (9,2.10) (10,2.11) (11,2.12) (12,2.13) (13,2.15) (14,2.16) (15,2.17) (16,2.18) (17,2.20) (18,2.21) (19,2.22) (20,2.23) (21,2.25) (22,2.26) (23,2.28) (24,2.29) (25,2.30) (26,2.32) (27,2.33) (28,2.35) (29,2.36) (30,2.38) (31,2.40) (32,2.41) (33,2.43) (34,2.45) (35,2.46) (36,2.48) (37,2.50) (38,2.52) (39,2.54) (40,2.56) (41,2.58) (42,2.60) (43,2.62) (44,2.64) (45,2.66) (46,2.68) (47,2.71) (48,2.73) (49,2.76) (50,2.78) (51,2.81) (52,2.83) (53,2.86) (54,2.89) (55,2.92) (56,2.95) (57,2.98) (58,3.01) (59,3.05) (60,3.08) (61,3.12) (62,3.15) (63,3.19) (64,3.23) (65,3.27) (66,3.32) (67,3.36) (68,3.41) (69,3.45) (70,3.51) (71,3.56) (72,3.61) (73,3.67) (74,3.73) (75,3.80) (76,3.86) (77,3.93) (78,4.01) (79,4.09) (80,4.17) (81,4.26) (82,4.36) (83,4.46) (84,4.57) (85,4.69) (86,4.83) (87,4.97) (88,5.13) (89,5.30) (90,5.49) (91,5.71) (92,5.96) (93,6.25) (94,6.59) (95,6.99) (96,7.51) (97,8.18) (98,9.20) (99,10.93) };
\addlegendentry{reads}
\addplot[color=red] coordinates {
(0,1.00) (1,1.00) (2,1.00) (3,1.00) (4,1.00) (5,1.00) (6,1.00) (7,1.00) (8,1.00) (9,1.00) (10,1.00) (11,1.00) (12,1.00) (13,1.00) (14,1.00) (15,1.00) (16,1.00) (17,1.00) (18,1.00) (19,1.00) (20,1.00) (21,1.00) (22,1.00) (23,1.01) (24,1.01) (25,1.01) (26,1.01) (27,1.01) (28,1.01) (29,1.01) (30,1.01) (31,1.01) (32,1.01) (33,1.02) (34,1.02) (35,1.02) (36,1.02) (37,1.02) (38,1.02) (39,1.02) (40,1.03) (41,1.03) (42,1.03) (43,1.03) (44,1.03) (45,1.04) (46,1.04) (47,1.04) (48,1.04) (49,1.05) (50,1.05) (51,1.05) (52,1.06) (53,1.06) (54,1.06) (55,1.07) (56,1.07) (57,1.07) (58,1.08) (59,1.08) (60,1.08) (61,1.09) (62,1.09) (63,1.10) (64,1.10) (65,1.11) (66,1.11) (67,1.12) (68,1.13) (69,1.13) (70,1.14) (71,1.14) (72,1.15) (73,1.16) (74,1.16) (75,1.17) (76,1.18) (77,1.19) (78,1.20) (79,1.21) (80,1.22) (81,1.23) (82,1.24) (83,1.25) (84,1.26) (85,1.27) (86,1.29) (87,1.30) (88,1.32) (89,1.33) (90,1.35) (91,1.37) (92,1.39) (93,1.41) (94,1.43) (95,1.46) (96,1.48) (97,1.51) (98,1.54) (99,1.58) };
\addlegendentry{writes}
\end{axis}
\end{tikzpicture}
\end{frame}

% \caption{Threshold nature of solution, and increasing memory usage on threshold approach}

\begin{frame}
\begin{itemize}
\frametitle{Difficulty control}
\item
Ratio $\frac{M}{N} \geq 0.7$ practically guarantees solution
% \pause
\item
crypto currencies need precise control
% \pause

% The implementation default $\frac{M}{N}=\frac{1}{2}$ gives a solution probability of roughly $2.2\%$,
%while the average number of cycles found increases slowly with size; from 2 at $2^{20}$ to 3 at $2^{30}$.

\item
introduce difficulty target $0 < T < 2^{256}$ is introduced
% \pause
\item
require sha256 digest of ascending proof cycle nonces be less than $T$
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Edge Trimming, suggested by David Andersen}
\begin{itemize}
\item
identify nodes of degree 1.
\item
Such {\em leaf edges} can never be part of a cycle.
\item
eliminate their incident edge.
\item $\frac{M}{N} \leq \frac{1}{2}$ implies expected degree of a node is then at most 1
\item
significant fraction of edges expected to be leaf edges.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Edge Trimming Implementation}
\begin{itemize}
\item
maintain bitvector of {\em alive} edges
% \pause
\item
Initialize 2-bit degree counters (one per even node) to 0
% \pause
\item
for all alive edges, increase counter for even endpoint,
capping the value at 2.
% \pause
\item
for all alive edges, if counter for even endpoint is less than 2,
kill the edge
% \pause
% set the edge to be not-alive.
\item repeat for all odd endpoints
% \pause
\item
Support {\em counter partitioning}, process $N / 2^{B}$ endpoints at a time
\end{itemize}
\end{frame}

% The diagrams in Figure~\ref{trimming} show two rounds of edge trimming on the earlier example. In round one
% even nodes 2 and 12 lose their single incident edge and in round two, odd nodes 11 and 15 lose
% their remaining single incident edge. At this point only the 6-cycle is left, so further trimming
% would be pointless.

\begin{frame}
\frametitle{Trimming Example}
\begin{tikzpicture}[scale=2,>=stealth]
\node  (2) at (1, 2) [shape=circle,draw] {2};
\node  (4) at (2, 2) [shape=circle,draw] {4};
\node  (8) at (3, 2) [shape=circle,draw] {8};
\node (10) at (4, 2) [shape=circle,draw] {10};
\node (12) at (5, 2) [shape=circle,draw] {12};
\node  (5) at (4,-1) [shape=circle,draw] {5};
\node  (9) at (2,-1) [shape=circle,draw] {9};
\node (11) at (5,-1) [shape=circle,draw] {11};
\node (13) at (3,-1) [shape=circle,draw] {13};
\node (15) at (1,-1) [shape=circle,draw] {15};
\only<1> \draw [-]  (2) -- (15);
\only<1-3> \draw [-]  (4) -- (9);
\draw [-]  (8) -- (5);
\only<1-2> \draw [-]  (4) -- (15);
\only<1> \draw [-] (12) -- (11);
\draw [-] (10) -- (5);
\draw [-]  (4) -- (13);
\only<1-2> \draw [-] (10) -- (11);
\draw [-] (10) -- (13);
\draw [-]  (8) -- (9);
\end{tikzpicture}
\end{frame}

% After all edge trimming rounds, the counter memory is freed, and allocated to a
% custom cuckoo\_hashtable (based on \cite{preshing2013}) that presents the same interface as the
% simple array in the basic algorithm, but gets by with much fewer locations, as long as its {\em load},
% the ratio of remaining edges to number of locations, is bounded away from 1; e.g. under 90 percent.

\begin{frame}
\frametitle{Time-Memory Trade-Offs (TMTOs)}
\begin{itemize}
\item use $k$ times less memory
% \pause
\item but run $c \times k$ times slower
% \pause
\item memory-hardness $c$
% \pause
\item based on breadth-first-search (BFS)
% \pause
\item either $L/2$ or $L$ levels
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Solution Rate Reduction}
\begin{tikzpicture}
% \begin{axis}[ymin=0, xtick={2,8,16,24,32,40,48,56,64}, xlabel={cycle length $L$}, ylabel={slowdown factor}, legend pos=north west]
\begin{axis}[ymin=0, xlabel={cycle length $L$}, ylabel={slowdown factor}, legend pos=north west]
\addplot [color=red] coordinates {
(2,2.8) (4,6.4) (6,9.7) (8,14.5) (10,13.0) (12,25.1) (14,29.0) (16,28.5)
(20,30.4) (24,46.0) (28,36.0) (32,43.3) (40,51.0) (48,82.2) (56,81.0) (64,72.7)
};
\addlegendentry{BFS($L/2$)}
\addplot [color=green] coordinates {
(4,14.4) (6,11.6) (8,20.7) (10,21.4) (12,13.9) (14,18.1) (16,26.8) (20,21.9)
(24,18.8) (28,23.3) (32,31.8) (40,42.6) (48,35.6) (56,22.2) (64,41.0)
};
\addlegendentry{BFS($L$)}
%\addplot [color=blue, mark=o] coordinates {
%(2, 1.4)
%};
%\addlegendentry{customized}
\addlegendentry{}
\end{axis}
\end{tikzpicture}
\end{frame}

\begin{frame}
\frametitle{Choice of cycle length}
\begin{itemize}
\item
% \pause
length 2 cycle: two nonces with identical edge endpoints
% \pause
\item {\em collision} in edge space.
% \pause
\item
Essentially the {\em Momentum} PoW, (Daniel Larimer 2013)
% \pause

%Second, the choice of $M=2^{26}$ gives a ratio $\frac{M}{N}$ of 1 rather than $\frac{1}{2}$ and as such
%prohibits the use of edge trimming.
\item likely has greater variety of efficient algorithms
% \pause
\item improved BFS($L/2$) TMTO algorithm
% \pause
\item slowdown only $1.75$, not so infeasible
% \pause
\item suggest length in range 20-64
\end{itemize}
\end{frame}

%The plot below shows the distribution of cycle lengths found for sizes $2^{10},2^{15},2^{20},2^{25}$,
%as determined from 100000,100000,10000, and 10000 runs respectively. The tails of the distributions
%beyond $L=100$ are not shown. For reference, the longest cycle found was of length 2120.

% \begin{frame}
% \frametitle{Cycle Length Distribution}
% \begin{tikzpicture}[scale=1.2]
% \begin{axis}[ymin=0, xlabel={cycle length $L$}, ylabel={probability}, legend pos=north east]
% \addplot[color=orange] coordinates {
% (4,0.24862) (6,0.15673) (8,0.10907) (10,0.07952) (12,0.05783) (14,0.04269) (16,0.0303)
% (18,0.02237) (20,0.01653) (22,0.01168) (24,0.00815) (26,0.00511) (28,0.00374) (30,0.00251)
% (32,0.00191) (34,0.00098) (36,0.00079) (38,0.00029) (40,0.0003) (42,0.00011) (44,0.00018)
% (46,8e-05) (48,2e-05) (50,3e-05) };
% \addlegendentry{10}
% \addplot[color=green] coordinates {
% (4,0.24822) (6,0.16551) (8,0.12317) (10,0.09749) (12,0.08105) (14,0.07036) (16,0.05871) (18,0.05308)
% (20,0.04717) (22,0.04189) (24,0.03801) (26,0.03342) (28,0.03205) (30,0.02822) (32,0.02521)
% (34,0.02282) (36,0.0212) (38,0.01852) (40,0.01814) (42,0.01668) (44,0.01511) (46,0.01356)
% (48,0.01246) (50,0.01145) (52,0.0101) (54,0.0093) (56,0.00861) (58,0.00778) (60,0.00768)
% (62,0.00672) (64,0.00589) (66,0.00565) (68,0.00517) (70,0.00455) (72,0.00435) (74,0.00375)
% (76,0.00348) (78,0.00286) (80,0.00276) (82,0.0023) (84,0.00224) (86,0.00204) (88,0.00165)
% (90,0.00164) (92,0.00134) (94,0.00126) (96,0.00114) (98,0.00103) (100,0.00101) };
% \addlegendentry{15}
% \addplot[color=red] coordinates {
% (4,0.249) (6,0.1666) (8,0.1309) (10,0.0977) (12,0.0821) (14,0.0754) (16,0.0612) (18,0.0569)
% (20,0.0504) (22,0.0412) (24,0.0438) (26,0.0385) (28,0.0364) (30,0.0349) (32,0.0328) (34,0.0286)
% (36,0.0263) (38,0.0283) (40,0.0244) (42,0.0237) (44,0.0213) (46,0.0197) (48,0.0185) (50,0.0171)
% (52,0.0169) (54,0.0204) (56,0.0161) (58,0.0155) (60,0.0153) (62,0.0158) (64,0.0135) (66,0.0135)
% (68,0.0118) (70,0.0158) (72,0.0137) (74,0.012) (76,0.0108) (78,0.0119) (80,0.0116) (82,0.0106)
% (84,0.0112) (86,0.0102) (88,0.0075) (90,0.0096) (92,0.0091) (94,0.0094) (96,0.0077) (98,0.0089)
% (100,0.006) };
% \addlegendentry{20}
% \addplot[color=blue] coordinates {
% (4,0.2439) (6,0.1661) (8,0.1216) (10,0.1031) (12,0.0816) (14,0.0755) (16,0.0635) (18,0.055) (20,0.0511) (22,0.0451) (24,0.0427) (26,0.0369) (28,0.0375) (30,0.0336) (32,0.0302) (34,0.0297) (36,0.0264) (38,0.0268) (40,0.0254) (42,0.0215) (44,0.021) (46,0.0205) (48,0.0206) (50,0.0182) (52,0.017) (54,0.0192) (56,0.0172) (58,0.0169) (60,0.0167) (62,0.0147) (64,0.0169) (66,0.0137) (68,0.0169) (70,0.0125) (72,0.0127) (74,0.0123) (76,0.0139) (78,0.0122) (80,0.0131) (82,0.0129) (84,0.012) (86,0.0119) (88,0.0102) (90,0.0088) (92,0.0102) (94,0.0115) (96,0.0108) (98,0.0089) (100,0.0104) };
% \addlegendentry{25}
% \end{axis}
% \end{tikzpicture}
% \end{frame}

\begin{frame}
\frametitle{Choice of graph size}
\begin{itemize}
\item Larger sizes makes it hard to fit in single memory chip
% \pause
\item also harder for botnets to mine without excessive swapping.
% \pause
%Sending a computer into swap-hell will likely alert its owner and trigger a cleanup,
%so botnet operators can be expected to eschew memory bound PoWs in favor of low-memory ones.
\item but allow fewer attempts per block interval, wasting effort spent on last attempt
% \pause
% progress freeness
\item suggest graph sizes $2^{28}$ - $2^{32}$
%  with the larger ones geared more toward longer block interval times and faster mining hardware.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Dynamic Sizing}
\begin{itemize}
\item memory chip capacities evolve
% \pause
\item
re-evaluate the graph size now and then
% \pause
\item
double size when difficulty drops too much
% graph size is deemed to have become "too easy" for existing hardware, and gets doubled.
\item smooth transition and avoid loss of hashing power
  with range of sizes (Myriad style)
%namely $k$ consecutive 2-powers for some small number $k\geq 2$.
%As with Myriad-coin, separate difficulty controls are maintained for each size,
% adjusted so that each size accounts for roughly $\frac{1}{k}$ of all blocks.

% Doubling graph sizes is then equivalent to disabling the smallest 2-power,
% and enabling a new largest one, whose initial difficulty target is twice that of the previous largest.
% Even if none of the hardware that was working on the smallest 2-power is repurposed for a larger size,
% since this hardware only accounted for a fraction $\frac{1}{k}$ of the rewards, the loss of
% % proof-of-work power should be acceptable.

% It remains to decide what exact form the ``difficulties too low'' condition should take.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Summary}
\begin{itemize}
\item
novel graph-theoretic proof-of-work design
\item scalable memory
% \pause
\item instant verifiability
% \pause
\item first where memory latency dominates the runtime.
% \pause
% Barring any unforeseen memory-time trade-offs, it makes for a
\item near-ideal memory bound PoW
% \pause
\item potential for decentralization of mining.
\end{itemize}
\end{frame}

\end{document}


\documentclass[epsfig,10pt,fullpage]{article}

\newcommand{\LabNum}{11}
\newcommand{\CommonDocsPath}{../../../common/docs}
\input{\CommonDocsPath/preamble.tex}

\begin{document}

\centerline{\huge Digital Logic}
~\\
\centerline{\huge Laboratory Exercise \LabNum}
~\\
\centerline{\large Implementing Algorithms in Hardware}
~\\

This is an exercise in using algorithmic state machine charts to implement algorithms as hardware circuits.

\section*{Background}
\addcontentsline{toc}{1}{Background}
Algorithmic State Machine (ASM) charts are a design tool that allow
the specification of digital systems in a form similar to a flow chart. An example of
an ASM chart is shown in Figure~\ref{fig:bit_counting_asm}. It represents a circuit that
counts the number of bits set to 1 in an n-bit input A ($A=a_{n-1}a_{n-2}{\ldots}a_1a_0$).
The rectangular boxes in this diagram represent the {\it states} of the digital system,
and actions specified inside of a state box occur on each active clock edge in this
state.  Transitions between states are specified by arrows.
The diamonds in the ASM chart represent conditional tests, and the ovals represent
actions taken only if the corresponding conditions are either true (on an arrow labeled 1)
or false (on an arrow labeled 0).

\begin{figure}[H]
\begin{center}
\includegraphics{figures/bit_counting_asm}
\end{center}
\caption{ASM chart for a bit counting circuit.}
\label{fig:bit_counting_asm}
\end{figure}

In this ASM chart, state $S1$ is the initial state. In this state the {\it result} is
initialized to 0, and data is loaded into a register {\it A}, until a start signal,
{\it s}, is asserted.  The ASM chart then transitions to state $S2$, where it increments
the {\it result} to count the number of 1's in register {\it A}. Since state $S2$
specifies a shifting operation, then $A$ should be implemented as a shift register.
Also, since the {\it result} is incremented, then this variable should be implemented as a
counter.  When register $A$ contains 0 the ASM chart transitions to state $S3$, where it sets an
output {\it Done} $=1$ and waits for the signal $s$ to be deasserted.

~\\
A key distinction between ASM charts and flow charts is a concept known as {\it implied timing}.
The implied timing specifies that all actions associated with a given state take place
only when the system is in that state when an active clock edge occurs. For example, when
the system is in state $S1$ and the start signal $s$ becomes 1, then the next active clock
edge performs the following actions: initializes {\it result} to 0, and transitions to
state $S2$. The action {\it right-shift A} does not happen yet, because the system is not yet
in state $S2$. For each active clock cycle in state $S2$, the actions highlighted in
Figure~\ref{fig:bit_counting_asm} take place, as follows: increment {\it result} if bit
$a_0 = 1$, change to state~$S3$ if $A = 0$ (or else remain in state $S2$), and shift $A$ to
the right.

~\\
The implementation of the bit counting circuit includes the counter to store the {\it result} and
the shift register $A$, as well as a finite state machine.  The FSM is often referred to as
the {\it control} circuit, and the other components as the {\it datapath} circuit.

\section*{Part I}
\addcontentsline{toc}{2}{Part I}
Write Verilog code to implement the bit-counting circuit using the ASM chart shown in
Figure~\ref{fig:bit_counting_asm} on a DE-series board. Include in your Verilog code the
datapath components needed, and make an FSM for the control circuit. The inputs to your
circuit should consist of an 8-bit input connected to slide switches {\it SW}$_{7-0}$, a
synchronous reset connected to {\it KEY}$_0$, and a start signal
({\it s}) connected to switch {\it SW}$_9$. Use the 50 MHz clock signal provided on the
board as the clock input for your circuit. Be sure to synchronize the $s$ signal to the
clock. Display the number of~1s counted in the input data on the 7-segment display HEX0, and
signal that the algorithm is finished by lighting up {\it LEDR}$_9$.

\section*{Part II}
\addcontentsline{toc}{3}{Part II}
We wish to implement a binary search algorithm, which searches through an array to locate
an 8-bit value {\it A} specified via switches {\it SW}$_{7-0}$. A block diagram for the
circuit is shown in Figure~\ref{fig:binary_search_cct}.

\begin{figure}[H]
	\begin{center}
		\includegraphics[]{figures/binary_search_circuit.pdf}
	\end{center}
\caption{A block diagram for a circuit that performs a binary search.}
\label{fig:binary_search_cct}
\end{figure}

The binary search algorithm works on a sorted array. Rather than comparing each value in
the array to the one being sought, we first look at the middle element
and compare the sought value to the middle element. If the middle element has a greater value,
then we know that the element we seek must be in the first half of the array. Otherwise, the
value we seek must be in the other half of the array. By applying this approach recursively,
we can locate the sought element in only a few steps.

~\\
In this circuit, the array is stored in a memory module that is implemented inside the
FPGA chip.
A diagram of the memory module that we need to create is depicted in Figure~\ref{fig:fig_RAM}.
This memory module has one read port and one write port, and is called a {\it synchronous
random-access memory (synchronous RAM)}. Note that the memory module includes registers for
synchronously loading addresses, input data, and the {\it Write} input. These registers
are required due to the design of the memory
resources in the Intel\textsuperscript{\textregistered} FPGA chip. Use the Quartus\textsuperscript{\textregistered} IP Catalog tool to create the memory
module, by clicking on
{\sf Tools} $>$ {\sf IP Catalog}. In the IP Catalog window choose the {\it RAM:~1-PORT} module,
which is found under the {\sf Basic Functions $>$  On Chip Memory} category.
Select {\sf Verilog HDL} as the type of output file to create, and give the file the name
{\it memory\_block.v}.

~\\
Follow through the provided sequence of dialogs to create a memory that is eight-bits wide
and 32 words deep. Figures~\ref{fig:fig5} and ~\ref{fig:fig6} show the
relevant pages and how to properly configure the memory.

\begin{figure}[H]
	\begin{center}
		\includegraphics[]{figures/figure_RAM.pdf}
	\end{center}
	\caption{The 32 {\sf x} 8 RAM with address register.}
	\label{fig:fig_RAM}
\end{figure}

~\\
To place data into the memory, you need to specify {\it initial values}
that should be stored in the memory once your circuit has been programmed into the FPGA chip.
This can be done by initializing the memory using the contents of a {\it memory initialization
file (MIF)}. The appropriate screen is illustrated in Figure~\ref{fig:fig7}. We have specified
a file named {\it my\_array.mif}, which then has to be created in the folder that
contains the Quartus project. An example of a memory initialization file is given in
Figure~\ref{fig:fig_MIF}.  Set the contents of your {\it MIF} file such that it contains a
sorted collection of integers.

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=0.4]{figures/figure5.png}
	\end{center}
	\caption{{Specifying memory size.}}
	\label{fig:fig5}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=0.4]{figures/figure6.png}
	\end{center}
	\caption{Specifying which memory ports are registered.}
	\label{fig:fig6}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=0.4]{figures/figure7.png}
	\end{center}
	\caption{Specifying a memory initialization file (MIF).}
	\label{fig:fig7}
\end{figure}

\begin{figure}[H]
\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
{\bf DEPTH} = 32;\\
{\bf WIDTH} = 8;\\
{\bf ADDRESS\_RADIX} = HEX;\\
{\bf DATA\_RADIX} = BIN;\\
{\bf CONTENT}\\
{\bf BEGIN}\\
\\
00	:	01;\\
01	:	02;\\
02	:	03\\
03	:	05;\\
04	:	06;\\
05	:	06;\\
06	:	07;\\
$\ldots$ (some lines not shown)\\
1E :	1F;\\
1F :	20;\\
\\
{\bf END};\\
\end{tabbing}
\end{minipage}
\end{center}
\caption{An example memory initialization file (MIF).}
\label{fig:fig_MIF}
\end{figure}

Your circuit should produce a 5-bit output {\it L}, which specifies the address in the memory
where the number {\it A} is located. In addition, a signal
{\it Found} should be set high to indicate that the number {\it A} was found in the memory,
and set low otherwise.

~\\
Perform the following steps:
\begin{enumerate}
\item Create an ASM chart for the binary search algorithm. Keep in mind that the memory
has registers on its input ports.  Assume that the array has a fixed size of 32 elements.
\item Implement the FSM and the datapath for your circuit.
\item Connect your FSM and datapath to the memory block as indicated in
Figure~\ref{fig:binary_search_cct}.
\item Include in your project the necessary pin assignments to implement your circuit on
your DE-series board. Use switch {\it SW}$_{9}$ to drive the {\it Start} input,
use {\it SW}$_{7{\ldots}0}$ to specify the value {\it A}, use
{\it KEY}$_0$ for {\it Resetn}, and use the board's 50 MHz clock signal
as the {\it Clock} input (be sure to synchronize the {\it Start} input to the clock).
Display the address of the data $A$, if found, on 7-segment displays
{\it HEX}1 and {\it HEX}0, as a hexadecimal number.  Finally, use
{\it LEDR}$_{9}$ for the {\it Found} signal.
\item Create a file called {\it my\_array.mif} and fill it with an ordered set of 32
eight-bit integer numbers.
\item Compile your design, and then download and test it.
\end{enumerate}

\input{\CommonDocsPath/copyright.tex}
\end{document}


% The LaTeX below is mostly computer-generated (for reasons of speed); don't expect it to be very readable. Sorry.

\documentclass[paper=a4, fontsize=12pt]{scrartcl}%
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\usepackage{mathrsfs}
\usepackage{sectsty}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{framed}
\usepackage{ifthen}
\usepackage{lastpage}
\usepackage[headsepline,footsepline,manualmark]{scrlayer-scrpage}
\usepackage[height=10in,a4paper,hmargin={1in,0.8in}]{geometry}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}%
\setcounter{MaxMatrixCols}{30}
%TCIDATA{OutputFilter=latex2.dll}
%TCIDATA{Version=5.50.0.2960}
%TCIDATA{LastRevised=Tuesday, February 19, 2019 22:31:04}
%TCIDATA{<META NAME="GraphicsSave" CONTENT="32">}
%TCIDATA{<META NAME="SaveForMode" CONTENT="1">}
%TCIDATA{BibliographyScheme=Manual}
%BeginMSIPreambleData
\providecommand{\U}[1]{\protect\rule{.1in}{.1in}}
%EndMSIPreambleData
\allsectionsfont{\centering \normalfont\scshape}
\setlength\parindent{20pt}
\newcommand{\CC}{\mathbb{C}}
\newcommand{\RR}{\mathbb{R}}
\newcommand{\QQ}{\mathbb{Q}}
\newcommand{\NN}{\mathbb{N}}
\newcommand{\PP}{\mathbb{P}}
\newcommand{\Z}[1]{\mathbb{Z}/#1\mathbb{Z}}
\newcommand{\ZZ}{\mathbb{Z}}
\newcommand{\id}{\operatorname{id}}
\newcommand{\lcm}{\operatorname{lcm}}
\newcommand{\set}[1]{\left\{ #1 \right\}}
\newcommand{\abs}[1]{\left| #1 \right|}
\newcommand{\tup}[1]{\left( #1 \right)}
\newcommand{\ive}[1]{\left[ #1 \right]}
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
\newcommand{\underbrack}[2]{\underbrace{#1}_{\substack{#2}}}
\newcommand{\powset}[2][]{\ifthenelse{\equal{#2}{}}{\mathcal{P}\left(#1\right)}{\mathcal{P}_{#1}\left(#2\right)}}
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}}
\newcommand{\nnn}{\nonumber\\}
\let\sumnonlimits\sum
\let\prodnonlimits\prod
\let\cupnonlimits\bigcup
\let\capnonlimits\bigcap
\renewcommand{\sum}{\sumnonlimits\limits}
\renewcommand{\prod}{\prodnonlimits\limits}
\renewcommand{\bigcup}{\cupnonlimits\limits}
\renewcommand{\bigcap}{\capnonlimits\limits}
\newtheoremstyle{plainsl}
{8pt plus 2pt minus 4pt}
{8pt plus 2pt minus 4pt}
{\slshape}
{0pt}
{\bfseries}
{.}
{5pt plus 1pt minus 1pt}
{}
\theoremstyle{plainsl}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}[theorem]{Conjecture}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{exercise}[theorem]{Exercise}
\newtheorem{examples}[theorem]{Examples}
\newtheorem{algorithm}[theorem]{Algorithm}
\newtheorem{question}[theorem]{Question}
\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newenvironment{statement}{\begin{quote}}{\end{quote}}
\newenvironment{fineprint}{\begin{small}}{\end{small}}
\iffalse
\newenvironment{proof}[1][Proof]{\noindent\textbf{#1.} }{\ \rule{0.5em}{0.5em}}
\newenvironment{question}[1][Question]{\noindent\textbf{#1.} }{\ \rule{0.5em}{0.5em}}
\fi
\newcommand{\myname}{Darij Grinberg}
\newcommand{\myid}{00000000}
\newcommand{\mymail}{dgrinber@umn.edu}
\newcommand{\psetnumber}{2}
\ihead{Solutions to homework set \#\psetnumber}
\ohead{page \thepage\ of \pageref{LastPage}}
\ifoot{\myname, \myid}
\ofoot{\mymail}
\begin{document}

\title{ \normalfont {\normalsize \textsc{University of Minnesota, School of
Mathematics} }\\[25pt] \rule{\linewidth}{0.5pt} \\[0.4cm] {\huge Math 4281: Introduction to Modern Algebra, }\\Spring 2019: Homework 2\\\rule{\linewidth}{2pt} \\[0.5cm] }
\author{Darij Grinberg}
\maketitle

%----------------------------------------------------------------------------------------
%	EXERCISE 1
%----------------------------------------------------------------------------------------
\rule{\linewidth}{0.3pt} \\[0.4cm]

\section{Exercise 1: gcd basics}

\subsection{Problem}

Prove the following:

\begin{enumerate}
\item[\textbf{(a)}] If $a_{1}, a_{2}, b_{1}, b_{2}$ are integers satisfying
$a_{1} \mid b_{1}$ and $a_{2} \mid b_{2}$, then $\gcd\left(  a_{1}, a_{2}
\right)  \mid\gcd\left(  b_{1}, b_{2} \right)  $.

\item[\textbf{(b)}] If $a, b, c, s$ are integers, then $\gcd\left(  sa, sb, sc
\right)  = \left|  s \right|  \gcd\left(  a, b, c \right)  $.
\end{enumerate}

\subsection{Solution}

\textbf{(a)} See
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class notes},
where this is Exercise 2.9.4. (The numbering may shift; it is one of the
exercises in the \textquotedblleft Common divisors, the Euclidean algorithm
and the Bezout theorem\textquotedblright\ section.)

\textbf{(b)} See
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class notes},
where this is Exercise 2.9.6. (The numbering may shift; it is one of the
exercises in the \textquotedblleft Common divisors, the Euclidean algorithm
and the Bezout theorem\textquotedblright\ section.)

%----------------------------------------------------------------------------------------
%	EXERCISE 2
%----------------------------------------------------------------------------------------
\rule{\linewidth}{0.3pt} \\[0.4cm]

\section{Exercise 2: Products of gcds}

\subsection{Problem}

Prove the following:

Any four integers $u,v,x,y$ satisfy $\gcd\left(  u,v\right)  \gcd\left(
x,y\right)  =\gcd\left(  ux,uy,vx,vy\right)  $.

\subsection{Solution}

See \href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class
notes}, where this is Exercise 2.10.10. (The numbering may shift; it is one of
the exercises in the \textquotedblleft Coprime integers\textquotedblright\ section.)

%----------------------------------------------------------------------------------------
%	EXERCISE 3
%----------------------------------------------------------------------------------------
\rule{\linewidth}{0.3pt} \\[0.4cm]

\section{Exercise 3: The gcd-lcm connection for three numbers}

\subsection{Problem}

Let $a, b, c$ be three integers. Prove that $\operatorname{lcm}\left(  a, b, c
\right)  \gcd\left(  bc, ca, ab \right)  = \left|  abc \right|  $.

\subsection{Solution}

See \href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class
notes}, where this is Exercise 2.11.2 \textbf{(b)}. (The numbering may shift;
it is one of the exercises in the \textquotedblleft Lowest common
multiples\textquotedblright\ section.)

%----------------------------------------------------------------------------------------
%	EXERCISE 4
%----------------------------------------------------------------------------------------
\rule{\linewidth}{0.3pt} \\[0.4cm]

\section{Exercise 4: Divisibility tests for $3, 9, 11, 7$}

\subsection{Problem}

Let $n$ be a positive integer. Let ``$d_{k} d_{k-1} \cdots d_{0}$'' be the
decimal representation of $n$; this means that $d_{0}, d_{1}, \ldots, d_{k}$
are digits (i.e., elements of $\left\{  0, 1, \ldots, 9 \right\}  $) such that
$n = d_{k} 10^{k} + d_{k-1} 10^{k-1} + \cdots+ d_{0} 10^{0}$. The digits
$d_{0}, d_{1}, \ldots, d_{k}$ are called the \textit{digits of $n$}.

(Incidentally, the quickest way to find these digits is by repeated division
with remainder: To obtain the decimal representation of $n \geq10$, you take
the decimal representation of $n // 10$ and append the digit $n \% 10$ at the
end. Thus,
\[
d_{0} = n \% 10, \qquad d_{1} = \left(  n // 10 \right)  \% 10, \qquad d_{2} =
\left(  \left(  n // 10 \right)  // 10 \right)  \% 10, \qquad\text{etc.}
\]
But in this exercise, you can just assume that the decimal representation exists.)

\begin{enumerate}
\item[\textbf{(a)}] Prove that $3 \mid n$ if and only if $3 \mid d_{k} +
d_{k-1} + \cdots+ d_{0}$. (In other words, a positive integer $n$ is divisible
by $3$ if and only if the sum of its digits is divisible by $3$.)

\item[\textbf{(b)}] Prove that $9 \mid n$ if and only if $9 \mid d_{k} +
d_{k-1} + \cdots+ d_{0}$. (In other words, a positive integer $n$ is divisible
by $9$ if and only if the sum of its digits is divisible by $9$.)

\item[\textbf{(c)}] Prove that $11 \mid n$ if and only if $11 \mid\left(  -1
\right)  ^{k} d_{k} + \left(  -1 \right)  ^{k-1} d_{k-1} + \cdots+ \left(  -1
\right)  ^{0} d_{0}$. (In other words, a positive integer $n$ is divisible by
$11$ if and only if the sum of its digits in the even positions minus the sum
of its digits in the odd positions is divisible by $11$.)

\item[\textbf{(d)}] Let $q = d_{k} 10^{k-1} + d_{k-1} 10^{k-2} + \cdots+ d_{1}
10^{0}$. (Equivalently, $q = n // 10 = \dfrac{n-d_{0}}{10}$; this is the
number obtained from $n$ by dropping the least significant digit.) Prove that
$7 \mid n$ if and only if $7 \mid q - 2 d_{0}$.

(This gives a recursive test for divisibility by $7$.)
\end{enumerate}

%Note to self: sol1.tex, problems 9-11.

\subsection{Solution sketch}

We will use the following quasi-trivial lemma:

\begin{lemma}
\label{lem.ent.mod.ndivx=ndivy}Let $n,x,y$ be three integers such that
$x\equiv y\operatorname{mod}n$. Then, we have $n\mid x$ if and only if $n\mid
y$.
\end{lemma}

\begin{proof}
[Proof of Lemma \ref{lem.ent.mod.ndivx=ndivy}.]$\Longrightarrow:$ Assume that
$n\mid x$. We must prove that $n\mid y$.

We have $n\mid x$, thus $x\equiv0\operatorname{mod}n$. But $x\equiv
y\operatorname{mod}n$ and thus $y\equiv x\equiv0\operatorname{mod}n$. Hence,
$n\mid y$. This proves the \textquotedblleft$\Longrightarrow$%
\textquotedblright\ direction of Lemma \ref{lem.ent.mod.ndivx=ndivy}.

$\Longleftarrow:$ Assume that $n\mid y$. We must prove that $n\mid x$.

We have $n\mid y$, thus $y\equiv0\operatorname{mod}n$. But $x\equiv
y\equiv0\operatorname{mod}n$. Hence, $n\mid x$. This proves the
\textquotedblleft$\Longleftarrow$\textquotedblright\ direction of Lemma
\ref{lem.ent.mod.ndivx=ndivy}.
\end{proof}

\textbf{(a)} We have $10\equiv1\operatorname{mod}3$. Thus, each $m\in
\mathbb{N}$ satisfies
\begin{equation}
10^{m}\equiv1^{m}=1\operatorname{mod}3. \label{sol.ent.base10.div39117.a.1}%
\end{equation}
Now,%
\begin{align*}
n  &  =d_{k}\underbrace{10^{k}}_{\substack{\equiv1\operatorname{mod}%
3\\\text{(by \eqref{sol.ent.base10.div39117.a.1})}}}+d_{k-1}%
\underbrace{10^{k-1}}_{\substack{\equiv1\operatorname{mod}3\\\text{(by
\eqref{sol.ent.base10.div39117.a.1})}}}+\cdots+d_{0}\underbrace{10^{0}%
}_{\substack{\equiv1\operatorname{mod}3\\\text{(by
\eqref{sol.ent.base10.div39117.a.1})}}}\\
&  \equiv d_{k}+d_{k-1}+\cdots+d_{0}\operatorname{mod}3.
\end{align*}
Thus, $3\mid n$ if and only if $3\mid d_{k}+d_{k-1}+\cdots+d_{0}$ (by Lemma
\ref{lem.ent.mod.ndivx=ndivy}, applied to $3$, $n$ and $d_{k}+d_{k-1}%
+\cdots+d_{0}$ instead of $n$, $x$ and $y$).

This solves part \textbf{(a)}. \\[0.4cm]

\textbf{(b)} The solution to part \textbf{(b)} is precisely the same as that
for part \textbf{(a)}, except that the $3$'s need to be replaced by $9$'s.
\\[0.4cm]

\textbf{(c)} We have $10\equiv-1\operatorname{mod}11$. Hence, each
$m\in\mathbb{N}$ satisfies
\begin{equation}
10^{m}\equiv\left(  -1\right)  ^{m}\operatorname{mod}11.
\label{sol.ent.base10.div39117.c.1}%
\end{equation}
Now,%
\begin{align*}
n  &  =d_{k}\underbrace{10^{k}}_{\substack{\equiv\left(  -1\right)
^{k}\operatorname{mod}11\\\text{(by \eqref{sol.ent.base10.div39117.c.1})}%
}}+d_{k-1}\underbrace{10^{k-1}}_{\substack{\equiv\left(  -1\right)
^{k-1}\operatorname{mod}11\\\text{(by \eqref{sol.ent.base10.div39117.c.1})}%
}}+\cdots+d_{0}\underbrace{10^{0}}_{\substack{\equiv\left(  -1\right)
^{0}\operatorname{mod}11\\\text{(by \eqref{sol.ent.base10.div39117.c.1})}}}\\
&  \equiv d_{k}\left(  -1\right)  ^{k}+d_{k-1}\left(  -1\right)  ^{k-1}%
+\cdots+d_{0}\left(  -1\right)  ^{0}\\
&  =\left(  -1\right)  ^{k}d_{k}+\left(  -1\right)  ^{k-1}d_{k-1}%
+\cdots+\left(  -1\right)  ^{0}d_{0}\operatorname{mod}11.
\end{align*}
Thus, $11\mid n$ if and only if $11\mid\left(  -1\right)  ^{k}d_{k}+\left(
-1\right)  ^{k-1}d_{k-1}+\cdots+\left(  -1\right)  ^{0}d_{0}$ (by Lemma
\ref{lem.ent.mod.ndivx=ndivy}, applied to $11$, $n$ and $\left(  -1\right)
^{k}d_{k}+\left(  -1\right)  ^{k-1}d_{k-1}+\cdots+\left(  -1\right)  ^{0}%
d_{0}$ instead of $n$, $x$ and $y$). This solves part \textbf{(c)}. \\[0.4cm]

\textbf{(d)} We have%
\begin{align*}
n  &  =d_{k}10^{k}+d_{k-1}10^{k-1}+\cdots+d_{0}10^{0}\\
&  =\underbrace{\left(  d_{k}10^{k}+d_{k-1}10^{k-1}+\cdots+d_{1}10^{1}\right)
}_{=10\cdot\left(  d_{k}10^{k-1}+d_{k-1}10^{k-2}+\cdots+d_{1}10^{0}\right)
}+d_{0}\underbrace{10^{0}}_{=1}\\
&  =10\cdot\underbrace{\left(  d_{k}10^{k-1}+d_{k-1}10^{k-2}+\cdots
+d_{1}10^{0}\right)  }_{=q}+d_{0}=10q+d_{0}.
\end{align*}
Now, we need to prove two claims:

\begin{statement}
\textit{Claim 1:} If $7\mid n$, then $7\mid q-2d_{0}$.
\end{statement}

\begin{statement}
\textit{Claim 2:} If $7\mid q-2d_{0}$, then $7\mid n$.
\end{statement}

\textit{Proof of Claim 1:} Assume that $7\mid n$. Then, $7\mid n=10q+d_{0}%
=d_{0}-\left(  -10q\right)  $, so that $d_{0}\equiv-10q\operatorname{mod}7$.
Hence,%
\[
q-2\underbrace{d_{0}}_{\equiv-10q\operatorname{mod}7}\equiv q-2\left(
-10q\right)  =\underbrace{21}_{\equiv0\operatorname{mod}7}q\equiv
0\operatorname{mod}7,
\]
so that $7\mid q-2d_{0}$. This proves Claim 1.

\textit{Proof of Claim 2:} Assume that $7\mid q-2d_{0}$. Thus, $q\equiv
2d_{0}\operatorname{mod}7$. Hence,%
\[
n=10\underbrace{q}_{\equiv2d_{0}\operatorname{mod}7}+d_{0}\equiv10\left(
2d_{0}\right)  +d_{0}=\underbrace{21}_{\equiv0\operatorname{mod}7}d_{0}%
\equiv0\operatorname{mod}7,
\]
so that $7\mid n$. This proves Claim 2.

Now, part \textbf{(d)} of the problem is solved.

%----------------------------------------------------------------------------------------
%	EXERCISE 5
%----------------------------------------------------------------------------------------
\rule{\linewidth}{0.3pt} \\[0.4cm]

\section{Exercise 5: A divisibility}

\subsection{Problem}

Let $n \in\mathbb{N}$. Prove that $7 \mid3^{2n+1} + 2^{n+2}$.

\subsection{Solution}

See \href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class
notes}, where this is Exercise 2.5.1. (The numbering may shift; it is one of
the exercises in the \textquotedblleft Substitutivity for
congruences\textquotedblright\ section.)

%----------------------------------------------------------------------------------------
%	EXERCISE 6
%----------------------------------------------------------------------------------------
\rule{0pt}{0.3pt} \\[0.4cm]

\section{Exercise 6: A binomial coefficient sum}

\subsection{Problem}

Let $n \in\mathbb{N}$. Prove that
\begin{align}
\sum_{k=0}^{n} \dbinom{-2}{k} = \left(  -1 \right)  ^{n} \left(  \left(  n+2
\right)  // 2 \right)  .
\end{align}

\subsection{Solution}

Recall the following fact (which was the claim of Exercise 3 \textbf{(d)} on
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/hw0s.pdf}{homework set \#0}):

\begin{proposition}
\label{prop.sol.binom.-2choosek-sum.upneg}Any $n\in\mathbb{Q}$ and
$k\in\mathbb{Q}$ satisfy
\[
\dbinom{-n}{k}=\left(  -1\right)  ^{k}\dbinom{k+n-1}{k}.
\]

\end{proposition}

Let us also recall another fact (the claim of Exercise 3 \textbf{(c)} on
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/hw0s.pdf}{homework set \#0}):

\begin{proposition}
\label{prop.sol.binom.-2choosek-sum.sym}If $n\in\mathbb{N}$ and $k\in
\mathbb{Q}$, then
\[
\dbinom{n}{k}=\dbinom{n}{n-k}.
\]

\end{proposition}

Next, we show a simple formula for the binomial coefficients in the exercise:

\begin{lemma}
\label{lem.sol.binom.-2choosek-sum.-2choosek}If $k\in\mathbb{N}$, then%
\[
\dbinom{-2}{k}=\left(  -1\right)  ^{k}\left(  k+1\right)  .
\]

\end{lemma}

\begin{proof}
[Proof of Lemma \ref{lem.sol.binom.-2choosek-sum.-2choosek}.]Let
$k\in\mathbb{N}$. Then, Proposition \ref{prop.sol.binom.-2choosek-sum.upneg}
(applied to $2$ instead of $n$) yields
\begin{equation}
\dbinom{-2}{k}=\left(  -1\right)  ^{k}\dbinom{k+2-1}{k}=\left(  -1\right)
^{k}\dbinom{k+1}{k} \label{pf.lem.sol.binom.-2choosek-sum.-2choosek.1}%
\end{equation}
(since $k+2-1=k+1$). But $k\in\mathbb{N}$ and thus $k+1\in\mathbb{N}$. Hence,
Proposition \ref{prop.sol.binom.-2choosek-sum.sym} (applied to $k+1$ instead
of $n$) yields%
\begin{align*}
\dbinom{k+1}{k}  &  =\dbinom{k+1}{\left(  k+1\right)  -k}=\dbinom{k+1}%
{1}\qquad\left(  \text{since }\left(  k+1\right)  -k=1\right) \\
&  =\dfrac{\left(  k+1\right)  \left(  \left(  k+1\right)  -1\right)  \left(
\left(  k+1\right)  -2\right)  \cdots\left(  \left(  k+1\right)  -1+1\right)
}{1!}\\
&  \qquad\left(  \text{by the definition of }\dbinom{k+1}{1}\right) \\
&  =\dfrac{k+1}{1!}\qquad\left(
\begin{array}
[c]{c}%
\text{since the}\\
\text{product }\left(  k+1\right)  \left(  \left(  k+1\right)  -1\right)
\left(  \left(  k+1\right)  -2\right)  \cdots\left(  \left(  k+1\right)
-1+1\right) \\
\text{has only one factor}%
\end{array}
\right) \\
&  =\dfrac{k+1}{1}=k+1.
\end{align*}
Hence, \eqref{pf.lem.sol.binom.-2choosek-sum.-2choosek.1} becomes
\[
\dbinom{-2}{k}=\left(  -1\right)  ^{k}\underbrace{\dbinom{k+1}{k}}%
_{=k+1}=\left(  -1\right)  ^{k}\left(  k+1\right)  .
\]
This proves Lemma \ref{lem.sol.binom.-2choosek-sum.-2choosek}.
\end{proof}

% (Note that Lemma \ref{lem.sol.binom.-2choosek-sum.-2choosek} also
% appears in \cite[Exercise 3.5 \textbf{(b)}]{detnotes}.)

Now, in order to solve the problem at hand, it suffices to prove the identity%
\begin{equation}
\sum_{k=0}^{n}\left(  -1\right)  ^{k}\left(  k+1\right)  =\left(  -1\right)
^{n}\left(  \left(  n+2\right)  //2\right)  .
\label{sol.binom.-2choosek-sum.new-goal}%
\end{equation}
Indeed, once \eqref{sol.binom.-2choosek-sum.new-goal} is proven, it will
follow that%
\[
\sum_{k=0}^{n}\underbrace{\dbinom{-2}{k}}_{\substack{=\left(  -1\right)
^{k}\left(  k+1\right)  \\\text{(by Lemma
\ref{lem.sol.binom.-2choosek-sum.-2choosek})}}}=\sum_{k=0}^{n}\left(
-1\right)  ^{k}\left(  k+1\right)  =\left(  -1\right)  ^{n}\left(  \left(
n+2\right)  //2\right)
\]
(by \eqref{sol.binom.-2choosek-sum.new-goal}), and thus the exercise will be solved.

Before we prove \eqref{sol.binom.-2choosek-sum.new-goal}, let us state some
basic facts about even and odd numbers:

\begin{proposition}
\label{prop.sol.binom.-2choosek-sum.evod}Let $u$ be an integer.

\textbf{(a)} The integer $u$ is even if and only if $u\%2=0$.

\textbf{(b)} The integer $u$ is odd if and only if $u\%2=1$.

\textbf{(c)} The integer $u$ is even if and only if $u\equiv0\mod 2$.

\textbf{(d)} The integer $u$ is odd if and only if $u\equiv1\mod 2$.

\textbf{(e)} If $u$ is even, then $\left(  -1\right)  ^{u}=1$.

\textbf{(f)} If $u$ is odd, then $\left(  -1\right)  ^{u}=-1$.

\textbf{(g)} We have $u=\left(  u//2\right)  \cdot2+\left(  u\%2\right)  $.
\end{proposition}

\begin{proof}
[Proof of Proposition \ref{prop.sol.binom.-2choosek-sum.evod}.]Parts
\textbf{(a)}, \textbf{(b)}, \textbf{(c)} and \textbf{(d)} of Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} are parts of Exercise 3 on
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/hw1s.pdf}{homework set \#1},
and their proofs can be found in
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class notes}.
Thus, we only need to prove parts \textbf{(e)}, \textbf{(f)} and \textbf{(g)} now.

\textbf{(e)} Assume that that $u$ is even. Then, $u\equiv0\operatorname{mod}2$
(by Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(c)}). In
other words, $2\mid u$. In other words, $u=2g$ for some $g\in\mathbb{Z}$.
Consider this $g$. From $u=2g$, we obtain $\left(  -1\right)  ^{u}=\left(
-1\right)  ^{2g}=\left(  \underbrace{\left(  -1\right)  ^{2}}_{=1}\right)
^{g}=1^{g}=1$. This proves Proposition \ref{prop.sol.binom.-2choosek-sum.evod}
\textbf{(e)}.

\textbf{(f)} Assume that that $u$ is odd. Then, $u\equiv1\operatorname{mod}2$
(by Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(d)}). In
other words, $2\mid u-1$. In other words, $u-1=2g$ for some $g\in\mathbb{Z}$.
Consider this $g$. From $u-1=2g$, we obtain $\left(  -1\right)  ^{u-1}=\left(
-1\right)  ^{2g}=\left(  \underbrace{\left(  -1\right)  ^{2}}_{=1}\right)
^{g}=1^{g}=1$. Now, $\left(  -1\right)  ^{u}=\left(  -1\right)
\underbrace{\left(  -1\right)  ^{u-1}}_{=1}=-1$. This proves Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(f)}.

\textbf{(g)} In Corollary 2.6.9 \textbf{(d)} of
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{the class notes},
we have proven $u=\left(  u//n\right)
n+\left(  u\%n\right)  $ for any positive integer $n$. Applying this to $n=2$,
we obtain $u=\left(  u//2\right)  \cdot2+\left(  u\%2\right)  $. This proves
Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(g)}.
\end{proof}

In order to prove \eqref{sol.binom.-2choosek-sum.new-goal}, we distinguish
between two cases:

\textit{Case 1:} The integer $n$ is even.

\textit{Case 2:} The integer $n$ is odd.

Let us first consider Case 1. In this case, the integer $n$ is even. Thus,
$\left(  -1\right)  ^{n}=1$ (by Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(e)}, applied to $u=n$).
Furthermore, $n$ is even, and thus $n\equiv0\operatorname{mod}2$ (by
Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(c)}, applied to
$u=n$). Hence, $\underbrace{n}_{\equiv0\operatorname{mod}2}+2\equiv
0+2=2\equiv0\operatorname{mod}2$. In other words, $n+2$ is even (by
Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(c)}, applied to
$u=n+2$). In other words, $\left(  n+2\right)  \%2=0$ (by Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(a)}, applied to $u=n+2$).
Now, Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(g)} (applied
to $u=n+2$) yields
\[
\left(  n+2\right)  =\left(  \left(  n+2\right)  //2\right)  \cdot
2+\underbrace{\left(  \left(  n+2\right)  \%2\right)  }_{=0}=\left(  \left(
n+2\right)  //2\right)  \cdot2.
\]
Solving this for $\left(  n+2\right)  //2$, we find $\left(  n+2\right)
//2=\left(  n+2\right)  /2$.

Now,%
\begin{align*}
\sum_{k=0}^{n}\left(  -1\right)  ^{k}\left(  k+1\right)   &  =1-2+3-4\pm
\cdots+\underbrace{\left(  -1\right)  ^{n}}_{=1}\left(  n+1\right) \\
&  =1-2+3-4\pm\cdots+\left(  n+1\right) \\
&  =\underbrace{\left(  1-2\right)  }_{=-1}+\underbrace{\left(  3-4\right)
}_{=-1}+\underbrace{\left(  5-6\right)  }_{=-1}+\cdots+\underbrace{\left(
\left(  n-1\right)  -n\right)  }_{=-1}+\left(  n+1\right) \\
&  =\underbrace{\underbrace{\left(  \left(  -1\right)  +\left(  -1\right)
+\left(  -1\right)  +\cdots+\left(  -1\right)  \right)  }_{n/2\text{ addends}%
}}_{=n/2\cdot\left(  -1\right)  =-n/2}+\left(  n+1\right) \\
&  =-n/2+\left(  n+1\right)  =n/2+1.
\end{align*}
Comparing this with%
\[
\underbrace{\left(  -1\right)  ^{n}}_{=1}\left(  \left(  n+2\right)
//2\right)  =\left(  n+2\right)  //2=\left(  n+2\right)  /2=n/2+1,
\]
we obtain $\sum_{k=0}^{n}\left(  -1\right)  ^{k}\left(  k+1\right)  =\left(
n+2\right)  //2$. Hence, \eqref{sol.binom.-2choosek-sum.new-goal} is proved in
Case 1.

Let us next consider Case 2. In this case, the integer $n$ is odd. Thus,
$\left(  -1\right)  ^{n}=-1$ (by Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(f)}, applied to $u=n$).
Furthermore, $n$ is odd, and thus $n\equiv1\operatorname{mod}2$ (by
Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(d)}, applied to
$u=n$). Hence, $\underbrace{n}_{\equiv1\operatorname{mod}2}+2\equiv
1+2=3\equiv1\operatorname{mod}2$. In other words, $n+2$ is odd (by Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(d)}, applied to $u=n+2$). In
other words, $\left(  n+2\right)  \%2=1$ (by Proposition
\ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(b)}, applied to $u=n+2$).
Now, Proposition \ref{prop.sol.binom.-2choosek-sum.evod} \textbf{(g)} (applied
to $u=n+2$) yields
\[
\left(  n+2\right)  =\left(  \left(  n+2\right)  //2\right)  \cdot
2+\underbrace{\left(  \left(  n+2\right)  \%2\right)  }_{=1}=\left(  \left(
n+2\right)  //2\right)  \cdot2+1.
\]
Solving this for $\left(  n+2\right)  //2$, we find $\left(  n+2\right)
//2=\left(  \left(  n+2\right)  -1\right)  /2=\left(  n+1\right)  /2$.

Now,%
\begin{align*}
\sum_{k=0}^{n}\left(  -1\right)  ^{k}\left(  k+1\right)   &  =1-2+3-4\pm
\cdots+\underbrace{\left(  -1\right)  ^{n}}_{=-1}\left(  n+1\right) \\
&  =1-2+3-4\pm\cdots-\left(  n+1\right) \\
&  =\underbrace{\left(  1-2\right)  }_{=-1}+\underbrace{\left(  3-4\right)
}_{=-1}+\underbrace{\left(  5-6\right)  }_{=-1}+\cdots+\underbrace{\left(
n-\left(  n+1\right)  \right)  }_{=-1}\\
&  =\underbrace{\left(  \left(  -1\right)  +\left(  -1\right)  +\left(
-1\right)  +\cdots+\left(  -1\right)  \right)  }_{\left(  n+1\right)  /2\text{
addends}}\\
&  =\left(  n+1\right)  /2\cdot\left(  -1\right)  =-\left(  n+1\right)  /2.
\end{align*}
Comparing this with%
\[
\underbrace{\left(  -1\right)  ^{n}}_{=-1}\underbrace{\left(  \left(
n+2\right)  //2\right)  }_{=\left(  n+1\right)  /2}=\left(  -1\right)  \left(
n+1\right)  /2=-\left(  n+1\right)  /2,
\]
we obtain $\sum_{k=0}^{n}\left(  -1\right)  ^{k}\left(  k+1\right)  =\left(
n+2\right)  //2$. Hence, \eqref{sol.binom.-2choosek-sum.new-goal} is proved in
Case 2.

We have now proven \eqref{sol.binom.-2choosek-sum.new-goal} in each of the two
Cases 1 and 2. Thus, \eqref{sol.binom.-2choosek-sum.new-goal} always holds. As
explained above, by proving \eqref{sol.binom.-2choosek-sum.new-goal}, we have
solved the exercise.

\subsection{Remark}

I have posed this exercise in a slightly different form as Exercise 1 on
homework set \#9 of
\href{http://www.cip.ifi.lmu.de/~grinberg/t/17f/index.html}{UMN Fall 2017 Math
4990}. (The form was different in that I wrote $\left\lfloor \dfrac{n+2}%
{2}\right\rfloor $ instead of $\left(  n+2\right)  //2$. Of course, this is
the same thing.) See also
\href{http://www.cip.ifi.lmu.de/~grinberg/t/17f/hw9os-chen.pdf}{Angela Chen's
solution} to that exercise.

The exercise is more or less a combination of \cite[Exercise 2.9]{detnotes}
and \cite[Exercise 3.5 \textbf{(b)}]{detnotes}. In fact, Lemma
\ref{lem.sol.binom.-2choosek-sum.-2choosek} above is the claim of
\cite[Exercise 3.5 \textbf{(b)}]{detnotes}, whereas the identity
\eqref{sol.binom.-2choosek-sum.new-goal} is the claim of \cite[Exercise
2.9]{detnotes} (except that \cite[Exercise 2.9]{detnotes} writes $%
\begin{cases}
n/2+1, & \text{if }n\text{ is even};\\
\left(  n+1\right)  /2, & \text{if }n\text{ is odd}%
\end{cases}
$ for $\left(  n+2\right)  //2$, but the equality of these two expressions is
easy to establish).

Yet another way to state the identity in the exercise is%
\[
\sum_{k=0}^{n}\dbinom{-2}{k}=\dfrac{1+\left(  -1\right)  ^{n}\cdot\left(
2n+3\right)  }{4}.
\]
(Here, the \textquotedblleft oscillator\textquotedblright\ $\left(  -1\right)
^{n}$ is being used instead of $\left(  n+2\right)  //2$ in order to obtain
different behavior for even and odd $n$.) Similar identities are%
\begin{align*}
\sum_{k=0}^{n}\dbinom{0}{k}  &  =1;\\
\sum_{k=0}^{n}\dbinom{-1}{k}  &  =\dfrac{1+\left(  -1\right)  ^{n}}{2}=\left(
n+1\right)  \%2;\\
\sum_{k=0}^{n}\dbinom{-3}{k}  &  =\dfrac{1+\left(  -1\right)  ^{n}\cdot\left(
2n^{2}+8n+7\right)  }{8};\\
\sum_{k=0}^{n}\dbinom{-4}{k}  &  =\dfrac{3+\left(  -1\right)  ^{n}\cdot\left(
4n^{3}+30n^{2}+68n+45\right)  }{48}.
\end{align*}
More generally, I suspect that if $u\in\mathbb{N}$, then there is a polynomial
$q_{u}\left(  x\right)  $ of degree $u$ with rational coefficients such that
each $n\in\mathbb{N}$ satisfies%
\[
\sum_{k=0}^{n}\dbinom{-\left(  u+1\right)  }{k}=\dfrac{1}{2^{u+1}}+\left(
-1\right)  ^{n}\cdot q_{u}\left(  n\right)  .
\]

\begin{thebibliography}{99999999}                                                                                %

%Feel free to add your sources -- or copy some from the source code
%of the class notes ( http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.tex ).

%This is the bibliography: The list of papers/books/articles/blogs/...
%cited. The syntax is: "\bibitem[name]{tag}Reference",
%where "name" is the name that will appear in the compiled
%bibliography, and "tag" is the tag by which you will refer to
%the source in the TeX file. For example, the following source
%has name "GrKnPa94" (so you will see it referenced as
%"[GrKnPa94]" in the compiled PDF) and tag "GKP" (so you
%can cite it by writing "\cite{GKP}").

\bibitem[GrKnPa94]{GKP}Ronald L. Graham, Donald E. Knuth, Oren Patashnik,
\textit{Concrete Mathematics, Second Edition}, Addison-Wesley 1994.\newline
See \url{https://www-cs-faculty.stanford.edu/~knuth/gkp.html} for errata.

\bibitem[Grinbe19]{detnotes}Darij Grinberg, \textit{Notes on the combinatorial
fundamentals of algebra}, 10 January 2019. \newline%
\url{http://www.cip.ifi.lmu.de/~grinberg/primes2015/sols.pdf} \newline The
numbering of theorems and formulas in this link might shift when the project
gets updated; for a ``frozen'' version whose numbering is guaranteed to match
that in the citations above, see
\url{https://github.com/darijgr/detnotes/releases/tag/2019-01-10} .
\end{thebibliography}

\end{document}


\lab{Pandas 2: Plotting}{Pandas 2: Plotting}
\objective{Clear, insightful visualizations are a crucial part of data analysis.
To facilitate quick data visualization, pandas includes several tools that wrap around matplotlib.
These tools make it easy to compare different parts of a data set and explore the data as a whole.
}

\section*{Overview of Plotting Tools} % =======================================

The main tool for visualization in pandas is the \li{plot()} method of the \li{Series} and \li{DataFrame}.
The method has a keyword argument \li{kind} that specifies the type of plot to draw.
The valid options for \li{kind} are detailed below.

\begin{table}[H]
\begin{tabular}{r|c|l}
Plot Type & \li{plot()} ID & Uses and Advantages \\ \hline
Line plot & \li{"line"} & Show trends ordered in data; easy to compare multiple data sets \\
Scatter plot & \li{"scatter"} & Compare exactly two data sets, independent of ordering \\
Bar plot & \li{"bar"}, \li{"barh"} & Compare categorical or sequential data \\
Histogram & \li{"hist"} & Show frequencies of one set of values, independent of ordering \\
% Kernel Density Estimation & \li{"kde"} & Estimate a smooth distribution \\
Box plot & \li{"box"} & Display min, median, max, and quartiles; compare data distributions \\
Hexbin plot & \li{"hexbin"} & 2D histogram; reveal density of cluttered scatter plots \\
\end{tabular}
\caption{Uses for the \li{plot()} method of the pandas \li{Series} and \li{DataFrame}.
The plot ID is the value of the keyword argument \li{kind}.
That is, \li{df.plot(kind="scatter")} creates a scatter plot.
The default \li{kind} is \li{"line"}.}
\label{table:pandas-plot-options}
\end{table}

The \li{plot()} method calls \li{plt.plot()}, \li{plt.hist()}, \li{plt.scatter()}, or another matplotlib plotting function, but it also assigns axis labels, tick marks, legends, and a few other things based on the index and the data.
Most calls to \li{plot()} specify the \li{kind} of plot and which \li{Series} to use as the \li{x} and \li{y} axes.
By default, the \li{index} of the \li{Series} or \li{DataFrame} is used for the \li{x} axis.

% In Pandas, a \li{DataFrame} is an ordered collection of \li{Series}.
% A Series is similar to a dictionary, with values assigned to various labels, or indices.
% Each Series becomes a column in the data frame, with each row corresponding to an index.
% When several Series are combined into a single data frame, it becomes very easy to compare and visualize data.
% Each entry has an associated index and column.

\begin{lstlisting}
>>> import pandas as pd
>>> from matplotlib import pyplot as plt

>>> crime = pd.read_csv("crime_data.csv", index_col="Year")
>>> crime.plot(y="Population")  # Plot population against the index (years).
\end{lstlisting}

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{figures/population.pdf}
\end{figure}

In this case, the call to the \li{plot()} method is essentially equivalent to the following code.

\begin{lstlisting}
>>> plt.plot(crime.index, crime["Population"], label="Population")
>>> plt.xlabel(crime.index.name)
>>> plt.xlim(min(crime.index), max(crime.index))
>>> plt.legend(loc="best")
\end{lstlisting}

The \li{plot()} method also takes in many keyword arguments for matplotlib plotting and annotation functions.
For example, setting \li{legend=False} disables the legend, providing a value for \li{title} sets the figure title, \li{grid=True} turns a grid on, and so on.
For more customizations, see \url{https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html}.

\subsection*{Visualizing an Entire Data Set} % --------------------------------

A good way to start analyzing an unfamiliar data set is to visualize as much of the data as possible to determine which parts are most important or interesting.
For example, since the columns in a \li{DataFrame} share the same index, the columns can all be graphed together using the index as the $x$-axis.
In fact, the \li{plot()} method attempts by default to plot \textbf{every} \li{Series} (column) in the \li{DataFrame}.
This is especially useful with sequential data, like the crime data set.

The crime data set has 11 columns, so the resulting figure, Figure \ref{fig:crime-all}, is fairly cluttered.
However, it does shows that the \li{"Population"} column is on a completely different scale than the others.
Dropping a few columns gives a better overview of the data, shown in Figure \ref{fig:crime-all-v2}.

\begin{lstlisting}
# Plot all columns together against the index.
>>> crime.plot(linewidth=1)

# Plot all columns together except for 'Population' and 'Total'.
>>> crime.drop(["Population", "Total"], axis=1).plot(linewidth=1)
\end{lstlisting}

\begin{figure}[H] % Plot everything together.
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/all_crime.pdf}
    \caption{All columns of the crime data set on the same figure, using the index as the $x$-axis.}
    \label{fig:crime-all}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/all_crime2.pdf}
    \caption{All columns of the crime data set except \li{"Population"} and \li{"Total"}.}
    \label{fig:crime-all-v2}
\end{subfigure}
\caption{}
\end{figure}

\begin{warn}
The \li{"Population"} column differs from the other columns because it is has \textbf{different units of measure}: population is measured by ``number of people,'' but all other columns are measured in ``number of crimes.''
Be careful not to plot parts of a data set together if those parts don't have the same units or are otherwise incomparable.
\end{warn}

To quickly plot several columns in separate subplots, use \li{subplots=True} and specify a shape tuple as the \li{layout} for the plots.
Subplots automatically share the same $x$-axis; set \li{sharey=True} to force them to share the same $y$-axis as well.

\begin{lstlisting}
>>> crime.plot(y=["Property", "Larceny", "Burglary", "Violent"],
...            subplots=True, layout=(2,2), sharey=True,
...            style=['-', '--', '-.', ':'])
\end{lstlisting}

\begin{figure}[H]
    \includegraphics[width=.7\textwidth]{figures/line_subplots.pdf}
\end{figure}

The \li{plot()} method can generate subplots of any kind of plot.
However, since subplots share an $x$-axis by default, histograms turn out poorly whenever there are columns with very different data ranges.
For histograms, use the \li{hist()} method of the \li{DataFrame} instead of the \li{plot()} method.
Specify the number of bins with the \li{bins} parameter.

\begin{lstlisting}
>>> crime[["Violent","Murder","Robbery","Property"]].hist(grid=False, bins=20)
\end{lstlisting}

\begin{figure}[H]
    \includegraphics[width=.7\textwidth]{figures/hist_subplots.pdf}
\end{figure}

Finally, the function \li{pd.plotting.scatter_matrix()} produces a table of plots where each column is plotted against each other column in separate scatter plots.
The plots on the diagonal, instead of plotting a column against itself, displays a histogram of that column.
This provides a way to very quickly do an initial analysis of the correlation between different columns.

\begin{lstlisting}
>>> pd.plotting.scatter_matrix(crime[["Population", "Total", "Violent"]])
\end{lstlisting}

\begin{figure}[H]
    \includegraphics[width=.7\textwidth]{figures/scatter_table.pdf}
\end{figure}

\section*{Patterns and Correlations} % ========================================

After visualizing the entire data set initially, the next step is usually to closely compare related parts of the data.
For example, Figure \ref{fig:crime-all-v2} suggests that the \li{"Murder"} and \li{"Forcible Rape"} columns are roughly on the same scale.
Since this data is sequential (indexed by time), start by plotting these two columns against the index.
Next, create a scatter plot of one of the columns versus the other to investigate correlations that are independent of the index.
Unlike other types of plots, using \li{kind="scatter"} requires both an \li{x} and a \li{y} column as arguments.

\begin{lstlisting}
# Plot 'Murder' and 'Forcible Rape' as lines against the index.
>>> crime.plot(y=["Murder", "Forcible Rape"])

# Make a scatter plot of 'Murder' against 'Forcible Rape', ignoring the index.
>>> crime.plot(kind="scatter", x="Murder", y="Forcible Rape", alpha=.8, rot=30)
\end{lstlisting}

\begin{figure}[H] % Plot murder against rape.
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/line_compare.pdf}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/scatter_compare.pdf}
\end{subfigure}
\end{figure}

What does these graphs show about the data?
First of all, rape is more common than murder.
Second, rates of rape appear to be steadily increased from the mid 1960's to the mid 1990's before leveling out, while murder rates stay relatively constant.
The disparity between rape and murder is confirmed in the scatter plot: the clump of data points at about 15,000 murders and 90,000 rapes shows that there have been many years where rape was relatively high while murder was somewhat low.

\begin{warn}
While analyzing data, especially while searching for patterns and correlations, \textbf{always} ask yourself if the data makes sense and is trustworthy.
What lurking variables could have influenced the data measurements as they were being gathered?

The crime data set is somewhat suspect in this regard.
The murder rate is likely accurate, since murder is conspicuous and highly reported, but what about the rape rate?
Are the number of rapes increasing, or is the percentage of rapes being reported increasing? (It's probably both!)
Be careful about drawing conclusions for sensitive or questionable data.
\end{warn}

Figure \ref{fig:crime-all-v2} also reveals some general patterns relative to time.
For instance, there seems to be a small bump in each type of crime in the early 1980's.
Slicing the entries from 1980 to 1985 provides a closer look.
Since there are only a few entries, we can treat the data as categorical and make a bar chart.

\begin{lstlisting}
# Plot 'Property' and 'Larceny' rates from 1980 to 1985.
>>> crime.loc[1980:1985, ["Property", "Larceny", "Burglary"]].plot(kind="barh",
...                     title="Crime rates from 1980 to 1985")

# Plot the most recent year's crime rates for comparison.
>>> crime.iloc[-1][["Property", "Larceny", "Burglary"]].plot(kind="barh",
...                     title="Crime rates in 2016", color=["C0", "C1", "C2"])
>>> plt.tight_layout()
\end{lstlisting}

\begin{figure}[H] % Plot the early 1980s and the last year.
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/bar_multi.pdf}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/bar_single.pdf}
\end{subfigure}
\end{figure}

\begin{info} % Use horizontal bar charts.
As a general rule, horizontal bar charts (\li{kind="hbar"}) are better than the default vertical bar charts (\li{kind="bar"}) because most humans can detect horizontal differences more easily than vertical differences.
If the labels are too long to fit on a normal figure, use \li{plt.tight_layout()} to adjust the plot boundaries to fit the labels in.
\end{info}

\section*{Distributional Visualizations} % ====================================

Histograms are good for examining the distribution of a \textbf{single} column in a data set.
While pandas is capable of plotting several histograms on the same plot, the results are usually hard to read.

\begin{lstlisting}
# Plot three histograms together.
>>> crime.plot(kind="hist", y=["Violent", "Vehicle Theft", "Burglary"],
...            bins=20, alpha=.7, rot=30)

# Plot three histograms, stacking one on top of the other.
>>> crime.plot(kind="hist", y=["Violent", "Vehicle Theft", "Burglary"],
...            bins=20, stacked=True, rot=30)
\end{lstlisting}

\begin{figure}[H] % Try to compare histograms.
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/bad_hist_unstacked.pdf}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/bad_hist_stacked.pdf}
\end{subfigure}
\end{figure}

Instead of using histograms to compare distributions of data, use box plots.
A \emph{box plot} (sometimes called a ``cat-and-whisker'' plot) shows the five number summary: the minimum, first quartile, median, third quartile, and maximum of the data.
While not quite the same as a histogram, box plots are much better suited to quickly compare relatable distributions.

\begin{lstlisting}
# Compare the distributions of three columns.
>>> crime.plot(kind="box", y=["Violent", "Vehicle Theft", "Burglary"])

# Compare the distributions of all columns but 'Population' and 'Total'.
>>> crime.drop(["Population", "Total"], axis=1).plot(kind="box", vert=False)
\end{lstlisting}

\begin{figure}[H] % Use box plots to compare distributions.
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/box_small.pdf}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/box_large.pdf}
\end{subfigure}
\end{figure}

% TODO: KDE plot?

% TODO: hexbin plots

% Bar plots, on the other hand, are particularly useful for comparing several categories of data over time, or whenever there is a sense of progression in the index.
% Line plots are better suited to show a more continuous index (such as each year in a century), whereas bar plots are better for a discrete index (a few distinct years).
% Line and bar plots work well when there is a logical progression in the index, such as time.
% However, when frequency of occurrence is more important than the location of the data, histograms and box plots can be more informative.
% Use \li{plot(kind="hist")} to produce a histogram.
% Standard histogram options, such as the number of bins, are also accepted as keyword arguments.
% The \li{alpha} keyword argument makes each bin slightly transparent.

% Sometimes it is helpful to visualize a distribution of values using the box-and-whisker plot which displays the median, first and third quartiles, and outliers.

% Scatter plots are commonly used in a myriad of areas and have a simple implementation in pandas.
% Unlike other plotting commands, \li{scatter} needs both an \li{x} and a \li{y} column as arguments. % TODO: incorporate above!

% The scatter plot option includes many features which can be used to make the plots easier to understand.
% For example, we can change the size of the point based on another column.

\begin{comment} % This should be a pivot table example.
Consider the pydataset \li{HairEyeColor}, which contains the hair and eye color of various individuals.
A scatter plot of hair color vs eye color is relatively useless unless we can see the frequencies with which each combination occurs.
Including the keyword argument \li{s} allows us to control the size of each point.
This can be set to a fixed value or the value in another column.
In the example below, the size of each point is set to the frequency with which each observation occurs.

\begin{lstlisting}
>>> hec = data("HairEyeColor")
>>> X = np.unique(hec["Hair"], return_inverse=True)
>>> Y = np.unique(hec["Eye"], return_inverse=True)
>>> hec["Hair"] = X[1]
>>> hec["Eye"] = Y[1]
>>> hec.plot(kind="scatter", x="Hair", y="Eye", s=hec["Freq"]*10)
>>> plt.xticks([0,1,2,3], X[0])
>>> plt.yticks([0,1,2,3], Y[0])
\end{lstlisting}

\begin{figure}[H]
    \centering
    \includegraphics[width=.7\textwidth]{figures/hair_eye_scatter.pdf}
    \caption{Frequency of Hair-Eye Color Combinations}
\end{figure}
\end{comment}

\subsection*{Hexbin Plots} % --------------------------------------------------

A scatter plot is essentially a plot of samples from the joint distribution of two columns.
However, scatter plots can be uninformative for large data sets when the points in a scatter plot are closely clustered.
\emph{Hexbin plots} solve this problem by plotting point density in hexagonal bins---essentially creating a 2-dimensional histogram.

The file \texttt{sat\_act.csv} contains 700 self reported scores on the SAT Verbal, SAT Quantitative and ACT, collected as part of the Synthetic Aperture Personality Assessment (SAPA) web based personality assessment project.
The obvious question with this data set is ``how correlated are ACT and SAT scores?''
The scatter plot of ACT scores versus SAT Quantitative scores, Figure \ref{fig:pandas-act-scatter}, is highly cluttered, even though the points have some transparency.
A hexbin plot of the same data, Figure \ref{fig:pandas-act-hexbin}, reveals the \textbf{frequency} of points in binned regions.

\newpage

\begin{lstlisting}
>>> satact = pd.read_csv("sat_act.csv", index_col="ID")
>>> list(satact.columns)
<<['gender', 'education', 'age', 'ACT', 'SATV', 'SATQ']>>

# Plot the ACT scores against the SAT Quant scores in a regular scatter plot.
>>> satact.plot(kind="scatter", x="ACT", y="SATQ", alpha=.8)

# Plot the densities of the ACT vs. SATQ scores with a hexbin plot.
>>> satact.plot(kind="Hexbin", x="ACT", y="SATQ", gridsize=20)
\end{lstlisting}

\begin{figure}[H]
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/scores_scatter.pdf}
    \caption{ACT vs. SAT Quant scores.}
    \label{fig:pandas-act-scatter}
\end{subfigure}
%
\begin{subfigure}{.49\textwidth}
    \includegraphics[width=\textwidth]{figures/scores_hexbin.pdf}
    \caption{Frequency of ACT vs. SAT Quant scores.}
    \label{fig:pandas-act-hexbin}
\end{subfigure}
\caption{}
\end{figure}

Just as choosing a good number of \li{bins} is important for a good histogram, choosing a good \li{gridsize} is crucial for an informative hexbin plot.
A large \li{gridsize} creates many small bins and a small \li{gridsize} creates fewer, larger bins.

See \url{http://pandas.pydata.org/pandas-docs/stable/visualization.html} for more types of plots available in Pandas and further examples.

\section*{Principles of Good Data Visualization} % ============================

Visualization is much more than a set of pretty pictures scattered throughout a paper for the sole purpose of providing contrast to the text.
When properly implemented, data visualization is a powerful tool for analysis and communication.
When writing a paper or report, the author must make many decisions about how to use graphics effectively to convey useful information to the reader.
Here we will go over a simple process for making deliberate, effective, and efficient design decisions.

\subsection*{Attention to Detail} % -------------------------------------------

Consider the plot in Figure \ref{fig:nolabels}.
What does it depict?
We can tell from a simple glance that it is a scatter plot of positively correlated data of some kind, with \li{temp}--likely temperature--on the $x$ axis and \li{cons} on the $y$ axis.
However, the picture is not really communicating anything about the dataset.
We have not specified the units for the $x$ or the $y$ axis, we have no idea what \li{cons} is, there is no title, and we don't even know where the data came from in the first place.

\begin{figure}[H]
    \centering
    \includegraphics[width=.7\textwidth]{figures/ice_cream_bad.pdf}
    \caption{Non-specific data.}
    \label{fig:nolabels}
\end{figure}

\subsection*{Labels and Citations} % ------------------------------------------

In a homework or lab setting, we sometimes (mistakenly) think that it is acceptable to leave off appropriate labels, legends, titles, and sourcing.
In a published report or presentation, this kind of carelessness is confusing at best and, when the source is not included, even plagiaristic.
Clearly, we need to explain our data in a useful manner that includes all of the vital information.

Consider again Figure \ref{fig:nolabels}.
This figure comes from the \li{Icecream} dataset within the \li{pydataset} package, which we store here in a dataframe and then plot:
\begin{lstlisting}
>>> from pydataset import data
>>> icecream = data("Icecream")
>>> icecream.plot(kind="scatter", x="temp", y="cons")
\end{lstlisting}

We have at this point reproduced the rather substandard plot in Figure \ref{fig:nolabels}.
Using \li{data('Icecream', show\_doc=True)} we find the following information:
\begin{enumerate}
    \item The dataset details ice cream consumption via four-weekly observations from March 1951 to July 1953 in the United States.
    \item \li{cons} corresponds to ``consumption of ice cream per head'' and is measured in pints.
    \item \li{temp} corresponds to temperature, degrees Fahrenheit.
    \item The listed source is: ``Hildreth, C. and J. Lu (1960) \emph{Demand relations with autocorrelated disturbances}, Technical Bulletin No 2765, Michigan State University.''
\end{enumerate}

We add these important details using the following code.
As we have seen in previous examples, pandas automatically generates legends when appropriate.
However, although pandas also automatically labels the $x$ and $y$ axes, our data frame column titles may be insufficient.
Appropriate titles for the $x$ and $y$ axes must also list appropriate units.
For example, the $y$ axis should specify that the consumption is in units of \emph{pints per head}, in place of the ambiguous label \li{cons}.

\begin{lstlisting}
>>> icecream = data("Icecream")
# Set title via the title keyword argument
>>> icecream.plot(kind="scatter", x="temp", y="cons", title="Ice Cream Consumption in the U.S., 1951-1953",)
# Override pandas automatic labelling using xlabel and ylabel
>>> plt.xlabel("Temp (Farenheit)")
>>> plt.ylabel("Consumption per head (pints)")
\end{lstlisting}

To arbitrarily add the necessary text to the figure, use either \li{plt.annotate()} or \li{plt.text()}.
Alternatively, add text immediately below wherever the figure is displayed.

\begin{lstlisting}
>>> plt.text(20, .1, r"Source: Hildreth, C. and J. Lu (1960) \emph{Demand"
...     "relations with autocorrelated disturbances}\nTechnical Bulletin No"
...     "2765, Michigan State University.", fontsize=7)
\end{lstlisting}

Both of these methods are imperfect, however, and can normally be just as easily replaced by a caption attached to the figure in your presentation or document setting.
We again reiterate how important it is that you source any data you use.
Failing to do so is plagiarism.

Finally, we have a clear and demonstrative graphic in Figure \ref{fig:labels}.

\begin{figure}[H]
    \centering
    \includegraphics[width=.7\textwidth]{figures/ice_cream_good.pdf}
    \caption{Source:  Hildreth, C. and J. Lu (1960) \emph{Demand relations with autocorrelated disturbances}, Technical Bulletin No 2765, Michigan State University.}
    \label{fig:labels}
\end{figure}

\begin{problem}
The \li{pydataset} module\footnote{Run \texttt{pip install pydataset} if needed.} contains numerous data sets, each stored as a pandas \li{DataFrame}.

\begin{lstlisting}
>>> from pydataset import data

# Call data() to see the entire list of data sets.
# To load a particular data set, enter its ID as an argument to data().
>>> titanic = data("Titanic")
# To see the information about a data set, call data() with show_doc=True.
>>> data("Titanic", show_doc=True)
<<Titanic

PyDataset Documentation (adopted from R Documentation. The displayed
examples are in R)

## Survival of passengers on the Titanic>>
\end{lstlisting}

Visualize and describe at least 5 of the following data sets with 2 or 3 figures each.
Comment on the implications and significance of each visualization and give a comprehensive summary of the data set.

\begin{itemize}
\item \li{"Arbuthnot"}: Ratios of male to female births in London from 1629-1710
\item \li{"trees"}: Girth, height and volume for black cherry trees
\item \li{"road"}: Road accident deaths in the United States
\item \li{"birthdeathrates"}: Birth and death rates by country
\item \li{"bfeed"}: Child breast feeding records
\item \li{"heart"}: Survival of patients on the waiting list for the Stanford heart transplant program
\item \li{"lung"}: Survival in patients with advanced lung cancer from the North Central Cancer Treatment group
\item \li{"birthwt"}: Risk factors associated with low infant birth weight
\item A data set of your choice
\end{itemize}
Include each of the following in each visualization.
\begin{itemize}
\item A clear title, with relevant information for the period or region the data was collected in.
\item Axis labels that specify units.
\item A legend (if appropriate).
\item The source.
You may include the source information in your plot or print it after the plot.
\end{itemize}
\end{problem}