\title{Computer Architecture - CS 301} % You may change the title if you want.

\author{Rishit Saiya - 180010027, Assignment - 11}

\date{\today}

\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{enumitem}
\usepackage{amsmath,mathtools}
\usepackage{amssymb}
\usepackage[super]{nth}
\usepackage{textcomp}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
}
\begin{document}
\maketitle

%----------------------------------------------------------------

\section{}
In the Direct-Mapped Cache, we first try to find all the dependencies of variables and then check the cache.

We are given byte-addressable, direct-mapped cache of size 1 KB. So, that translates as follows:
\begin{equation*}
    Cache \, Size = 2^{10} \, bytes
\end{equation*}
Next, we are given information about Line Size (64 bytes) as follows:
\begin{equation*}
    Line \, Size = 2^6 \, bytes
\end{equation*}
We know that Number of Address bits is 16. Now, to calculate the number of bits assigned to Index bits we do as follows:
\begin{equation*}
    Index = \frac{Blocks}{Cache \, Line}
\end{equation*}
or,
\begin{equation*}
    Index \, Bits = \frac{2^{10}}{2^6} = 2^4 = 16
\end{equation*}
	So, Index will be of 4 bits.

So, finally tag bits become as follows:
\begin{equation*}
    Tag Bits = Address \, Bits - Index \, Bits - Offset
\end{equation*}
or,
\begin{equation*}
    Tag \, Bits = 16 - 4 - 6 = 6
\end{equation*}

We are the following sequence of accesses (16 bit addresses in hexadecimal format): \\
0x0001, 0x0002, 0x0003, 0x0041, 0x0081, 0x00A1, 0x00C1, 0x0401, 0x0402, 0x0001, 0x0002, 0x0003.

Table 1 shows the iterations on how the Cache is being filled in each value entered in iteration.

\begin{table}[]
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
\textbf{Address} & \textbf{Binary}     & \textbf{Tag Bits} & \textbf{\begin{tabular}[c]{@{}c@{}}Tag Field\\ (Index Bits)\end{tabular}} & \textbf{\begin{tabular}[c]{@{}c@{}}Tag Field\\ (Bits)\end{tabular}} & \textbf{Hit/Miss} \\ \hline
0x0001           & 0000 0000 0000 0001 & 000000            & 0000                                                                      & 000000                                                              & Miss              \\ \hline
0x0002           & 0000 0000 0000 0010 & 000000            & 0000                                                                      & 000000                                                              & Hit               \\ \hline
0x0003           & 0000 0000 0000 0011 & 000000            & 0000                                                                      & 000000                                                              & Hit               \\ \hline
0x0041           & 0000 0000 0100 0001 & 000000            & 0001                                                                      & 000000                                                              & Miss              \\ \hline
0x0081           & 0000 0000 1000 0001 & 000000            & 0010                                                                      & 000000                                                              & Miss              \\ \hline
0x00A1           & 0000 0000 1010 0001 & 000000            & 0010                                                                      & 000000                                                              & Hit               \\ \hline
0x00C1           & 0000 0000 1100 0001 & 000000            & 0011                                                                      & 000000                                                              & Miss              \\ \hline
0x0401           & 0000 0100 0000 0001 & 000001            & 0000                                                                      & 000001                                                              & Miss              \\ \hline
0x0402           & 0000 0000 1000 0001 & 000001            & 0000                                                                      & 000001                                                              & Hit               \\ \hline
0x0001           & 0000 0000 0000 0001 & 000000            & 0000                                                                      & 000000                                                              & Miss              \\ \hline
0x0002           & 0000 0000 0000 0010 & 000000            & 0000                                                                      & 000000                                                              & Hit               \\ \hline
0x0003           & 0000 0000 0000 0011 & 000000            & 0000                                                                      & 000000                                                              & Hit               \\ \hline
\end{tabular}
\caption{Direct Mapped Cache}
\end{center}
\end{table}

The details in each iterations are done with the considerations like the cache was empty before the $1^{st}$ iteration. We see that there were \textbf{6 Cache Hits} and \textbf{6 Cache Miss} after 12 iterations.
%----------------------------------------------------------------
\section{}

In the 2-way Associative Cache, we first try to find all the dependencies of variables and then check the cache.

We are given byte-addressable, direct-mapped cache of size 1 KB. So, that translates as follows:
\begin{equation*}
    Cache \, Size = 2^{10} \, bytes
\end{equation*}
Next, we are given information about Line Size (64 bytes) as follows:
\begin{equation*}
    Line \, Size = 2^6 \, bytes
\end{equation*}
Number of Address bits is 16. Given that number of cache blocks per set is 2, we calculate the number of sets in the cache as follows:
\begin{equation*}
    Index = \frac{Blocks}{Cache \, Line \times cache \, blocks}
\end{equation*}
or,
\begin{equation*}
    Index \, Bits = \frac{2^{10}}{2^6 \times 2} = 2^3 = 8
\end{equation*}
So, Index will be of 3 bits.

So, finally tag bits become as follows:
\begin{equation*}
    Tag Bits = Address \, Bits - Index \, Bits - Offset
\end{equation*}
or,
\begin{equation*}
    Tag \, Bits = 16 - 3 - 6 = 7
\end{equation*}

We are the following sequence of accesses (16 bit addresses in hexadecimal format): \\
0x0001, 0x0002, 0x0003, 0x0041, 0x0081, 0x00A1, 0x00C1, 0x0401, 0x0402, 0x0001, 0x0002, 0x0003.

Table 2 shows the iterations on how the Cache is being filled in each value entered in iteration.

\begin{table}[]
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
\textbf{Address} & \textbf{Binary}     & \textbf{Tag Bits} & \textbf{\begin{tabular}[c]{@{}c@{}}Tag Field\\ (Index Bits)\end{tabular}} & \textbf{\begin{tabular}[c]{@{}c@{}}Tag Field\\ (Bits)\end{tabular}} & \textbf{Hit/Miss} \\ \hline
0x0001           & 0000 0000 0000 0001 & 0000000           & 000                                                                       & 0000000                                                             & Miss              \\ \hline
0x0002           & 0000 0000 0000 0010 & 0000000           & 000                                                                       & 0000000                                                             & Hit               \\ \hline
0x0003           & 0000 0000 0000 0011 & 0000000           & 000                                                                       & 0000000                                                             & Hit               \\ \hline
0x0041           & 0000 0000 0100 0001 & 0000000           & 001                                                                       & 0000000                                                             & Miss              \\ \hline
0x0081           & 0000 0000 1000 0001 & 0000000           & 010                                                                       & 0000000                                                             & Miss              \\ \hline
0x00A1           & 0000 0000 1010 0001 & 0000000           & 010                                                                       & 0000000                                                             & Hit               \\ \hline
0x00C1           & 0000 0000 1100 0001 & 0000000           & 011                                                                       & 0000000                                                             & Miss              \\ \hline
0x0401           & 0000 0100 0000 0001 & 0000010           & 000                                                                       & 0000001                                                             & Miss              \\ \hline
0x0402           & 0000 0000 1000 0001 & 0000010           & 000                                                                       & 0000001                                                             & Hit               \\ \hline
0x0001           & 0000 0000 0000 0001 & 0000000           & 000                                                                       & 0000000                                                             & Hit               \\ \hline
0x0002           & 0000 0000 0000 0010 & 0000000           & 000                                                                       & 0000000                                                             & Hit               \\ \hline
0x0003           & 0000 0000 0000 0011 & 0000000           & 000                                                                       & 0000000                                                             & Hit               \\ \hline
\end{tabular}
\caption{2-way Associative Cache}
\end{center}
\end{table}

The details in each iterations are done with the considerations like the cache was empty before the $1^{st}$ iteration. We see that there were \textbf{7 Cache Hits} and \textbf{5 Cache Miss} after 12 iterations.
%----------------------------------------------------------------
\section{}

In the 4-way Associative Cache, we first try to find all the dependencies of variables and then check the cache.

We are given byte-addressable, direct-mapped cache of size 128 B. So, that translates as follows:
\begin{equation*}
    Cache \, Size = 2^{7} \, bytes
\end{equation*}
Next, we are given information about Line Size (16 bytes) as follows:
\begin{equation*}
    Line \, Size = 2^4 \, bytes
\end{equation*}
Let the number of address bits be 8. Offset will be of 4 bits then. Given that number of cache blocks per set is 4, we calculate the number of sets in the cache as follows:
\begin{equation*}
    Index = \frac{Blocks}{Cache \, Line \times cache \, blocks}
\end{equation*}
or,
\begin{equation*}
    Index \, Bits = \frac{2^{7}}{2^4 \times 4} = 2^3 = 2
\end{equation*}
So, Index will be of 1 bits.

So, finally tag bits become as follows:
\begin{equation*}
    Tag Bits = Address \, Bits - Index \, Bits - Offset
\end{equation*}
or,
\begin{equation*}
    Tag \, Bits = 8 - 1 - 4 = 3
\end{equation*}

\subsection{LRU performing better than LFU}
Table 3 shows where LRU is used as Replacement Policy.

