\documentclass[12pt]{cdblatex}
\usepackage{exercises}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% --------------------------------------------------------------------------------------------
\section*{Exercise 4.2 Inconsistent free indices}

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w#}::Indices(position=independent).

   def deriv (poly):

       \delta^{a}::Weight(label=\epsilon).

       bah := @(poly).

       substitute     (bah,$x^{a} -> x^{a} + \delta^{a}$)
       distribute     (bah)

       foo := @(bah) - @(poly).

       keep_weight    (foo, $\epsilon = 1$)
       substitute     (foo, $\delta^{a} -> 1$)

       return foo

   # ---------------------------------------------------------------

   poly := c^{a}
         + c^{a}{}_{b} x^b
         + c^{a}{}_{b c} x^b x^c.    # cdb (ex-0402.100,poly)

   dpoly = deriv (poly)              # cdb (ex-0402.101,dpoly)

\end{cadabra}

\begin{dgroup*}
   \Dmath*{  p = \Cdb*{ex-0402.100} }
   \Dmath*{ dp = \Cdb*{ex-0402.101} }
\end{dgroup*}

\end{document}


  \subsection{House size crowds out investment}

  However, when comparing households on a total wealth basis, i.e.\ their liquid assets plus expected house liquidation, we can see that house size crowds out investment for households with low liquid wealth. In figure~\ref{fig:liquidAssetsByEquity}, we can consider a household whose house size is equal to 5 (5 times their yearly net income) and liquid assets are 0, so their total expected wealth is 5. As this household becomes wealthier, they invest all of their liquid assets in the stock market (such that their risky share is 100 percent), up to the point where they start rebalancing their portfolio between the risky and the safe asset. In this region, they are constrained from investing in the stock market by their low liquid wealth, as they surely would like to invest more in the market. This point becomes clearer by comparing the household to an equally wealthy peer with a smaller house.  At the point where the household with house size of 5 has liquid wealth of 1 (1 times their yearly net income), they are investing less into the stock market in absolute terms than an equally wealthy household whose house size is equal to 2 and liquid assets are equal to 4. The total expected wealth of both these households is 6, but the household with the larger house is investing fewer assets in the stock market than the household with the smaller house. As their total wealth increases, however, both households are unconstrained by their house size and end up investing about the same amount into the stock market in absolute terms.

  \providecommand{\figName}{}
  \renewcommand{\figName}{liquidAssetsByEquity}
  \providecommand{\figFile}{}
  \renewcommand{\figFile}{\figName}
  \input{\FigDir/\figName} % Read in the tex to generate the figure


\documentclass[utf8x,hyperref={pdfpagelabels=false}]{beamer}

\usepackage[utf8x]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
%\logo{\includegraphics[width=.8in]{UdeM_NoirBleu_logo_Marie_crop}}

\usetheme{Malmoe}  % Now it's a beamer presentation with the lisa theme!
\usecolortheme{beaver}
\setbeamertemplate{footline}[page number]
\setbeamertemplate{navigation symbols}{}

\lstloadlanguages{Python}

\definecolor{darkgreen}{RGB}{0,93,21}
\definecolor{greenblue}{RGB}{40,110,126}
\definecolor{lightgray}{RGB}{246,246,246}
\definecolor{bordergray}{RGB}{193,193,193}
\definecolor{lightblue}{RGB}{0,114,168}
\definecolor{methblue}{RGB}{0,31,108}

\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}

\mode<presentation>

\title{Introduction to Theano}
\author{%
\footnotesize
Arnaud Bergeron \newline
(slides adapted by Frédéric Bastien from slides by Ian G.) \newline
(further adapted by Arnaud Bergeron)
}
\date{February 26, 2015}

\lstdefinestyle{theano}{
language=Python,
basicstyle=\fontfamily{pcr}\selectfont\footnotesize,
keywordstyle=\color{darkgreen}\bfseries,
commentstyle=\color{greenblue}\itshape,
%commentstyle=\color{blue}\itshape,
stringstyle=\color{violet},
showstringspaces=false,
tabsize=4,
backgroundcolor=\color{lightgray},
frame=single,
emph={[2]__init__,make_node,perform,infer_shape,c_code,make_thunk,grad,R_op},emphstyle={[2]\color{methblue}},
emph={[3]self},emphstyle={[3]\color{darkgreen}},
moredelim=**[is][{\color{red}}]{`}{`}
}

% We don't have code till the end of the file.
\lstdefinestyle{output}{
language={},
basicstyle=\ttfamily\footnotesize,
backgroundcolor=\color{white},
frame={},
breaklines=true,
emph={[2]},
emph={[3]},
}

\lstset{style=theano}

\newcommand{\code}[1]{\lstinline[emph={[2]}]|#1|}

\begin{document}

\begin{frame}[plain]
 \titlepage
% \vspace{-5em}
% \includegraphics[width=1in]{../hpcs2011_tutorial/pics/lisabook_logo_text_3.png}
% \hfill
% \includegraphics[width=.8in]{../hpcs2011_tutorial/pics/UdeM_NoirBleu_logo_Marie_crop}
\end{frame}

\section{Outline}
\begin{frame}{High level}\setcounter{page}{1}
  \begin{itemize}
  \item Overview of library (3 min)
  \item Building expressions (30 min)
  \item Compiling and running expressions (30 min)
  \item Modifying expressions (25 min)
  \item Debugging (30 min)
  \item Citing Theano (2 min)
  \end{itemize}
\end{frame}

\begin{frame}{Overview of Library}
  Theano is many things
  \begin{itemize}
  \item Language
  \item Compiler
  \item Python library
  \end{itemize}
\end{frame}

\begin{frame}{Overview}
  Theano language:
  \begin{itemize}
  \item Operations on scalar, vector, matrix, tensor, and sparse variables
  \item Linear algebra
  \item Element-wise nonlinearities
  \item Convolution
  \item Extensible
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Overview}
  Using Theano:
  \begin{itemize}
  \item define expression $f(x,y) = x + y$
\begin{lstlisting}
>>> z = x + y
\end{lstlisting}
  \item compile expression
\begin{lstlisting}
>>> f = theano.function([x, y], z)
\end{lstlisting}
  \item execute expression
\begin{lstlisting}
>>> f(1, 2)
3
\end{lstlisting}
  \end{itemize}
\end{frame}

\section{Building}

\begin{frame}{Building expressions}
  \begin{itemize}
  \item Scalars
  \item Vectors
  \item Matrices
  \item Tensors
  \item Broadcasting
  \item Reduction
  \item Dimshuffle
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Scalar math}
\begin{lstlisting}
from theano import tensor as T
x = T.scalar()
y = T.scalar()
z = x+y
w = z*x
a = T.sqrt(w)
b = T.exp(a)
c = a ** b
d = T.log(c)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Vector math}
\begin{lstlisting}
from theano import tensor as T
x = T.vector()
y = T.vector()
# Scalar math applied elementwise
a = x * y
# Vector dot product
b = T.dot(x, y)
# Broadcasting
c = a + b
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Matrix math}
\begin{lstlisting}
from theano import tensor as T
x = T.matrix()
y = T.matrix()
a = T.vector()
# Matrix-matrix product
b = T.dot(x, y)
# Matrix-vector product
c = T.dot(x, a)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Tensors}
   \begin{itemize}
    \item Dimensionality defined by length of ``broadcastable'' argument
    \item Can add (or do other elemwise op) on two
      tensors with same dimensionality
    \item Duplicate tensors along broadcastable axes to
      make size match
  \end{itemize}
\begin{lstlisting}
from theano import tensor as T
tensor3 = T.TensorType(
    broadcastable=(False, False, False),
    dtype='float32')
x = tensor3()
\end{lstlisting}
\end{frame}

\begin{frame}{Broadcasting}
\begin{tabular}{lcccccccl}
    &
    \begin{tabular}{cc}
        1 & 2 \\
        3 & 4 \\
        5 & 6 \\
    \end{tabular} &
    + &
    \begin{tabular}{cc}
        1 & 2 \\
    \end{tabular} &
    = &
    \begin{tabular}{cc}
        1 & 2 \\
        3 & 4 \\
        5 & 6 \\
    \end{tabular} &
    + &
    \begin{tabular}{cc}
        1 & 2 \\
        \color{blue} 1 & \color{blue} 2 \\
        \color{blue} 1 & \color{blue} 2 \\
    \end{tabular} &
    \hspace{-1.3em}
    \tikz[baseline={([yshift=-.5ex]current bounding box.center)}]{
        \draw [->, very thick] (0,0) -- (0,-1.2);
    } \\[1.5em]
    shape: & (3, 2) & & (2,) & & (3, 2) & & ({\color{blue}3}, 2) &
\end{tabular}
\vfill
\begin{itemize}
    \item Pad shape with 1s on the left : $(2,) \equiv (1,2)$
    \item Two dimensions are compatible when they have the same length or one of them is broadcastable
    \item broadcastable dimensions must have a length of 1
    \item Adding tensors of shape (8, 1, 6, 1) and (7, 1, 5) gives a tensor of shape (8, 7, 6, 5)
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Reductions}
\begin{lstlisting}
from theano import tensor as T
tensor3 = T.TensorType(
    broadcastable=(False, False, False),
    dtype='float32')
x = tensor3()
total = x.sum()
marginals = x.sum(axis=(0, 2))
mx = x.max(axis=1)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Dimshuffle}
\begin{lstlisting}
from theano import tensor as T
tensor3 = T.TensorType(
    broadcastable=(False, False, False),
    dtype='float32')
x = tensor3()
y = x.dimshuffle((2, 1, 0))
a = T.matrix()
b = a.T
# Same as b
c = a.dimshuffle((0, 1))
# Adding to larger tensor
d = a.dimshuffle((0, 1, 'x'))
e = a + d
\end{lstlisting}
\end{frame}

\begin{frame}{Exercices}
Work through the "Building Expressions" section of the ipython notebook.
\end{frame}

\section{Compiling/Running}
\begin{frame}{Compiling and running expression}
  \begin{itemize}
  \item \code{theano.function}
  \item shared variables and updates
  \item compilation modes
  \item compilation for GPU
  \item optimizations
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{\code{theano.function}}

\begin{lstlisting}
>>> from theano import tensor as T
>>> x = T.scalar()
>>> y = T.scalar()
>>> from theano import function
>>> # first arg is list of symbolic inputs
>>> # second arg is symbolic output
>>> f = function([x, y], x + y)
>>> # Call it with numerical values
>>> # Get a numerical output
>>> f(1., 2.)
array(3.0)
\end{lstlisting}
\end{frame}

\begin{frame}{Shared variables}
  \begin{itemize}
  \item It’s hard to do much with purely functional programming
  \item \emph{shared variables} add just a little bit of imperative programming
  \item A \emph{shared variable} is a buffer that stores a numerical value for a Theano variable
  \item Can write to as many shared variables as you want, once each, at the end of the function
  \item  Modify outside Theano function with \code{get_value()} and \code{set_value()} methods.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Shared variable example}
\begin{lstlisting}
>>> from theano import shared
>>> x = shared(0.)
# Can also use a dict for more complex code
>>> updates = [(x,  x + 1)]
>>> f = function([], updates=updates)
>>> f()
>>> x.get_value()
1.0
>>> x.set_value(100.)
>>> f()
>>> x.get_value()
101.0
\end{lstlisting}
\end{frame}

\begin{frame}{Which dict?}
  \begin{itemize}
  \item Use theano.compat.python2x.OrderedDict
  \item Not collections.OrderedDict
  \begin{itemize}
  \item This isn’t available in older versions of python, and will limit the portability of your code.
  \end{itemize}
  \item Not \code{\{\}} aka dict
  \begin{itemize}
  \item The iteration order of this built-in class is not deterministic so if Theano accepted this, the same script could compile different C programs each time you run it.
  \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Compilation modes}
  \begin{itemize}
  \item Can compile in different modes to get different kinds of programs
  \item Can specify these modes very precisely with arguments to \code{theano.function()}
  \item Can use a few quick presets with environment variable flags
  \end{itemize}
\end{frame}

\begin{frame}{Example preset compilation modes}
  \begin{description}[FAST\_RUN]
  \item[FAST\_RUN] Default. Spends a lot of time on
compilation to get an executable that runs
fast.
  \item[FAST\_COMPILE] Doesn’t spend much time compiling.
Executable usually uses python
instead of compiled C code. Runs slow.
  \item[DEBUG\_MODE] Adds lots of checks.
