\documentclass[a4paper]{article}

\usepackage{amsmath, amssymb}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{pgfplots, pgfplotstable}

\def\mean#1{\left< #1 \right>}
\sisetup{
    round-mode=places,
    round-precision=1
}

\title{Investigating the Effect of String Tension on Fundamental Frequency}
\date{18 March 2015}
\author{Tarik Onalan}

\begin{document}
    \maketitle
    \section{Introduction}
        \subsection{Purpose}
            Understand how string tension affects the fundamental frequency of the
            string.
        \subsection{Hypothesis}
            As tension increases, the fundamental frequency of the string will also
            increase.
        \subsection{Variables}
            \textbf{Independent Variable}
            \begin{itemize}
                \item Tension [\si\N]
            \end{itemize}
            \textbf{Dependent Variable}
            \begin{itemize}
                \item Fundamental frequency [\si\hertz]
            \end{itemize}
            \textbf{Controlled Variables}
            \begin{itemize}
                \item String length [\SI{0.50}{\m}]
                \item String mass [\SI{0.085}{\kg}]
                \item Type of string
            \end{itemize}
    \section{Materials}
        \begin{itemize}
            \item 1 $\cdot$ \SI{0.50}{\m} string
            \item 1 $\cdot$ pitch sensor
            \item 1 $\cdot$ spring scale
        \end{itemize}
    \section{Procedure}
        \begin{enumerate}
            \item Turn on pitch sensor
            \item Tighten string to \SI{0}{\N}
            \item ``Pluck'' string
            \item Record frequency
            \item Repeat steps 2-4 with \SI{5}{\N}, \SI{10}{\N}, and \SI{15}{\N}
            \item Repeat steps 2-5 as necessary for data collection
        \end{enumerate}
    \section{Data}
        \begin{center}
            Table 1: Raw Data:
            \pgfplotstabletypeset[
                multicolumn names,
                col sep=comma,
                display columns/0/.style={
                    column name=Tension,
                    column type={S},string type},
                display columns/1/.style={
                    column name=Trial 1,
                    column type={S},string type},
                display columns/2/.style={
                    column name=Trial 2,
                    column type={S},string type},
                display columns/3/.style={
                    column name=Trial 3,
                    column type={S}, string type},
                display columns/4/.style={
                    column name=Average,
                    column type={S}, string type},
                columns/t/.append style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={}{$\pm0.1$}
                    }
                },
                columns/1/.append style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={}{$\pm0.1$}
                    }
                },
                columns/2/.append style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={}{$\pm0.1$}
                    }
                },
                columns/3/.append style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={}{$\pm0.1$}
                    }
                },
                columns/avg/.append style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={}{$\pm0.1$}
                    }
                },
                every head row/.style={
                    before row={\toprule},
                    after row={
                         & \si\s & \si\s & \si\s & \si\s\\
                        \midrule}
                },
                every last row/.style={after row=\bottomrule}
            ]{foobar.csv}
        \end{center}
        \begin{tikzpicture}
            \begin{axis}[
                scale=1.75,
                title={Fundamental Frequency Relative to String Tension},
                xlabel={Tension [\si\N]},
                ylabel={Fundamental Frequency [\si\hertz]},
                legend pos=south east
            ]
                \addplot[
                    color=blue,
                    only marks
                ] plot [
                    error bars/.cd,
                        x dir=both,
                        y dir=both,
                        x explicit,
                        y explicit,
                        x fixed=0.1,
                        y fixed=0.1
                ] table [
                    x=t,
                    y=avg,
                    col sep=comma
                ]{foobar.csv};
                \addplot[
                    color=red,
                    mark=none,
                    very thick,
                    domain=0:15
                ]{1.97318355*sqrt(1.50045554*x)};
                \addlegendentry{Average}
                \addlegendentry{$1.97\sqrt{1.50x}$}
            \end{axis}
        \end{tikzpicture}
    \section{Calculations}
        \subsection{Average}
            \begin{equation}
                \mean{\si\hertz(\si\N)}
            \end{equation}
            \begin{equation}
                \frac{\displaystyle\sum_i{\si\hertz(\si\N)}}{i}
            \end{equation}
            \begin{equation}
                \frac{\si\hertz(\si\N_1)+\si\hertz(\si\N_2)+...+\si\hertz(\si\N_{i-1})+\si\hertz(\si\N_i)}{i}
            \end{equation}
        \subsection{Error and Minimization}
            \begin{center}
                Error was calculated using sum squared error, which is defined as follows:
                \begin{equation}
                    E=\displaystyle\sum_{i=1}^n{(y_i-f(x_i))^2}
                \end{equation}
                A program carried out the minimization of the above function, which entails
                tracing the derivative of the function with respect to a given parameter
                $k \in \{a, b, c, d, ...\}$ to the local minima of the function
                \begin{equation}
                    E'=\frac{\partial}{\partial k}(\displaystyle\sum_{i=1}^n{(y_i-f(x_i))^2})
                \end{equation}
            \end{center}
        \subsection{Line of Best Fit}
            \begin{center}
                When choosing a model for my data, I had to keep in mind the basic
                features.  I knew that the tension and fundamental frequency were related
                by the function
                \begin{equation}
                    ?(x)=\frac{\sqrt{\frac{T}{m/L}}}{2L}
                \end{equation}
                where $T$ is tension, $m$ is mass, and $L$ is length. Because of this,
                I assumed my data would also follow a square-root function.
                \\
                I constructed my function like so:
                \begin{equation}
                    f(x)=a\sqrt{bx+c}+d
                \end{equation}
                As a control, I also constructed a linear function:
                \begin{equation}
                    g(x)=a(bx+c)+d
                \end{equation}
                As I had predicted, the square-root function yielded much less error
                when modeling the data, which is why I chose it as my model. The errors
                for the square-root and linear functions are shown below:
                \\
                \bf{Error}
                \\
                \begin{tabular}{|c|c|}
                    \hline
                    $f(x)$ & $g(x)$
                    \\\hline
                    $\num[round-precision=5]{0.00471860747464}$ & $\num[round-precision=5]{3.94833443333}$
                \end{tabular}
            \end{center}
    \section{Conclusion}
        My hypothesis, that the fundamental frequency of the string would increase as the
        tension of the string increased, was correct. The graph of the data shows
        a marked upward trend. When there was \SI{0.0}{\N} of force on the string, the
        fundamental frequency was \SI{0.0}{\hertz}. However, when the tension was increased
        to \SI{15.0}{\N}, the fundamental frequency increased to \SI{9.3}{\hertz}. Also,
        given that the data very closely matches the square-root used to model it, and
        the square root function is a monotonically increasing function, it would be
        safe to assume that the relationship shown by the data, that the fundamental
        frequency increases as string tension increases, is true.
        \\
        A difficulty I experienced with this lab was keeping the spring scale still
        while conducting a trial. While I did have it attached to an immobile object,
        the scale was prone to slippage, and would occasionally slacken the string.
        This is slightly shown in the data, where there are dips in the fundamental
        frequency (see \SI{5}{\N} trial 2, \SI{10}{\N} trial 1, \SI{15}{\N} trial 2).
        However, these errors are not large enough to nullify my conclusion.
        \\
        In the future, I would like to be able to conduct more trials, so any errors
        like the one described above could be ``averaged away'', per se. While the
        errors in my data were not particularly large, it would have been better to
        conduct more trials to see exactly where the fundamental frequency was.
\end{document}


\chapter{Development Plan}
\label{ch:developmentplan}

\begin{wrapfigure}{r}{0.3\textwidth}
  \vspace{-40pt}
  \begin{center}
    \includegraphics[width=0.25\textwidth]{graph/phoalgorithm}
  \end{center}
  \caption{Toolkit Outline}
  \vspace{-90pt}
\end{wrapfigure}

This provides a general outline of the Photometry Toolkit and how I
currently envision it working.

\section{Algorithm Outline}