\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \begin{tabular}[c]{@{}c@{}}\textbf{Set Index}\\\textbf{Bit}~\end{tabular} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
10010011                                                                     & 4                  & 1                                                                         & 0                      & Miss                \\
\hline
10010010                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010111                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010101                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10110001                                                                     & 5                  & 1                                                                         & 1                      & Miss                \\
\hline
01010000                                                                     & 2                  & 1                                                                         & 2                      & Miss                \\
\hline
01011000                                                                     & 2                  & 1                                                                         & 2                      & Hit                 \\
\hline
01110101                                                                     & 3                  & 1                                                                         & 3                      & Miss                \\
\hline
10110011                                                                     & 5                  & 1                                                                         & 2                      & Hit                 \\
\hline
11010011                                                                     & 6                  & 1                                                                         & 0                      & Miss                \\
\hline
01110011                                                                     & 3                  & 1                                                                         & 3                      & Hit                 \\
\hline
\end{tabular}
\caption{LRU Replacement Policy}
\end{table}

Table 4 shows where LFU is used as Replacement Policy.

\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \begin{tabular}[c]{@{}c@{}}\textbf{Set Index}\\\textbf{Bit}~\end{tabular} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
10010011                                                                     & 4                  & 1                                                                         & 0                      & Miss                \\
\hline
10010010                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010111                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010101                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10110001                                                                     & 5                  & 1                                                                         & 1                      & Miss                \\
\hline
01010000                                                                     & 2                  & 1                                                                         & 2                      & Miss                \\
\hline
01011000                                                                     & 2                  & 1                                                                         & 2                      & Hit                 \\
\hline
01110101                                                                     & 3                  & 1                                                                         & 3                      & Miss                \\
\hline
10110011                                                                     & 5                  & 1                                                                         & 2                      & Hit                 \\
\hline
11010011                                                                     & 6                  & 1                                                                         & 3                      & Miss                \\
\hline
01110011                                                                     & 3                  & 1                                                                         & 3                      & Miss                 \\
\hline
\end{tabular}
\caption{LFU Replacement Policy}
\end{table}

From Table 3 and Table 4 we can see that there are \textbf{6 hits} out of 11 cases \& \textbf{5 hits} out of 11 cases for LRU \& LFU respectively. So, LRU performs better than LFU.

\subsection{LFU performing better than LRU}
Table 5 shows where LRU is used as Replacement Policy.

\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \begin{tabular}[c]{@{}c@{}}\textbf{Set Index}\\\textbf{Bit}~\end{tabular} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
10010011                                                                     & 4                  & 1                                                                         & 0                      & Miss                \\
\hline
10010010                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010111                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010101                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
01110001                                                                     & 3                  & 1                                                                         & 1                      & Miss                \\
\hline
10110000                                                                     & 5                  & 1                                                                         & 2                      & Miss                \\
\hline
01011000                                                                     & 2                  & 1                                                                         & 2                      & Miss                \\
\hline
01010101                                                                     & 2                  & 1                                                                         & 3                      & Hit                 \\
\hline
10110011                                                                     & 5                  & 1                                                                         & 2                      & Hit                 \\
\hline
01110011                                                                     & 6                  & 1                                                                         & 0                      & Miss                \\
\hline
10010101                                                                     & 4                  & 1                                                                         & 1                      & Miss                \\
\hline
\end{tabular}
\caption{LRU Replacement Policy}
\end{table}

Table 6 shows where LFU is used as Replacement Policy.
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \begin{tabular}[c]{@{}c@{}}\textbf{Set Index}\\\textbf{Bit}~\end{tabular} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
10010011                                                                     & 4                  & 1                                                                         & 0                      & Miss                \\
\hline
10010010                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010111                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
10010101                                                                     & 4                  & 1                                                                         & 0                      & Hit                 \\
\hline
01110001                                                                     & 3                  & 1                                                                         & 1                      & Miss                \\
\hline
10110000                                                                     & 5                  & 1                                                                         & 2                      & Miss                \\
\hline
01011000                                                                     & 2                  & 1                                                                         & 2                      & Miss                \\
\hline
01010101                                                                     & 2                  & 1                                                                         & 3                      & Hit                 \\
\hline
10110011                                                                     & 5                  & 1                                                                         & 2                      & Hit                 \\
\hline
01110011                                                                     & 6                  & 1                                                                         & 0                      & Miss                \\
\hline
10010101                                                                     & 4                  & 1                                                                         & 1                      & Hit                 \\
\hline
\end{tabular}
\caption{LFU Replacement Policy}
\end{table}

From Table 5 and Table 6 we can see that there are \textbf{5 hits} out of 11 cases \& \textbf{6 hits} out of 11 cases for LRU \& LFU respectively. So, LFU performs better than LRU.
%----------------------------------------------------------------
\section{}

In the Fully Associative Cache, we first try to find all the dependencies of variables and then check the cache.

We are given byte-addressable, direct-mapped cache of size 128 B. So, that translates as follows:
\begin{equation*}
    Cache \, Size = 2^{7} \, bytes
\end{equation*}
Next, we are given information about Line Size (16 bytes) as follows:
\begin{equation*}
    Line \, Size = 2^4 \, bytes
\end{equation*}
Let us say that number of Address bits is 8. Hence Offset will be of 4 bits. We calculate the number of sets in the cache as follows:
\begin{equation*}
    Index = \frac{Blocks}{Cache \, Line}
\end{equation*}
or,
\begin{equation*}
    Index \, Bits = \frac{2^{7}}{2^4} = 2^3 = 8
\end{equation*}
So, Index will be of 3 bits.

So, finally tag bits become as follows:
\begin{equation*}
    Tag Bits = Address \, Bits - Index \, Bits
\end{equation*}
or,
\begin{equation*}
    Tag \, Bits = 8 - 4 = 4
\end{equation*}

\subsection{LRU performing better than LFU}
Table 7 shows where LRU is used as Replacement Policy.

\begin{table}
\centering
\begin{tabular}{|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
01000010                                                                     & 4                  & 0                      & Miss                \\
\hline
01000100                                                                     & 4                  & 0                      & Hit                 \\
\hline
01001000                                                                     & 4                  & 0                      & Hit                 \\
\hline
01010000                                                                     & 5                  & 1                      & Miss                \\
\hline
00100001                                                                     & 2                  & 2                      & Miss                \\
\hline
00100011                                                                     & 2                  & 2                      & Hit                 \\
\hline
00110011                                                                     & 3                  & 3                      & Miss                \\
\hline
01010100                                                                     & 5                  & 1                      & Hit                 \\
\hline
01010001                                                                     & 5                  & 1                      & Hit                 \\
\hline
01110011                                                                     & 6                  & 4                      & Miss                \\
\hline
00110101                                                                     & 3                  & 3                      & Hit                 \\
\hline
00010001                                                                     & 1                  & 5                      & Miss                \\
\hline
00010000                                                                     & 1                  & 5                      & Hit                 \\
\hline
01110000                                                                     & 7                  & 6                      & Miss                \\
\hline
01110001                                                                     & 7                  & 6                      & Hit                 \\
\hline
01110101                                                                     & 7                  & 6                      & Hit                 \\
\hline
10001000                                                                     & 8                  & 7                      & Miss                \\
\hline
01110101                                                                     & 6                  & 4                      & Hit                 \\
\hline
10010101                                                                     & 9                  & 0                      & Miss                \\
\hline
10001011                                                                     & 8                  & 7                      & Hit                 \\
\hline
\end{tabular}
\caption{LRU Replacement Policy}
\end{table}

Table 8 shows where LFU is used as Replacement Policy.
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
01000010                                                                     & 4                  & 0                      & Miss                \\
\hline
01000100                                                                     & 4                  & 0                      & Hit                 \\
\hline
01001000                                                                     & 4                  & 0                      & Hit                 \\
\hline
01010000                                                                     & 5                  & 1                      & Miss                \\
\hline
00100001                                                                     & 2                  & 2                      & Miss                \\
\hline
00100011                                                                     & 2                  & 2                      & Hit                 \\
\hline
00110011                                                                     & 3                  & 3                      & Miss                \\
\hline
01010100                                                                     & 5                  & 1                      & Hit                 \\
\hline
01010001                                                                     & 5                  & 1                      & Hit                 \\
\hline
01110011                                                                     & 6                  & 4                      & Miss                \\
\hline
00110101                                                                     & 3                  & 3                      & Hit                 \\
\hline
00010001                                                                     & 1                  & 5                      & Miss                \\
\hline
00010000                                                                     & 1                  & 5                      & Hit                 \\
\hline
01110000                                                                     & 7                  & 6                      & Miss                \\
\hline
01110001                                                                     & 7                  & 6                      & Hit                 \\
\hline
01110101                                                                     & 7                  & 6                      & Hit                 \\
\hline
10001000                                                                     & 8                  & 7                      & Miss                \\
\hline
01110101                                                                     & 6                  & 4                      & Hit                 \\
\hline
10010101                                                                     & 9                  & 0                      & Miss                \\
\hline
10001011                                                                     & 8                  & 7                      & Miss                \\
\hline
\end{tabular}
\caption{LFU Replacement Policy}
\end{table}

From Table 7 and Table 8 we can see that there are \textbf{12 hits} out of 20 cases \& \textbf{10 hits} out of 20 cases for LRU \& LFU respectively. So, LRU performs better than LFU.

\subsection{LFU performing better than LRU}
Table 9 shows where LRU is used as Replacement Policy.
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
01000010                                                                     & 4                  & 0                      & Miss                \\
\hline
01000100                                                                     & 4                  & 0                      & Hit                 \\
\hline
01001000                                                                     & 4                  & 0                      & Hit                 \\
\hline
01010000                                                                     & 5                  & 1                      & Miss                \\
\hline
00100001                                                                     & 2                  & 2                      & Miss                \\
\hline
00100011                                                                     & 2                  & 2                      & Hit                 \\
\hline
01100011                                                                     & 3                  & 3                      & Miss                \\
\hline
01010100                                                                     & 5                  & 1                      & Hit                 \\
\hline
01010001                                                                     & 5                  & 1                      & Hit                 \\
\hline
01100001                                                                     & 6                  & 4                      & Miss                \\
\hline
01101001                                                                     & 6                  & 4                      & Hit                 \\
\hline
01100011                                                                     & 3                  & 3                      & Miss                \\
\hline
00100101                                                                     & 1                  & 5                      & Hit                 \\
\hline
00010001                                                                     & 1                  & 5                      & Miss                \\
\hline
01110000                                                                     & 7                  & 6                      & Hit                 \\
\hline
01110000                                                                     & 7                  & 6                      & Hit                 \\
\hline
10000001                                                                     & 8                  & 7                      & Miss                \\
\hline
01100101                                                                     & 6                  & 4                      & Hit                 \\
\hline
10011000                                                                     & 9                  & 0                      & Miss                \\
\hline
01000101                                                                     & 4                  & 2                      & Hit                 \\
\hline
\end{tabular}
\caption{LRU Replacement Policy}
\end{table}

Table 10 shows where LFU is used as Replacement Policy.
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|}
\hline
\begin{tabular}[c]{@{}c@{}}\textbf{Address}\\\textbf{(Binary)}~\end{tabular} & \textbf{Tag Value} & \textbf{Set Entry Bit} & \textbf{Hit/Miss}   \\
\hline
01000010                                                                     & 4                  & 0                      & Miss                \\
\hline
01000100                                                                     & 4                  & 0                      & Hit                 \\
\hline
01001000                                                                     & 4                  & 0                      & Hit                 \\
\hline
01010000                                                                     & 5                  & 1                      & Miss                \\
\hline
00100001                                                                     & 2                  & 2                      & Miss                \\
\hline
00100011                                                                     & 2                  & 2                      & Hit                 \\
\hline
01100011                                                                     & 3                  & 3                      & Miss                \\
\hline
01010100                                                                     & 5                  & 1                      & Hit                 \\
\hline
01010001                                                                     & 5                  & 1                      & Hit                 \\
\hline
01100001                                                                     & 6                  & 4                      & Miss                 \\
\hline
01101001                                                                     & 6                  & 4                      & Hit                 \\
\hline
01100011                                                                     & 3                  & 3                      & Hit                \\
\hline
00100101                                                                     & 1                  & 5                      & Hit                 \\
\hline
00010001                                                                     & 1                  & 5                      & Miss                \\
\hline
01110000                                                                     & 7                  & 6                      & Hit                 \\
\hline
01110000                                                                     & 7                  & 6                      & Miss                \\
\hline
10000001                                                                     & 8                  & 7                      & Hit                 \\
\hline
01100101                                                                     & 6                  & 4                      & Hit                 \\
\hline
10011000                                                                     & 9                  & 0                      & Miss                \\
\hline
01000101                                                                     & 4                  & 2                      & Hit                 \\
\hline
\end{tabular}
\caption{LFU Replacement Policy}
\end{table}