Raises error messages in situations other modes don't check for.
  \end{description}
\end{frame}

\begin{frame}{Compilation for GPU}
  \begin{itemize}
  \item Theano's current back-end only supports 32 bit on GPU
  \item CUDA supports 64 bit, but is slow in gamer card
  \item \code{T.fscalar}, \code{T.fvector}, \code{T.fmatrix} are all 32 bit
  \item \code{T.scalar}, \code{T.vector}, \code{T.matrix} resolve to 32 or 64 bit depending on theano’s floatX flag
  \item floatX is float64 by default, set it to float32
  \item Set the device flag to gpu (or a specific gpu, like gpu0)
  \item Optional: warn\_float64=\{'ignore', 'warn', 'raise', 'pdb'\}
  \end{itemize}
\end{frame}

\begin{frame}{Optimizations}
  \begin{itemize}
  \item Theano changes the symbolic expressions
    you write before converting them to C code
  \item It makes them faster
  \begin{itemize}
  \item $(x+y)+(x+y) \to 2\times(x + y)$
  \end{itemize}
  \item It makes them more stable
  \begin{itemize}
  \item $\exp(a)/\sum{\exp(a)} \to \operatorname{softmax}(a)$
  \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Optimizations (2)}
Sometimes optimizations discard error checking and produce incorrect output rather than an exception.
\begin{lstlisting}
>>> x = T.scalar()
>>> f = function([x], x/x)
>>> f(0.)
array(1.0)
\end{lstlisting}
\end{frame}

\begin{frame}{Exercises}
Work through the "Compiling and Running" section of the ipython notebook.
\end{frame}

\section{Modifying expressions}
\begin{frame}{Modifying expressions}
  \begin{itemize}
  \item The \code{grad()} method
  \item Variable nodes
  \item Types
  \item Ops
  \item Apply nodes
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{The \code{grad()} method}
\begin{lstlisting}
>>> x = T.scalar('x')
>>> y = 2. * x
>>> g = T.grad(y, x)
>>> from theano.printing import min_informative_str
# Print the unoptimized graph
>>> print min_informative_str(g)
A. Elemwise{mul}
 B. Elemwise{second,no_inplace}
  C. Elemwise{mul,no_inplace}
   D. TensorConstant{2.0}
   E. x
  F. TensorConstant{1.0}
 <D>
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{The \code{grad()} method}
\begin{lstlisting}
>>> x = T.scalar('x')
>>> y = 2. * x
>>> g = T.grad(y, x)
>>> from theano.printing import min_informative_str
# Print the optimized graph
>>> f = theano.function([x], g)
>>> theano.printing.debugprint(f)
DeepCopyOp [@A] ''   0
 |TensorConstant{2.0} [@B]
\end{lstlisting}
\end{frame}

\begin{frame}{Theano variables}
  \begin{itemize}
  \item A \emph{variable} is a theano expression.
  \item Can come from \code{T.scalar()}, \code{T.matrix()}, etc.
  \item Can come from doing operations on other variables.
  \item Every variable has a type field, identifying its \emph{type}, such as \code{TensorType((True, False), 'float32')}
  \item Variables can be thought of as nodes in a graph
  \end{itemize}
\end{frame}

\begin{frame}{Ops}
  \begin{itemize}
  \item  An Op is any class that describes a function operating on some variables
  \item Can call the op on some variables to get a
new variable or variables
  \item An Op class can supply other forms of
information about the function, such as its
derivative
  \end{itemize}
\end{frame}

\begin{frame}{Apply nodes}
  \begin{itemize}
  \item The Apply class is a specific instance of an application of an Op.
  \item Notable fields:
    \begin{description}[\texttt{outputs}]
    \item[\texttt{op}] The Op to be applied
    \item[\texttt{inputs}] The Variables to be used as input
    \item[\texttt{outputs}] The Variables produced
    \end{description}
  \item The \code{owner} field on variables identifies the Apply that created it.
  \item Variable and Apply instances are nodes and owner/
    inputs/outputs identify edges in a Theano graph.
  \end{itemize}
\end{frame}

\begin{frame}{Exercises}
Work through the "Modifying" section in the ipython notebook.
\end{frame}

\section{Debugging}
\begin{frame}{Debugging}
  \begin{itemize}
  \item DEBUG\_MODE
  \item Error message
  \item \code{theano.printing.debugprint()}
  \item \code{min_informative_str()}
  \item compute\_test\_value
  \item Accessing the FunctionGraph
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Error message: code}
\begin{lstlisting}
import numpy as np
import theano
import theano.tensor as T
x = T.vector()
y = T.vector()
z = x + x
z = z + y
f = theano.function([x, y], z)
f(np.ones((2,)), np.ones((3,)))
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,allowframebreaks]{Error message}
\vspace{1em}
\begin{lstlisting}[style=output]
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    f(np.ones((2,)), np.ones((3,)))
  File "/Users/anakha/Library/Python/2.7/site-packages/theano/compile/function_module.py", line 606, in __call__
    storage_map=self.fn.storage_map)
  File "/Users/anakha/Library/Python/2.7/site-packages/theano/compile/function_module.py", line 595, in __call__
    outputs = self.fn()
ValueError: Input dimension mis-match. (input[0].shape[0] = 3, input[1].shape[0] = 2)
Apply node that caused the error: Elemwise{add,no_inplace}(<TensorType(float64, vector)>, <TensorType(float64, vector)>, <TensorType(float64, vector)>)
Inputs types: [TensorType(float64, vector), TensorType(float64, vector), TensorType(float64, vector)]
Inputs shapes: [(3,), (2,), (2,)]
Inputs strides: [(8,), (8,), (8,)]
Inputs values: [array([ 1.,  1.,  1.]), array([ 1.,  1.]), array([ 1.,  1.])]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag  'optimizer=fast_compile'. If that does not work, Theano  optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high'  for a debugprint and storage map footprint of this apply node.
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Error message: exception\_verbosity=high}
\begin{lstlisting}[style=output]
Debugprint of the apply node:
Elemwise{add,no_inplace} [@A] <TensorType(float64, vector)> ''
 |<TensorType(float64, vector)> [@B] <TensorType(float64, vector)>
 |<TensorType(float64, vector)> [@C] <TensorType(float64, vector)>
 |<TensorType(float64, vector)> [@C] <TensorType(float64, vector)>

Storage map footprint:
 - <TensorType(float64, vector)>, Shape: (3,), ElemSize: 8 Byte(s), TotalSize: 24 Byte(s)
 - <TensorType(float64, vector)>, Shape: (2,), ElemSize: 8 Byte(s), TotalSize: 16 Byte(s)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Error message: optimizer=fast\_compile}
\begin{lstlisting}[style=output]
Backtrace when the node is created:
  File "test.py", line 7, in <module>
    z = z + y
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{debugprint}
\begin{lstlisting}
>>> from theano.printing import debugprint
>>> debugprint(a)
Elemwise{mul,no_inplace} [@A] ''
 |TensorConstant{2.0} [@B]
 |Elemwise{add,no_inplace} [@C] 'z'
   |<TensorType(float64, scalar)> [@D]
   |<TensorType(float64, scalar)> [@E]
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{min\_informative\_str}
\begin{lstlisting}
>>> x = T.scalar()
>>> y = T.scalar()
>>> z = x + y
>>> z.name = 'z'
>>> a = 2. * z
>>> from theano.printing import min_informative_str
>>> print min_informative_str(a)
A. Elemwise{mul,no_inplace}
 B. TensorConstant{2.0}
 C. z
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{compute\_test\_value}
\begin{lstlisting}
>>> from theano import config
>>> config.compute_test_value = 'raise'
>>> x = T.vector()
>>> import numpy as np
>>> x.tag.test_value = np.ones((2,))
>>> y = T.vector()
>>> y.tag.test_value = np.ones((3,))
>>> x + y
...
ValueError: Input dimension mis-match.
(input[0].shape[0] = 2, input[1].shape[0] = 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Accessing a function’s fgraph}
\begin{lstlisting}
>>> x = T.scalar()
>>> y = x / x
>>> f = function([x], y)
>>> debugprint(f.maker.fgraph.outputs[0])
DeepCopyOp [@A] ''
 |TensorConstant{1.0} [@B]
\end{lstlisting}
\end{frame}

\begin{frame}{Exercises}
Work through the "Debugging" section of the ipython notebook.
\end{frame}

\section*{}
\begin{frame}{Citing Theano}
Please cite both of the following papers in all work that uses Theano:
  \begin{itemize}
  \item Bastien, Frédéric, Lamblin, Pascal, Pascanu, Razvan, Bergstra, James, Goodfellow, Ian, Bergeron, Arnaud, Bouchard, Nicolas, and
     Bengio,Yoshua. Theano: new features and speed improvements. Deep Learning and Unsupervised Feature Learning NIPS 2012
    Workshop, 2012.
  \item Bergstra, James, Breuleux, Olivier, Bastien, Frédéric, Lamblin, Pascal, Pascanu, Razvan, Desjardins, Guillaume, Turian, Joseph, Warde-
     Farley, David, and Bengio,Yoshua. Theano: a CPU and GPU math expression compiler. In Proceedings of the Python for Scientific
      Computing Conference (SciPy), June 2010. Oral Presentation.
  \end{itemize}
\end{frame}

\begin{frame}{Example acknowledgments}
We would like to thank the developers of Theano \textbackslash citep\{bergstra+al:2010-scipy,Bastien-Theano-2012\}.
We would also like to thank NSERC, Compute Canada, and Calcul Québec for providing computational resources.
\end{frame}

\begin{frame}
\begin{center}
\bibliography{strings,strings-short,ml,aigaion-shorter}
\Huge
Questions?
\end{center}
\end{frame}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}

\title{Modeling with Python and SMAP}
\author{Sarah Koehler}
\date{April 2015}

\begin{document}

\maketitle

\section{Modeling}
Many say modeling is an art. Let's look at the simplest forms of this art.

Linear Model:
\begin{align}
x(k+1) &= Ax(k) + Bu(k)  \\
y(k) &= Cx(k) + Du(k)
\end{align}
\noindent Let's assume $y(k) = x(k)$.

In the most general case, define $x$ to be all physical \textit{outputs} and $u$ to be all \textit{controllable inputs}. You can also add an additive $ + Ed(k)$ where $d$ are all \textit{uncontrollable inputs}.

A form that works well for Temperature dynamics is a bilinear form. Specifically:
\begin{align}
T(k+1) = AT(k) + ...
\end{align}
(TO DO: To be copied from ME 231 write ups)

\pagebreak
\section{Identification Method}
Two methods are Least Squares and Lasso. An oversimplified explanation is that Least Squares considers all data streams equally important, while Lasso selects the important streams (i.e. feature selection). Both select the corresponding weight for that stream.

Least Squares from Python scikit-learn:
\begin{figure}[h]
\centering
\includegraphics[width=\columnwidth]{LeastSquaresPython.pdf}
\caption{Documentation on Least Squares}
\label{fig:lasso}
\end{figure}

\pagebreak
Lasso from Python scikit-learn:
\begin{figure}[h]
\centering
\includegraphics[width=\columnwidth]{LassoPython.pdf}
\caption{Documentation on Lasso}
\label{fig:lasso}
\end{figure}

\section{Data Manipulation}
This is the most annoying part and it would be great to never ever have to do this again.

TO DO: Copy python code when it's done and/or write the math here.

\section{Validation}
Cross-validation is important! The model usually fits the day it was identified on.

TO DO: Show some plots of same day verification and cross day verification.

\section{Matlab code}
\begin{verbatim}
% % Interpolate data
% interp_start = PDTtoUTC(datenum(2014,9,4,4,0,0));
% interp_end = PDTtoUTC(datenum(2014,9,4,16,0,0));
interp_start = datenum(2014,8,30,10,1,0);
interp_end = datenum(2014,8,30,16,0,0);
interp_times = (interp_start:1/24/60:interp_end)';

fan_interp = interp1(fan_data(:,1),fan_data(:,2),interp_times);
temp_interp = interp1(temp_data(:,1),temp_data(:,2),interp_times);
supplytemp_interp = interp1(supplytemp_data(:,1),supplytemp_data(:,2),interp_times);

% Solve least squares using quadprog
N = length(interp_times)-2;
H = 2*(blkdiag(zeros(2,2),eye(N)));
f = zeros(N+2,1);
Aeq = [zeros(N,2),-eye(N)];
beq = zeros(N,1);
for k = 1:N
   f(k+2) = -2*temp_interp(k+1);
   Aeq(k,1:2) = [-fan_interp(k)*temp_interp(k),fan_interp(k)*supplytemp_interp(k)];
   beq(k) = -temp_interp(k);