\begin{enumerate}
\item Stereo Processing
\item Initialize Photometry
  \begin{itemize}
    \item \textbf{Initialize DEMs and Blend} \hfill \\
      This should be done with the already provided The DEM variance found is optional written here.
      \texttt{image2plate} and \texttt{platereduce}.
    \item \textbf{Ingest DRGs} \hfill \\
      This involves thresholding the DRG for shadows and then solves
      for a grassfire weighting. The weighting and the masking are the
      same thing and should be stored inside the alpha channel for
      quick access. The masks used is optional written.
    \item \textbf{Seed Reflectance Images} \hfill \\
      Load up camera models, and use SPICE to determine Sun's
      location.
    \item \textbf{Seed Exposure Time} \hfill \\
      Somehow uses ratio of reflectance returns from previous step.
    \item \textbf{Seed Albedo Image} \hfill \\
      Use this equation:
      \[
      A_{ij}=\sum_{k}\frac{I^{k}_{ij}}{R^{k}_{ij}T^{k}}
      \]
  \end{itemize}
\item Iterate Solution
  \begin{itemize}
    \item \textbf{Re-estimate Exposure} \hfill \\
      \[
      \hat{T^{k}} = T^{k}+\frac{\sum_{ij}(I^{k}_{ij}-T^{k}A_{ij}R^{k}_{ij})A_{ij}R^{k}_{ij}S^{k}_{ij}}{\sum_{ij}(A_{ij}R^{k}_{ij}S^k_{ij})^{2}}
      \]
    \item \textbf{Re-estimate Albedo} \hfill \\
      \[
      \hat{A_{ij}} = A_{ij}+\frac{\sum_{k}(I^k_{ij}-T^kA_{ij}R^k_{ij})T^kR^k_{ij}S^{k}_{ij}}{\sum_{k}(T^kR^k_{ij}S^k_{ij})^2}
      \]
    \item \textbf{Re-estimate DEM} \hfill \\
      \emph{MAGIC!}
    \item \textbf{Re-calculate Reflectance} \hfill \\
      Refer back to the cameras.
    \item \textbf{Calculate Error} \hfill \\
      Error is used to determine if we're improving.
      \[
      \epsilon=\sum_{k}\sum_{ij}((I^k_{ij}-A_{ij}T^kR^k_{ij})S^k_{ij})^2
      \]
  \end{itemize}
\end{enumerate}

\section{Content of Files}

These files represent the results and working files from one session
of the Photometry Toolkit. One project is inside a directory
masquerading as file with extension \texttt{.ptk}. Here is a list of
the possible contents contents. These files are only created one need.

\begin{itemize}
  \item \textbf{photometrytk.dat} \hfill \\
    This is a binary file written with google protobuf. This contains
    all the meta information about a PhoTK project. It is also what is
    served and modified as a service. The reason for this extra
    complexity is that multiple processes on multiple machines will be
    trying to access this file.
  \item \textbf{photometrytk.bak} \hfill \\
    Back up file of \texttt{photometrytk.dat} that is written
    periodically in the case of a fault. Users can rename this file to
    \texttt{photometrytk.dat} if they think the original file is
    corrupt.
  \item \textbf{Albedo.plate} \hfill \\
    Is a PixelGrayA<uint8> platefile. This contains the final result of
    a mosaic.
  \item \textbf{DEM.plate} \hfill \\1
    Is a PixelGrayA<int16> platefile. This contains the input DEM
    platefile. \emph{There is currently not a complete idea about how this
    is to be made or used by PhoTK.}
  \item \textbf{DEMVariance.plate} \hfill \\
    Is a PixelGrayA<int16> platefile. \emph{This is still in design.}
  \item \textbf{DRG.plate} \hfill \\
    Is a PixelGrayA<uint8> platefile. It contains the input images.
  \item \textbf{Reflectance.plate} \hfill \\
    Is a PixelGrayA<uint8> platefile. \emph{This is still in design.}
  \item \textbf{Error.plate} \hfill \\
    Is a PixelGrayA<uint8> platefile. \emph{This is still in design.}
\end{itemize}

The platefiles Albedo, DEM, and DEMVariance are very similar and
operate like normal plates do. These files are global image mosaics
where the highest transaction ID represents the current working
version.

%% DEMVariance and Mask are both helpful plate files that are
%% used for reporting; but they're not used again after the
%% initialization step.

The platefiles DRG, Reflectance, and Error are a little more
tricky. They represent multiple K Cameras and their history. Their
transaction IDs involve a little math so that $ID_{transaction} =
100*k+i$. Where $k$ is the camera number and $i$ is the iteration
number. We provide only enough room for a hundred transactions.

%% There's also one other file, the \emph{PhoFile} which contains most of
%% the project information. If your familiar with the original Photometry
%% module, this is the model params and the global params stitched
%% together. I imagine this this file will probably use
%% proto-buffers. Here's an outline of the contents.

%% \begin{itemize}
%%   \item PhoFile \hfill \\
%%     \begin{description}
%%       \item[Project Message] \hfill \\
%%         This contains Datum along with various information transaction
%%         IDs.
%%       \item[Repeated Camera Message] \hfill \\
%%         This contains the exposure time along with the Vector3 for the
%%         Sun Position and the Spacecraft Position.
%%     \end{description}
%% \end{itemize}

\section{Executables}

%% \begin{wrapfigure}{r}{0.5\textwidth}
%%    \begin{center}
%%      \includegraphics[width=0.5\textwidth]{graph/exec_init}
%%    \end{center}
%%    \caption{Initialization Executables}
%% \end{wrapfigure}

Everything inside the Photometry Toolkit will hopefully fit
inside 2 python scripts, \texttt{phoinit.py} and
\texttt{phosolve.py}. As expected, \texttt{phoinit.py} creates all the
files to start with. \texttt{phosolve.py} performs all the
iterations. In the end when the user wants the results, they must
perform \texttt{snapshot} or \texttt{plate2tile} to get the results.

At this time of writing, only \texttt{phosolve.py} has been
written. Everything is still largely manual and requires some thought
by the user. The rest of this section outlines how to do some tasks
that will hopefully add up to a full PhotometryTK session.

\subsection{Creating a mosaicked DEM}

This step is not currently used, but I thought it was worth noting as
there are a few tricks. Mosaicking images in platefiles,is currently
done with a utility called \texttt{platereduce}. It is good for
calculating weighted averages or weighted variances. Calculating
variances is mostly just useful for calculating DEM precision. If you
are master of \texttt{snapshot}, you'll find that using
\texttt{platereduce} to be very similar.

I assume you have a working \texttt{index\_server} setup. The first
steps are to apply \texttt{grassfirealpha} to all input images. This
creates an alpha channel with feathered weights. The weights are
highest value in the center of the image and lowest value near the
edge. After that we'll enter all the alpha'd images into a single
platefile. I'm using \texttt{xargs} to make this step multiprocess so
I can utilize my entire machine. The \texttt{-P} option is the number
of simultaneous processes to have running. Scale that setting to the
appropriate value for your machine.

\begin{verbatim}
  > echo *DEM.tif | xargs -n1 -P15 grassfirealpha
  > echo *grass.tif | xargs -n1 -P15 image2plate -o pf:///index/DEM_blend.plate \
    --file tif -m equi
\end{verbatim}

Notice on \texttt{image2plate} that I set the output platefile to a
TIF. This is important as the default option of PNG will shave bits on
alpha'd values. I'm also using the 'equi' output option for an
equirectangular projection. The default is to use a TOAST projection.

We are now ready to run \texttt{platereduce}. The options
\texttt{begin\_transaction} and \texttt{end\_transaction} specify the
range of transaction IDs to read as input. The \texttt{transaction-id}
option specify the output transaction ID to use.

Reducing plate can be a very slow task, because of this we'll be doing
it in parallel with multiple commands. This requires us to tell the
\texttt{index\_server} by hand when we are starting a transaction and
ending one. Notice how this is done if the following example.

\begin{verbatim}
  > platereduce pf:///index/DEM_blend.plate -t 2000 --start "Averaging DEMs"
  > for i in {0 .. $(NUM_LEVELS)}; do platereduce --end_t 1999 -t 2000
       pf:///index/DEM_blend.plate -f WeightedAvg & done
  > platereduce pf:///index/DEM_blend.plate -t 2000 --finish
\end{verbatim}

At this point the averaged DEM should be in transaction ID 2000 and it
averaged all transaction IDs between 0 and 1999.

\subsection{Creating a PhotometryTK project}