From Table 9 and Table 10 we can see that there are \textbf{11 hits} out of 20 cases \& \textbf{12 hits} out of 20 cases for LRU \& LFU respectively. So, LFU performs better than LRU.

%----------------------------------------------------------------
\section{}
Given that Cache size is 8 words/blocks and Line Size is given as 2. So, we calculate number of blocks/lines as follows:
\begin{equation*}
    Blocks = \frac{Cache \, Size}{Line \, Size}
\end{equation*}
or,
\begin{equation*}
    Blocks = \frac{8}{2} = 4
\end{equation*}
So, Offset is of 1 but and number of blocks of Cache is 4. \\

\textbf{\textit{LRU (Least Recently Used)}}: \\
Least Recently Used (LRU) discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item.

Reference I took help from to solve this question: \href{http://pages.cs.wisc.edu/~sohi/cs552/Homework/hw4/hw4_sol.html}{Reference 1}, \href{https://acsweb.ucsd.edu/~jkalyana/grinch/ECE30/ece30_files/Study_Problems_9.pdf}{Reference 2}.

Table 11 shows the iterations on how the Cache is being filled in each value entered in iteration.

\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\textbf{Address}  & \textbf{Tag Value} & \begin{tabular}[c]{@{}c@{}}\textbf{Offset}\\\textbf{ (Last Bit)} \end{tabular} & \begin{tabular}[c]{@{}c@{}}\textbf{Cache}\\\textbf{ Block} \end{tabular} & \textbf{Hit/Miss}   \\
\hline
20                & 10                 & 0                                                                              & 1                                                                        & Miss                \\
\hline
21                & 10                 & 1                                                                              & 1                                                                        & Hit                 \\
\hline
22                & 11                 & 0                                                                              & 2                                                                        & Miss                \\
\hline
23                & 11                 & 1                                                                              & 2                                                                        & Hit                 \\
\hline
24                & 12                 & 0                                                                              & 3                                                                        & Miss                \\
\hline
25                & 12                 & 1                                                                              & 3                                                                        & Hit                 \\
\hline
26                & 13                 & 0                                                                              & 4                                                                        & Miss                \\
\hline
27                & 13                 & 1                                                                              & 4                                                                        & Hit                 \\
\hline
28                & 14                 & 0                                                                              & 1 (LRU)                                                                  & Miss                \\
\hline
29                & 14                 & 1                                                                              & 1                                                                        & Hit                 \\
\hline
22                & 11                 & 0                                                                              & 2                                                                        & Hit                 \\
\hline
30                & 15                 & 0                                                                              & 3                                                                        & Miss                \\
\hline
21                & 10                 & 1                                                                              & 3                                                                        & Miss                \\
\hline
23                & 11                 & 1                                                                              & 2                                                                        & Hit                 \\
\hline
31                & 15                 & 1                                                                              & 3                                                                        & Hit                 \\
\hline
\end{tabular}
\caption{Fully Associative Cache - LRU}
\end{table}

The addresses are given to us. When we remove the last binary bit from the given addresses, we get the offset. Alternatively, just even address will have \textbf{0} a offset and odd address will have \textbf{1} as address. Since we are given that we have 2 lines, we put the data accordingly in the 4 boxes of caches. We see that at Address 28, Cache Line 1 (LRU) was assigned. It is to denote that LRU was implemented from there onward. We then decide Hit/Miss by previous idea only. \\

We see the Hit Rate as follows:
\begin{equation*}
    Hit \, Rate = \frac{8}{15} = 0.53
\end{equation*}

Table 12 shows the Cache data after all the iterations.

\begin{table}[]
\begin{center}
\begin{tabular}{c|c|c|}
\cline{2-3}
\textbf{}                                                                            & \multicolumn{2}{c|}{\textbf{Cache Data}}        \\ \hline
\multicolumn{1}{|c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Cache\\ Block\end{tabular}}} & \textbf{{[}0{]} Index} & \textbf{{[}1{]} Index} \\ \hline
\multicolumn{1}{|c|}{1}                                                              & 28                     & 29                     \\ \hline
\multicolumn{1}{|c|}{2}                                                              & 22                     & 23                     \\ \hline
\multicolumn{1}{|c|}{3}                                                              & 30                     & 31                     \\ \hline
\multicolumn{1}{|c|}{4}                                                              & 20                     & 21                     \\ \hline
\end{tabular}
\caption{Cache Contents - Fully Associative LRU}
\end{center}
\end{table}
%----------------------------------------------------------------
\section{}
Given that Cache size is 8 words/blocks and Line Size is given as 2 and each set is with 2 entries. So, we calculate number of blocks/lines as follows:
\begin{equation*}
    Blocks = \frac{Cache \, Size}{Line \, Size}
\end{equation*}
or,
\begin{equation*}
    Blocks = \frac{8}{2 \times 2} = 2
\end{equation*}
So, Offset is of 1 but and number of blocks of Cache is 4. \\