end

% options = optimset('Algorithm','interior-point-convex');
% [x,cost] = quadprog(H,f,[],[],Aeq,beq,[],[],[],options);
[x,cost] = quadprog(H,f,[],[],Aeq,beq);

a = x(1);
b = x(2);

%% Least Squares via A\b
A = Aeq(:,1:2);
b = zeros(N,1);
for k = 1:N
   b(k) = -temp_interp(k)+temp_interp(k+1);
end

x = A\b;
a = x(1);
b = x(2);

\end{verbatim}

\section{Python code}
At this point can open up Python code like Arka did and expose the issues we have with its structure.

TO DO: Can also make a list of issues here:
\begin{itemize}
\item Cannot choose system ID procedure without digging into code
\item Need a library of models to choose from? Not everything is linear or bilinear...
\end{itemize}

\end{document}


%%==========================
%% Chapter 4.0: Introduction
%%==========================

\documentclass[../dissertation.tex]{subfiles}

\begin{document}
% \setcounter{section}{-1}
\section{Introduction}\label{sec4.0:Intro}

Following our deep dive into the properties of the Green's functions, let us take
a brief respite to see where we are in the process of understanding the direct
map for the Intermediate Long Wave equation Inverse Scattering Transform. In Section
\ref{sec0:DM} we introduced the notion of a Jost Solution, which we now repeat for
reference:
\begin{defn}[Jost solutions]\label{defn4:jost}
	Recall the linear spectral problem
	\begin{align}\label{eq4:SpecProb}
		L_\delta (\Psi)
			:= \frac{1}{i} \frac{\partial}{\partial x} \Psi^+
				- \zeta \left(\Psi^+ - \Psi^-\right) = u \Psi^+,
	\end{align}
	The Jost solutions $M_1$, $M_e$, $N_1$, $N_e$ are solutions to the linear
	spectral problem \eqref{eq4:SpecProb} whose lower boundary values
	$M_1^+$, $M_e^+$, $N_1^+$, $N_e^+$ as defined in \eqref{eq0:bndryvaluedefn}
	obey the following asymptotic conditions
	\begin{subequations}\label{eq4:JostDEasymp}
		\begin{align}
			\lim_{x\to -\infty}
					\inn{x}
					\left(
						M_1^+(x; \lambda, \delta) - 1
					\right)
				&= \lim_{x\to \infty}
						\inn{x}
						\left(
							N_1^+(x; \lambda, \delta) - 1
						\right)
				= 0 \\
			\lim_{x\to -\infty}
					\inn{x} \left(
						M_e^+(x; \lambda, \delta) - e^{i\lambda x}
					\right)
				&= \lim_{x\to \infty}
						\inn{x}
						\left(
							N_e^+(x; \lambda, \delta) - e^{i\lambda x}
						\right)
				= 0
		\end{align}.
	\end{subequations}

	Additionally, we require the upper boundary values $M_{(\dotarg)}^-$, $N_{(\dotarg)}^-$
	(where $(\dotarg)$ represents either the subscript $1$ or $e$) of
	$M_{(\dotarg)}$, $N_{(\dotarg)}$ to have a decomposition
	\begin{align*}
		M_1^- - 1 &= M_1^{(1)} + M_1^{(2)} \qquad &\text{and}& \qquad
		&N_1^- - 1 &= N_1^{(1)} + N_1^{(2)} \\
		M_e^- - e^{i\lambda x}\,e^{-2\delta\lambda} &= M_e^{(1)} + M_e^{(2)} \qquad &\text{and}& \qquad
		&N_e^- - e^{i\lambda x}\,e^{-2\delta\lambda} &= N_e^{(1)} + N_e^{(2)} \\
	\end{align*}
	satisfying
	\begin{align*}
		\inn{x}^{1+\upsilon} \left|M_{(\dotarg)}^{(1)}(x)\right| \lesssim 1
			\quad (\text{for } x \ll -1), \qquad
		\inn{x}^{1+\upsilon} \left|N_{(\dotarg)}^{(1)}(x)\right| \lesssim 1
			\quad (\text{for } x \gg 1),
	\end{align*}
	and
	\begin{align*}
		\inn{\dotarg}^\tau M_{(\dotarg)}^{(2)},
			~\inn{\dotarg}^\tau N_{(\dotarg)}^{(2)} \in L^2(\mathbb R)
	\end{align*}
	for any $\upsilon \in \left(0,\frac{1}{2}\right)$ and $\tau \in [0,1)$.
\end{defn}

The direct scattering map
$\mathscr D$ maps $u$ to the reflection coefficient
$r(\lambda) := b(\lambda) / a(\lambda)$ where $a$ and $b$ are determined by
the following formulas involving the boundary value $M_1^+$ of the Jost solution
$M_1$
\begin{subequations}
	\label{eq3:ScatData}
	\begin{align}
		\label{eq3:ScatDataA}
		a(\lambda)
			&= 1 + i \alpha(\lambda)
				\int_{\mathbb R}
					u(x) \, M_1^+(x;\lambda, \delta, u)
				\, \mathrm{d}x \\
		\label{eq3:ScatDataB}
		b(\lambda)
			&= i \beta(\lambda)
				\int_{\mathbb R}
					e^{-i\lambda x} \, u(x) \, M_1^+(x; \lambda, \delta, u)
				\, \mathrm{d}x,
	\end{align}
\end{subequations}
where we have written the Jost solutions as functions of $u$ in order to
explicitly highlight the dependence of the Jost solutions on the eigenfunction $u$.
The goal of this chapter is to both prove that $\mathscr D$ is well-defined and
Lipschitz continuous for $|\lambda| > 1$.

As we see from \eqref{eq3:ScatData}, proving that $\mathscr D$ is well defined
requires the Jost solutions to exist and be unique. Further, in order to prove
that $\mathscr D$ is Lipschitz continuous, we need to establish that
the four maps from $u$ to each of the four Jost solutions $M_1$, $M_e$, $N_1$,
and $N_e$ are themselves Lipschitz as maps from $B_X(0, c_0)$ to
$\inn{\dotarg}L^\infty(\mathbb R)$. As is common practice in the inverse
scattering world, to prove these desired results we seek to reformulate
\eqref{eq4:SpecProb} with asymptotic conditions \eqref{eq4:JostDEasymp} as a set
of integral equations that are easier to analyze. This approach is relevant in
our case given the limited theory about partial differential equations
involving functions analytic in a complex strip and the functions' lower and
upper boundary values along the corresponding boundary values of the strip.