Creating a project is done with the command \texttt{phoinitfile}. With
this command you set what reflectance type you are solving for. You
also specify which map projection to use for the project file. Current
options are TOAST, Plate Equirectangular, and Polar. You also specify
the number iterations to use for solving for the solution
here. \emph{The number of iterations is not changeable after this
  step.}

\begin{verbatim}
  > phoinitfile usgspolar -m polar --max 100
\end{verbatim}

At this point we are ready to start serving the project out. This is
accomplished with the utility called \texttt{ptk\_server}. The PTK
server is both the platefile index server and PhoTK project file
server. \emph{You will not be able to run another instance reliably of
  \texttt{index\_server} at the same time as \texttt{ptk\_server}.} It
is possible that you might have an exchange name
collision. \emph{Currently, PhoTK does not support the use of ZMQ
  urls.}

\begin{verbatim}
  > ptk_server usgspolar.ptk --url pf:///polar
\end{verbatim}

The PTK server only serves one project at a time, therefor the first
argument is the input project file \emph{(that is secretly a
  directory)}. The second argument specify the URL prefix we will be
using for this project. A later points we will be needing our
project's URL. It is the combination of the URL prefix and then the
project file's name. For this example it is: \texttt{pf:///polar/usgspolar.ptk}

At this point we are ready to start inserting our input images into
the project. This is done with \texttt{phodrg2plate}. This is a clone
of \texttt{image2plate} but it gets most of its settings from the
served project file. Here's an example of me inserting files.

\begin{verbatim}
  > echo *.tif | xargs -n1 -P15 phodrg2plate pf:///polar/usgspolar.ptk
\end{verbatim}

If you look inside the project file, you'll see that the platefile
\texttt{DRG.plate} has been created.

\subsection{Solving a PhotometryTK project}

%%\begin{wrapfigure}{r}{0.4\textwidth}
  \begin{center}
    \includegraphics[width=0.4\textwidth]{graph/exec_solve}
  \end{center}
%%\caption{Iteration Executables}
%%\end{wrapfigure}

Running the actually iterations of solving is pretty easy since it is
currently hidden away in a python script. The file script
\texttt{phosolve.py} does all the steps of re-estimating exposure and
updating albedo. In the future, calculating reflectance and
shape-from-shading will be inserted as optional steps in this file.

\begin{verbatim}
  > phosolve.py --threads 15 --level 9 --iter 100 pf:///polar/usgspolar.ptk
\end{verbatim}

The level option is just what level of the image pyramid we wish to
perform our calculations. The lower the number \emph{(the higher up
  the pyramid)} will increase the speed at which things are
processed. However there is more likely a chance of error. I usually
run my sessions at about two thirds down the pyramid.