Table 13 shows the iterations on how the Cache is being filled in each value entered in iteration.
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
\textbf{Address}  & \textbf{Tag Value} & \begin{tabular}[c]{@{}c@{}}\textbf{Offset}\\\textbf{ (Last Bit)} \end{tabular} & \begin{tabular}[c]{@{}c@{}}\textbf{Set Entry}\\\textbf{ Bit} \end{tabular} & \textbf{Hit/Miss}   \\
\hline
20                & 5                  & 0                                                                              & 0                                                                          & Miss                \\
\hline
21                & 5                  & 1                                                                              & 0                                                                          & Hit                 \\
\hline
22                & 5                  & 0                                                                              & 0                                                                          & Miss                \\
\hline
23                & 5                  & 1                                                                              & 0                                                                          & Hit                 \\
\hline
24                & 6                  & 0                                                                              & 1                                                                          & Miss                \\
\hline
25                & 6                  & 1                                                                              & 1                                                                          & Hit                 \\
\hline
26                & 6                  & 0                                                                              & 1                                                                          & Miss                \\
\hline
27                & 6                  & 1                                                                              & 1                                                                          & Hit                 \\
\hline
28                & 7                  & 0                                                                              & 0 (LRU)                                                                    & Miss                \\
\hline
29                & 7                  & 1                                                                              & 0                                                                          & Hit                 \\
\hline
22                & 5                  & 0                                                                              & 0                                                                          & Hit                 \\
\hline
30                & 7                  & 0                                                                              & 1                                                                          & Miss                \\
\hline
21                & 5                  & 1                                                                              & 1                                                                          & Miss                \\
\hline
23                & 5                  & 1                                                                              & 0                                                                          & Hit                 \\
\hline
31                & 7                  & 1                                                                              & 1                                                                          & Hit                 \\
\hline
\end{tabular}
\caption{2-way Associative Cache - LRU}
\end{table}

The addresses are given to us. When we remove the last binary bit from the given addresses, we get the offset. Alternatively, just even address will have \textbf{0} a offset and odd address will have \textbf{1} as address. Since we are given that we have 2 sets here, we put the data accordingly in the 2 sets in each line of caches. We see that at Address 28, Set Entry Bit 0 (LRU) was assigned. It is to denote that LRU was implemented from there onward. We then decide Hit/Miss by previous idea only. \\

We see the Hit Rate as follows:
\begin{equation*}
    Hit \, Rate = \frac{8}{15} = 0.53
\end{equation*}

Table 14 shows the Cache data after all the iterations.

\begin{table}[]
\begin{center}

\begin{tabular}{c|c|c|c|c|}
\cline{2-5}
\textbf{}                                                                            & \multicolumn{2}{c|}{\textbf{Set 0}}             & \multicolumn{2}{c|}{\textbf{Set 1}}             \\ \hline
\multicolumn{1}{|c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Cache\\ Block\end{tabular}}} & \textbf{{[}0{]} Index} & \textbf{{[}1{]} Index} & \textbf{{[}0{]} Index} & \textbf{{[}1{]} Index} \\ \hline
\multicolumn{1}{|c|}{1}                                                              & 28                     & 29                     & 20                     & 21                     \\ \hline
\multicolumn{1}{|c|}{2}                                                              & 22                     & 23                     & 30                     & 31                     \\ \hline
\end{tabular}
\caption{Cache Contents - 2 Way Associative LRU}
\end{center}
\end{table}
%----------------------------------------------------------------
\end{document}


\documentclass[12pt]{amsart}
\usepackage{amssymb,amscd,amsmath}
\usepackage{fullpage}
%\usepackage{showkeys} %Shows references. Hide afterwards!
\usepackage{tikz-cd}
%\usepackage{natbib}
\usepackage{todonotes}
\setlength\parindent{0pt}
\usepackage{verbatim} %For long comments
\usepackage{caption}
\usepackage{float}
\usetikzlibrary{intersections}

   %%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%% MATHEMATICS %%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%

\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}

\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem{example}{Example}