Following our analysis of $G_L$ and $G_R$ in Chapters \ref{cptr01:GF} and
\ref{cptr03:xContin} we are almost now in a position to prove (under the right hypotheses)
the equivalence of the Jost solutions and solutions to the following integral
equations
\begin{subequations}
	\label{eq4:JostIE}
	\begin{align}
		\label{eq4:JostIEleft}
		\begin{pmatrix}
			M_1^+(x; \zeta, \delta) \\
			M_e^+(x; \zeta, \delta)
		\end{pmatrix}
			&=
				\begin{pmatrix}
					1 \\
					e^{i\lambda x}
				\end{pmatrix}
				+ \int_{\mathbb R} G_L(x - x'; \zeta, \delta)
					u(x')
					\begin{pmatrix}
						M_1^+(x'; \zeta, \delta) \\
						M_e^+(x'; \zeta, \delta)
					\end{pmatrix}
					\, \mathrm{d}x' \\[0.3\baselineskip]
		\label{eq4:JostIEright}
		\begin{pmatrix}
			N_1^+(x; \zeta, \delta) \\
			N_e^+(x; \zeta, \delta)
		\end{pmatrix}
			&=
				\begin{pmatrix}
					1 \\
					e^{i\lambda x}
				\end{pmatrix}
				+ \int_{\mathbb R} G_R(x - x'; \zeta, \delta)
					u(x')
					\begin{pmatrix}
						N_1^+(x'; \zeta, \lambda) \\
						N_e^+(x'; \zeta, \lambda)
					\end{pmatrix}
					\, \mathrm{d}x'.
	\end{align}
\end{subequations}
In Section \ref{sec4:equiv} we prove this equivalence.
We begin by presenting the framework we use to prove this equivalence,
followed by proving in Subsection \ref{subsec4:DEtoIE} that Jost solutions solve the
corresponding integral equations \eqref{eq4:JostIE}. We prove solutions to
\eqref{eq4:JostIE} are Jost solutions in the following subsection, Section
\ref{subsec4:IEtoDE}.
However, for reasons that are made obvious in
Section \ref{sec4:equiv}, prior to proving
the equivalence of Jost solutions and solutions to \eqref{eq4:JostIE}, we do need
several results about both the existence of solutions of solutions to \eqref{eq4:JostIE}
and the continuity of maps from $u$ to \eqref{eq4:JostIE} solutions. For this reason,
we begin this chapter by studying the \eqref{eq4:JostIE} solutions in Section
\ref{sec4:Exist}, and note that because we prove in Section \ref{sec4:equiv} the
equivalence of Jost solutions and the solutions to the integral equations \eqref{eq4:JostIE},
the results proven in Section \ref{sec4:Exist} about the existence and uniqueness
\eqref{eq4:JostIE} solutions applies to Jost solutions.

The final section of this chapter, Section \ref{sec4:DM}, is the
\textit{rasion d'\^etre} of this dissertation, in that after
\pageref{lastpagePenultimateSection} pages of diligent mathematical exploration,
we arrive at our study of the ILW direct scattering map. We begin Section
\ref{sec4:DM} by verifying the so-called ``scattering equations'' which are
instrumental in the formulation of the ILW inverse scattering map. We then prove
that as a map from $B_X(0, c_0)$ to $L_\lambda^\infty(\mathbb R)$ the direct
scattering map $\mathscr D$ is well-defined (Theorem \ref{thm4:Dwelldefined}),
where  $B_X(0, c_0)$ in the space $X$ of radius $c_0$ and $c_0$ is chosen
according to our work in Section \ref{sec4:Exist} to ensure the existence of the
Jost solutions. In the remainder of Section \ref{sec4:DM}, we turn our attention
towards understanding the Lipschitz continuity properties of $\mathscr D$. Specifically,
we prove that as a map from $B_X(0, c_0)$ to
$L_\lambda^\infty\big((-\infty,k]\cap[k, \infty) \big)$, the direct scattering map
$\mathscr D$ is Lipschitz continous for all $k>0$ (Theorem \ref{thm4:DlipR}). As
an almost immediate consequence of our proof of Theorem \ref{thm4:DlipR}, we
also prove Corollary \ref{cor4:Lip}, which holds that for every
$u \in B_X(0,c_0)$ with the property that
\[
	\int_{\mathbb R} u \, M_1^+(x; \lambda = 0) \, \mathrm{d}x \ne 0,
\]
there is a neighborhood $\mathcal N(u)$ in $B_X(0, c_0)$ about $u$ for which
the map $\mathscr D: \mathcal N(u) \to L^\infty_\lambda(\mathbb R)$ is
Lipschitz continuous.

While we have not yet found a proof that $\mathscr D$ is Lipschitz continuous
uniformly in the parameter $\lambda$ for all real $\lambda$ (which is necessary
as the scattering data are functions of $\lambda$), we do discuss the regimes
under which we are currently able to prove that $\mathscr D$ is Lipschitz
continuous.

A final remark as we set off on the ultimate leg of our mathematical
peregrination within this dissertation:
given the $\delta$-dilation property satisfied by both $G_L$ and $G_R$, throughout
the remainder of this Chapter, we again take $\delta = 1$ noting that the more general
case of $\delta>0$ arbitrary follows from this dilation property and the results
contained within this chapter.

\end{document}


\chapter{Lifting line theory}


\chapter{Introduction}

\section{Financial Contracts}

In the real world, two parties may create a \textit{financial contract} to describe a set of payments to be made between them under certain conditions. These contracts can suffer from numerous issues in real world usage; for one thing, the financial world is plagued with jargon, which can make contracts difficult to interpret, and complicated to compose. This can lead to unneeded verbosity, where certain terms may be repeated multiple times in a single contract. Traditional financial contracts can also be difficult to analyse mathematically in terms of potential cost or value, due to their complexity and potential ambiguity - resulting in the appearance of unnoticed errors in contract definitions.

\section{A Combinator Domain-Specific Language to Represent Contracts}

A hypothetical solution to alleviate some of these issues has been proposed by Simon Peyton Jones, Jean-Marc Eber, and Julian Seward in the paper \textit{Composing Contracts: An Adventure in Financial Engineering}\cite{SPJ}. Their proposed solution employs a combinator domain-specific language, which is a type of functional programming language where specific terms, i.e. \textit{combinators}, can be composed to produce a program. This solution involves using combinators to represent financial contracts. This is possible as these financial contracts can typically be described such that they are composed of smaller contracts - i.e. \textit{sub-contracts}. These combinators can be as simple as \texttt{one}, which requires the counter-party to pay a single unit of a given currency to the owner. They can also describe transformations on inner combinators, such as \texttt{scale}, which multiplies any monetary values in inner combinators by a given value. The contract \texttt{scale(5, one(GBP))} will therefore require the counter-party to pay the owner \pounds 5. \\

The definition of financial contracts using a combinator DSL has numerous upsides. For one thing, even with few combinators a contract writer can define a huge variety of financial contracts, thus reducing the amount of jargon required. If any contracts which cannot be represented by the combinator DSL are found, new combinators can easily be added due to their modular nature. Programmatic definition of financial contracts can also cut down on needless repetition, and there is no room for interpretation. Additionally, the use of combinators facilitates mathematical analysis of financial contracts' values, by the composable nature of these values - where each combinator's value is a function of their inner combinators' values. While there is an implementation of an evaluation process for financial contracts written in the DSL, there is no programmatic implementation of these contracts.

\section{Smart Contracts}

Since the original DSL was first described, cryptocurrencies have proliferated; because of this, it has become popular to represent financial contracts using \textit{smart contracts}. Smart contracts are programs which can be deployed to a blockchain, and then called at any time to execute some specified code. One specific functionality they provide is the ability to obtain and transfer cryptocurrencies, thus facilitating payments between multiple parties under specified conditions in an automated manner\cite{Eth}. Writing smart contract representations of financial contracts allows financial institutions to use blockchains between institutions for payment of funds according to these financial contracts, or simply to keep track of existing financial contracts for auditing purposes thanks to the immutability of blockchains. \\

While existing smart contract languages can provide a strict and unambiguous smart contract representation of a financial contract, i.e. a \textit{financial smart contract}, there are issues with this manner of implementation. One such issue is that many smart contract languages are exceedingly error-prone, meaning that it is very possible to write a financial smart contract with unintended consequences - in fact, it has been estimated that 45\% of smart contracts on the Ethereum blockchain (a platform for hosting smart contracts) contain vulnerabilities\cite{EthSec}. \\

Take the smart contract code in listing \ref{listing:reentrancy}, written in Solidity (a programming language for smart contracts); this code simply transfers funds from the smart contract to the caller, and then decrements a \textit{balance} representing the funds the caller can withdraw. To the untrained eye (and the Solidity compiler), this code snippet contains no errors; in actuality, it contains one of the most severe kinds of vulnerabilities, allowing a reentrancy attack from a malicious user. A reentrancy attack is where funds are transferred before a function's invocation is finished, allowing the function to be called again before the transfer is complete and the balance is decremented (as transfers can trigger function calls)\cite{eth-known-attacks}. This can result in the smart contract being drained of all of its funds. This is an example of a severe vulnerability that can be easy to miss when implementing smart contracts in a smart contract language. \\

\begin{lstlisting}[language=Solidity, caption=A Solidity function which is vulnerable to a reentrancy attack$^1$., captionpos=b, label=listing:reentrancy]
function withdrawOneWei() public {
    msg.sender.call.value(1);
    balances[msg.sender] = balances[msg.sender] - 1;
}
\end{lstlisting}
\stepcounter{footnote}
\footnotetext{Solidity syntax highlighting obtained from the \texttt{solidity-latex-highlighting} package written by Sergei Tikhomirov, used under the MIT license, available at \url{https://github.com/s-tikhomirov/solidity-latex-highlighting}.}

Smart contracts are also difficult to analyse mathematically in terms of definitive costs, as most smart contract languages are relatively complex (in comparison to the combinator DSL mentioned earlier). Complex functionality like iteration and recursion can make the outcome of program execution difficult to evaluate, and also makes errors relatively easy to introduce and difficult to discover. These features are not required for the representation of financial contracts, as demonstrated by their absence from the aforementioned DSL. Overall, the issues mentioned here make smart contract languages a risky choice when creating financial smart contracts, as it can be easy to allow erroneous behaviour to occur, and difficult to find such errors by analysis.

\section{Our Contributions}

In this work, we present several contributions with the aim of improving the ease of implementation and reducing the risk of erroneous behaviour for financial smart contracts: \\

\begin{enumerate}
    \item \textbf{SmartFin}: A combinator DSL for representing financial contracts, derived from the \textit{original DSL} created by Peyton Jones et al.\cite{SPJ}, with slight modifications to enable a smart contract implementation. The design of SmartFin is detailed in chapter \ref{combinator-DSL}.
    \item \textbf{SmartFin Smart Contract Implementation}: An Ethereum-compatible smart contract that can represent any given SmartFin financial contract as a financial smart contract. The implementation of this smart contract is detailed in chapters \ref{smart-contract-impl} and \ref{combinators-main}.
    \item \textbf{Web Client}: A web client for managing SmartFin smart contracts. The implementation of the web client is detailed in chapter \ref{web-client}. The web client implements the following functionality:
    \begin{itemize}
        \item Composition of SmartFin financial contracts, with syntax verification and detailed error reporting with stack traces.
        \item Evaluation of SmartFin financial contracts in a step-by-step manner, to calculate the value that a contract is worth given all required external input (provided by the user) at each required step. The times that all evaluated payments would occur can also be displayed.
        \item Deployment of SmartFin financial smart contracts to any compatible blockchain.
        \item Monitoring of any deployed SmartFin financial smart contracts state, and displaying this state to a user.
        \item Interaction with deployed SmartFin financial smart contracts, allowing users to provide any input that the financial contract requires. \\
    \end{itemize}
\end{enumerate}

With all of these tools together, a user can define a financial contract in the SmartFin DSL using the web client; this SmartFin financial contract can be evaluated in the web client in a step-by-step manner, or can be passed to the implemented smart contract and deployed to a connected compatible blockchain. The deployed financial smart contract can be monitored and interacted with through the web client, and as such all of the required functionality to use SmartFin financial smart contracts is available in the web client. The implemented smart contract can take any given SmartFin contract definition in its constructor and modify its state so that its behaviour matches the given financial contract's behaviour, by implementing logic for all combinators' semantics. A dependency graph of the contributions implemented is depicted in figure \ref{fig:contributions-block}. \\

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{contributions-block.png}
    \caption{A dependency graph of the modules implemented for this project, with their approximate \textit{Lines of Code} (not including tests).}
    \label{fig:contributions-block}
\end{figure}

\section{Challenges}

\subsubsection{SmartFin Smart Contract Implementation}

Implementing the SmartFin combinator DSL in a smart contract is no small feat; while the number of combinators is relatively limited, representing \textit{any} set of combinators requires a robust generic design when it comes to the implementation of the combinators' semantics and state. Furthermore, this design needed be applicable to \textit{all} combinators, and some are quite unintuitive to represent programmatically. The representation of SmartFin combinators is described in chapter \ref{combinators-main}.

\subsubsection{Ethereum Limitations}

Due to limitations of the Ethereum platform, it is not possible to create a one-to-one representation of a SmartFin contract in smart contract form. Designing solutions to the issues caused by the nature of the Ethereum platform required compromises to be made, and minimising the resulting compromises required designing multiple alternative solutions and evaluating them objectively. Solutions to issues stemming from the Ethereum platform are discussed in chapter \ref{smart-contract-impl}.

\subsubsection{SmartFin Contract Evaluation}

Evaluating SmartFin financial contracts is not a simple problem, even when approaching it in a step-by-step manner, for a couple of reasons. One issue is that SmartFin contracts deal heavily with time, making time an important factor in the evaluation of SmartFin contracts. Keeping track of distinct periods of time based on the SmartFin contract and user interaction was required for step-by-step analysis, requiring a system of describing these time periods to be designed and implemented. Furthermore, keeping track of the current state while requiring the user to enter data can be quite complex, and the ability for the user to revert to any earlier state of step-by-step evaluation can make this even worse. Additionally, the evaluation process requires backtracking for certain combinators, thus resulting in even more complicated behaviour. The implementation of step-by-step evaluation is described in section \ref{client-evaluate}.


\documentclass[11pt,a4paper]{article}

% These are extra packages that you might need for writing the equations:
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\lstset {language=C++,
		 basicstyle=\ttfamily,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{purple}\ttfamily,
         morecomment=[l][\color{magenta}]{\#},
       	 basicstyle=\tiny}

% You need the following package in order to include figures in your report:
\usepackage{graphicx}

% With this package you can set the size of the margins manually:
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

% Enter the exercise number, your name and date here:
\noindent\parbox{\linewidth}{
 \parbox{.25\linewidth}{ \large ICP, Exercise 01 }\hfill
 \parbox{.5\linewidth}{\begin{center} \large Beat Hubmann \end{center}}\hfill
 \parbox{.2\linewidth}{\begin{flushright} \large Sep 30, 2018 \end{flushright}}
}
\noindent\rule{\linewidth}{2pt}

\section{Introduction}

Several small experiments on a basic congruential random number generator were conducted.

\section{Algorithm Description}
The congruential random number generator employed generates pseudo-random numbers $x_i$ based on the linear recurrence relation $x_{i+1} = (c \cdot x_i)\; \text{mod} \; p \: \text{where} \: c, p \in \mathbb{R} $~.

\section{Results}

\subsection{Task 1}

\subsubsection{Subtask 1.1}
200 random numbers were generated using $c = 3, \: p = 31$ and plotted for the square test. As shown in figure~\ref{fig:1a}, three lines were observed after normalizing the generated numbers with $x_i \gets \frac{x_i}{p}$.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure1a.eps}
\end{center}
\caption{Square test for congruential random number generator with $c = 3, \: p = 31$.}
\label{fig:1a}
\end{figure}

\subsubsection{Subtask 1.2}
As in the previous task, 200 random numbers were generated using $c = 3, \: p = 31$ and plotted for the cube test. As shown in figure~\ref{fig:1b}, regular patterns can be observed after normalizing the generated numbers with $x_i \gets \frac{x_i}{p}$, but identifying planes is somewhat difficult owing to the spacing caused by the short period.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure1b.eps}
\end{center}
\caption{Cube test for congruential random number generator with $c = 3, \: p = 31$.}
\label{fig:1b}
\end{figure}

\subsubsection{Subtask 1.3}
The random number generator was modified to run with $c = 2836, \: p = 127773$ to yield a substantially longer period and improved pseudo-randomness. As evident in figures~\ref{fig:1c}~and~\ref{fig:1d}, regular patterns appear much less common.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure1c.eps}
\end{center}
\caption{Square test for congruential random number generator with $c = 2836, \: p = 127773$.}
\label{fig:1c}
\end{figure}

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure1d.eps}
\end{center}
\caption{Cube test for congruential random number generator with $c = 2836, \: p = 127773$.}
\label{fig:1d}
\end{figure}

\subsection{Task 2}
In polar coordinates, the angle $\phi$ can be chosen from a uniform distribution as the setup is invariant to rotation about the centre of the circle. The radius coordinate~$r$ however needs to be transformed $r \gets \sqrt{z} \;, \; z \sim \text{Unif} (0,1) $ as sampling from a uniform distribution would yield too much mass near the centre of the circle. Plotting 200 random numbers generated with $c = 2836, \: p = 127773$ on a circle with radius $R=1$ and center $(0, 0)$ yields the result shown in figure~\ref{fig:2}.

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.9]{figure2.eps}
\end{center}
\caption{Output of congruential random number generator with $c = 2836, \: p = 127773$ plotted on circle with radius $R=1$ and center $(0, 0)$.}
\label{fig:2}
\end{figure}

\subsection{Task 3}
The code as submitted with this report was run several times to generate 2000 random numbers distributed over $k=10$ bins.\\
For $c = 3, \: p = 31$, the average score was $\chi^2 = 0.038$ which seems extremely unlikely when comparing to Knuth's table~\cite{knuth}.\\
For $c = 2836, \: p = 127773$, the average score was $\chi^2 = 6.778$ which is around the $p=40\%$ mark on Knuth's table~\cite{knuth}.\\
Clearly and as expected, $c = 3, \: p = 31$ make for a poor result in terms of randomness, whereas $c = 2836, \: p = 127773$ already achieve quite a respectable score.

\section{Discussion}
The results were in line with the theoretical expectations from class. I personally had issues getting decent plots as I had shied away from Gnuplot until now. Also, my c++ is somewhat rusty and I apologise for my somewhat ugly code.

\begin{thebibliography}{99}

\bibitem{knuth}
  Knuth, Ervin D.,
  \emph{The art of computer programming},
  Addison Wesley, Massachusetts,
  3rd edition,
  1997.

\end{thebibliography}

\end{document}


\documentclass{article}

\title{Complexity Measures}
\author{Vladimir Feinberg}

\input{../defs}

\begin{document}

\maketitle

Complexity measures evaluate the expressiveness of a hypothesis class; they are useful to the extent with which they relate sample and generalization error.

\section{Setup}

We suppose that our data comes in the form of ordered pairs from $\mcX\times \mcY$. Samples follow a particular distribution $(x, y)\sim D$. A hypothesis class $\mcH$ is set of functions $\mcX\rightarrow\mcY$.

A common approach to supervised learning is ERM, where $m$ iid samples from $D$, $S$, are used to find the $h\in\mcH$ minimizing a specified loss $\ell:\mcY^2\rightarrow\R$ over this set. Complexity measures then let us quantify exactly how much loss we can expect when sampling from $D$ again.

We seek to quantify the generalization gap with the help of our notions of complexity. For a fixed $h\in \mcH$:

$$
\varepsilon= \E\left[\ell\pa{h(x), y)}|(x,y)\sim D\right]-\E\left[\ell\pa{h(x), y)}|(x,y)\sim \Uniform(S)\right]
$$

Analysis of Rademacher complexity is agnostic to $h,\ell$; the hypothesis class might as well consist of functions $g:\mcX\times\mcY\rightarrow\R$ yielding their composition. VC dimension analysis, however, requires $\mcY=\{0, 1\}$ and $\ell(a, b)=\indicator\{a=b\}$. VC dimension is still useful for regression problems, by thresholding hypotheses $h\mapsto \indicator{h>\beta}$ for fixed $\beta$.\footnote{\url{https://stats.stackexchange.com/questions/140430}}

Thus, it is useful to find bounds on $\varepsilon$, the difference between the generalization loss $\E\left[\ell\pa{h(x), y)}\right]$, where $(x,y)\sim D$, and sample loss, where the loss is the expectation before taken for $(x,y)$ is uniform over $S$.

Let the gap between the generalization and sample error be $\varepsilon$.

\section{Complexity Measures}

The empirical Rademacher complexity $\hat{R}_S$ assumes a fixed sample $S$ from $D^m$. It relates complexity of a function class $\mcG$ containing vectorized functions $g\in \mcG$ which take elements $z_i=(x_i, y_i)$ in $S$ and return costs through the correlation of $\mcG$ with noise. Let $\vsigma\sim \Uniform\pa{\pm 1}^m$. Rademacher complexity is then the average empirical one.
$$
\hat{R}_S(\mcG)=\E_\vsigma \sup_g \frac{1}{m}\sum_{i=1}^m{g(z) \sigma_i},\;\;\; R_m(\mcG)=\E_S\hat{R}_S(\mcG)
$$

VC dimension accomplishes a similar task for binary classification by rating the complexity of a hypothesis class $\mcH$. Let hypotheses $\mcH\ni h:\mcX\rightarrow \mcY=\ca{\pm 1}$ be applied elementwise over a vector of inputs $\vx$. First we define the growth function $\Pi_\mcH:\N\rightarrow\N$, which defines the maximum number of distinctions a hypothesis class can make over all sets of points in the input space:
$$
\Pi_\mcH(m)=\max_{\vx\in\mcX^m}\card{\set{h(\vx)}{h\in\mcH}}
$$
Then the VC dimension of $\mcH$ is then $\max\set{m\in\N}{\Pi_\mcH(m)=2^m}$.

\section{Overview of Results}

Proofs can be found in a \nurl{http://ittc.ku.edu/~beckage/ml800/VC_dim.pdf}{cogent write-up} by Prof. Beckage from the University of Kansas.

\subsection{VC Generalization Bounds}

Upper bound. If $d$ is the VC-dimension of $\mcH$, then for any $D$ wp $1-\delta$:
$$
\varepsilon\le \tilde{O}\pa{\sqrt{\frac{d-\log \delta}{m}}}
$$
The above inequality is random since it depends on $S$, the $D^m$-valued rv. TODO. find source removing tilde?

Agnostic lower bound. We may find a $D$ such that with a fixed nonzero probability (a non-negligible set of candidate samples $S$), the following holds:
$$
\varepsilon\ge \Omega\pa{\sqrt{\frac{d}{m}}}
$$

The above implies that in the common case of agnostic hypothesis learning, where we do not know distribution $D$, VC-dimension is, \textit{up to logarithmic factors, asymptotically efficient} in quantifying the generalization gap.

Realizability. Suppose $D$ is realizable wrt $\mcH$, so that there exists an $f\in\mcH$ such that for almost any $(x,y)$ sampled from $D$, $f(x)=y$. Then all statements above hold but with $\sqrt{\varepsilon}$ instead of $\varepsilon$.

\subsection{Growth Function Bounds}

Sauer's Lemma implies that VC dimension $d$ bounds the growth function: in a graph of the logarithm of the growth function vs $m$, growth is linear since $\Pi_\mcH(n)=n$ for $n\le d$. Then for $n>d$, growth is at most logarithmic, i.e., $\log\Pi_\mcH=O(\log m)$. With Massart's Lemma we have wp $1-\delta$:
$$
\varepsilon\le O\pa{\sqrt{\frac{\log\Pi_\mcH(m)-\log\delta}{m}}}
$$
Since the above would be large if $\log\Pi_\mcH(m)\simeq m$, it is clear why Sauer's Lemma enables the essential relationship between learnability and complexity.

\subsection{Rademacher bounds}

With $R_m$ either the empirical or expected Rademacher complexity over the sample for a given $h,\ell$ we have again wp $1-\delta$:
$$
\varepsilon\le 2R_m+O\pa{\frac{\log\nicefrac{1}{\delta}}{m}}
$$
$R_m$ may be NP-hard to compute, depending on $\mcH$. This tells us Rademacher complexity could only be a useful improvement over VC-bounds, asymptotically, if we have an efficient approximation for the empirical Rademacher complexity or some knowledge of $D$ as required to compote the true Rademacher complexity.

\section{Hardness of Learning}

Rademacher and Gaussian Complexities: Risk Bounds and Structural Results by Bartlett and Mendelson.

\end{document}


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{amsmath}

\usepackage[hmargin=3cm,vmargin=6.0cm]{geometry}
%\topmargin=0cm
\topmargin=-2cm
\addtolength{\textheight}{6.5cm}
\addtolength{\textwidth}{2.0cm}
%\setlength{\leftmargin}{-5cm}
\setlength{\oddsidemargin}{0.0cm}
\setlength{\evensidemargin}{0.0cm}

%misc libraries goes here

\begin{document}

\section*{Student Information }
%Write your full name and id number between the colon and newline
%Put one empty space character after colon and before newline
Full Name : ERTUĞRUL AYPEK \\
Id Number : 2171270 \\

% Write your answers below the section tags
\section*{Answer 1}
\begin{table}[H]
\small
\centering
\caption{ Membership Table for question 1.a }

\begin{tabular}{|c|c|c|c|c|c|c|c|c|}	%% specify column number
\hline 							%% line draw
\textbf{A} & \textbf{B} & \textbf{$\overline{A}$} & \textbf{$\overline{B}$} & \textbf{A $\cap$ B} & \textbf{A $\cup$ $\overline{B}$} & \textbf{$\overline{A}$ $\cup$ B} & \textbf{(A $\cup$ $\overline{B}$) $\cap$ ($\overline{A}$ $\cup$ B)} & \textbf{A $\cap$ B $\subseteq$ (A $\cup$ $\overline{B}$) $\cap$ ($\overline{A}$ $\cup$ B)}\\
\hline

1 & 1 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \\ \hline
1 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 1 \\ \hline
0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 \\ \hline
0 & 0 & 1 & 1 & 0 & 1 & 1 & 1 & 1 \\
\hline

\end{tabular}
\end{table}

As shown in the above table, the statement "(A $\cap$ B)$\subseteq$(A $\cup$ $\overline{B}$) $\cap$ ($\overline{A}$ $\cup$ B)" is true.
\\
\\
\\

\begin{table}[H]
\small
\centering
\caption{ Membership Table for question 1.b }

\begin{tabular}{|c|c|c|c|c|c|c|c|c|}	%% specify column number
\hline 							%% line draw
\textbf{A} & \textbf{B} & \textbf{$\overline{A}$} & \textbf{$\overline{B}$} & \textbf{$\overline{A}$ $\cap$ $\overline{B}$} & \textbf{A $\cup$ $\overline{B}$} & \textbf{$\overline{A}$ $\cup$ B} & \textbf{(A $\cup$ $\overline{B}$) $\cap$ ($\overline{A}$ $\cup$ B)} & \textbf{$\overline{A}$ $\cap$ $\overline{B}$ $\subseteq$(A $\cup$ $\overline{B}$) $\cap$ ($\overline{A}$ $\cup$ B)}\\
\hline

1 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline
1 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 1 \\ \hline
0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 \\ \hline
0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\hline

\end{tabular}
\end{table}

As shown in the fifth and eighth columns of above membership table for question 1.b, the statement "($\overline{A}$ $\cap$ $\overline{B}$)$\subseteq$(A $\cup$ $\overline{B}$) $\cap$ ($\overline{A}$ $\cup$ B)" is true.

\section*{Answer 2}
In order to prove this equation we need to show each side is a subset of the other. \\
Proving right side is a subset of the left side:

Since f has an inverse on the domains of $\textit{A}$x$\textit{C}$ and $\textit{B}$x$\textit{C}$, f is one-to-one and onto on these domains. Which means there must be a unique image for each element of $\textit{f}$ and likewise $\textit{f}^{-1}$. This allows us to do followings:
\\
\\
$\textit{f}^{-1}$( ( A $\cap$ B ) x C) $\supseteq$ $\textit{f}^{-1}$ (A x C) $\cap$ $\textit{f}^{-1}$ (B x C) \\
$\textit{f}^{-1}$( ( A $\cap$ B ) x C) $\supseteq$ $\textit{f}^{-1}$ ( ( A x C) $\cap$ ( B x C ) ) ...(because f has inverse, explained at 2. paragraph) \\
$\textit{f}^{-1}$( ( A $\cap$ B ) x C) $\supseteq$ $\textit{f}^{-1}$ ( ( A $\cap$ B ) x C ) ...(by Distributive laws)
\\
\\
\\
\\
\\
Doing the same to prove left side is a subset of the right side: \\
$\textit{f}^{-1}$( ( A $\cap$ B ) x C) $\subseteq$ $\textit{f}^{-1}$ (A x C) $\cap$ $\textit{f}^{-1}$ (B x C) \\
$\textit{f}^{-1}$ ( ( A x C) $\cap$ ( B x C ) ) $\subseteq$ $\textit{f}^{-1}$ (A x C) $\cap$ $\textit{f}^{-1}$ (B x C) ...(by Distributive laws)\\
$\textit{f}^{-1}$ (A x C) $\cap$ $\textit{f}^{-1}$ (B x C) $\subseteq$ $\textit{f}^{-1}$ (A x C) $\cap$ $\textit{f}^{-1}$ (B x C)  ...(because f has inverse, explained at 2. paragraph) \\
\\
\\
So, the equation is true.

\section*{Answer 3}

$\textbf{a.}$
\\
$\textit{f(x)}$ = $\textit{$ln( x^{2} + 5)$}$ is not one-to-one since for x=5 $\in\Re$ and for x=-5 $\in\Re$ there is the same image $\textit{ln(30)}$
\\

$\textit{f(x)}$ = $\textit{ln( $x^{2}$ + 5)}$ is not onto since there is no element in domain so that $\textit{$ln( x^{2} + 5)$}$=0 $\Rightarrow$ ($x^{2}$=-4).
\\ \\ \\ \\
$\textbf{b.}$
\\
$\textit{f(x)}$ = $e^{e^{x^{7}}}$ is one-to-one since for an arbitrary a $\in\Re$, there is a unique image under f which is $e^{e^{a^{7}}}$ $\in\Re$ \\

$\textit{f(x)}$ = $e^{e^{x^{7}}}$ is not onto since for $e^{e^{x^{7}}}$ = 0 $\Rightarrow$ $x^{7}$ = log0, there is no element x $\in\Re$. \\

\section*{Answer 4}
$\textbf{Answer for 4.a}$ \\
There are three cases to consider: \\
(i)A and B are both finite, \\
(ii)A is infinite and B is finite,\\
(iii) A and B are both countably infinite.\\
\\
case(i): When A and B are both finite, AxB is also finite and therefore, countable. \\
csae(ii): Because A is countably infinite, its 	elements can be listed in an infinite sequence $a_{1}$, $a_{2}$, ... ,$a_{n}$, ... and because B is finite, its terms can be listed as 	$b_{1}$,$b_{2}$, ... ,$b_{m}$ for some positive integer m. We can list the elements of AxB as ($a_{1}$,$b_{1}$),($a_{1}$,$b_{2}$), ... ,($a_{1}$,$b_{m}$), ... ,($a_{n}$,$b_{1}$),($a_{n}$,$b_{2}$), ..., ($a_{n}$,$b_{m}$), ... \\
case(iii): Because A and B are both countably infinite, its elements can be listed in an infinite sequence. Their elements can be listed as $a_{1}$, $a_{2}$, ... ,$a_{n}$, ... and $b_{1}$, $b_{2}$, ... ,$b_{n}$, ... respectively. And we can list the elements of AxB by alternating and putting into tuples as ($a_{1}$,$b_{1}$), ($a_{1}$,$b_{2}$),($a_{2}$,$b_{1}$), ($a_{2}$,$b_{2}$), ($a_{1}$,$b_{3}$), ($a_{3}$,$b_{1}$), ($a_{2}$,$b_{3}$), ($a_{3}$,$b_{2}$), ($a_{3}$,$b_{3}$), ... \\
\\
\\
$\textbf{Answer for 4.b}$ \\
If A is uncountable and A$\subseteq$B, then B is uncountable. Suppose A represents all real numbers between (0,1). A is an uncountable set as proved in the textbook p.173-174. And suppose B represents all real numbers between (0,2). B has two parts: (0,1) and (1,2). As we said that the set of all real numbers between (0,1) is uncountable, B has this uncountable part too. So we say that a set with an uncountable subset is uncountable. So, if A is uncountable and A$\subseteq$B, then B is uncountable.
\\
\\
\\
$\textbf{Answer for 4.c}$ \\
If B is countable and A$\subseteq$B, then A is countable. Suppose B represents the positive integers such that there is a one-to-one correspondence function f(x)=x from $Z^{+}$ to B. And suppose A represents the positive even integers such that f(k)=t where t=2k k$\in$ $Z^{+}$. B can be listed as $b_{1}$=1, $b_{2}$=2, $b_{3}$=3, ... , $b_n$=n, ... and A can be listed as $a_{1}$=2, $a_{2}$=4, $a_{3}$=6, ... , $a_n$=2n, ... So we say that any countable subset of a countable set is countable. So, if B is countable and A$\subseteq$B, then A is countable.

\section*{Answer 5}
\textbf{Answer for 5.a} \\
To prove that we need to find a pair of witnesses C and k.
\textbf{Answer for 5.b} \\
To prove that we need to find a pair of witnesses C and k.
\section*{Answer 6}
\textbf{Answer for 6.a} \\
($3^{x}$-1)\textit{mod}($3^{y}$-1)=$3^{\textit{x mody}}$-1 \\
Using Theorem 4 in p.241 in textbook to write the left side differently; \\
($3^{x}$-1)\textit{mod}($3^{y}$-1) = $3^{x}$-1 + $k_{1}$*($3^{y}$-1)\\
Using Theorem 4 in p.241 in textbook to write the right side differently; \\
$3^{\textit{x mody}}$-1=$3^{x+k_{2}y}$-1\\
\\
$3^{x}$-1 + $k_{1}$*($3^{y}$-1) = $3^{x+k_{2}y}$-1 \\
The equation holds when $k_{1}$=0 and $k_{2}$=0.

\textbf{Answer for 6.b} \\
277= 123*2 + 31 \\
123= 31*3 + 30 \\
31= 30*1 + 1 \\
30=1*30 + 0 \\
gcd(277,123)=gcd(123,31)=gcd(31,30)=1 \\

\end{document}


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

\marginpar{Tuesday\\ 2020-9-29, \\ compiled \\ \today}

The procedure for hypothesis testing will look like
%
\begin{align}
\mathbb{P}(\text{hypothesis} | \text{data})
= \frac{\mathbb{P}(\text{data} | \text{hypothesis}) \mathbb{P}(\text{hypothesis}) }{\mathbb{P}(\text{data})}
\,.
\end{align}

The key difference from the frequentist approach is that, while there the parameters have certain fixed values, here we can describe our \emph{belief} about their values through a probability distribution.

The things we will want to do can be classified into
\begin{enumerate}
    \item \textbf{hypothesis testing}, ``are CMB data consistent with gaussianity?'';
    \item \textbf{parameter estimation}, ``what is the value of the mass of the Sun?'';
    \item \textbf{model selection}, ``is GR the correct theory of gravity?''.
\end{enumerate}

\subsection{Parameter estimation}

We start with an example: the toss of a coin.
The question is: we toss it \(N\) times and get \(R\) heads. Is it a fair coin?

If \(H \in [0,1]\) is the probability of getting heads in a single coin flip, then
%
\begin{align}
\mathbb{P}(R \text{ heads} | H, I) \propto H^{R} (1 - H)^{N-R}
\,.
\end{align}

Here, \(I\) is the other information we have about the coin: the fact that every throw is independent, the fact that there are no outcomes beyond heads or tails.

This is the \textbf{likelihood}, what we want to do is to invert the relation, finding a probability density function for \(H\) given the data. The probability \(\mathbb{P}(\text{data})\), also called the \textbf{evidence}, is not something we need to calculate when doing parameter estimation: we can just write
%
\begin{align}
\mathbb{P}(\text{parameters} | \text{data}) \propto \mathbb{P}(\text{data} | \text{parameters}) \mathbb{P}(\text{parameters})
\,,
\end{align}
%
since we are computing probability density functions, which need to be normalized in order to make sense.
This is a useful parameter estimation toy problem, since we only have one parameter to estimate.

In order to use the formula we need a prior, \(\mathbb{P}(\text{hypothesis})\). This is hard in general, and it depends on the problem.
We might want a prior which is peaked around \num{.5} for a regular coin, if we have no reason to think that it is unfair.
Let us suppose we have doubts about the honesty of who gave us the coin: then, we might want a noninformative prior, like a flat one.
Let us suppose we are in this case: \(\mathbb{P}(\text{parameters}) = \const\).

Since the prior is flat, the posterior is proportional to the likelihood:
%
\begin{align}
\mathbb{P}(H | R \text{ heads}, I) \propto
\mathbb{P}(R \text{ heads} | H, I)
\propto H^{R} (1 - H)^{N-R}
\,.
\end{align}

We can simulate this experiment! We need a binomial random number generator.

\todo[inline]{Do the simulation!}

As \(N\) increases, the posterior ``zeroes in'' onto the correct value.
If we split the \(N\) simulated throws in two, and use the posterior from the first batch as a prior for the second, we get the same result!

% \todo[inline]{What would be the procedure if we wanted to test the hypothesis ``the coin is fair''?}

\end{document}


% Created 2021-12-12 Sun 15:49
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\author{Calvin Roth}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={Calvin Roth},
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 27.2 (Org mode 9.5)},
 pdflang={English}}
\begin{document}

\newcommand{\pr}{$\mathbb{Pr}$}

\newcommand{\ppr}{\mathbb{P}\mathbb{r}}

\section{Setup}
Fix G, a directed erdos-renyi graph, let $Dom_{G}$ be the set of graphs that have the same degree sequence as G. Typically, we will use H to be a member of this set. Additionally, let $p(H)$ be profit vector of H. Throughtout this analysis all parameters are fixed.
\pr
\textbf{Primary Questions}
\begin{enumerate}
  \item What is $\| H + H^{T}\|$(What norm is the right norm is a good question) \\
  \item Let q(H) be an altered price vector, where we fix the norms terms. What is the distribution of q(H) \\
  \item What is probability distribution of these price vectors in the doman H. \\
\end{enumerate}

We want to resolve the question of what is $Pr[v(H)_{i} = k]$
\begin{align*}
  \ppr [v(H)_{i} = ] &= \ppr [ \frac{a-c}{2} + \frac{a-c}{2} \frac{\rho}{\|H+H^{T}} K(H+H^{T}, \frac{\rho}{\|H+H^{T}\|}) = k ]\\
  &= \ppr [\frac{1}{\|H+H^{T}\|} K(H^{T}+H, \frac{\rho}{\|H+H^{T}}) = \underbrace{( \frac{2}{\rho(a-c)} [k - \frac{a-c}{2}] )}_{k'}]
\end{align*}

\subsection{Number of kth neighbors}
We will define two generating functions.

The first will be $g_{0}(z) = \sum_{k=0} p_{k} z^{k}$ where $p_{k}$ is the probabality of a given node having degree k. We know that $p_{k} \approx \frac{e^{-\lambda} \lambda^{k}}{k!}$ where $\lambda = (n-1)p$. Therefore $g_{0}(z) = \sum_{k=0} \frac{e^{-\lambda}\lambda^{k}}{k!} z^{k} = e^{-\lambda} e^{z\lambda}$.

The next generating function will be of the distribution of the degree size of a neighboring vertices.  We analyzed this in networks class. We will call if $g_{1} = (z)$ and the probability distribution of this neighbor $p_{k}^{(2)}$. We have
\begin{align*}
  g_{1}(z) &= \sum_{k=0} p_{k}^{(k)} z^{k} \\
       &= \sum_{k=0} \frac{k p_{k}}{E[d]} z^{k} \\
       &= \frac{1}{E[d]} \sum_{k=0} k p_{k} z^{k} \\
       &= \frac{1}{E[d]}  z D_{z} \sum_{k=0} p_{k} z^{k}\\
       &= \frac{1}{E[d]} z D_{z} (e^{-\lambda} e^{z\lambda}) \\
       &= \frac{z e^{-\lambda} \lambda e^{z\lambda}}{E[d]}
\end{align*}

To see why these are helpful, we will now derive the size of 2 distance neighbors from a node which we will call $d_{2}$.

\begin{align*}
  g_{2}(k) &= \sum p^{(2)}_{m} * z^{m} \underbrace{P_{2}(k|m)}_{\text{Distribution of 2 neighbors given starting node had degree m}} \\
           &= \sum_{m} p^{(2)}_{m} \left( \sum_{x_{1} + x_{2} + \cdots + x_{m} =k } \prod_{j} Pr[q_{x_{j}}]  \right) \\
           &= \sum_{m} p^{(2)}_{m} {(\sum q_{i} z^{i})}^{m} \\
           &= \sum_{m} p^{(2)}_{m} (d_{1}(z))^{m} \\
  &= g_{0}(g_{1}(z))
\end{align*}

To make this transition clear, consider what $(\sum_{i=0}^{\infty} q_{i} z^{i})(\sum_{i=0}^{\infty} q_{i} z^{i})$ organized by terms of z. The term of $z^{k}$ will have coefficient $(q_{0}q_{k} + q_{1}q_{k-1}+\cdots + q_{k-1}q_{1} + q_{k}q_{0} ) = \sum_{i+j=k} q_{i}q_{j}$.

For us this is interesting because $g_{0}(z)$ is completely fixed by the true graph G(or said alternatively is exactly the same for all the H graphs with the same degree sequence) but $g_{1}$ was not fixed directly by H. $d_{1}$ is a random function dependent on the graph it is based on. I'm not sure how this $g_{1}$ correlates with the first degree information which we fixed. Importantly, is the $g_{1}$ functions the same for our graphs vs all erdos-renyi graphs with the same n and p or does this also depend indirectly on the information we fixed. Ideally it is the first case and if so we would have shown that walk sizes are a function of the true graph's information applied to a random variable which if things are nice only depends on n and p.

We can continue this for higher distance walks. Now we will use $P_{3}(k|m)$ to be the probability that a node has k 3 distance given it has m 2-distance neighbors.  The three step generating function, $g_{3}(z)$, can be expressed as
\begin{align*}
  g_{3}(z) &= \sum_{k} \sum_{m} p^{2}_{m}  P_{3} (k|m) z^{k} \\
           &= \sum_{m} p^{2}_{m} \sum_{x_{1} + x_{2}+\cdots x_{m}=k} z^{k} \prod_{j}^{m} Pr[q_{x_{j}}] \\
           &= \sum_{m} p^{2}_{m} g_{1} (z)^{m} \\
  &= g_{2} ( g_{1} ( z)) = g_{0} ( g_{1} ( g_{1} ( z)))
\end{align*}

In general, the generating function representing the walks of length $\ell$ is $g_{0} ( g_{1}^{\ell-1} ( z))$

These have been very helpful and contains much of this analysis.
https://people.cs.clemson.edu/~isafro/ns14/l15.pdf
https://static.squarespace.com/static/5436e695e4b07f1e91b30155/t/5445263ee4b0d3d410795e1f/1413817918272/random-graphs-with-arbitrary-degree-distributions-and-their-applications.pdf

\section{Norms}
Currently unsure how I can bound the norms more tightly than what was done in the price discrimination paper.
\end{document}


% !TeX spellcheck = en_US
% !TeX encoding = UTF-8
\chapter{Code}

\begin{code}
\captionof{listing}{Calculating threshold for pruning}
\label{code:threshold}
\begin{minted}[frame=lines,bgcolor=code_bg,linenos]{python}
weights = []
for param, data in self.named_parameters():
    if 'bias' not in param and key in param:
        weights += list(data.cpu().data.abs().numpy().flatten())
threshold = np.percentile(np.array(weights), percent)
\end{minted}
\end{code}

\begin{code}
\captionof{listing}{Calculating binary mask}
\label{code:mask}
\begin{minted}[frame=lines,bgcolor=code_bg,linenos]{python}
masks = {}
for l, layer in enumerate(self.recurrent_layers):
    masks[l] = []
    for param, data in layer.named_parameters():
        if 'bias' not in param and key in param:
            mask = torch.ones(data.shape,
                              dtype=torch.bool,
                              device=data.device)
            mask[torch.where(abs(data) < threshold)] = False
            masks[l].append(mask)
\end{minted}
\end{code}

% -----------------------------------------------------------------------------------------------------------
% ------------------------------------------------- TABLE ---------------------------------------------------
% -----------------------------------------------------------------------------------------------------------
\chapter{Tables}\label{app:tables}

\begin{table}[h]
	\centering
	\begin{tabular}{|l|c|c|c|c|}
	    \hline
		 & \shortstack{\textbf{All}\\\textbf{ properties}} & \shortstack{\textbf{Only nodes}\\\textbf{ and edges}} & \shortstack{\textbf{Without nodes}\\\textbf{ and edges}} & \shortstack{\textbf{Only}\\\textbf{ variances}}\\
		\hline
		Random Forest $R^{2}$ & 0.61 & 0.71 & -0.05 & 0.06\\
		\hline\hline
		\textbf{Property} & \multicolumn{4}{|l|}{\textbf{Feature importance}}\\
		\hline
		layers & 0.04 & & 0.06 & \\
        nodes & 0.02 & 0.23 & & \\
        edges & 0.01 & 0.29 & & \\
        source\_nodes & 0.08 & 0.29 & & \\
        sink\_nodes & 0.11 & 0.19 & & \\
        diameter & 0.0 & & 0.0 & \\
        density & 0.03 & & 0.03 & \\
        average\_shortest\_path\_length & 0.03 & & 0.03 & \\
        eccentricity\_mean & 0.03 & & 0.04 & \\
        eccentricity\_var & 0.04 & & 0.06 & 0.21 \\
        eccentricity\_std & 0.05 & & 0.09 & \\
        degree\_mean & 0.03 & & 0.03 & \\
        degree\_var & 0.03 & & 0.06 & 0.17 \\
        degree\_std & 0.03 & & 0.04 & \\
        closeness\_mean & 0.04 & & 0.05 & \\
        closeness\_var & 0.07 & & 0.09 & 0.22 \\
        closeness\_std & 0.07 & & 0.08 & \\
        nodes\_betweenness\_mean & 0.05 & & 0.08 & \\
        nodes\_betweenness\_var & 0.08 & & 0.08 & 0.24 \\
        nodes\_betweenness\_std & 0.07 & & 0.07 & \\
        edge\_betweenness\_mean & 0.05 & & 0.06 & \\
        edge\_betweenness\_var & 0.02 & & 0.03 & 0.16 \\
        edge\_betweenness\_std & 0.02 & & 0.02 & \\
		\hline
	\end{tabular}
	\caption[RNN\_ReLU - Feature importance scores for the Random Forest regressor under different circumstances]{Structural properties of the randomly structured RNN\_ReLU's base random graphs and their feature importance scores for the Random Forest regressor under different circumstances.}
	\label{tab:relu_fis}
\end{table}

\begin{table}[h]
	\centering
	\begin{tabular}{|l|c|c|c|c|}
	    \hline
		 & \shortstack{\textbf{All}\\\textbf{ properties}} & \shortstack{\textbf{Only nodes}\\\textbf{ and edges}} & \shortstack{\textbf{Without nodes}\\\textbf{ and edges}} & \shortstack{\textbf{Only}\\\textbf{ variances}}\\
		\hline
		Random Forest $R^{2}$ & 0.87 & 0.75 & 0.78 & 0.76\\
		\hline\hline
		\textbf{Property} & \multicolumn{4}{|l|}{\textbf{Feature importance}}\\
		\hline
		layers & 0.01 & & 0.04 & \\
        nodes & 0.01 & 0.11 & & \\
        edges & 0.01 & 0.12 & & \\
        source\_nodes & 0.67 & 0.71 & & \\
        sink\_nodes & 0.01 & 0.06 & & \\
        diameter & 0.0 & & 0.01 & \\
        density & 0.01 & & 0.02 & \\
        average\_shortest\_path\_length & 0.01 & & 0.02 & \\
        eccentricity\_mean & 0.01 & & 0.02 & \\
        eccentricity\_var & 0.02 & & 0.03 & 0.17 \\
        eccentricity\_std & 0.02 & & 0.03 & \\
        degree\_mean & 0.01 & & 0.03 & \\
        degree\_var & 0.01 & & 0.03 & 0.15 \\
        degree\_std & 0.01 & & 0.03 & \\
        closeness\_mean & 0.01 & & 0.03 & \\
        closeness\_var & 0.02 & & 0.11 & 0.36 \\
        closeness\_std & 0.01 & & 0.16 & \\
        nodes\_betweenness\_mean & 0.04 & & 0.11 & \\
        nodes\_betweenness\_var & 0.01 & & 0.15 & 0.25 \\
        nodes\_betweenness\_std & 0.02 & & 0.14 & \\
        edge\_betweenness\_mean & 0.03 & & 0.03 & \\
        edge\_betweenness\_var & 0.01 & & 0.02 & 0.08 \\
        edge\_betweenness\_std & 0.01 & & 0.02 & \\
		\hline
	\end{tabular}
	\caption[GRU - Feature importance scores for the Random Forest regressor under different circumstances]{Structural properties of the randomly structured GRU's base random graphs and their feature importance scores for the Random Forest regressor under different circumstances.}
	\label{tab:gru_fis}
\end{table}

% -----------------------------------------------------------------------------------------------------------
% ------------------------------------------------- PLOTS ---------------------------------------------------
% -----------------------------------------------------------------------------------------------------------

\chapter{Plots}

\section{Randomly structured RNN\_Tanh}\label{app:rs_tanh}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.4\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/graph_nodes.png}
        \caption{} \label{fig:tanh_graph_nodes}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.4\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/graph_edges.png}
        \caption{} \label{fig:tanh_graph_edges}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.4\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/graph_density.png}
        \caption{} \label{fig:tanh_graph_density}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.4\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/graph_layers.png}
        \caption{} \label{fig:tanh_graph_layers}
    \end{subfigure}

\caption[Comparison of basic graph properties and number of layers in WS and BA based RNN\_Tanh models]{Comparison of basic graph properties and number of layers in Watts–Strogatz and Barabási–Albert based RNN\_Tanh models} \label{fig:tanh_graphs}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/jointplot_test_acc_nodes.png}
        \caption{Correlation between test\_acc and the number of nodes} \label{fig:jp_tanh_node}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/jointplot_test_acc_closeness_var.png}
        \caption{Correlation between test\_acc and closeness in base random graph} \label{fig:jp_tanh_close}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/jointplot_test_acc_nodes_betweenness_var.png}
        \caption{Correlation between test\_acc and node betweenness in base random graph} \label{fig:jp_tanh_between}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/tanh/jointplot_test_acc_edges.png}
        \caption{Correlation between test\_acc and the number of edges} \label{fig:jp_tanh_edge}
    \end{subfigure}%

\caption[Correlation between test accuracy of RNN\_Tanh and its different graph and recurrent network properties - 1]{Correlation between test accuracy of RNN\_Tanh and its different graph and recurrent network properties} \label{fig:tanh_correlation}
\end{figure}

\newpage
\section{Randomly structured RNN\_ReLU}\label{app:rs_relu}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/graph_nodes.png}
        \caption{} \label{fig:relu_graph_nodes}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/graph_edges.png}
        \caption{} \label{fig:relu_graph_edges}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/graph_density.png}
        \caption{} \label{fig:relu_graph_density}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/graph_layers.png}
        \caption{} \label{fig:relu_graph_layers}
    \end{subfigure}

