\subsection{Prime-Time Ratio} \label{subsec:primetime}

ISPs design networks to handle peak demand, which is usually observed
during prime-time hours, when subscribers heavily consume real-time
entertainment traffic, such as video.  The FCC defines prime-time as the
local time from 7:00--11:00 p.m.~\cite{fcc2014measuring-broadband}. To
measure the concentration of network usage during prime-time, we use
Sandvine's definition of the \emph{prime-time ratio}: the ratio of the
average (hourly) traffic demand during prime-time hours to the average
demand in non-prime-time hours~\cite{sandvine20141h, sandvine20142h}.
We measured the prime-time ratio of the subscribers in the control and
treatment groups considering each contiguous four-hour period in each
day. Our experiment shows that, in fact, the evening hours with the
largest prime-time ratio are 8:00~p.m.--12:00~a.m., so we use this time
interval for our definition of prime time.

%\begin{figure}[t]
%\begin{minipage}{1\linewidth}
%\centering
%\includegraphics[width=1\linewidth]{figures/prime-time-ratio-per-device-cdf-MEAN.pdf}
%\caption{Prime-Time Ratio\label{fig:cdf-prime-time-ratio}}
%\end{minipage}
%\end{figure}

%\begin{subfigure}[]{.32\linewidth}
%\includegraphics[width=1\linewidth]{figures/primetime_usage_per_day_per_subs.pdf}
%\caption{\label{fig:pt}}
%\end{subfigure}

%\begin{subfigure}[b]{.32\linewidth}
%\includegraphics[width=1\linewidth]{figures/nonprimetime_usage_per_day_per_subs.pdf}
%\caption{\label{fig:non-pt}}
%\end{subfigure}

\begin{table}[t]
\centering
\begin{tabular}{cc|c|c|c|}
\cline{3-5}
                                               &           & \begin{tabular}[c]{@{}c@{}}Hourly traffic\\ in PT\end{tabular} & \begin{tabular}[c]{@{}c@{}}Hourly traffic\\ in Non-PT\end{tabular} & \begin{tabular}[c]{@{}c@{}}Prime-time\\ Ratio\end{tabular} \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{Weekday}} & treatment & 233.12                                                         & 124.18                                                             & 1.88                                                       \\
\multicolumn{1}{|c|}{}                         & control   & 225.40                                                         & 104.30                                                             & 2.16                                                       \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{Weekend}} & treatment & 246.93                                                         & 143.08                                                             & 1.73                                                       \\
\multicolumn{1}{|c|}{}                         & control   & 238.15                                                         & 133.16                                                             & 1.79                                                       \\ \hline
\end{tabular}
\caption{\green{Hourly traffic demand during (and outside) prime-time hours per subscriber (MB). Prime-time traffic demand is defined as the average traffic demand during a prime-time hour.}\label{tab:prime-time-demand}}
\end{table}

Table~\ref{tab:data-stats} shows that the average hourly prime-time downstream traffic per
1,000 subscribers is
209.5 GB for the treatment group, compared to 205.1 GB for the control
group, \green{about a 2\% difference.}
In contrast, during an average hour
{\em outside} of prime time, the traffic per 1,000 subscribers is 122.3
GB for the treatment group, compared to
108.5 GB for the control group, amounting to \green{about a 12\% difference.} This
more significant difference in demand during hours outside of the
daily prime-time is also apparent from the weekly usage patterns in
Figure~\ref{fig:traffic-demand-timeseries}. \sgfoot{ \textbf{Interesting reviewer comment we can add here}: Throughout the paper, I was wondering the extent to which congestion was affecting the results.  It's well-known that Comcast and other ISPs over-subscribe their network, relying on statistical multiplexing.  Thus, during peak hours, it may be the case that both control, and treatment groups are receiving the same level of service.  In this case, the results the low level of increase during prime time (2\%) relative to the higher level of increase outside of prime time (12\%) might be explained.  I don't know if the authors can tell the extent to which congestion is a factor given the data, would be in would be an interesting discussion nevertheless.}