\theoremstyle{remark}
\newtheorem*{remark}{Remark}

   %%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%% MATHEMATICS %%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%

   %%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%% COMMANDS %%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%

	\newcommand{\R}{\mathbb{R}}
	\newcommand{\N}{\mathbb{N}}
	\newcommand{\C}{\mathbb{C}}
	\newcommand{\Z}{\mathbb{Z}}
	\newcommand{\Q}{\mathbb{Q}}
	\newcommand{\h}{\mathcal{H}}
	\newcommand{\Lcal}{\mathcal{L}}
	\newcommand{\Int}{\mathcal{O}}
	\newcommand{\BigOh}{\mathcal{O}}
	\newcommand{\abs}[1]{\left|#1\right|}
	\newcommand{\diffquo}{\frac{d}{dx}}
	\newcommand{\A}{\mathfrak{A}}
	\newcommand{\T}{\mathcal{T}}
	\newcommand{\norm}[1]{|#1|} %Norm
	\newcommand{\Hom}{\text{Hom}}
	\newcommand{\Div}{\text{Div}}
	\newcommand{\dv}{\operatorname{div}}
	\newcommand{\Ext}{\text{Ext}}
	\newcommand{\Eis}{\text{Eis}}
	\newcommand{\Res}{\text{Res}}
	\newcommand{\GL}{\mathrm{GL}}
	\newcommand{\SL}{\mathrm{SL}}
	\DeclareMathOperator{\ord}{ord}
	\DeclareMathOperator{\Gal}{Gal}
    \renewcommand{\Re}{\text{Re}\,}
    \renewcommand{\Im}{\text{Im}\,}
    \renewcommand{\mod}{\text{ mod}\,}
	\newcommand{\abcd}[4]{\begin{pmatrix}#1&#2\\#3& #4\end{pmatrix}}
	\newcommand{\sabcd}[4]{\left(\begin{smallmatrix}#1&#2\\#3& #4\end{smallmatrix}\right)}

   %%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%% COMMANDS %%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%

   %%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%% TITLE %%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%
\font\myfont=cmr12 at 20pt
	\title{\myfont Einf\"uhrung in die Kryptographie \vspace{\baselineskip} \\ Introduction to cryptography
  \\ \vspace{\baselineskip} \normalfont Sommerschule Faszination Mathematik, 15.06.- 19.06.2018}
	\author{Michael and R\'ois\'in Neururer}
	%\date{Sommerschule Faszination Mathematik, \today}
	\usepackage{hyperref}
	\hypersetup{pdfauthor={Michael O. Neururer},%
	            pdftitle={TITLE},%
	            pdfsubject={Number Theory},%
	            pdfcreator={XeLaTeX},%
	            colorlinks=true,%
	            linkcolor=[rgb]{0,.6,1},
	            citecolor=blue}

   %%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%% TITLE %%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\maketitle
\newpage
%\tableofcontents
\section{Modular Arithmetic}

\subsection*{Adding around the clock}
\hfill\\
We can all read a clock and know that if it is 11 o'clock now, then it is 1 o'clock in 2 hours time. In 12 hours it will be 11 o'clock again. So can we say that $11+2=1$ (on a clock)?

\vspace{10pt}

Try the following and think about how you work them out:\\

\begin{enumerate}
\item If it is 9 o'clock now, what time is it in 57 hours?\\
\item If it is Saturday today, what day is it in 11 days?\\
\item If it is June now, what month will it be in 1000 months?\\
\item If it is midday now, will it be light or dark in 539 hours?\\
\end{enumerate}

%%Here we would highlight that it is the remainer when we divide bythe relevant number (eg 12 on a clock) but I don't want to write this in as I want them to figure it out themselves.

\subsection*{Modulo}
\hfill\\
Looking at the clock examples, in mathematical notation we would write:
\[
11+2 \equiv 1 \pmod {12}\quad
\text{ and }\quad
12 \equiv 0 \pmod{12}\quad
\text{ and }\quad
539 \equiv 11 \pmod{12}
\]
%%Spacing issue above

Look at the following addition tables for mod 5 and mod 6. Make sure you understand them and then have a go at the following exercises.
\begin{table}[!htb]
    \caption*{}
    \begin{minipage}{.5\linewidth}
      \caption*{Addition modulo $5$}
      \centering
\begin{tabular}{c||c|c|c|c|c}
+&0&1&2&3&4\\
\hline\hline
0&0&1&2&3&4\\
\hline
1&1&2&3&4&0\\
\hline
2&2&3&4&0&1\\
\hline
3&3&4&0&1&2\\
\hline
4&4&0&1&2&3
\end{tabular}
    \end{minipage}%
    \begin{minipage}{.5\linewidth}
      \centering
        \caption*{Addition modulo $6$}
\begin{tabular}{c||c|c|c|c|c|c}
+&0&1&2&3&4&5\\
\hline\hline
0&0&1&2&3&4&5\\
\hline
1&1&2&3&4&5&0\\
\hline
2&2&3&4&5&0&1\\
\hline
3&3&4&5&0&1&2\\
\hline
4&4&5&0&1&2&3\\
\hline
5&5&0&1&2&3&4
\end{tabular}
    \end{minipage}
\end{table}
\subsection*{Exercises}
\begin{enumerate}
\item Construct the addition table for mod 7.
\item What is $3^5 \mod 7$?
\item Find the $x$ between 0 and 11 which solves $10+x\equiv 2\pmod{12}$.
\item What is $3^{1000} \mod 5$?
\end{enumerate}

\subsection*{Multiplication mod n}
\hfill \\
As well as adding, we can multiply. Have a look at the multiplication tables for $n=5$ and $n=6$.\\

\begin{table}[!htb]
    \caption*{}
    \begin{minipage}{.5\linewidth}
      \caption*{Multiplication modulo $5$}
      \centering
\begin{tabular}{c||c|c|c|c|c}
$\cdot$&0&1&2&3&4\\
\hline\hline
0&0&0&0&0&0\\
\hline
1&0&1&2&3&4\\
\hline
2&0&2&4&1&3\\
\hline
3&0&3&1&4&2\\
\hline
4&0&4&3&2&1

        \end{tabular}
    \end{minipage}%
    \begin{minipage}{.5\linewidth}
      \centering
        \caption*{Multiplication modulo $6$}
\begin{tabular}{c||c|c|c|c|c|c}
$\cdot$&0&1&2&3&4&5\\
\hline\hline
0&0&0&0&0&0&0\\
\hline
1&0&1&2&3&4&5\\
\hline
2&0&2&4&0&2&4\\
\hline
3&0&3&0&3&0&3\\
\hline
4&0&4&2&0&4&2\\
\hline
5&0&5&4&3&2&1
        \end{tabular}
    \end{minipage}
\end{table}

There are some key differences. In the table for mod 5 there is a 1 in every row. This means that for every non-zero number $a$, there is another number $b$ which gives $a\cdot b \equiv 1$. We call such a number an \textbf{inverse}. Another difference is that in the table for mod 6 there are ``extra zeros'': it can happen that two numbers which are not 0 multiply together to give 0. These differences are related. The rows in the mod 6 table which have extra zeros don't have a 1. The difference is due to the fact that 5 is a prime and 6 is not.

\begin{definition}
A \textbf{prime number} is a whole number which is only divisible by 1 and itself.
\end{definition}

We will later show that when multiplying mod p, where p is prime, every non-zero integer has an inverse. For now we can show the following:

\begin{lemma}
If $n$ is not a prime, then there are non-zero integers that do not have inverses modulo $n$.
\end{lemma}
%%highlight idea of proof by contradiction
\begin{proof}
If $n$ is not a prime then there exist $a, b \neq 1$ such that $n = a \cdot b$. Now $a \not\equiv 0 \pmod n$ and $b \not\equiv 0 \pmod n$, but $n=a\cdot b \equiv 0 \pmod n$. Suppose $a$ has an inverse mod $n$. This means there exists an $x$ such that $a\cdot x \equiv 1 \pmod n$. Then, multiplying both sides by $b$ gives $b\cdot a \cdot x \equiv b \pmod n$, which gives $0\equiv b \pmod n$ which is not true. Therefore we cannot have such a number $x$ and so $a$ has no inverse mod $n$.
\end{proof}

\section{Euclidean Division and the Euclidean Algorithm}

\begin{definition}
Given two integers $a$ and $b$ with $b\neq0$ we say $b$ divides $a$ and write $b\mid a$ if there exist an integer $q$ such that $a=b\cdot q$. We say $b$ is a \textbf{divisor} of $a$.
\end{definition}

\subsection*{Euclidean division}
\hfill \\

Euclidean division (Division mit Rest) is the process of dividing two integers to give a quotient and a remainder. For example. We can divide 25 by 3. This gives us a quotient of 8 with a remainder of 1. In this example 3 is the \textbf{divisor}.
\begin{theorem}
Given two integers $a$ and $b$, with $b \neq0$, there exist unique integers $q$ and $r$ such that $a = bq+r$ and $0\le r<b$.
\end{theorem}
\begin{proof}
We can prove this geometrically. Suppose $a \ge b$. Take a line of length $a$. We can divide this up into lines of length $b$. Either there will be nothing left, and $r=0$ or there is a certain length left. This is necessarily less than $b$, or else we could take another length $b$ out.

Suppose $a<b$. Then we can't take any lines of length $b$ out and so $q = 0$ and $r = a <b$
\end{proof}

Every pair of integers will always have 1 as a common divisor, but they may have other common divisors. For example, 12 and 30 have 1, 2, 3, and 6 as common divisors. We are interested in the greatest common divisor. In this example, it is 6 as 6 is the largest divisor of both 12 and 30.

\begin{definition} Given two integers $a$ and $b$ we say and integer $d$ is the \textbf{greatest common divisor} of $a$ and $b$ if:
\begin{enumerate}
\item $d\mid a$ and $d\mid b$, and
\item If $d'\mid a$ and $d' \mid b$ then $d' \mid d$
\end{enumerate}
We write gcd($a$, $b$)=$d$.
\end{definition}

\begin{definition}
Two integers $a$ and $b$ are \textbf{coprime} if their greatest common divisor is 1. That is, gcd(a,b)=1.
\end{definition}
\subsection*{Excersises}
\begin{enumerate}
\item Why does the above definition give the greatest common divisor?
\item Why can we be sure that there will always be a gcd?
\item Find gcd(32, 54)
\item Find gcd(925, 65)
\end{enumerate}

\begin{definition}
Two integers $a$ and $b$ are \textbf{coprime} if their greatest common divisor is 1. That is, gcd(a,b)=1.
\end{definition}

\subsection*{Euclidean Algorithm}
\hfill \\

The \textbf{Euclidean algorithm} is an efficient method for computing the gcd of two integers. If we are trying to compute the gcd of two integers a and b, we can replace the larger of the two numbers by its remainder when divided by the smaller of the two. We repeat this process until we reach a zero remainder.

\begin{example}
Let's take $a=1071$ and $b=462$. We are looking to compute gcd(1071, 462).

We divide 1071 by 462 to get a quotient of 2 and a remainder of 147

\[1071= 2\times 462 + 147 \qquad q_0 = 2 \qquad r_0=147\]

Now we repeat the process with 462 and 147 to get

\[462= 3\times 147 + 21 \qquad q_1 = 3 \qquad r_1=21\].

Repeating the process for a second time gives

\[147= 7\times 21 + 0 \qquad q_2 = 7 \qquad r_2=0\].

We have a zero remainder so the algorithm ends and the last non-zero remainder gives us the gcd of a and b. So we have that gcd(1071, 462)=21.
\end{example}

\subsection*{Exercises}
\begin{enumerate}
\item Compute gcd(925, 65).
\item Comupte gcd(5671, 342)
\end{enumerate}

\section{Bezout's Identity, Fermat's Little Theorem...}
In this section we will use modular arithmetic and the Euclidean algorithm to prove some number theory results which we will need on our journey towards cryptography.

\subsection*{Bezout's Identity}
\begin{lemma}
For coprime integers $a$, $b$, there are integers $x$ and $y$ with $ax-by=1$.
\end{lemma}
\begin{proof}
Let's apply the Euclidean algorithm to the integers $a$ and $b$, continuing until we have a zero remainder. Once we have stopped we have
\[
a = q_0 b +r_0
b = q_1 r_0 + r_1
...
r_{n+1} = q_n r_n +0
\]
Since $a$ and $b$ are coprime we know that $r_n=1$. Rearranging the above equations we get
\[
r_0 = a - q_0 b \\
r_1 = b - q_1r_0 = b - q_1a + q_1q_0b = a(-q_1) - b (-1-q_1q_0)\\
.... \\
1 = r_n = r_{n-1} - q_nr_{n-2} = .....
\]

which will give some required $x$ and $y$ such that $ax-by=1$.

\end{proof}

Bezout's identity is in fact a particular case of the fact that we can write the gcd of $a$ and $b$ as a linear combination of $a$ and $b$. That is, if gcd($a$,$b$)= $d$. Then there exist integers $x$ and $y$ such that $d=ax+by$. The method for finding $x$ and $y$ described in the proof above is known as the \textbf{extended Euclidean algorithm}

\subsection*{Exercise}
Find the gcd of 148 and 272 and write it as a linear combination of these integers.\\

%%%add in alternative modular arithmetic proof

We can now prove the following two statements from earlier in the course.

\begin{lemma}
Let $p$ be prime. If $p$ divides $a\cdot b$ then $p$ divides $a$ or $p$ divides $b$.
\end{lemma}

\subsection*{Excersise}
Try and prove the above lemma yourself. Here are some hints if you are stuck:
\begin{enumerate}
\item Consider the following two cases separately: if $p$ divides $a$, if $p$ does not divide $a$.
\item If $p$ does not divide $a$ what does this say about $a$ and $p$?
\item Can you apply Bezout's identity?
\item What can you say mod $p$?
\end{enumerate}

%\begin{proof}
%If $p$ divides $a$ then we are done.

%If $p$ does not divide $a$, then $p$ and $a$ are coprime so, by Bezout, we have $ax-py=1$ for some integers $x$ and $y$. Rearranging gives $ax = 1 +py$ and so $ax \equiv 1 \pmod p$. Multiplying by $b$  we have $bax \equiv b \pmod p$ but $ab \equiv 0 \pmod p$ since $p$ divides $a\cdot b$ So $b \equiv 0 \pmod p$ and $p$ divides $b$.

%\end{proof}

\begin{lemma}
Let $p$ be prime. Every non-zero number mod $p$ has an inverse.
\end{lemma}
\begin{proof}
Let $a$ be non-zero mod $p$. Then $a$ is coprime to $p$, so by Bezout we have integers $x$ and $y$ such that $ax -py=1$. This means that $ax \equiv 1 \pmod p$ and so $x$ is an inverse of $a$ mod $p$.
\end{proof}

\subsection*{Question}
Why can we assume $a$ coprime to $p$ in the above proof?

\vspace{10pt}

We can now use all we have proved so far to prove Fermat's Little Theorem.

\begin{theorem}[Fermat's Little Theorem]
For any $a$ coprime to $p$ we have $a^{p-1} \equiv 1 \pmod p$.
\end{theorem}

\begin{proof}
We look at the numbers $a$, $2a$, $3a$, ....$(p-1)a$ modulo $p$. Since $a$ and $p$ are coprime, and the numbers $1, 2, ... , p-1$ are all less than $p$, none of these numbers are divisible by $p$, so none of them is 0 mod $p$.\\
Can two of these numbers be the same modulo $p$? Suppose $ak \equiv al\pmod p$ with $k \neq l$. Then $a(k-l)\equiv 0 \pmod p$ which means $p$ divides $a(k-l)$. But $a$ and $p$ are coprime, so $p$ doesn't divide $a$ and since $k$ and $l$ are less than $p$, $p$ cannot divide $k-l$. So this cannot happen and the numbers $a$, $2a$, $3a$, ....$(p-1)a$ are all different modulo $p$.\\
It follows that they are the same as the numbers $1, 2, ...., (p-1)$ mod $p$ just in a different order.
So we have
\[
a\cdot 2a \cdot 3a \cdots (p-1)a = a^{p-1}\cdot 1\cdot 2 \cdots (p-1) \equiv 1 \cdot 2 \cdots (p-1) \pmod p
\]

Now, dividing each side by $1 \cdot 2 \cdots (p-1)$ gives us
\[
a^{p-1} \equiv 1 \pmod p
\]
\end{proof}

\section{Cryptography}

These days, we have many different ways of communicating with each other. Many of our communications are over internet and mobile networks and there is a possibility that anyone else with access to the internet could gain access to our messages as they are transmitted. Cryptography allows us to keep our communications safe.
In fact, people have needed to keep communications secret throughout history: Generals sending instructions to their soldiers; Leaders discussing mutual enemies; revolutionaries plotting their next move. Historically, the people were sending physical paper messages rather than the electronic communications we use today, but the problem of ensuring the message could not be read by anyone other than the intended recipient remained.

\subsection*{Caesar Cipher}
\hfill \\

The Caesar cipher is a way of encrypting a message by shifting the alphabet along. For example, Alice wants to send Bob the message
\begin{center}
``Run away''
\end{center}
to Bob. They decide in advance that they are going to use a shift of 3. This means each letter gets replaced by the corresponding letter when the alphabet is shifted 3 places.
\hfill \\

\begin{center}
\scalebox{0.8}{
\begin{tabular}{l |*{26}{c}}
plaintext & A & B & C & D & E & F & G & H &I &J&K&L&M&N&O&P&Q&R&S&T&U&V&W&X&Y&Z \\
ciphertext & D & E & F & G & H &I &J&K&L&M&N&O&P&Q&R&S&T&U&V&W&X&Y&Z&A&B&C

\end{tabular}
}
\end{center}
\hfill \\

Alice's message becomes:
\begin{center}
UXQDZDB
\end{center}

The Caesar Cipher is an example of secret key cryptography also known as \textbf{symmetric key cryptography}. It uses a single key (3 in the above example) to both encrypt and decrypt the message.

Mathematically, the Caesar cipher is the addition of a fixed number modulo 26, where we encode A=1, B=2, .... , Z= 26.

In the above example, we first convert Alice's message to a string of integers
\[18\: 21\: 14\: 1\: 23\: 1\: 25\]
And then we add 3 modulo 26 to each number to get the encrypted message
\[21\: 24\: 17\: 4\: 26\: 4\: 2\]
\subsection*{Exercise}
\begin{enumerate}
\item How does Bob decrypt Alice's message?
\item Suppose instead of adding modulo 26, we want to multiply each number by a fixed number. If Alice multiplies each number by 13, can Bob decrypt the message?
\item Determine which numbers are valid keys for the multiplication algorithm and determine how to decrypt the message.
\end{enumerate}

A major problem with symmetric key cryptography is the exchange of the key. Once Alice and Bob have a key they can send encrypted messages between themselves. But how to they exchange the key itself? If they can meet up and talk about it, then why not just tell each other the message in the first place? If Alice sends Bob the key then Carol could intercept the key and subsequently read all their messages. This is the problem solved by \textbf{public key cryptography}.

\subsection*{Public key cryptography}
\hfill \\
Let's go back to Alice, Bob and their secret message. Alice has a personal padlock to which only she has the key. She can make this padlock publicly available so that anyone can put it on their message, however only she will be able to open it. Bob simply puts one of Alice's padlock on his message and sends it to her. It's safe, because no one can open it but Alice. The padlock is Alice's \textbf{public key} while the key to open it is her \textbf{private key}. The RSA algorithm is an example of public key cryptography.

Rather than a physical padlock and key, we use what is known as a \textbf{trapdoor function}. This is a function that is easy to perform in one direction but extremely difficult to reverse, unless you have some specific information.

\subsection*{Exercises}
\begin{enumerate}
\item Calculate $23 \cdot 37$.
\item Find the prime factors of 943.
\end{enumerate}

Which of the previous two exercises was easier? Now imagine we had primes with 300 digits in them! RSA is based on this principle that multiplying primes is easy, while factorising numbers into primes is very difficult.

\subsection*{RSA}
\begin{itemize}
\item Choose two large distinct primes $p$, $q$.
\item Compute $
n = p\cdot q$ and $\phi = (p-1)(q-1)$.
\item Choose an integer $e$ coprime to $\phi$ with $1<e<\phi$.
\item Find $d$, the inverse of $e \pmod \phi$, i.e. $d\cdot e \equiv 1 \pmod \phi$.
\item Throw away $p, q, \phi$. Keep $n, e, d$.
\item To encrypt a message $x < n$, calculate $y \equiv x^e \pmod n$.
\item To decrypt $y$, compute $y^d \pmod n$.
\end{itemize}
\hfill \\
The public key is $n$ and $e$. Anyone can use these numbers to encrypt a message. The private key is $d$. Only someone who knows $d$ can decrypt the message.

Now we just need to prove that this works! Does $(x^e)^d \pmod n$ give back $x$?
First we need the following result:
\begin{lemma}
Let $p, q$ be different primes. If $x \equiv y \pmod p$ and $x \equiv y \pmod q$ then $x \equiv y \pmod {p\cdot q}$
\end{lemma}

\begin{proof}
We can write $x = y+kp=y+lq$. This gives $kp = lq$. Since $p$ and $q$ are different primes $p$ divides $l$ and $q$ divides $k$. So we can write $k = k'q$ and $l = l'p$ which gives $x = y + k'pq = y + l'pq$ which means $x \equiv y \pmod {pq}$.
\end{proof}

\begin{theorem}
RSA works. That is, for distinct primes $p$ and $q$, and $d$, $e$ calculated as above, $(x^e)^d \equiv x \pmod {pq}$.
\end{theorem}
\begin{proof}
By the above lemma, we only need to check mod $p$ and mod $q$.

If $x \equiv 0 \pmod p$ we are done.

If $x$ is coprime to $p$ then by Fermat's little Theorem we have
\[
x^{ed} = x^{1+\phi k}=x^{1+(p-1)(q-1)k}=x\cdot (x^{(p-1)})^{(q-1)k}\equiv x \cdot 1 \pmod p
\]
The same works for $q$, so $x^{ed}\equiv x \pmod {pq}$.
\end{proof}

\begin{center}
	\fbox{Is RSA really safe? Can we get $d$ from knowing $n$ and $e$?}
\end{center}

We know that $d\cdot e \equiv 1 \pmod \phi$ and $\phi$ we got from the prime factors of $n$: $p$ and $q$. So if someone can find the factors of $n$ then they can find $d$. However, factorising a number with two large (very large!) prime factors is extremely difficult.

%
%Bibliography
%

\end{document}


\chapter{{\tt Cartography} Module}\label{ch:cartography-module}

[{\em Note: The documentation for this chapter is not yet complete... }]
$$
$$
The earliest robots in space were not planetary rovers -- they were
unmanned probes that studied the planetary bodies in our solar system
from afar.  Today there are roughly twenty extraterrestrial spacecraft
in active communication with earth (and only three planetary rovers), so
the bulk of the extraterrestrial data that we receive consists of
imagery that originated on round(ish) surfaces.  The natural thing to
do with this data is to merge it together into a map, but when doing
so we are faced with the same problem that has plagued cartographers
for hundreds of years: how does one flatten the globe?  This is the job
of the Vision Workbench Cartography module: to make maps.

Before diving into an introduction on planetary cartography, we will
point out another problem that is relatively new to Cartography.  The
amount of map data that we have collected about Earth and the other
bodies in our solar system is {\em immense}.  It is often impossible
to store an entire mapping data set in memory all at once, so
intelligent paging, caching, and storage strategies must be used in
order to make working with this data tractable.  For this reason, the
Cartography module is particularly powerful when used in conjunction
with Mosaic module (see Chapter \ref{ch:mosaic-module}) or the
upcoming Plate File module, which is designed to efficiently process
and combine extremely large data sets.

We will begin this chapter with a quick summary of the third party
libraries that are needed to compile the Vision Workbench.  We will
then describe the \verb#GeoReference# class, which creates a
relationship between pixel coordinates in an image and coordinates on
a globe.  Next, we will discuss the \verb#GeoTransform# class, which
provides a simple means of re-projecting map data.  We finish this
section with methods for reading and writing image files with embedded
geospatial metadata.

\section{Software Dependencies}

The Cartography module is currently built on top of two third party
libraries:

\begin{itemize}
\item GDAL   [ {\tt http://www.remotesensing.org/gdal/ } ]
\item Proj.4 [ {\tt http://proj.maptools.org/ }]
\end{itemize}

In order to enable the Cartography module, you must have these
libraries installed on your system before you configure and build the
Vision Workbench.  You may need to use the \verb#PKG_PATHS# directive
in the \verb#config.options# file if you install them in a
non-standard location as discussed in Chapter \ref{ch:gettingstarted}.

Once the library for the Cartography module has been built, the header
files for GDAL and Proj.4 are no longer needed, so you can rely solely
on linking in libraries when building your own application.

\section{The {\tt GeoReference} Class}

When you point at a location on a map, you probably want to know where
that location can be found in the real world.  This relationship
depends first and foremost on the familiar notion of a map's scale.
However, this relationship is also affected by a subtle, but extremely
important dependence on how the map is {\em projected}.  That is, the
image depicts a scene that sits on the surface of a spheroid.
However, the image is flat, so at best it represents a very slightly
distorted view of the surface.

One can imagine all sorts of different ways that the surface can be
warped or projected onto a flat plane (or, at the very least,
projecting onto a manifold that can be unfolded into a plane without
distorting distances and areas -- a sphere cannot be unfolded in this
way).  Generations of cartographers have struggled with this
topological challenge, and as a result they have developed many
different ways to ``un-fold'' the globe so that it can be represented
as a flat image.  Rather than attempt a description of these many
techniques here, we suggest you look at this excellent web site
describing all aspects of map projections.

\begin{verbatim}
  http://www.progonos.com/furuti/MapProj/CartIndex/cartIndex.html
\end{verbatim}

The Proj.4 manual is also recommended as a reference for the specific
map projections supported by the Vision Workbench.

Now would be a good time to take a break from reading this section of
the documentation to look over these references.  When you return, we
will dive into some code examples.

\subsection{The Datum}

A Vision Workbench \verb#GeoReference# object is composed of three items:

\begin{itemize}
\item {\bf The Datum}: Describes the approximate shape of the
  planetary body, as either a sphere or an ellipsoid.
\item {\bf The Projection}: As discussed above, this is the technique
  used to represent spherical coordinates in a flat projection.
\item {\bf The Affine Transform}: This is the geometric transformation
  between pixel coordinates in the image to coordinates in the map
  projection space.
\end{itemize}

\subsection{The Transformations}

Let's start by being explicit about the coordinate systems we will be
working with.  For images, we adopt the usual Vision Workbench
coordinate system wherein the upper left corner of the image is the
origin, the $u$ coordinate increases as you move right along the
columns of the image, and the $v$ coordinate increases as you move
down the rows.

For a planetary body, the coordinate of a point on the surface is
typically measured in longitude, latitude, and radius ($\theta, \phi,
r$) or in a geodetic format of longitude, latitude, and height
($\theta, \phi, h$). Lines of latitude are perpendicular to the axis
of rotation and are measured from the center line, the {\em equator}
(+/-90 degrees).  Lines of Longitude are vertical, passing through
both the North and South poles of the planet.  It is measured from a
vertical arc on the surface called the {\em meridian}.  We will
generally adopt an East positive frame of reference (latitude
increases to the east of the meridian 0-360 degrees).  The radius is
measured from the point to the planet's center of mass except in the
case of a flatten sphere. Thus the need for the alternative of height
measurements, which is the measured distance between a point and the
surface of a datum along the normal of the longitude, latitude
location.  Note that this coordinate system is similar but not
identical to spherical coordinates in a mathematical sense, where
``latitude'' would be measured from the North pole rather than the
equator.

Under this set of assumptions, if we have a pixel ($u,v$) in the image,
and we want to relate it to some planetary coordinates, we must do two
things. First we most convert the pixel measurement to units in the
projective domain. We call this domain the point measurement and its
units are defined by the projection. It can be in degrees in the case
of equirectangular projection or in terms of meters for UTM or
stereographic projection. We choose to not acknowledge this, hence the
rather non-descriptive name, point. Converting to point however is
simple and is just the forward transform through the affine
matrix. From here we must convert this point measurement to longitude
and latitude. This is achieved through the Proj4 library.

What you need to gather from the above information is that the
\verb#GeoReference# class converts measurements between 3 domains; pixel,
point, and longitude and latitude. The projection defines how a
spherical measurement is described on a plane \emph{(point
  space)}. The affine describes the translation and resolution of the
map by figuring how to convert pixels to the point measurement.

\subsection{Putting Things Together}

Creating GeoReferences is harder than reading them. Here’s how to read
and write a georeference from an image.

\begin{table}[htb]\begin{centering}
\begin{tabular}{|c|p{5 in}|} \hline
Function & Description \\ \hline \hline
\verb#read_georeference(geo,filename)# & Given a path \verb#filename#, this function will create and provide a reference to \verb#geo# a \verb#GeoReference#. \\ \hline
\verb#read_georeference(geo,rsrc)# & Given a disk image resource, \verb#rsrc#, this function will create and provide a reference to \verb#geo#. \\ \hline
\verb#read_georeferenced_image(im,geo,filename)# & Reads simultaneously both view \verb#im# and \verb#geo# from path \verb#filename#.\\ \hline
\verb#write_georeference(rsrc,geo)# & Writes \verb#geo# to a disk image resource, \verb#rsrc#. \\ \hline
\verb#write_georeferenced_image(filename,im,geo)# & Writes view \verb#im# and \verb#geo# to output file, \verb#filename#. \\ \hline
\end{tabular}
\caption{Reading and writing of {\tt GeoReference} Objects}
\end{centering}
\end{table}

We only support reading and writing of georeferenced images that the
GDAL library supports. We do however support read only of
georeferenced information from PDS images.

Once you have a GeoReference, you now have a powerful class that
allows conversion of measurements between pixel, point, and longitude
latitude measurements. Here’s a list of methods provided by
GeoReference that allow conversion:

\begin{table}[htb]\begin{centering}
\begin{tabular}{|c|p{5 in}|} \hline
Method & Description \\ \hline \hline
 & Single Vector Transforms \\ \hline
\verb#pixel_to_point(pix)# & Converts Vector2 \verb#pix# to Vector2 point. \\ \hline
\verb#point_to_pixel(pnt)# & Converts Vector2 \verb#pnt# to Vector2 pixel.\\ \hline
\verb#point_to_lonlat(pnt)# & Converts Vector2 \verb#pnt# to Vector2 longitude latitude.\\ \hline
\verb#lonlat_to_point(ll)# & Converts Vector2 \verb#ll# to Vector2 point.\\ \hline
\verb#pixel_to_lonlat(pix)# & Converts Vector2 \verb#pix# to Vector2 longitude latitude.\\ \hline
\verb#lonlat_to_pixel(ll)# & Converts Vector2 \verb#ll# to Vector2 pixel.\\ \hline
 & Bounding Box Transforms \\ \hline
\verb#pixel_to_point_bbox(pix)# & Converts BBox2i \verb#pix# to BBox2 point.\\ \hline
\verb#point_to_pixel_bbox(pnt)# & Converts BBox2 \verb#pnt# to BBox2i pixel.\\ \hline
\verb#point_to_lonlat_bbox(pnt)# & Converts BBox2 \verb#pnt# to BBox2 longitude latitude.\\ \hline
\verb#lonlat_to_point_bbox(ll)# & Converts BBox2 \verb#ll# to BBox2 point.\\ \hline
\verb#pixel_to_lonlat_bbox(pix)# & Converts BBox2i \verb#pix# to BBox2 longitude latitude.\\ \hline
\verb#lonlat_to_pixel_bbox(ll)# & Converts BBox2 \verb#pll# to BBox2i pixel.\\ \hline
\end{tabular}
\caption{Methods for transforming measurements in {\tt GeoReference}.}
\end{centering}
\end{table}

Say however you want to convert a geocentric measurement
\emph{(longitude, latitude, radius)} or geodetic measurement
\emph{(longitude, latitude, height)} to cartesian space? This is
provided for you in the Datum class, which is used by the
\verb#GeoReference# class. Getting access to it is as simple as
calling the \verb#datum()# method of \verb#GeoReference#. From there
you are given access to the following transforms of \verb#Datum#:

\begin{table}[htb]\begin{centering}
\begin{tabular}{|c|p{5 in}|} \hline
Method & Description \\ \hline \hline
\verb#radius(lon,lat)# & Returns double, in units of datum, the geodetic radius.\\ \hline
\verb#radius_of_curvature(lon,lat)# & Returns double, the radius of curvature in the prime vertical.\\ \hline
\verb#geocentric_latitude(lat)# & Returns double, geocentric latitude corresponding to geodetic latitude, \verb#lat#.\\ \hline
\verb#geocentric_radius(lon,lat,alt)# & Returns double, return distance from the center of the planet.\\ \hline
\verb#geodetic_to_cartesian(llh)# & Returns Vector3 in cartesian, given geodetic Vector3 \verb#llh#.\\ \hline
\verb#cartesian_to_geodetic(xyz)# & Returns Vector3 in geodetic, given cartesian Vector3 \verb#xyz#.\\ \hline
\end{tabular}
\caption{Methods for transforming measurements in {\tt Datum}.}
\end{centering}
\end{table}

Notice that we only provide cartesian transforms for geodetic
measurements. Geodetic measurements are safe for both flattened and
spherical datums alike. Geocentric to Cartesian is much simpler and
from its name you can tell that it only supports spherical datums. If
you wish to convert between geocentric measurements and cartesian, you
have two options which are listed below. Remember, these are for
geocentric measurements only!

From {\it SimplePointImageManipulation.h}:
\begin{verbatim}
  Vector3 llr(30,49,173700);
  Vector3 xyz = cartography::lon_lat_radius_to_xyz( llr );
  llr = cartography::xyz_to_lon_lat_radius( xyz );
\end{verbatim}

From {\it Datum.h}:
\begin{verbatim}
  Vector3 xyz =ç∂
    datum.geodetic_to_cartesian( geocentric -
                                 Vector3(0,0,datum.radius(geocentric[0],
                                                          geocentric[1]) ) );
\end{verbatim}

%% - The GeoReference Object
%%   - Creating
%%     - proj4str
%%     - datum and transform
%%   - << streaming
%%   - Setting projections

\begin{table}[t]\begin{centering}
\begin{tabular}{|l|l|l|} \hline
Method & Description \\ \hline \hline
\verb#set_equirectangular()# & Equirectangular Projection \\ \hline
\verb#set_sinusoidal()# & Sinusoidal Projection \\ \hline
\verb#set_mercator()# & Mercator Projection \\ \hline
\verb#set_orthographic()# & Orthographic Projection \\ \hline
\verb#set_stereographic()# & Stereographic Projection \\ \hline
\verb#set_oblique_stereographic()# & Oblique Stereographic Projection \\ \hline
\verb#set_lambert_azimuthal()# & Lambert Azimuthal Projection \\ \hline
\verb#set_lambert_conformal()# Lambert \emph{(Conic)} Conformal Projetion \\ \hline
\verb#set_UTM()# & Universal Transverse Mercator (UTM) Projection (Earth only) \\ \hline
\end{tabular}
\caption{Currently supported {\tt GeoReference} map projections.}
\label{tbl:georeference-map-projections}
\end{centering}\end{table}

\section{Geospatial Image Processing}
\subsection{The {\tt GeoTransform} Functor}
%%     - Use to reproject maps
%%     - Formulate a source and destination \verb#GeoReference# object
%%     -
%%   - \verb#xyz_to_latlon()#

\section{Georeferenced File I/O}
\subsection{{\tt DiskImageResourceGDAL}}


\section{Discussion}
\label{sec:discussion}

In this chapter, we've focused on why causal graphs are important, how to create them, how to test them, and on how to use them in applied problems with latent confounding.
Our latent confounding example showed that statistical inference of our models' parameters may still be a challenge, even with a correct causal graph.
Such challenges lead us to the following set of post-graph-construction topics:
\begin{itemize}
   \item model estimation
   \item model checking
   \item experimental design
   \item experiment analysis
   \item decision analysis
\end{itemize}
To us, each item above is important for getting credible results from our analysis and for maximizing our interventions' benefits.
Accordingly, even though we will neglect details due to space and time constraints, we briefly discuss these topics below.

\subsection{Analysis}
\label{sec:discussion-analysis}
Perhaps most obviously, after creating and testing a causal graph, we will use it to estimate the effects of our interventions.
To compute our effect estimates, we will need to evaluate statements such as the probability of a particular node taking a given value, conditional on the values of that node's parents.
These probabilities will come from our estimated models, so model estimation is critically important to our effect estimation.
Thankfully, this is the part of causal inference that choice \textit{modellers} are most familiar with.
For instance, \citet{kostic_2020_uncovering} first use the PC and GES causal discovery algorithms to generate a causal graph, then they test the graph qualitatively, and finally they estimate models corresponding to this causal graph.
As another example, \citet{garrido_2020_estimating} start from where we end: at a known (or selected) causal graph.
They then use neural network density estimators to model the necessary probabilities for estimating one's causal effects of interest.

Next, after estimating the models for our causal graph but before interpreting or using our results, we should check our entire system of models.
Here, there are multiple, complementary ways of performing these diagnostics.
We can check our models separately, jointly, or in subsets.
For thoroughness' sake, we can even perform all these checks instead of one kind.

From a disaggregate perspective, we can consider a sequential application of model checking exercises, one per estimated model.
Ideally, each model checking process will include the use of visual diagnostics, as (for example) described in \citet{brathwaite_2018_check}.
Alternatively, we can check subsets of models together instead of checking one model at a time.
For example, \citet{tran_2016_model} jointly check their models for all variables that their intervention will set (i.e. the treatment assignment variables), and then they separately check their outcome variable models.
Finally, we can check all our models jointly by defining global diagnostic measures over all nodes in our causal graph.
See \citet{williamson_2013_model} for a demonstration.

\subsection{Experimentation}
\label{sec:discussion-experimentation}
Following model diagnostics, we are ready to use our models and causal graph to inform real interventions.
These interventions can come in two kind: an experiment or a ``full-scale'' implementation of one's policy.
If our intervention is experimental, then we are likely interested in one of two aims.
We either want to decide between one or more treatment options, or we want to learn about our system, though not necessarily to make a decision.
In both cases, however, we pay great attention to the design of our experiments.

When experimenting to make decisions, such as whether to launch a given treatment or not, we pay extra attention to the size of our experiment.
Specifically, we want our experiment's sample size to be large enough such that after we update our beliefs using the experimental data, that we have at least our minimum desired probability of making the correct decision.
The decision can be to declare the effect of a treatment statistically different from zero, but more frequently, the decision will be more fundamental such as ``implement treatment A.''
For thorough explanations of how to conceptualize and design experiments in a Bayesian, model-based setting, see \citet{chaloner_1995_bayesian} and \citet{wang_2002_simulation}.
For examples and guidance on how to use one's causal graph structure to guide the general design of one's experiment, beyond sample size, see \citet{madrigal_2007_cluster}.
There, the structure of one's causal graph is used to inform general design decisions such as the clustered allocation of individuals to treatment, and the experimental design is itself analyzed graphically.
Additionally, note the relations to reinforcement learning where an agent has to perform experiments in order to discover the action/intervention that will maximize her expected, counterfactual reward.
In this context, \citet{lee_2018_structural} have shown that designing our experiments without guidance from one's causal model is generally suboptimal, and that we can achieve optimality by leveraging our causal graph to design our experimentation plan.

Now, let's transition from experimentation for decision making to consider experimentation for learning.
Imagine that we are at a transportation network company and that we are running a pricing experiment to learn about price elasticities of our customers.
Here, there is no immediate decision being made, but we learn about an edge in our causal graph: the edge from price of a trip (treatment) to purchase of the ride (outcome).
In other cases, we may experiment to learn not just about the strength of an edge, but about the presence of edges and the structure of the graph more generally.
For example, we may wish to remove residual ambiguity from a causal discovery process that outputs a Markov Equivalence Class of graphs instead of a single causal DAG.
In these situations, we are interested in optimally designing an experiment (or series of experiments) to learn a causal graph (or its properties).
We are further interested in how we can leverage potentially multiple experimental datasets to improve our causal graphs.
For a review of the literature on experimentation for learning and construction/refinement of a preliminary causal graph, see \citet{hyttinen_2013_experiment} and \citet[Sec. 3.1.2]{kalisch_2014_causal}.
For more recent approaches in this vein, see works such as \citet{triantafillou_2015_constraint, kocaoglu_2017_experimental, brouillard_2020_differentiable, rantanen_2020_learning}.

\subsection{Decision}
\label{sec:discussion-decision}

Finally, after we have run any experiments that we are interested in, we still must decide how to intervene in our population.
Three major types of questions come to mind immediately:
\begin{enumerate}
   \item Should we launch the treatment(s) at all?
   \item Should we launch the treatments to everyone?
   \item How should the treatments be dispersed/implemented?
\end{enumerate}

After updating our posterior beliefs with the experimental data, we'll want to analyze and come to a conclusion about launching our treatment(s).
In particular, we will wish to determine the expected distribution of impacts under each alternative decision.
Here, \citet{manski_2019_treatment} should provide the basic idioms of thought and pointers to the larger literature on treatment choice from a decision theoretic perspective.

Next, if we've decided to launch the treatments, we come to the question of who should receive the treatment(s)?
Everyone?
A select few?
Are there certain subgroups that should receive the treatment but not others?
These questions fundamentally revolve around the level and nature of heterogeneity in treatment effects.
We will, with good reason, want to search for evidence of heterogeneity and characterize it if found.
In doing so, we should consult articles such as \citet{pearl_2017_detecting} and \citet{webster_2020_subgroup} for guidance on how to perform one's subgroup analysis in light of one's causal graph.
This should help us avoid drawing incorrect conclusions or misinterpreting our analyses.

Thirdly, we will need to answer logistical questions about the levels and the frequency of treatment.
With regard to choosing the levels of (possibly continuous and multiple) treatments, recent work on causal Bayesian optimization represents the state of the art in this area \citep{aglietti_2020_causal}.
Moreover, the entire field of reinforcement learning focuses on running experiments and learning from past observations to determine the treatment arms/levels that will maximize one's reward (however we define it).
Accordingly, we stand to gain much by consulting the work on and principles from causal reinforcement learning (c.f. \citet{bareinboim_2015_bandits}) when choosing our optimal treatment plan.