\caption[Comparison of basic graph properties and number of layers in WS and BA based RNN\_ReLU models]{Comparison of basic graph properties and number of layers in Watts–Strogatz and Barabási–Albert based RNN\_ReLU models} \label{fig:relu_graphs}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/jointplot_test_acc_nodes.png}
        \caption{Correlation between test\_acc and the number of nodes} \label{fig:jp_relu_node}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/jointplot_test_acc_edges.png}
        \caption{Correlation between test\_acc and the number of edges} \label{fig:jp_relu_edge}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/jointplot_test_acc_source_nodes.png}
        \caption{Correlation between test\_acc and the number of source nodes} \label{fig:jp_relu_source}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/relu/jointplot_test_acc_nodes_betweenness_var.png}
        \caption{Correlation between test\_acc and node betweenness in base random graph} \label{fig:jp_relu_between}
    \end{subfigure}

\caption[Correlation between test accuracy of RNN\_ReLU and its different graph and recurrent network properties - 1]{Correlation between test accuracy of RNN\_ReLU and its different graph and recurrent network properties} \label{fig:relu_correlation}
\end{figure}

\newpage
\section{Randomly structured LSTM}\label{app:rs_lstm}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/graph_nodes.png}
        \caption{} \label{fig:lstm_graph_nodes}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/graph_edges.png}
        \caption{} \label{fig:lstm_graph_edges}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/graph_density.png}
        \caption{} \label{fig:lstm_graph_density}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/graph_layers.png}
        \caption{} \label{fig:lstm_graph_layers}
    \end{subfigure}