We also calculated the prime-time ratio per day over weekends and
weekdays, as shown in Table \ref{tab:prime-time-demand}.  On weekends,
the prime-time ratios for the treatment and control groups are 1.73 and
1.79 respectively. On the weekdays, the prime-time ratio for the control
group is 2.16 compared to 1.88 for the treatment group.
In terms of absolute demand, the prime-time demand
on weekdays in the treatment group is within 4\%
of that in the control group. In contrast, the demand in
{\em non-prime-time hours} is 19\% higher for the treatment group on weekdays,
and only 7.5\% higher on weekends. \red{The large difference in non-prime-time
demand of the control and treatment groups on weekdays suggests
that many of the users in the treatment group may in fact be subscribers
who work from home, and thus have a higher demand during non-prime-time hours
and weekdays on the higher service tier.}\origfoot{The increased non-prime-time demand in the
control group suggests that many of the users in both the control and
treatment groups (\ie, those subscribers who are already on high service
tiers) may in fact be subscribers who work from home and thus increase
their demand more during non-prime-time and weekdays as a result of the
service tier upgrade.}

While 6\% of the subscribers in both groups had a prime-time ratio over
100, we also observed that 9\% of the control group and 14\% of the
treatment group had prime-time ratios {\em less than 1}, indicating that
these users actually had higher demand during the day than they did
during prime time. Similarly, these users may be small home
businesses or subscribers who work at home.

%\if 0
%For our dataset, the prime-time ratio for the treatment and control groups
%were 1.70 and 1.93 respectively. By definition, the prime-time ratio is measured using
%total traffic volume in a day. However, not all subscribers contribute equally
%to the traffic volume. We observed that the median \emph{prime-time ratio per subscriber}
%is 3.39 for the \treatment{} group and 2.91 for the \control{} group.
%The prime time ratio of the higher tier is more than the lower tier when calculated per
%subscriber, but by volume it is the inverse.
%The overall demand of subscribers in \treatment{} has increased substantially
%on a per-subscriber basis, although the total volume does not show the same increase.
%This result indicates that individual subscribers that do not contribute substantially
%to the traffic volume are the ones who have higher usage in prime-time as compared to their
%lower tier counterparts.
%\fi


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

\title{Problem Set 8}
\author{Mason Ross Hayes}
\date{23 March 2020}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{hyperref}

\begin{document}

\maketitle

\section{Results}

All relevant questions in PS8 should be answered implicitly by the following table:

\begin{table}[!htbp] \centering
  \caption{Coefficients using Different Optimization Methods}
  \label{}
\begin{tabular}{@{\extracolsep{0pt}} ccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
 & True\_Betas & LM & OLS\_ClosedForm & LBFGS\_Algo & NelderMead\_Algo & GradientDescent \\
\hline \\[-1.8ex]
X1 & $1.500$ & $1.501$ & $1.501$ & $1.501$ & $1.501$ & $1.501$ \\
X2 & $$-$1$ & $$-$0.991$ & $$-$0.991$ & $$-$0.996$ & $$-$0.996$ & $$-$0.991$ \\
X3 & $$-$0.250$ & $$-$0.247$ & $$-$0.247$ & $$-$0.256$ & $$-$0.256$ & $$-$0.247$ \\
X4 & $0.750$ & $0.744$ & $0.744$ & $0.745$ & $0.745$ & $0.744$ \\
X5 & $3.500$ & $3.504$ & $3.504$ & $3.507$ & $3.507$ & $3.504$ \\
X6 & $$-$2$ & $$-$1.999$ & $$-$1.999$ & $$-$1.999$ & $$-$1.999$ & $$-$1.999$ \\
X7 & $0.500$ & $0.502$ & $0.502$ & $0.497$ & $0.497$ & $0.502$ \\
X8 & $1$ & $0.997$ & $0.997$ & $1.002$ & $1.002$ & $0.997$ \\
X9 & $1.250$ & $1.256$ & $1.256$ & $1.246$ & $1.246$ & $1.256$ \\
X10 & $2$ & $1.999$ & $1.999$ & $1.996$ & $1.996$ & $1.999$ \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}

\begin{table}[!htbp] \centering
  \caption{LM Output}
  \label{}