After this step is completed you will find that \texttt{Albedo.plate}
has been created in your project file. Viewing it can be
tricky. \emph{(In my example, the Albedo's url will be
  pf:///polar\_index/Albedo.plate.)} However you will find that only
one level of the pyramid has actually be populated. That makes it
difficult to locate that solution. Things are easier for the viewer if
you mipmap the solution up the pyramid.

\begin{verbatim}
  > pmipmap --level 9 --threads 15 pf:///polar_index/Albedo.plate
\end{verbatim}

Viewing should now be much easier in VWV.

\begin{verbatim}
  > vwv pf:///polar_index/Albedo.plate
\end{verbatim}

\subsection{Using ZMQ instead}

The above examples are using RabbitMQ for the message passing. I've
since become a fan of using ZeroMQ as it doesn't require a server to
be started. Remember that RabbitMQ has an erlang executable running in
the background that occasionally crashes after a length of time.

So here's the above examples rewritten to show how to use ZMQ, however
not everything works at this point in time.

\begin{verbatim}
  > phoinitfile secret_base -m polar --max 100
  > ptk_server secret_base.ptk --url zmq://*.5566

In new terminal:
  > parallel -j -2 phodrg2plate zmq://localhost:5566/secret_base.ptk ::: *.tif
  > phoitalbedo zmq://localhost:5566/secret_base.ptk -l 10
\end{verbatim}

Unforunately, \texttt{vwv} across ZMQ doesn't seem to be working at all. For
the time being I just kill the \texttt{ptk\_server} and run \texttt{vwv} on the
platefile directly.

\subsection{Running once at high resolution and extracting result}

Meh.

%% \subsection{An example run of PhotometryTK}

%% Eventually the the python scripts should negate the need for
%% this. Here are the actual executables called and the settings to
%% use. Delete this section some time in the future.

%% \begin{verbatim}
%%   > sudo $(START UP AMQP COMMAND)
%%   > (in a different terminal) index_server $(PROJECT DIR)
%%   > cd $(DEM_DIR)
%%   > ls *DEM.tif | xargs -n 1 -P 10 grassfirealpha --nodata -10000
%%   > ls *grass.tif | xargs -n 1 -P 10 image2plate -o
%%       pf://index/DEM_blend.plate --file tif -m equi
%%   > for i in {0..$(NUM LEVELS)}; do platereduce --end_t 1999 -t 2000
%%       pf://index/DEM_blend.plate -l $i & done
%%   > plate2plate -o pf://index/DEM.plate -i pf://index/DEM_blend.plate
%%       --filter identity --bottom 10 --skim
%%   > (in other terminal, kill index_server)
%%   > rm -rf DEM_blend.plate
%%   > (in other terminal, restart index_server)
%%   > phoinitfile debug_apollo
%%   > // Below can be ran in parallel
%%   > echo *.tif | xargs -n1 echo |
%%       xargs -n1 -P10 phodrg2plate pf://ptk/project.ptk
%%   > echo {0..9} | xargs -n1 echo |
%%       xargs -n1 -P10 -I{} phoitalbedo -j {} -n 10 pf://ptk/project.ptk
%%   > mipmap pf://index/Albedo.plate
%% \end{verbatim}


\documentclass[12pt]{article}
\usepackage{fullpage,enumitem,amsmath,amssymb,graphicx,amsthm}

\begin{document}

\begin{center}
{\Large CS221 Fall 2017 Homework [Foundations]}

\begin{tabular}{rl}
Name: & yf-yang
\end{tabular}
\end{center}

By turning in this assignment, I agree by the Stanford honor code and declare
that all of this is my own work.

\section*{Problem 1}

\begin{enumerate}[label=(\alph*)]
  \item
    Let $\theta^{\ast} = \underset{\theta}{arg\,min} f(\theta)$, then
    \begin{equation}\frac{\partial f(\theta^{\ast})}{\partial \theta^{\ast}} = \sum_{i=1}^{n}{w_i(\theta^{\ast} - x_i)} = 0\end{equation}
    \begin{equation}\theta^{\ast}\sum_{i=1}^{n}{w_i} = \sum_{i=1}^{n}{w_i x_i}\end{equation}
    \begin{equation}\theta^{\ast} = \frac{\sum_{i=1}^{n}{w_i x_i}}{\sum_{i=1}^{n}{w_i}}\end{equation}
    Without loss of generality, we can assume $\sum_{i=1}^{n}{w_i}=1$, then
    \begin{equation}\theta^{\ast} = \mathop{\mathbb{E}_{\mathbf{w}}}[\mathbf{x}]\end{equation}
    In order to prove $\theta^{\ast}$ is a global minimum, we need to show
    \begin{equation}\frac{\partial^2 f(\theta^{\ast})}{\partial {\theta^{\ast}}^2} = \sum_{i=1}^{n}{w_i}\end{equation}
    Note that Eqn(5) is strictly positive if all $w_i$ are positive, so $\theta^{\ast}$ should be a global minimum, while when some $w_i$ is negative, $sum_{i=1}^{n}{w_i}$ may be negative, then the global minimum doesn't hold. \qed

  \item
    \begin{equation} f(\mathbf{x})=\sum_{i=1}^{d}{\max_{s\in \{-1,1\}}{s x_i}} = \sum_{i=1}^{d}{{\lvert}x_i{\rvert}} \end{equation}
    \begin{equation} g(\mathbf{x})=\max_{s\in \{-1,1\}}{\sum_{i=1}^{d}{s x_i}} = \max_{s\in \{-1,1\}}{s \sum_{i=1}^{d}{x_i}} = {\lvert}\sum_{i=1}^{d}{x_i}{\rvert} \end{equation}
    According to Triangular Inequality, we can conclude that $f(\mathbf{x}) > g(\mathbf{x})$ \qed

  \item
    Let $\mathbf{X_i} =$ (number of points you roll the i-th time), $\mathbf{N} =$ (number of rolls), then
    \begin{align*} \mathop{\mathbb{E}}[\sum_{i=1}^{\mathbf{N}}{\mathbf{X_i}}] &= \sum_{n=0}^{\infty}f_{\mathbf{N}}(n+1)\mathop{\mathbb{E}}[\mathbf{X}|n+1] \\
    &= \sum_{n=0}^{\infty}\frac{1}{6}(\frac{5}{6})^n\frac{b-a}{5}n \\
    &= \frac{b-a}{30}\sum_{n=0}^{\infty}n(\frac{5}{6})^n \\
    &= b-a\end{align*}  \qed

  \item
    Let $M(p) = \log{L(p)}$, then
    \begin{equation}M(p) = 4\log{p}+3\log{(1-p)}\end{equation}
    At $M(p)$'s maximum, which is also $L(p)$'s maximum we have
    \begin{equation}\frac{\partial{M(p)}}{\partial p}=\frac{4}{p}-\frac{3}{1-p}=0\end{equation}
    \begin{equation}p = \frac{4}{7}\end{equation}
    Intuitively, in order to get 4 Heads in 7 flips with max probability(on average $\frac{4}{7}$ Head in one flip), we should let the probability of Heads in one flip, which is the Expectation of Heads in one flip be exactly $\frac{4}{7}$(because $L(p)$ is a continuous function and Expectation of Heads in one flip should be where the probability is the maximum. \qed

  \item
    \begin{equation}\frac{\partial f}{\partial w_k} = \sum_{i=1}^{n}\sum_{j=1}^{n}2(a_{ik}-b_{jk})(\mathbf{a_i^\top w-b_j^\top w})+2\lambda w_k\end{equation}
    \begin{equation}\frac{\partial f}{\partial \mathbf w} = 2(\sum_{i=1}^{n}\sum_{j=1}^{n}(\mathbf{a_i^\top w-b_j^\top w})(\mathbf{a_{i}-b_{j}})+\lambda\mathbf w)\end{equation} \qed
\end{enumerate}

\section*{Problem 2}

\begin{enumerate}[label=(\alph*)]
  \item
    Start from 1-dimensional case with only one line segment to be placed, then the number of cases is
    \begin{equation}\sum_{i=1}^n n=\frac{n(n+1)}{2}\end{equation}
    With 6 independent parts and two independent axis, the total number should be
    \begin{equation}((\frac{n(n+1)}{2})^6)^2=\frac{n(n+1)}{2})^{12}\end{equation}
    And
    \begin{equation}\lim_{n\to\infty}\frac{n(n+1)}{2})^{12}=O(n^{24})\end{equation}\qed

  \item
    See prob2-2.py for the code.
    The complexity is $O(n^2)$.\qed

  \item
    \begin{equation} f(n) =
    \begin{cases}
      1, n = 0, 1 \\
      \sum_{i=0}^{n-1}{f(i)}, n \ge 2
   \end{cases}
   \end{equation}
   Then, we can prove $f(n)$ has a more direct form:
   \begin{equation}f(n) = 2^{n-1}, n \in \mathbb{N^+}\end{equation}
   We can prove that by induction: \\
   \textbf{Base Case}: Obviously, we have $f(1) = 2^0 = 1$ \\
   \textbf{Inductive Step}:
   \begin{equation}f(n) = \sum_{i=1}^{n-1}2^{i-1}+f(0)=2^{n-1}-1+1=2^{n-1}\end{equation}\qed

 \item
    \begin{align}\sum_{i=1}^{n}\sum_{j=1}^{n}{(\mathbf{a_i^\top w-b_j^\top w})}^2 + \lambda \left\lVert\mathbf{w}\right\rVert_2^2 &= \sum_{i=1}^{n}\sum_{j=1}^{n}{(\mathbf{w^\top a_i a_i^\top w-w^\top a_i b_j^\top w + w^\top b_j b_j^\top w})} + \lambda \left\lVert\mathbf{w}\right\rVert_2^2\\
    &= \mathbf{w^\top} (\sum_{i=1}^{n}\sum_{j=1}^{n}{(\mathbf{a_i a_i^\top-a_i b_j^\top+b_j b_j})}) \mathbf{w}+ \lambda \left\lVert\mathbf{w}\right\rVert_2^2\end{align}
    See prob2-4.py for the code. \qed

\end{enumerate}

\end{document}


\input{frontmatter.tex}
\begin{document}
\input{4955_heading.tex}

\vspace{2em}

This document details a proposal for the combinational and learning AI that will play a major role in the game Sasq-watch, developed by Cryptid Games.

\section{High Level Overview}

The AI will be made up of three components, detailed below. Each of these components is a stripped down model of certain AI principles, namely a neural network, a genetic combination algorithm, and a recurrent neural network.

\begin{enumerate}
    \item A state machine with transfer weights in the graph determined by a vector given to the state machine at the start of the game
    \item A combination algorithm which takes the personal weight vectors of each player and joins them into one weight vector for propogation into the state machine. This combinational algorithm will also be used to combine each player's weight vector with the modified game vector created after the learning phase.
    \item A "learning phase" algorithm that, after being successfully defeated, goes back through the weight vector and modifies weights to direct the AI in a different direction, preventing the same attack from being successfully executed in the same way again.
\end{enumerate}

The aim with these components is to simulate, at the high level, the same processes that make recurrent nerual networks highly efficient at learning when presented with vast sums of training data and plenty of computation time are being stripped down and manually guided so that they can be applied to a hand-crafted game experience.

\section{State Machine}

The state machine sits in a place halfway between a state machine and a neural network. Figure \ref{fig:state1} is an example of a very small machine that will be used for discussion purposes.

\begin{figure}
\begin{tikzpicture}[scale=0.2]
\tikzstyle{every node}+=[inner sep=0pt]
\draw [black] (12.6,-16.8) circle (3);
\draw (12.6,-16.8) node {$A$};
\draw [black] (34.8,-6.5) circle (3);
\draw (34.8,-6.5) node {$B_1$};
\draw [black] (34.8,-24.5) circle (3);
\draw (34.8,-24.5) node {$B_2$};
\draw [black] (15.32,-15.54) -- (32.08,-7.76);
\fill [black] (32.08,-7.76) -- (31.14,-7.65) -- (31.56,-8.55);
\draw (28.03,-12.18) node [below] {$\vec{v_i}$};
\draw [black] (15.43,-17.78) -- (31.97,-23.52);
\fill [black] (31.97,-23.52) -- (31.37,-22.78) -- (31.05,-23.73);
\draw (17.86,-21.36) node [below] {$\vec{v_{i+1}}$};
\end{tikzpicture}
\caption{An example state machine for discussion}
\label{fig:state1}
\end{figure}

State $A$ represents a specific behavior of the cryptid, while states $B_1$ and $B_2$ are two behaviors that can occur from state $A$. $\vec{v_i}$ refers to the $i$th value in $\vec{v}$. State A is made up of two components: a cryptid action function and a transfer decision function. The cryptid action function describes the behavior that the cryptid will perform (i.e. forrage, travel, sleep, etc.). This is the main function of how the state machine interacts with the game, which allows us to leverage pre-existing knowledge and resources for Unity to simply create the AI using a state machine. The second component, the transfer decision function could work in one of two ways.

The first way it could work is very similar to the way a neural network moves through its weights. The decision function outputs a number $n$ such that $n \in [-1, 1]$. $\vec{v_i}$ could remain normal while $\vec{v_{i+1}}$ is negated. Then, the state transfers anytime the transfer function evaluates to a value above $\vec{v_i}$ or below $\vec{v_{i+1}}$. This design could also work by assigning ranges to each edge of the state machine's graph with a specific range dedicated for the "remain in this state" decision. I believe this would be the worse of the two options because it limits the number of possible states we can transfer to, and it requires incredibly complex transfer functions.

The second way it could work is by having the transfer function provide a binary conclusion of transfer or stay. Then, when it decides on a transfer, the weights of all outgoing edges serve as the ratios for transfers. For example, the transfer from state $A$ would transfer to state $B_1$ with probability $P(\vec{v_i})$ and would transfer to state $B_2$ with probability $P(\vec{v_{i+1}})$. This allows for non-deterministic actions from the cryptid, and the computation of state transfers will be far less significant.

\section{Vector Combination Algorithm}

The vector combination algorithm fulfills several roles in the game. First, it allows four players joining the game to combine their individual cryptid decision vectors in a non-deterministic way, and it allows for the group decision vector to be combined with individual player vector after the game, letting the game affect future games. The combination algorithm is going to be similar to the genetic combination algorithms present in genetic learning alrogithms. The table below demonstrates this phenomenon.

\begin{tabular}{ccccc}
$\vec{v_0}$ & & $\vec{u_0}$ & & $\vec{v_0}$ \\
$\vec{v_1}$ & & $\vec{u_1}$ & $\Longrightarrow$ & $\vec{u_1}$ \\
$\vec{v_2}$ & & $\vec{u_2}$ & & $\vec{u_2}$ \\
\vdots & & & & \\
$\vec{v_n}$ & & $\vec{u_n}$ & & $\vec{v_n}$ \\
\end{tabular}

The first element in each vector will be a simple count of the number of games a vector has "played" in. In this way, the combination algorithm can weight the cross pollination of "genes" based upon how "experienced" that vector is. The combination of the first two values could, for example, take the form of the equation:

\begin{equation}
    \frac{{\log_{10}(\vec{v_0})}\vec{v_1} + {\log_{10}(\vec{u_0})}\vec{u_1}}{\log_{10}(\vec{v_0}) + \log_{10}(\vec{u_0})} = \vec{game_1}
\end{equation}

Then, after the games conclusion, to update a player's weight vector, the same calculation can be performed with $\vec{game}$ taking the place of $\vec{v}$ in the equation above and the weight ratios being the ratio between total games played by all players and total games played by this player.

\section{Learning Algorithm}

The learning algorithm takes advantage of the fact that a state machine is really just a graph. As the game runs, every state transition pushes the state onto a stack. Then, when the game is finished, we can assume that the last few transitions led to the game loss. Therefore, we step back through the states and adjust the weights that allow arriving in those states. Then, we can perform a simple graph traversal to see if it becomes more difficult to arrive at that state through a given transition. If it does, then we consider it a successful change and continue to "prune" the next element on the stack by performing the same action. This can be repeated for a variable depth, allowing us to modify the amount of the game that is modified each time. A possible solution is that the depth is inversely related to how many games have been played.

\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

\section{Symmetries and conservation laws}

\marginpar{Tuesday\\ 2020-3-17, \\ compiled \\ \today}

Our aim is to describe the fundamental constituents of matter with a Quantum Field Theory.
The method used to derive the equations of motion is a variational principle: we will find a Lagrangian density for various particles, and then apply the variational principle to find their equations of motion.

A guiding principle on the description of these fundamental particles is based on using their symmetries.
We have Nöether's theorem in Quantum Field Theory: from these symmetries we are able to find conserved quantities.

These symmetries are described with groups, since we can compose their application; the theory describing groups is very rich.
For this lecture we will base ourselves on Peskin's chapter 2 \cite[]{peskinConceptsElementaryParticle2019}.

A group \(G\) is a set of elements endowed with an operation. The set of elements can be either discrete or continuous. Examples of discrete transformations are the parity transformation \(P\): \(P \vec{x} = -\vec{x}\) and the time swap \(T\): \(T x^{\mu } = (-x^{0}, \vec{x})\). Continuous symmetries, on the other hand, are parametrized by one or more continuous-valued parameters.

We distinguish:
\begin{enumerate}
  \item \textbf{spacetime} symmetries: groups which transform our coordinate system for spacetime, such as Lorentz and Poincaré transformations;
  \item \textbf{internal} symmetries: groups which transform a certain field, or a certain property of our quantum system.
\end{enumerate}

For our set to be a group, we need to be able to define an operation --- we will usually call it multiplication --- between the elements of the group, such that if \(a, b \in G\) then \(ab \in G\). Also, we must have
\begin{enumerate}
  \item associativity: \((ab)c = a(bc)\);
  \item existence of the identity \(\mathbb{1}\), such that \(\mathbb{1} a = a \mathbb{1} = a\);
  \item existence of inverses: there exists \(a^{-1}\) such that \(a a^{-1} = a^{-1} a = \mathbb{1}\).
\end{enumerate}

What is of interest to us is the association of the group with a transformation which is a symmetry: this is called a \emph{representation}, which associates to each \(g \in G\) a unitary operator \(U_g\) acting on the quantum states. We ask that this representation should preserve the group structure, that is to say, \(U_{gh} = U_g U_h\) and \(U_{g^{-1}} = U_{g}^{-1}\).

We call a transformation a symmetry if, after performing the transformation, the dynamics of the system do not change.

For a quantum mechanical system, we are interested in the observables: these are described by operators, whose eigenvalues are the observations we make, and which in the Heisenberg picture evolve like
%
\begin{align}
- i \hbar \dv{}{t} O(t) = [H, O(t)]
\,;
\end{align}
%
if an operator commutes with the Hamiltonian, \([H,O]=0\), then the operator's expectation value on any state is constant --- which is to say, the operator is constant.

If we perform a transformation in the form
%
\begin{align}
\ket{\psi } \rightarrow \ket{\psi'} = U \ket{\psi }
\,,
\end{align}
%
then the operators will change by
%
\begin{align}
O \rightarrow O^{\prime } = U ^\dag O U
\,.
\end{align}

Note that whether we have \(U ^\dag O U\) or \(U O U ^\dag\) does not matter, since we ask observables \(O\) to be Hermitian, so \(O = O ^\dag\).

We know that these transformations must always be unitary, because the conservation of probability implies that we must have \(\braket{\psi }{\psi } = \const\): so,
%
\begin{align}
U ^\dag U = \mathbb{1}
\,.
\end{align}

This can be also stated as \(U ^\dag = U^{-1}\).

So, the function associating a unitary operator \(U\) to an element \(g\) of the group is called its \emph{unitary representation}.

A transformation \(G\) is a symmetry if \(\forall a \in G\) we have
%
\begin{align}
[U(a), H]  =0
\,,
\end{align}
%
that is, the unitary representation of the group element always commutes with the Hamiltonian.

If we have a state \(\ket{\psi }\) with energy \(H \ket{\psi } = E \ket{\psi }\), then the transformation commuting with the Hamiltonian means that \(\ket{\psi'} = U \ket{\psi }\) has the same energy:
%
\begin{align}
H (U \ket{\psi }) = HU \ket{\psi } \overset{[H, U] = 0}{=} UH \ket{\psi } =
 U E \ket{\psi } = E \qty(u \ket{\psi })
\,,
\end{align}
%
so the eigenvalue of \(U \ket{\psi }\) is the same as that of \(\ket{\psi }\).

Now, we can move to an example, taken from Peskin \cite[eq.\ 2.38 onward]{peskinConceptsElementaryParticle2019}.
Consider the discrete group \(\mathbb{Z}_{2}\), which only has the elements \(1\) and \(-1\), with the same multiplication rules as those we would have if these elements were integers.
So, the group is closed with respect to multiplication.
It can be easily checked that this is indeed a group based on our definition.

In order for this to be of interest to us, we can consider a quantum mechanical system and find a unitary representation acting on its Hilbert space.

Let us suppose we have a QM system with a basis made of two states \(\ket{\pi^{+}}\) and \(\ket{\pi^{-}}\). Let us define the \emph{charge conjugation} operator \(C\), by:
%
\begin{align}
C \ket{\pi^{+}} = \ket{\pi^{-}}
\qquad \text{and} \qquad
C \ket{\pi^{-}} = \ket{\pi^{+}}
\,.
\end{align}

So, we can find a unitary representation of \(\mathbb{Z}_{2}\) in this system: we need to define \(U(1)\) and \(U(-1)\). We define
%
\begin{subequations}
\begin{align}
U(1) = \mathbb{1} =\left[\begin{array}{cc}
1 & 0 \\
0 & 1
\end{array}\right]
\qquad \text{and} \qquad
U(-1) = \sigma_{x} = \left[\begin{array}{cc}
0 & 1 \\
1 & 0
\end{array}\right]
\,,
\end{align}
\end{subequations}
%
where the matrices are to be interpreted as acting on vectors expressed to the basis \(\qty{\ket{\pi_{+}}, \ket{\pi_{-}}}\).

So, we can say that our unitary representation looks like
%
\begin{align}
\mathbb{Z}_{2} \rightarrow \qty{\mathbb{1}, C}
\,.
\end{align}

Now, if \([C, H] =0\) (and \(\mathbb{1}\) commutes with \(H\), which is always the case) then we say that ``\(H\) has the symmetry \(\mathbb{Z}_{2}\)'': this implies that the energies of the two \(\pi_{\pm}\) particles are equal.

The interesting question to determine will be whether this is actually the case for our given group.

Groups can be subdivided into abelian and non-abelian ones.
A group is abelian if for every \(a, b\) in \(G\) we have \(ab = ba\), or equivalently, \([a, b] =0\).
It is not if this is not the case, that is, there exist \(a, b\) such that \(ab \neq ba\).

The condition on the elements directly translates to a condition on the matrices of the unitary representation.
If we have commuting matrices, we can simultaneously diagonalize them: for example, in the case of \(\mathbb{Z}_{2}\) we can go to a basis in which
%
\begin{subequations}
\begin{align}
C = \left[\begin{array}{cc}
1 & 0 \\
0 & -1
\end{array}\right]
\,,
\end{align}
\end{subequations}
%
specifically the states on which this matrix will act will need to be
%
\begin{align}
\ket{\pi_{1}} = \frac{\ket{\pi^{+}} + \ket{\pi^{-}}}{\sqrt{2}}
\qquad \text{and} \qquad
\ket{\pi_{2}} = \frac{\ket{\pi^{+}} - \ket{\pi^{-}}}{\sqrt{2}}
\,,
\end{align}
%
since then \(C \ket{\pi_1 } = \ket{\pi_1 }\) (we write \(C = +1\)) and \(C \ket{\pi_{2}} = - \ket{\pi_2 }\) (we write \(C = -1\)).
We will often use this notation, confusing operator and eigenvalue.

In the case of nonabelian groups it is not in general possible to diagonalize all the matrices; we can however do a change of basis and write the matrices as a block matrix with the smallest possible blocks:
%
\begin{subequations}
\begin{align}
U_{R} = \left[\begin{array}{ccc}
U_1  & 0 & 0 \\
0 & U_2  & 0 \\
0 & 0 & \dots
\end{array}\right]
\,,
\end{align}
\end{subequations}
%
where the matrices \(U_i\) are called the \textbf{irreducible unitary representations of \(G\)}.
The dimension of the matrices \(U_i\) tells us the dimension of these irreducible unitary representations.

\todo[inline]{Add more details on irreps --- maybe not here? They can be found in professor Rigolin's intro to groups.}

Do note that some elements of a nonabelian group can commute: for example, in the rotation group we have
%
\begin{align}
[J^{i}, J^{j}] = \epsilon^{ijk} J_{k}
\,,
\end{align}
%
so if we take \(i = j\), that is, we consider rotations along the same axis, they will commute since then the Kronecker symbol is equal to zero.

\subsection{Continuous transformations: space translations}

An element of the group can be written as
%
\begin{align}
U(a) = e^{ - i a P }
\,,
\end{align}
%
where the operator \(P\), whose eigenvalue is the momentum, is called the generator of the transformation.

If we consider a plane wave we can clearly see how this action works: if we start from
%
\begin{align}
\braket{x}{p} = e^{i p x}
\,,
\end{align}
%
we can apply the operator \(U(a)\) to \(\ket{p}\), which will yield \(e^{-ipa}\) (since eigenvectors of an operator are also eigenvectors of its exponential): so we find
%
\begin{align}
\bra{x} U(a) \ket{p} = e^{i p (x-a)}
\,,
\end{align}
%
which means that by acting with this operator we have effectively performed a translation with displacement \(a\).

If our system is invariant under translations, then Nöether's theorem tells us that the momentum is conserved.

In order to be a physical observable \(P\) needs to be Hermitian: \(P = P ^\dag\).

So, the adjoint of the transformation \(U(a)\) is
%
\begin{align}
U ^\dag (a) =
\sum _{n} \qty(\frac{(-iaP)^{n}}{n!}) ^\dag
= \sum _{n} \frac{(iaP ^\dag)^{n}}{n!}
=
e^{i a P ^\dag} = e^{iaP} = U^{-1}(a)
\,,
\end{align}
%
which confirms the fact that the transformation is unitary.

Let us suppose that the momentum operator \(P\) commutes with the Hamiltonian: \([P,H] = 0\). Then,
%
\begin{align}
[U(a), H] = 0
\,,
\end{align}
%
that is, the Hamiltonian is translation-invariant.

All this is to say that a constant of motion \(O\) corresponds to an operator \(O\) which commutes with the Hamiltonian.
This is formalized by Nöther's theorem, which establishes the equivalence between symmetries and conservation laws:
%
\begin{align}
[O, H] = 0
\iff
[U_O, H] = 0
\,.
\end{align}

As an example, take the group \(G\) of 3D rotations.
They depend on a continuous parameter \(\vec{\alpha}\), just like translations depended on the parameter \(a\).

The rotation is written as
%
\begin{align}
U(\vec{\alpha}) = e^{-i \vec{\alpha} \cdot \vec{J}}
\,,
\end{align}
%
where the components of the angular momentum have the following commutation relations:
%
\begin{align}
[J^{i}, J^{j}] = i \epsilon^{ijk} J^{k}
\,.
\end{align}

We will be able to compose the representations of rotations:
%
\begin{align}
U(\vec{\beta}) U(\vec{\alpha}) = U(\vec{\gamma})
\,.
\end{align}

This space of 3D rotations is called SO(3), since every rotation corresponds to a 3x3 matrix which is a rotation matrix --- it is orthogonal and has determinant 1.

Now, we seek \textbf{representations} of these rotations: so, we choose the dimension \(d\) of a quantum-mechanical vector and describe how it changes upon the action of the unitary matrices found by exponentiating certain \(d\)-dimensional generators \(J^{i}\), which must have the algebra discussed above.

If we look for 1D representations of the generators \(J^{i}\) the only option we find is \(J^{i} = 0\), which means that we are not actually performing a rotation. This is because scalars commute with each other.
Which states transform this way? These are scalar states, with spin 0.

For 2D representations, we have
%
\begin{align}
J^{i} =\frac{1}{2} \sigma^{i}
\,,
\end{align}
%
where the \(\sigma^{i}\) are the Pauli matrices.

We can also find 3D representations, which look like
%
\begin{subequations}
\begin{align}
J^{1}= \left[\begin{array}{ccc}
0 & 0 & 0 \\
0 & 0 & -i \\
0 & i & 0
\end{array}\right]
\qquad
J^{2}= \left[\begin{array}{ccc}
0 & 0 & i \\
0 & 0 & 0 \\
-i & 0 & 0
\end{array}\right]
\qquad
J^{1}= \left[\begin{array}{ccc}
0 & -i & 0 \\
i & 0 & 0 \\
0 & 0 & 0
\end{array}\right]
\,
\end{align}
\end{subequations}
%
and represent a spin 1 particle. In general, spin \(s\) corresponds to a \(2s+1\)-dimensional representation.

A rotation in 2D, represented by an element of SO(2), corresponds to a phase shift, so we can say that it is equivalent to an element of U(1).
This then allows us to see that SO(2) is abelian.

In general, we write for a unitary \(n \times n\) representation
%
\begin{align}
U(n) \rightarrow e^{-i \alpha^{n} t^{a}}
\,,
\end{align}
%
where the generators \(t^{a}\) are Hermitian matrices corresponding to Hermitian operators.
In particular, conventionally we say that one of these is the identity: \(t^{0} = \mathbb{1}\) (which must always be included in the group, lest we lose closure).

So, we omit it and say that we have \(n^2-1\) generators for the SU\((n)\) group.
We shall see that each of these generators corresponds to a particle, and for the weak interaction we will have \(2^2-1 = 3\) particles, while for the strong one we will have \(3^2-1=8\).

In general, if \(t^{a}\) are the generators of an abstract Lie group, we can describe the algebra of the group by
%
\begin{align}
\qty[t^{a}, t^{b}] = i f^{abc} t^{c}
\,,
\end{align}
%
so, the commutator is decomposed into a linear combination of the generators, whose coefficients \(f^{abc}\) are called the \textbf{structure constants} of the group.

\end{document}


\section{Link Path Loss}\label{sec:reachi-experiments}
In this section, we present the method for simulating link \gls{pathloss} from \cite{paper:linkmodel}, as well
as why the model does not work for our needs. For our simulations, we want to simulate the performance of
nodes in a \gls{manet}. The performance is, however, heavily dependent on network conditions and the
capabilities of the technology~\cite[p.~10]{paper:linkmodel}. The author of \cite{paper:linkmodel} presents
methods for evaluating the performance of a wireless network and proceeds to introduce methods for simulating
\gls{pathloss} on a multi-link model, based on real-world performance measurements. \medbreak

The author of \cite{paper:linkmodel} describes the \gls{pathloss} of a link to be the sum of two parts: A
deterministic distance-dependent part, that describes the mean signal attenuation at any given link distance,
and a stochastic shadow fading part, which is the \gls{pathloss} caused by terrain, buildings, vegetation and
cars. With this \gls{pathloss}, it is possible to simulate the \gls{rssi} on a given link, by subtracting the
\gls{pathloss} from the transmission power of the simulated radio.
%
\begin{eq}\label{eq:ld}
    \mathit{pl}_d(l) = 55 \log_{10}(d(l)) - 18.8
\end{eq}

The distance-dependent \gls{pathloss} is computed using the $\mathit{pl}_d(l)$ function shown in
\autoref{eq:ld}~\cite[p.~25]{paper:linkmodel}, where the function $d(l)$ denotes the distance of a link in
meters. Computing the shadow fading \gls{pathloss}, on the other hand, is not as trivial. The shadow fading
part of the \gls{pathloss} is based on the correlation between angles of link pairs sharing a common nodes,
and bears a significant practical limitation in the sense that the shadow fading part depends on a Cholesky
factorisation with a computational complexity of $O(N^6)$~\cite[p.~31]{paper:linkmodel}, where $N$ is the
total number of nodes in the network. \medbreak

Through personal communication with the author of \cite{paper:linkmodel}, we received access to logs from
field experiments for the Reachi project. These field experiments were conducted in different locations with
an early prototype of the Reachi device. The logs contain \gls{gps} coordinates, as well as \gls{rssi}
information for detected neighbours of each node. Examining these logs have shown discrepancies between the
\gls{pathloss} model from \cite{paper:linkmodel} and the measured \gls{rssi}.
\autoref{plot:reachi-experiments:measurements-vs-ld} plots samples drawn from the $\mathit{pl}_d(l)$ function
and measurements from a log containing field measurements from an experiment in Marikina, in the Phillippines.
Since the log contained a total of 17761 links, the measurements are summarised based on the distance of the
link, and each link was sorted into distance buckets with 20-meter intervals. The average \gls{rssi} for all
links in a bucket is plotted in \autoref{plot:reachi-experiments:measurements-vs-ld}. The plot shows that the
\gls{rssi} computed with the distance-dependent \gls{pathloss} does not fit with the measured \gls{rssi}.
\medbreak

As mentioned earlier, the shadow fading \gls{pathloss} is based on the correlation between angles of link
pairs that share a common node. An assumption for this is that link pairs with a high correlation, where the
angle between them is low, will have close to the same shadow fading~\cite{paper:linkmodel}. However, this
does not seem to be the case. \autoref{plot:reachi-experiments:avg-rssi-angle-phili-rude} shows a plot where we
compare the Marikina log from earlier, with another field experiment log from Rude Skov. For both logs, pairs
of links sharing a common node were sorted, based on the angle between them, into buckets of 5 \degree
intervals, and we computed the average \gls{rssi} for these buckets, after removing the distance-dependent
\gls{pathloss}. This means that only the shadow fading part of the \gls{pathloss} is included in the
\gls{rssi} plotted in \autoref{plot:reachi-experiments:avg-rssi-angle-phili-rude}. Under the assumption that
highly correlated link pairs should result in less shadow fading \gls{pathloss}, the traces on
\autoref{plot:reachi-experiments:avg-rssi-angle-phili-rude} should increase gradually as the angle increases.
This is not the case. \medbreak

Because of this, and the fact that computing the shadow fading path loss is not feasible for a very large
number of nodes, we instead propose our model for approximating the shadow fading \gls{pathloss}.

%As mentioned above, the angle based approach relies on the assumption that link pairs with a high correlation,
%i.e. low angle between them, will have close to the same shadow fading. We have however not been able to
%produce reliable proof that the assumption is correct. To produce proof, the Marikina and Rude skov log was
%used.

%For both logs, link pairs were created and sorted based on their angle into separate bucekts of
%5$\degree$ intervals. The average \gls{rssi} for each bucket was then computed. Before computing the average
%\gls{rssi}, the distance dependent part $l_d$ was removed from the links \gls{rssi} measurement, to isolate
%the stochastic part. The resulting data can be seen plotted on
%\autoref{plot:reachi-experiments:avg-rssi-angle-phili-rude}. Under the previous assumption that a high
%correlation gives smaller stochastic path loss, then the traces on
%\autoref{plot:reachi-experiments:avg-rssi-angle-phili-rude} should increase gradually as the angle increases.
%Clearly the measurements does increase as the angle increase, but the increaes are not steady but rather vary
%greatly. Too greatly to say that with certainty that the assumption holds on the received logs.

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
                height=10.5cm, width=0.95\textwidth,
                ylabel={RSSI},
                xlabel={Distance in meters},
                axis lines*=left,
                xmin=0, xmax=750,
                enlargelimits=false,
                ymin=-120, ymax=-20,
                xtick={0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750},
                ymajorgrids=true,
                xmajorgrids=true,
                grid style=dashed,
                restrict y to domain=-120:-20,
                samples=600
            ]

            \addplot[very thick, solid, cyan, mark=*] coordinates {(20, -28.32345013477089) (40, -44.85830258302583) (60, -52.77323717948718) (80, -60.21201657458563) (100, -66.47435897435898) (120, -69.68905472636816) (140, -71.5976496922216) (160, -73.7866473149492) (180, -75.53428571428572) (200, -76.89289392378991) (220, -77.88135593220339) (240, -77.8035019455253) (260, -77.36784140969164) (280, -77.14030612244898) (300, -77.75299760191847) (320, -79.71686746987952) (340, -79.15481171548117) (360, -79.90728476821192) (380, -81.30909090909091) (400, -81.79746835443038) (420, -81.52272727272727) (440, -79.2) (460, -79.42105263157895) (480, -79.4375) (500, -79.0) (520, -77.91666666666667) (540, -83.0) (560, -81.27272727272727) (580, -83.57142857142857) (600, -86.0) (620, -83.4) (640, -86.5) (660, -81.42857142857143) (680, -79.0) (700, -82.71428571428571) (740, -77.0)};
            \addlegendentry{Marikina field measurements};

            \addplot[domain=0:740, very thick, solid, red] {26 - ld(x)};
            \addlegendentry{Computed \gls{rssi}};
        \end{axis}
    \end{tikzpicture}
    \caption{Average RSSI pr. distance bucket.}\label{plot:reachi-experiments:measurements-vs-ld}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
                height=10.5cm, width=0.95\textwidth,
                ylabel={Average RSSI - $\mathit{pl}_d(l)$},
                xlabel={Angle buckets},
                axis lines*=left,
                xmin=0, xmax=180,
                xtick={0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,170,180},
                enlargelimits=false,
                ymin=0, ymax=90,
                ymajorgrids=true,
                xmajorgrids=true,
                grid style=dashed
            ]

            \addplot[thick, solid, cyan, mark=*] coordinates {(5,7.581215097994852)(10,9.763581254686114)(15,11.59234807189617)(20,11.084399582147876)(25,9.411670936293284)(30,13.486232393853125)(35,19.91110051641767)(40,12.453590557884946)(45,14.70313120243339)(50,15.804939314638208)(55,16.87697870999425)(60,15.320902904198922)(65,18.673392004683087)(70,18.48508963612783)(75,21.147550183881993)(80,8.302184073487094)(85,10.641508684206432)(90,12.343183048128944)(95,14.396643466792797)(100,12.27504884861337)(105,17.994872329043496)(110,13.156645245862158)(115,11.595967499573286)(120,14.245146945189493)(125,14.528392002600814)(130,15.96981736460891)(135,13.70427190114752)(140,17.692009757218077)(145,17.79486440192431)(150,20.947579654378668)(155,25.611175147469265)(160,20.819124899373364)(165,12.495568407794714)(170,17.784042978338544)(175,9.646875797269503)(180,10.597197732034614)};
            \addlegendentry{Marikina};

            \addplot[thick, solid, red, mark=triangle*] coordinates {(5,8.968175725354994)(10,7.741408675352775)(15,9.042742837449076)(20,8.108954051047618)(25,11.273321343260399)(30,11.67692967914715)(35,13.401460743255232)(40,8.053793318539391)(45,14.17474549040368)(50,22.68855220716477)(55,15.026896145186285)(60,17.5755161176863)(65,85.01233086400362)(70,59.69038212814202)(75,62.41948703096457)(80,44.72712810089782)(85,20.029080610052482)(90,29.737954905927445)(95,43.39235466255519)(100,64.720015835849)(105,13.965589072181558)(110,61.334883149884675)(115,36.30455538419785)(120,33.32223486902817)(125,26.172354285886417)(130,45.62355888699502)(135,55.8474401182669)(140,47.511612928722954)(145,49.70429537245245)(150,50.649080550831435)(155,52.34402139529124)(160,51.45218633523966)(165,51.805898996936115)(170,52.39131328924595)(175,48.78102582759046)(180,51.91529108595685)};
            \addlegendentry{Rude Skov};
        \end{axis}
    \end{tikzpicture}
    \caption{Average \gls{rssi} per angle bucket without distance dependent \gls{pathloss}.}
    \label{plot:reachi-experiments:avg-rssi-angle-phili-rude}
\end{figure}


\section{Adversarial Example Landscape}
\transitionFrame{Adversarial Example Landscape}

\begin{frame}{Investigating the ``Inner Maximization''}
  \onslide<+->{%
    \begin{equation}
      \min_{\params} \rho(\params) \text{, where } \rho(\params) = \mathbb{E}_{(\X,\y) \sim \distr} \sbrack{\red{\max_{\delta \in \sPerturb} \loss (\X + \perturb, \y ; \params)}}
    \end{equation}
  }

  \begin{itemize}[<+->]
    \setlength{\itemsep}{20pt}
    \item Preceding theoretical analysis of \textbf{\red{inner maximization}} described PGD's usefulness to provide guarantees regarding first-order adversaries
    \item \textbf{Goal of this Section}: Demonstrate \textit{empirically} that the theoretical analysis holds even in environments that are theoretically \textit{intractable}
      \begin{itemize}[<+->]
        \setlength{\itemsep}{8pt}
        \item \textbf{Recall}: Inner maximization is highly \textit{non-concave} and not continuously-differentiable
        \item \textbf{Question}: Why are we interested in concavity and not convexity?
      \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Experimental Setup}
  \onslide<+->{Setup applies for all experiments in this section}
  \begin{itemize}[<+->]
    \setlength{\itemsep}{10pt}
    \item \textbf{Datasets}: MNIST \& CIFAR10

    \item \textbf{Procedure}: Select example,~$\X$, u.a.r.\ from the dataset, then for each random restart:
      \begin{enumerate}[<+->]
        \setlength\itemsep{6pt}
        \item Select initial perturbation,~${\perturb \in \sPerturb}$, u.a.r.
        \item Perform PGD on perturbed example, ${\X + \perturb}$
      \end{enumerate}

    \item \textbf{\# Random Restarts}: Varies by experiment

    \item \textbf{Loss Function}: Cross-entropy
      \onslide<+->{
        \begin{equation}\label{eq:CrossEntropy}
          \loss(y,\hat{y}) = \sum_{c \in \mathcal{C}} -y_c \log \left( \hat{y}_{c} \right)
        \end{equation}
      }
  \end{itemize}
\end{frame}

\begin{frame}{Experiment~\#1: $\Delta\loss$ vs.\ \#Iterations}
  \onslide<+->{\textbf{Goal}: Study change in adversarial loss for each iteration of PGD}
  \begin{itemize}[<+->]
    \item \textbf{\# Random Restarts}: 20
  \end{itemize}

  \begin{columns}
    \begin{column}{0.23\textwidth}
      \begin{center}
        \onslide<+->{\includegraphics[scale=0.32]{loss_v_iter/mnist_standard.pdf}}
      \end{center}
    \end{column}
    \begin{column}{0.2\textwidth}
      \begin{center}
        \onslide<+->{\includegraphics[scale=0.32]{loss_v_iter/mnist_adv.pdf}}
      \end{center}
    \end{column}
    \begin{column}{0.21\textwidth}
      \begin{center}
        \onslide<+->{\includegraphics[scale=0.32]{loss_v_iter/cifar_standard.pdf}}
      \end{center}
    \end{column}
    \begin{column}{0.22\textwidth}
      % \vspace{-9pt}
      \begin{center}
        \onslide<+->{\includegraphics[scale=0.32]{loss_v_iter/cifar_adv.pdf}}
      \end{center}
    \end{column}
  \end{columns}
  \vfill
  \onslide<+->{\green{\textbf{Takeaways}}}
  \begin{itemize}[<+->]
    \item Adversarial training significantly reduces loss on adversarial examples.
    \item Loss values are \textbf{\blue{well-concentrated}}
      \begin{itemize}
        \item Echoes \textit{folklore belief} that neural network training possible since many local minima with similar loss values
      \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Experiment~\#2: Absence of Outliers}
  \onslide<+->{\textbf{Goal}: Verify security guarantee across many examples \& random restarts}
  \begin{itemize}[<+->]
    \item \textbf{\# Examples} ($x$): 5
    \item \textbf{\# Random Restarts}: 100K
  \end{itemize}

  \vspace{-15pt}
  \begin{columns}
    \begin{column}{0.7\textwidth}
      \begin{center}
        \onslide<4->{\includegraphics[scale=0.19]{loss_hist/mnist}}

        \onslide<6->{\includegraphics[scale=0.19]{loss_hist/cifar}}
      \end{center}
    \end{column}
    \begin{column}{0.25\textwidth}
      \vspace{20pt}
      \onslide<5->{
        \begin{itemize}
          \setlength{\itemsep}{20pt}
          \item \textbf{\blue{Blue}}: Standard training
          \item \textbf{\red{Red}}: Adversarial training
        \end{itemize}
      }
    \end{column}
  \end{columns}

  \vfill
  \onslide<7->{\green{\textbf{Takeaway}}: No outliers (i.e., high loss adversarial examples) \& concentrated losses}
\end{frame}

\begin{frame}{Experiment~\#3: ``Mode Collapse''}
  \onslide<+->{\textbf{\blue{Mode Collapse}}: Common problem in GANs where the generated outputs have limited diversity.}
  \vfill
  \onslide<+->{\textbf{Goal}: Demonstrate that the generated adversarial examples are noticeably distinct:}
  \begin{itemize}[<+->]
    \item \textbf{\# Random Restarts}: 10,000
    \item \textbf{Metric}: Inter-adversarial example (Euclidean) distance
  \end{itemize}
  \vfill
  \onslide<+->{\textbf{\green{Result}}: Inter-maxima distance is distributed close to the expected distance between \textit{two random points} in the $\ell_{\infty}$\-/ball and cosine similarity between points is close to 90\textsuperscript{$\circ$}.}
  \begin{itemize}[<+->]
    % \item Empirically demonstrates no adversarial example ``mode collapse''
    \item Recall the ``curse of dimensionality'' so take with a grain of salt
  \end{itemize}
  \vfill
  \onslide<+->{\textbf{\green{Takeaway}}: No evidence of mode collapse}
\end{frame}