\caption[Comparison of basic graph properties and number of layers in WS and BA based LSTM models]{Comparison of basic graph properties and number of layers in Watts–Strogatz and Barabási–Albert based LSTM models} \label{fig:lstm_graphs}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/jointplot_test_acc_nodes.png}
        \caption{Correlation between test\_acc and the number of nodes} \label{fig:jp_lstm_node}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/jointplot_test_acc_edges.png}
        \caption{Correlation between test\_acc and the number of edges} \label{fig:jp_lstm_edge}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/jointplot_test_acc_source_nodes.png}
        \caption{Correlation between test\_acc and the number of source nodes} \label{fig:jp_lstm_source}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/jointplot_test_acc_closeness_var.png}
        \caption{Correlation between test\_acc and closeness in base random graph} \label{fig:jp_lstm_close}
    \end{subfigure}

\caption[Correlation between test accuracy of LSTM and its different graph and recurrent network properties - 1]{Correlation between test accuracy of LSTM and its different graph and recurrent network properties - 1} \label{fig:lstm_correlation_1}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/jointplot_test_acc_nodes_betweenness_var.png}
        \caption{Correlation between test\_acc and node betweenness in base random graph} \label{fig:jp_lstm_node_bn}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/lstm/jointplot_test_acc_edge_betweenness_var.png}
        \caption{Correlation between test\_acc and edge betweenness in base random graph} \label{fig:jp_lstm_edge_bn}
    \end{subfigure}