\begin{tabular}{@{\extracolsep{5pt}}lc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & Y \\
\hline \\[-1.8ex]
 X1 & 1.501$^{***}$ \\
  & (0.002) \\
  & \\
 X2 & $-$0.991$^{***}$ \\
  & (0.003) \\
  & \\
 X3 & $-$0.247$^{***}$ \\
  & (0.003) \\
  & \\
 X4 & 0.744$^{***}$ \\
  & (0.003) \\
  & \\
 X5 & 3.504$^{***}$ \\
  & (0.003) \\
  & \\
 X6 & $-$1.999$^{***}$ \\
  & (0.003) \\
  & \\
 X7 & 0.502$^{***}$ \\
  & (0.003) \\
  & \\
 X8 & 0.997$^{***}$ \\
  & (0.003) \\
  & \\
 X9 & 1.256$^{***}$ \\
  & (0.003) \\
  & \\
 X10 & 1.999$^{***}$ \\
  & (0.003) \\
  & \\
\hline \\[-1.8ex]
Observations & 100,000 \\
R$^{2}$ & 0.971 \\
Adjusted R$^{2}$ & 0.971 \\
Residual Std. Error & 0.500 (df = 99990) \\
F Statistic & 338,240.000$^{***}$ (df = 10; 99990) \\
\hline
\hline \\[-1.8ex]
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

\end{document}


% Some basic packages
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{tikz}
\usepackage{textcomp}
\usepackage{url}
\usepackage{graphicx}
\usepackage{float}
\usepackage{booktabs}
\usepackage{enumitem}
\usepackage{arydshln}
\delimitershortfall=0pt
\setlength{\dashlinegap}{2pt}
\usepackage{enumerate}
\usepackage{amsmath,amssymb,fancyhdr,array}

\pagestyle{fancy}

\usepackage{xintfrac, xintbinhex, xinttools}

\rhead{1608/1609 WS18/19}

\makeatletter
\def\DtoB@get@ND #1/#2[#3]{% #2 must be = 1 here, if input was in
                         % decimal notation
    \edef\DtoB@s{\xintiiSgn{#1}}%
    \edef\DtoB@A{\xintiiAbs{#1}}%
    \def\DtoB@L{#3}%
    \ifnum#3<\z@
       \let\DtoB@N\DtoB@A
       \edef\DtoB@D{\xintiiE{1}{-#3}}%
    \else
       \edef\DtoB@N{\xintiiE{\DtoB@A}{#3}}%
       \def\DtoB@D{1}%
    \fi
}%
\newcommand\ParseFromDecimalToIEEEBinary[1]{%
   % assume #1 is a decimal number (I will write code for fractions
   % another day)
   \expandafter\DtoB@get@ND\romannumeral0\xintrez{#1}%
   % we should here handle \DtoB@s = 0 but no time tonight, assume input non-zero
   \edef\DtoB@S@bit{\the\numexpr(1-\DtoB@s)/2}% 1 if number < 0, 0 if number > 0
   \edef\DtoB@U{\xintDecToBin{\DtoB@N}}%
   \edef\DtoB@V{\xintDecToBin{\DtoB@D}}%
   \edef\DtoB@Uk{\the\numexpr\expandafter\xintLength\expandafter{\DtoB@U}-\@ne}%
   \edef\DtoB@Vl{\the\numexpr\expandafter\xintLength\expandafter{\DtoB@V}-\@ne}%
   % next step should perhaps compare k and l first
   % important that we are comparing here two strings of 1s and 0s of
   % exact same length
   \ifnum\pdfstrcmp{\DtoB@U\romannumeral\xintreplicate{\DtoB@Vl}{0}}
                   {\DtoB@V\romannumeral\xintreplicate{\DtoB@Uk}{0}}=\m@ne
       \edef\DtoB@E{\the\numexpr\DtoB@Uk-\DtoB@Vl-\@ne}%
   \else
       \edef\DtoB@E{\the\numexpr\DtoB@Uk-\DtoB@Vl}%
   \fi
   \edef\DtoB@Eshifted@bits{\expandafter\@gobble
          \romannumeral0\xintdectobin{\the\numexpr \DtoB@E + 127 + 256\relax}}%
   \ifnum\DtoB@E>23
     \edef\DtoB@f@frac{\DtoB@A/\xintiiPow{2}{\DtoB@E-23}[\DtoB@L]}%
     %  use rather bintodec conversion of 10000...000 in above?
   \else
     \edef\DtoB@f@frac{\xintiiMul{\DtoB@A}{\xintiiPow{2}{23-\DtoB@E}}/1[\DtoB@L]}%
   \fi
   \edef\DtoB@f@int{\xintNum{\DtoB@f@frac}}% truncates to an int
   \edef\DtoB@M@bits{\expandafter\@gobble
                     \romannumeral0\xintdectobin{\DtoB@f@int}}%
   %\edef\DtoBresult{\DtoB@S@bit\DtoB@Eshifted@bits\DtoB@M@bits}%
   \let\IEEEsign\DtoB@S@bit
   \let\IEEEexponent\DtoB@Eshifted@bits
   \let\IEEEmantissa\DtoB@M@bits
}%

\makeatother

\newcommand\ieee[1]{%
\ParseFromDecimalToIEEEBinary{#1}%
\begin{center}
{\footnotesize\setlength{\tabcolsep}{1pt}\begin{tabular}[t]{|*{32}{w{c}{1em}|}}
\firsthline
\xintListWithSep{&}{\xintSeq[-1]{31}{0}}\\
\hline
S \romannumeral\xintreplicate{8}{&E}\romannumeral\xintreplicate{23}{&M}\\
\hline
\IEEEsign & \xintListWithSep{&}{\IEEEexponent} & \xintListWithSep{&}{\IEEEmantissa}\\
\hline
\end{tabular}\par}%
\end{center}
}

% Don't indent paragraphs, leave some space between them
\usepackage{parskip}

\usepackage{subcaption}
\usepackage{multicol}
\usepackage{xcolor}

% Other font I sometimes use.
% \usepackage{cmbright}

% Math stuff
\usepackage{amsmath, amsfonts, mathtools, amsthm, amssymb, braket}
% Fancy script capitals
\usepackage{mathrsfs}
\usepackage{cancel}

% Bold math
\usepackage{bm}

%Make implies and impliedby shorter
\let\implies\Rightarrow
\let\impliedby\Leftarrow
\let\iff\Leftrightarrow
\let\epsilon\varepsilon

% Add \contra symbol to denote contradiction
\usepackage{stmaryrd} % for \lightning
\newcommand\contra{\scalebox{1.5}{$\lightning$}}

% \let\phi\varphi

% Command for short corrections
% Usage: 1+1=\correct{3}{2}

\definecolor{correct}{HTML}{009900}
\newcommand\correct[2]{\ensuremath{\:}{\color{red}{#1}}\ensuremath{\to }{\color{correct}{#2}}\ensuremath{\:}}
\newcommand\green[1]{{\color{correct}{#1}}}

% horizontal rule
\newcommand\hr{
    \noindent\rule[0.5ex]{\linewidth}{0.5pt}
}

% hide parts
\newcommand\hide[1]{}

% Environments
\makeatother
% For box around Definition, Theorem, \ldots
\usepackage{mdframed}
\mdfsetup{skipabove=1em,skipbelow=0em}
\theoremstyle{definition}
\newtheorem*{definition}{Definition}
\newtheorem*{recall}{Recall}
\newtheorem*{property}{Property}
\newtheorem*{consequence}{Consequence}
\newtheorem*{lemma}{Lemma}
\newtheorem*{proposition}{Proposition}
\newtheorem*{theorem}{Theorem}
\newmdtheoremenv[nobreak=true]{law}{Law}
\newmdtheoremenv[nobreak=true]{corollary}{Corollary}
\newmdtheoremenv{conclusion}{Conclusion}
\newmdtheoremenv{assumption}{Assumption}
\newtheorem*{intermezzo}{Intermezzo}
\newtheorem*{observation}{Observation}
\newtheorem*{exercise}{Exercise}
\newtheorem*{remark}{Remark}
\newtheorem*{problem}{Problem}
\newtheorem*{terminology}{Terminology}
\newtheorem*{application}{Application}
\newtheorem*{question}{Question}
\newtheorem*{example}{Example}
\newtheorem*{notation}{Notation}
\newtheorem*{previouslyseen}{As previously seen}

\newcommand{\matr}[1]{\mathbf{#1}} % undergraduate algebra version
\newcommand{\matri}[1]{$\mathbf{#1}$} % undergraduate algebra version
\renewcommand{\mod}[1]{\left|\matr{#1}\right|}
\newcommand\p[2]{\ensuremath\frac{\partial #1}{\partial #2}}
\newcommand{\rank}[1]{\text{rank}\left(\mathbf{#1}\right)}
\newcommand{\inverse}{^{-1}}
\newcommand{\augmented}[2]{\begin{array}{c:c}\mathbf{#1} & \mathbf{#2}\end{array}}
\newcommand{\rowequiv}{\mathrel{\underset{\sim}{R}}}
\newtheorem*{note}{Note}
\newmdtheoremenv[nobreak=true]{prop}{Proposition}
% End example and intermezzo environments with a small diamond (just like proof
% environments end with a small square)
\usepackage{etoolbox}
\AtEndEnvironment{vb}{\null\hfill$\diamond$}%
\AtEndEnvironment{intermezzo}{\null\hfill$\diamond$}%
% \AtEndEnvironment{opmerking}{\null\hfill$\diamond$}%

% Fix some spacing
% http://tex.stackexchange.com/questions/22119/how-can-i-change-the-spacing-before-theorems-with-amsthm
\makeatletter
\def\thm@space@setup{%
  \thm@preskip=\parskip \thm@postskip=0pt
}

  \renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
    \hskip -\arraycolsep
    \let\@ifnextchar\new@ifnextchar
  \array{#1}}

% Exercise
% Usage:
% \oefening{5}
% \suboefening{1}
% \suboefening{2}
% \suboefening{3}
% gives
% Oefening 5
%   Oefening 5.1
%   Oefening 5.2
%   Oefening 5.3
\newcommand{\oefening}[1]{%
    \def\@oefening{#1}%
    \subsection*{Oefening #1}
}

\newcommand{\suboefening}[1]{%
    \subsubsection*{Oefening \@oefening.#1}
}

\newcommand{\prob}{\mathbb P}

% \lecture starts a new lecture (les in dutch)
%
% Usage:
% \lecture{1}{di 12 feb 2019 16:00}{Inleiding}
%
% This adds a section heading with the number / title of the lecture and a
% margin paragraph with the date.

% I use \dateparts here to hide the year (2019). This way, I can easily parse
% the date of each lecture unambiguously while still having a human-friendly
% short format printed to the pdf.

\usepackage{xifthen}
\def\testdateparts#1{\dateparts#1\relax}
\def\dateparts#1 #2 #3 #4 #5\relax{
    \marginpar{\small\textsf{\mbox{#1 #2 #3 #5}}}
}

\def\@lecture{}%
\newcommand{\lecture}[3]{
    \ifthenelse{\isempty{#3}}{%
        \def\@lecture{Lecture #1}%
    }{%
        \def\@lecture{Lecture #1: #3}%
    }%
    \subsection*{\@lecture}
	%No date
  % \marginpar{\small\textsf{\mbox{#2}}}

}

% These are the fancy headers

% LE: left even
% RO: right odd
% CE, CO: center even, center odd
% My name for when I print my lecture notes to use for an open book exam.
% \fancyhead[LE,RO]{Gilles Castel}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

\fancyhead[RO,LE]{Giorgio Grigolo}
% Right odd,  Left even
\fancyfoot[LE, RO]{\thepage}
% Todonotes and inline notes in fancy boxes
\usepackage{todonotes}
\usepackage{tcolorbox}

% Make boxes breakable
\tcbuselibrary{breakable}

% Figure support as explained in my blog post.
\usepackage{import}
\usepackage{xifthen}
\usepackage{pdfpages}
\usepackage{transparent}
\newcommand{\incfig}[1]{%
    \def\svgwidth{\columnwidth}
    \import{./figures/}{#1.pdf_tex}
}
 %http://tex.stackexchange.com/questions/76273/multiple-pdfs-with-page-group-included-in-a-single-page-warning
\pdfsuppresswarningpagegroup=1

% My name
\author{Giorgio Grigolo}


\section{Tables}

There is a rich set of commands and options that support sophisticated tables. For most tables, however, a relatively simple environment suffices:

\begin{verbatim}
   \begin{tabular}{<cols>}
     [\hline]
     <text> & <text> & <text> \\
     [\hline]
     ...
   \end{tabular}
\end{verbatim}

The \verb2<cols>2 block is comprised of the letters l, c, and r, which stand for left, center, or right justification of a column. Each column of the table must have its justification defined by one of these letters. Additionally, the ``\verb2|2'' character may appear in the \verb2<cols>2 block each time a vertical line is desired to set off a column in the table. The \verb2<text>2 blocks are where cell text appears. Each ``\&'' represents a column delimiter. The \verb2\\2 characters delimit rows. The \verb2\hline2 command can be used to insert horizontal lines that form row boundaries.

The \verb2\cline{<m>-<n>}2 command may be used instead of \verb2\hline2 to insert a horizontal line that spans columns \q m\q\ through \q n\q. The \verb2\vline2 command draws a vertical line within a row at the location the command appears.

Here is an example that uses all the basic table constructs:

\begin{center}
  \begin{tabular}{l||c|r|} \hline
    leftward & center                & rightward\\
    \cline{2-3}
    left     & center \vline\ center & right\\
    \cline{2-3}
    left     & center                & right\\
    \hline
  \end{tabular}
\end{center}

Commonly, tables are made to float so whitespace usage can be optimized. (The same is true of figures. Collectively, tables and figures are often referred to as {\itshape floats}.) This allows tables to appear on other pages than where they appear to be defined on in the source code.

This uses the table environment:

\begin{alltt}
\ind\verb2\begin{table}2\q[\q[h][t][b][p]\q]\q
\ind\verb2  <header_text>\\2\q[\q<spacing>\q]\q
\ind\verb2  \begin{tabular}{<cols>}2
\ind  ...
\ind\verb2  \end{tabular}\\2\q[\q<spacing>\q]\q
\ind  <footer_text>
\ind\verb2\end{table}2
\end{alltt}

There are many options that can be used to configure floats, and most of them are rather specialized.

The `h t b p' options control preferences for where the float should appear. These are short for here, top, bottom, and page. The \q h\q\ option means prefer the current location. The \q t\q\ and \q b\q\ options mean prefer the top and bottom of the current page, respectively. The \q p\q\ option means prefer a special page dedicated to floats. The default is \verb2[tbp]2.

A numbered caption can be included using the following command:

\begin{alltt}
\ind\verb2\caption2\q[\q\verb2<short_title>2\q]\q\verb2{[\label{<target>}]<caption_text>}2
\end{alltt}

The \verb2<short_title>2 does not appear in the caption; it appears as the table title in a generated list of tables. The \verb2<caption_text>2 is the main caption text and may be rather long. It functions as the short title if \verb2<short_title>2 is omitted. The optional, but useful, \verb2\label2 command has a \verb2<target>2 string that can be used in \verb2\ref2 and \verb2\pageref2 commands to cross-reference the float's index number and page, respectively.

Caption text is center justified if it is on one line. Otherwise, it is set as a normal paragraph. The width can be controlled by placing the caption command in a \verb2\parbox2 or \verb2\minipage2 command.

The caption appears before the table if the command is placed at the start of the float environment. It is placed after the table if it is placed at the end of the float environment.

The float index number for captions is incremented every time a float is used, not when a caption is used. This means caption numbers may not be sequential if you don't consistently use captions. Interestingly, this behavior holds if you show a float environment as verbatim text (that is, no intent to show an actual table, but simply to show the syntax). To overcome this problem, you can use this command:

\ind\verb2\addtocounter{table}{<number>}2

where the number may be any integer. The number is added to the current index value to give the desired index value.

The floating table code appears in the source code here, but the table in a generated file appears either at the top or bottom of the page.

\addtocounter{table}{-1}

  \begin{table}
    \caption{Leading caption.}
    \begin{center}
      {\hspace*{-1.5in}\sffamily Floating Table Header} \\[1ex]
      \begin{tabular}{l||c|r|} \hline
        leftward & center                & rightward\\
        \cline{2-3}
        left     & center \vline\ center & right\\
        \cline{2-3}
        left     & center                & right\\
        \hline
      \end{tabular}\\[0.5ex]
      {\hspace*{-2.25in} \sffamily Table Footer}
    \end{center}
  \end{table}


\section*{Exercises}

\begin{ex}
  Balance the following chemical reactions.

  \begin{enumerate}
  \item $KNO_3+H_2CO_3\rightarrow K_2CO_3+HNO_3$

  \item $AgI+Na_2S\rightarrow Ag_2S+NaI$

  \item $Ba_3N_2+H_2O\rightarrow Ba(OH)_2+NH_3$

  \item $CaCl_2+Na_3PO_4\rightarrow Ca_3(PO_4)_2+NaCl$
  \end{enumerate}
  % \begin{sol}
  % \end{sol}
\end{ex}


\documentclass{article}

\title{Assignment 1 \\ STATS 101}

\begin{document}

\maketitle

Please complete the following problems.
\newline

Remember:
\begin{itemize}
\item do not rename external files or edit them in any way.
\item do not use global paths in your script. Instead, use \verb|setwd()| interactively in the console, but do not include this code in your submission.
\item do not destroy or overwrite any variables in your program.
\end{itemize}

It might help to clear all the objects from your workspace, and then \verb|source| your file before you submit it. This will often uncover bugs.

\section{}
Make a vector called \verb|myVector| and have it store the integers $1,2,3$

\section{}
Make a string called \verb|myString| and have it store your name.

\section{}
Read in the data set called \verb|data.csv| and store the data frame as \verb|myDataFrame|.

\end{document}


\section{C Memory Model}

The C memory model is probably unlike most that you've seen before. Instead of allocating an object with type safety, we either use an automatic variable or request a sequence of bytes with \keyword{malloc} or another family member and later we \keyword{free} it.

\subsection{Structs}

In low-level terms, a struct is a piece of contiguous memory, nothing more.
Just like an array, a struct has enough space to keep all of its members.
But unlike an array, it can store different types. Consider the contact struct declared above.

\begin{lstlisting}[language=C]
struct contact {
  char firstname[20];
  char lastname[20];
  unsigned int phone;
};

struct contact person;
\end{lstlisting}

We will often use the following typedef, so we can write use the struct name as the full type.

\begin{lstlisting}[language=C]
typedef struct contact contact;
contact person;

typedef struct optional_name {
  ...
} contact;
\end{lstlisting}

If you compile the code without any optimizations and reordering, you can expect the addresses of each of the variables to look like this.

\begin{lstlisting}[language=C]
&person           // 0x100
&person.firstname // 0x100 = 0x100+0x00
&person.lastname  // 0x114 = 0x100+0x14
&person.phone     // 0x128 = 0x100+0x28
\end{lstlisting}

All your compiler does is say "reserve this much space".
Whenever a read or write occurs in the code, the compiler will calculate the offsets of the variable.
The offsets are where the variable starts at.
The phone variables starts at the \keyword{0x128}th bytes and continues for sizeof(int) bytes with this compiler.
\textbf{Offsets don't determine where the variable ends though}.
Consider the following hack seen in a lot of kernel code.

\begin{lstlisting}[language=C]

typedef struct {
  int length;
  char c_str[0];
} string;

const char* to_convert = "person";
int length = strlen(to_convert);

// Let's convert to a c string
string* person;
person = malloc(sizeof(string) + length+1);
\end{lstlisting}

Currently, our memory looks like the following image.
There is nothing in those boxes

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{introc/drawings/memory_model_empty.eps}
\caption{Struct pointing to 11 empty boxes}
\end{figure}

So what happens when we assign length?
The first four boxes are filled with the value of the variable at length.
The rest of the space is left untouched.
We will assume that our machine is big endian.
This means that the least significant byte is last.

\begin{lstlisting}[language=C]
person->length = length;
\end{lstlisting}

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{introc/drawings/memory_model_length.eps}
\caption{Struct pointing to 11 boxes, 4 filled with 0006, 7 junk}
\end{figure}

Now, we can write a string to the end of our struct with the following call.

\begin{minted}{C}
strcpy(person->c_str, to_convert);
\end{minted}

\begin{figure}[H]
\centering
\includegraphics[width=.7\textwidth]{introc/drawings/memory_model_full.eps}
\caption{Struct pointing to 11 boxes, 4 filled with 0006, 7 the stirng ``person''}
\end{figure}

We can even do a sanity check to make sure that the strings are equal.

\begin{minted}{C}
strcmp(person->c_str, "person") == 0 //The strings are equal!
\end{minted}

What that zero length array does is point to the \textbf{end of the struct} this means that the compiler will leave room for all of the elements calculated with respect to their size on the operating system (ints, chars, etc).
The zero length array will take up no bytes of space.
Since structs are continuous pieces of memory, we can allocate \textbf{more} space than required and use the extra space as a place to store extra bytes.
Although this seems like a parlor trick, it is an important optimization because to have a variable length string any other way, one would need to have two different memory allocation calls.
This is highly inefficient for doing something as common in programming as is string manipulation.

\subsection{Strings in C}

In C, we have
\href{https://en.wikipedia.org/wiki/Null-terminated_string}{Null
	Terminated} strings rather than
\href{https://en.wikipedia.org/wiki/String_(computer_science)\#Length-prefixed}{Length
	Prefixed} for historical reasons.
For everyday programmers, remember to NUL terminate your string!
A string in C is defined as a bunch of bytes ended by `\0' or the NUL Byte.

\subsection{Places for strings}

Whenever you define a string literal - one in the form \keyword{char*\ str\ =\ "constant"} -- that string is stored in the \emph{data} section. Depending on your architecture, it is \textbf{read-only}, meaning that any attempt to modify the string will cause a SEGFAULT.
One can also declare strings to be either in the writable data segment or the stack. To do so, specify a length for the string or put brackets instead of a pointer \keyword{char str[] = "mutable"} and put in the global scope or the function scope for the data segment or the stack respectively.
If one, however, \keyword{malloc}'s space, one can change that string to be whatever they want.
Forgetting to NUL terminate a string has a big effect on the strings! Bounds checking is important.
The heartbleed bug mentioned earlier in the book is partially because of this.

Strings in C are represented as characters in memory.
The end of the string includes a NUL (0) byte.
So "ABC" requires four(4) bytes.
The only way to find out the length of a C string is to keep reading memory until you find the NUL byte.
C characters are always exactly one byte each.

\subsubsection{String literals are constant}

A string literal is naturally constant.
Any write will cause the operating system to produce a SEGFAULT.

\begin{lstlisting}[language=C]
char array[] = "Hi!"; // array contains a mutable copy
strcpy(array, "OK");

char *ptr = "Can't change me"; // ptr points to some immutable memory
strcpy(ptr, "Will not work");
\end{lstlisting}

String literals are character arrays stored in the code segment of the program, which is immutable.
Two string literals may share the same space in memory.
An example follows.

\begin{lstlisting}[language=C]
char *str1 = "Mark Twain likes books";
char *str2 = "Mark Twain likes books";
\end{lstlisting}

The strings pointed to by \keyword{str1} and \keyword{str2} may actually reside in the same location in memory.

Char arrays, however, contain the literal value which has been copied from the code segment into either the stack or static memory.
These following char arrays reside in different memory locations.

\begin{lstlisting}[language=C]
char arr1[] = "Mark Twain also likes to write";
char arr2[] = "Mark Twain also likes to write";
\end{lstlisting}

Here are some common ways to initialize a string include. Where do they reside in memory?

\begin{lstlisting}[language=C]
char *str = "ABC";
char str[] = "ABC";
char str[]={'A','B','C','\0'};
\end{lstlisting}

\begin{lstlisting}[language=C]
char ary[] = "Hello";
char *ptr = "Hello";
\end{lstlisting}

We can also print out the pointer and the contents of a C-string easily. Here is some boilerplate code to illustrate this.

\begin{lstlisting}[language=C]
char ary[] = "Hello";
char *ptr = "Hello";
// Print out address and contents
printf("%p : %s\n", ary, ary);
printf("%p : %s\n", ptr, ptr);
\end{lstlisting}

As mentioned before, the char array is mutable, so we can change its contents.
Be careful to write within the bounds of the array.
C does \emph{not} do bounds checking at compile-time, but invalid reads/writes can get your program to crash.

\begin{lstlisting}[language=C]
strcpy(ary, "World"); // OK
strcpy(ptr, "World"); // NOT OK - Segmentation fault (crashes by default; unless SIGSEGV is blocked)
\end{lstlisting}

Unlike the array, however, we can change \keyword{ptr} to point to another piece of memory,

\begin{lstlisting}[language=C]
ptr = "World"; // OK!
ptr = ary; // OK!
ary = "World"; // NO won't compile
// ary is doomed to always refer to the original array.
printf("%p : %s\n", ptr, ptr);
strcpy(ptr, "World"); // OK because now ptr is pointing to mutable memory (the array)
\end{lstlisting}

Unlike pointers, that hold addresses to variables on the heap, or stack, char arrays (string literals) point to read-only memory located in the data section of the program. This means that pointers are more flexible than arrays, even though the name of an array is a pointer to its starting address.

In a more common case, pointers will point to heap memory in which case the memory referred to by the pointer \textbf{can} be modified.


\chapter{General}
\section{General Tips}
\runinhead{Information Source.} Keep the source information rather than derived information (e.g. keep the array index rather than array element).
\runinhead{Information Transformation.} Need you keep the raw information to avoid information loss (e.g. after converting \pyinline{str} to \pyinline{list}, you should keep \pyinline{str}).
\runinhead{Element Data Structure} When working with ADT, you should use a more intelligence data structure as type to avoid allocating another ADT to maintain the state (e.g. \javainline{java.util.PriorityQueue<E>}).
\runinhead{Solving unseen problems.} Solving unseen problems is like a search problems. You need to explore different options, either with dfs or bfs.