\caption[Correlation between test accuracy of LSTM and its different graph and recurrent network properties - 2]{Correlation between test accuracy of LSTM and its different graph and recurrent network properties - 2} \label{fig:lstm_correlation_2}
\end{figure}

\newpage
\section{Randomly structured GRU}\label{app:rs_gru}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/graph_nodes.png}
        \caption{} \label{fig:gru_graph_nodes}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/graph_edges.png}
        \caption{} \label{fig:gru_graph_edges}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/graph_density.png}
        \caption{} \label{fig:gru_graph_density}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/graph_layers.png}
        \caption{} \label{fig:gru_graph_layers}
    \end{subfigure}

\caption[Comparison of basic graph properties and number of layers in WS and BA based GRU models]{Comparison of basic graph properties and number of layers in Watts–Strogatz and Barabási–Albert based GRU models} \label{fig:gru_graphs}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/jointplot_test_acc_nodes.png}
        \caption{Correlation between test\_acc and the number of nodes} \label{fig:jp_gru_node}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/jointplot_test_acc_edges.png}
        \caption{Correlation between test\_acc and the number of edges} \label{fig:jp_gru_edge}
    \end{subfigure}%

    \bigskip
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/jointplot_test_acc_source_nodes.png}
        \caption{Correlation between test\_acc and the number of source nodes} \label{fig:jp_gru_source}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/jointplot_test_acc_degree_var.png}
        \caption{Correlation between test\_acc and degree of the base random graph} \label{fig:jp_gru_degree}
    \end{subfigure}

\caption[Correlation between test accuracy of GRU and its different graph and recurrent network properties - 1]{Correlation between test accuracy of GRU and its different graph and recurrent network properties - 1} \label{fig:gru_correlation_1}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/jointplot_test_acc_closeness_var.png}
        \caption{Correlation between test\_acc and closeness in base random graph} \label{fig:jp_gru_close}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\linewidth]{images/results/random/gru/jointplot_test_acc_nodes_betweenness_var.png}
        \caption{Correlation between test\_acc and node betweenness in base random graph} \label{fig:jp_gru_node_bn}
    \end{subfigure}

\caption[Correlation between test accuracy of GRU and its different graph and recurrent network properties - 2]{Correlation between test accuracy of GRU and its different graph and recurrent network properties - 2} \label{fig:gru_correlation_2}
\end{figure}


\section{Code examples}
\label{sec:code_examples}

You can input equations like this if you want them on their own line:
\begin{equation}\label{eq:Q_def}
	\boxed{Q=\frac{\omega}{\Delta \omega_\mathrm{FWHM}}=\mathcal{F} \cdot \frac{\omega}{\Delta \omega_\mathrm{FSR}}}
\end{equation}

Note the \verb|\label{}| that permits to reference the equation everywhere. For example here \cref{eq:Q_def}. The equations can be inline $ Q=\frac{\omega}{2 \gamma} $.

You can include images and refer to them as shown in \cref{fig:turtle}.

\begin{figure}[ht]
	\centering
	\includegraphics[width=0.8\textwidth]{./fig/ch2/tirturtle.jpg}
	\caption{\label{fig:turtle} A picture of a sea turtle showing a wonderful animal and the effect on total internal reflection on the surface of the sea.}
\end{figure}

You can use the package chemformula to quickly input material names like this \ch{CaF2}.

It is a good idea to use the \verb|siunitx| package for the quantities since it prevents the number splitting from the unit, reduces the space between the two and can format strange units correctly. An example is \SI{0.5}{\micro m / \celsius}.


\documentclass{beamer}
\usetheme{Warsaw}
\usepackage{nhtvslides}
\usepackage{graphicx}
\usepackage{listings}
\lstset{language=CAML,
basicstyle=\ttfamily\footnotesize,
frame=shadowbox,
breaklines=true}
\usepackage[utf8]{inputenc}

\title{Building a physics engine - part 4: broad phase of collision detection}

\author{Dr. Giuseppe Maggiore}

\institute{NHTV University of Applied Sciences \\
Breda, Netherlands}

\date{}

\begin{document}
\maketitle

\begin{frame}{Table of contents}
\tableofcontents
\end{frame}

\section{Broad phase of collision detection}
\begin{slide}{Broad phase of collision detection}{Increasing performance, in general}{
\item What is the fastest instruction?
\pause
\item The one that is not run!
}\end{slide}

\begin{slide}{Broad phase of collision detection}{Increasing performance, in collision detection}{
\item How do we increase performance in a collision detection system?
\item Quickly and cheaply exclude pairs of colliders
\item Process known as \textit{collision culling}
\begin{itemize}
\item We \textit{ensure lack} of collisions
\item \textit{Presence is ensured} only during narrow phase
\end{itemize}
\item Akin to frustum/occlusion culling
}\end{slide}

\section{Bounding spheres}
\begin{slide}{Collision culling}{Bounding spheres}{
\item An obvious choice is bounding spheres
\item Identical w.r.t. rotation
\item Fast to check against other spheres
}\end{slide}

\begin{slide}{Collision culling}{Intersection of bounding spheres}{
\item Two spheres, $\langle C_0, r_0 \rangle$ and $\langle C_1, r_1 \rangle$
\item Intersection when $|C_1 - C_0| \leq r_1 + r_0$
\item Intersection also when $|C_1 - C_0|^2 \leq (r_1 + r_0)^2$
}\end{slide}

\begin{slide}{Collision culling}{Intersection of bounding spheres}{
\item If the spheres are moving, then we can increase their radii by their speed
\item Or we can project their relative speed
\pause
\item $\sigma = |(V_1 - V_2) \cdot \frac{C_1 - C_0}{|C_1 - C_0|}|$
\item $|C_1 - C_0| \leq r_1 + r_0 + \sigma$
}\end{slide}

\begin{frame}{Moving spheres}
\center
\includegraphics[height=5cm]{Pics/MovingSpheres.png}
\end{frame}

\section{Space partitioning}
\begin{slide}{Space partitioning}{Space partitioning}{
\item We can also decompose space in axis-aligned-bounding-boxes (``bins'')
\item Even earlier no-collision determination
\item This would reduce the number of sphere-to-sphere checks
}\end{slide}

\begin{frame}{SLIDE}
\center
\includegraphics[height=5cm]{Pics/SpacePartitioning.png}
\end{frame}

\begin{slide}{Space partitioning}{Space partitioning}{
\item We can divide space in \textit{bins}; each bin is an AABB
\item Sphere intersection with an AABB bounded by points $L$ and $U$
\item No intersection if $|C_j - L_j| \leq r_j + \sigma_j$ or $C_j - U_j| \leq r_j + \sigma_j$ for all axes $j = x,y,z$
}\end{slide}

\begin{slide}{Space partitioning}{Space partitioning}{
\item When a sphere moves, it only moves to a neighbouring bin; less checks
\item We can find the right bin directly with modulus operations (hashing)
}\end{slide}

\section{Bounding boxes}
\begin{slide}{Axis aligned bounding boxes}{AABB intersection}{
\item A simple and powerful algorithm exists for determining intersection groups of AABBs
\item It is particularly fast, especially if the AABBs do not move too much between frames
}\end{slide}

\begin{slide}{Axis aligned bounding boxes}{AABB intersection}{
\item Update AABBs (if needed)
\item Insertion sort the extremes of each box; one list for every axis (2 for 2D, 3 for 3D, etc.)
\begin{itemize}
\item After the first frame the list is \textit{nearly sorted}
\item $O(n)$ complexity
\end{itemize}
}\end{slide}

\begin{slide}{Axis aligned bounding boxes}{AABB intersection}{
\item Run sweep algorithm
\begin{itemize}
\item Active intervals $= \emptyset$
\item When a beginning value is encountered, add it as intersecting all active intervals; add it to active intervals
\item When end value is encountered, remove it from the active intervals
\end{itemize}
\item Intersections must be confirmed across all axes
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB}{
\item An OBB is characterized by a center and three directions (columns of the rotation matrix)
\item The vertices are $P = C + \sigma_0 e_0 U_0 + \sigma_1 e_1 U_1 + \sigma_2 e_2 U_2$
\begin{itemize}
\item $\sigma_i = 1$ or $\sigma_i = -1$
\item $e_i$ are the half extents
\end{itemize}
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT}{
\item With the separating axis test, it may seem that we need to test $6$ face normals for one, $6$ for the other, and $12^2 = 144$ edge pair cross products
\item That's quite a lot!
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT}{
\item The OBB is symmetric, so many tests are redundant
\begin{itemize}
\item Three unique face directions
\item Three unique edge directions
\end{itemize}
\item The minimum number of required tests is $3$ face normals for one, $3$ for the other, and $3^2 = 9$ edge pair cross products
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT}{
\item We project both OBBs onto one of the unique potential separating directions $Q + tD$
\item We look for an extremal vertex such that $\max_P D \cdot (P - Q)$
\item $D \cdot (P - Q) = D \cdot (C + \sigma_0 e_0 U_0 + \sigma_1 e_1 U_1 + \sigma_2 e_2 U_2 - Q)$
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT}{
\item We are maximizing, so we do not try all the $\sigma_i$ combinations
\begin{eqnarray}
D \cdot (P - Q) &=& D \cdot (C + \sigma_0 e_0 U_0 + \dots  - Q) \\
&=& D \cdot (C - Q) + \sigma_0 e_0 D \cdot U_0 + \dots \\
&=& D \cdot (C - Q) + \sum_{i=0}^2 |e_i D \cdot U_i|
\end{eqnarray}
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT}{
\item Maximization results in $\max_P D \cdot (P - Q) = \underbrace{D \cdot (C - Q)}_{\gamma} + \underbrace{\sum_{i=0}^2 |e_i D \cdot U_i|}_{r}$
\item Minimization results in $\min_P D \cdot (P - Q) = D \cdot (C - Q) - \sum_{i=0}^2 |e_i D \cdot U_i|$
\item The interval is thus $[\gamma - r, \gamma + r]$
\item \textbf{Important:} the separating directions \textit{must be unit length}, and the edges as well
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT}{
\item We project both OBBs onto their intervals $[\gamma_1 - r_1, \gamma_1 + r_1]$ and $[\gamma_2 - r_2, \gamma_2 + r_2]$
\item They intersect when $|\gamma_2 - \gamma_1| < r_1 + r_2$
}\end{slide}

\begin{slide}{Oriented bounding boxes}{OBB SAT - final optimization}{
\item Some separating directions are taken from the cross product of two edge directions: $D = U_i^1 \times U_k^2$
\item When we plug those directions $D$ in the above formulas, we get $U_i^1 \times U_k^2 \cdot U_j^1$
\item We can rewrite $U_i^1 \times U_k^2 \cdot U_j^1 = U_i^1 \times U_j^1 \cdot U_k^2$
\item We can cache the products $U_i^1 \times U_j^1$, so we do not have to recompute them
}\end{slide}

\begin{slide}{Moving objects}{Moving objects}{
\item We ignore the angular velocity; does not improve much, and is very complex to handle
\item We can enlarge the interval radii by projecting the current relative velocity onto the separating direction
\item $s = D \cdot (V_2 - V_1)$
}\end{slide}

\section{Further optimizations}
\begin{slide}{Further optimizations}{Islands}{
\item Apply collision response system only to groups of objects in contact
\item Flood-fill algorithm
}\end{slide}

\begin{slide}{Further optimizations}{Sparse matrices for collision response}{
\item Avoid multiplying lots of zeroes
\item Store matrix row as list of \texttt{int * float} entries
}\end{slide}

\section{Assignment}
\begin{slide}{Assignment}{Assignment}{
\item Before the end of next week
\item Group-work archive/video on Natschool or uploaded somewhere else and linked in your report
\item Individual report by each of you on Natschool
\item Build a broad phase collision detector that supports a combination of bounding spheres, AABBs, bins, and OBBs
}\end{slide}

\begin{frame}{That's it}
\center
\fontsize{18pt}{7.2}\selectfont
Thank you!
\end{frame}

\end{document}

\begin{slide}{SECTION}{SLIDE}{
\item i
}\end{slide}

\begin{frame}[fragile]{SLIDE}
\begin{lstlisting}
CODE
\end{lstlisting}
\end{frame}

\begin{frame}{SLIDE}
\center
%\includegraphics[height=5cm]{Pics/recursive_multiplier.png}
\end{frame}