\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}


\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}


\documentclass[a4paper,english,notitlepage,longbibliography,showpacs,preprintnumbers,amsmath,amssymb,aps,prx,nofootinbib,12pt,superscriptaddress]{revtex4-1}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{comment}
\usepackage{hyperref}
\hypersetup{
 bookmarks=true,                % show bookmarks bar?
 unicode=false,                 % non-Latin characters in Acrobat’s bookmarks
 pdftoolbar=true,               % show Acrobat’s toolbar?
 pdfmenubar=true,               % show Acrobat’s menu?
 pdffitwindow=false,            % window fit to page when opened
 pdfstartview={FitH},           % fits the width of the page to the window
 pdftitle={SFR derivations},    % title
 pdfauthor={Szil{\'a}rd Szalay},        % author
 pdfsubject={},       % subject of the document
 pdfcreator={pdflatex},         % creator of the document
 pdfproducer={vim},             % producer of the document
 pdfkeywords={SFR} {BCR}, % list of keywords
 pdfnewwindow=true,             % links in new window
 colorlinks=true,               % false: boxed links; true: colored links
 linktoc=page,                  % defines which part of an entry in the table of contents is made into a link
%%%%%%%%% colored links
 linkcolor=blue,                % color of internal links       (red)
 citecolor=blue,                % color of links to bibliography
 filecolor=magenta,             % color of file links
 urlcolor=blue                  % color of external links
}
%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[inline]{showlabels}
\renewcommand{\showlabelfont}{\footnotesize\ttfamily\color{OliveGreen}}
%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\ceil}[1]{\lceil #1 \rceil}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{SFR-BCR}

\noindent Binary variables: $x_1,x_2,\dots,x_n\in\{0,1\}$,
and let $X:=\sum_{i=1}^n x_i$, $2\leq k\in\mathbb{N}$, $l = \ceil{\log k}$

\subsection{SFR-BCR-1,2}
\noindent We have the $l+1$ auxiliary variables $y_0,y_1,\dots,y_{l-1},z\in\{0,1\}$.
(In the formulas, summations for $i,i' = 1,2,\dots,n$ and $j,j' = 0,1,\dots,l-1$ are understood.)\\

\noindent We have the function (See Observation 1. from \cite{Boros2018QuadratizationsOS}) %% Would a proper citation be needed
\begin{equation}
\label{eq:Ak}
A_k(X,y,z) = X - (k-2^l)z - (k+1)(1-z) - \sum_{j}2^jy_j.
\end{equation}
After substituting for X, and collecting the terms, the equation for SFR-BCR-1 becomes
\begin{equation}
\label{eq:Ak1}
A_k(X,y,z) = -(1+k) +\sum_i x_i - \sum_{j}2^jy_j + (1+2^l)z.
\end{equation}
Similarly for SFR-BCR-2 we have
\begin{equation}
\label{eq:Ak2}
A_{n-k}({n-X},y,z) = -(1-k) +\sum_i x_i - \sum_{j}2^jy_j + (1+2^l)z.
\end{equation}
For the  two cases
(top/bottom = SFR-BCR-1/SFR-BCR-2),
by recognizing their similarities we can simplify the equation to
\begin{equation}
\label{eq:Ak12}
\left.\begin{aligned}
&A_k(X,y,z) \\
&A_{n-k}({n-X},y,z)
\end{aligned}\right\}
= -(1\pm k) \pm \sum_i x_i - \sum_j 2^jy_j + (1+2^l)z.
\end{equation}
Then the squares are
\begin{equation}
\label{eq:Ak12sq}
\begin{split}
\left.\begin{aligned}
&A_k(X,y,z)^2 \\
&A_{n-k}({n-X},y,z)^2
\end{aligned}\right\}
= (1\pm k)^2 \mp2(1\pm k)\sum_i x_i +2(1\pm k)\sum_j 2^jy_j -2(1\pm k)(1+2^l)z & \\
+ \sum_i\sum_{i'} x_ix_{i'} \mp2\sum_i\sum_j 2^jx_iy_j \pm2 (1+2^l)\sum_i zx_i & \\
+ \sum_j\sum_{j'} 2^{j+j'}y_jy_{j'} - 2(1+2^l)\sum_j z 2^jy_j & \\
+ (1+2^l)^2z^2. &
\end{split}
\end{equation}
Because $z\in\{0,1\}$, we have $z^2=z$, so we can join the two terms,
\begin{equation}
-2(1\pm k)(1+2^l)z + (1+2^l)^2z^2 = (1+2^l)(2^l\mp2k-1)z,
\end{equation}
so we end up with
\begin{equation}
\label{eq:alpha12}
\begin{split}
&\left.\begin{aligned}
&A_k(X,y,z)^2 \\
&A_{n-k}({n-X},y,z)^2
\end{aligned}\right\} = \\
&\underbrace{(1\pm k)^2}_{\alpha}
+ \underbrace{\mp 2(1\pm k)}_{\alpha^{b}} \sum_i x_i
+ \sum_j \underbrace{(1\pm k)2^{j+1}}_{\alpha^{b_{a,1}}}y_j
+ \underbrace{(1+2^l)(2^l\mp2k-1)}_{\alpha^{b_{a,2}}}z \\
&+ \underbrace{1}_{\alpha^{bb}} \sum_{i,i'} x_ix_{i'}
+ \sum_{i,j} \underbrace{\mp2^{j+1}}_{\alpha^{bb_{a,1}}}x_iy_j
+ \underbrace{\pm2(1+2^l)}_{\alpha^{bb_{a,2}}}\sum_i x_iz \\
&+ \sum_{j,j'} \underbrace{2^{j+j'}}_{\alpha^{b_ab_{a,1}}}y_jy_{j'}
+ \sum_j \underbrace{-(1+2^l)2^{j+1}}_{\alpha^{b_ab_{a,2}}} y_j z.
\end{split}
\end{equation}
The coefficients are
\begin{subequations}
\label{eq:BCR12alpha}
\begin{align}
\alpha &= (1\pm k)^2, \\
%
\alpha^{b} &= \mp 2(1\pm k), \\
\alpha^{b_{a,1}} &= (1\pm k)2^{j+1}, \\
\alpha^{b_{a,2}} &= (1+2^l)(2^l\mp2k-1), \\
%
\alpha^{bb} &= 1, \\
\alpha^{bb_{a,1}} &= \mp2^{j+1}, \\
\alpha^{bb_{a,2}} &= \pm2(1+2^l), \\
\alpha^{b_ab_{a,1}} &= 2^{j+j'}, \\
\alpha^{b_ab_{a,2}} &= -(1+2^l)2^{j+1}.
\end{align}
\end{subequations}

Dictionary: \\
$l\mapsto m-1$, ($l+1=m$ auxiliary variables),\\
$x_i\mapsto b_i$, \\
$y_j\mapsto b_{a_j}$ ,
($b_{\text{a},j}$ would be a better choice, $\text{a}$ is a label, $j$ is an index, they should be on the same level.
Also the indexing of the $\alpha^{\dots}$ coefficients could be made more expressive.)\\
$z\mapsto b_{a_m}$, ($b_{\text{a},m}$ would be better)\\
$k\mapsto c$.\\
Note that, in this case, the $j$ indices of the auxiliary bits have to be shifted, since they are ranging from $1$, not $0$.
(This is not the case in the next subsection.)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{SFR-BCR-3,4}
We have now the $l$ auxiliary variables $y_1,y_2,\dots,y_{l-1},z\in\{0,1\}$.
(In the formulas, summations for $i,i' = 1,2,\dots,n$ and $j,j' = 1,2,\dots,l-1$ are understood.)

We have the functions
\begin{equation}
\label{eq:Akp}
A'_k(X,y,z) = X - (k-2^l)z - (k+1)(1-z) - \sum_{j}2^jy_j.
\end{equation}
Note that, compared to \eqref{eq:Ak},
 the difference is only in the range of index $j$ of the sum in the last term.
For the  two cases
(top/bottom = SFR-BCR-3/SFR-BCR-4),
after substituting and collecting the terms,
\begin{equation}
\label{eq:Akp34}
\left.\begin{aligned}
&A'_k(X,y,z) \\
&A'_{n-k}({n-X},y,z)
\end{aligned}\right\}
= -(1\pm k) \pm \sum_i x_i - \sum_j 2^jy_j + (1+2^l)z.
\end{equation}
(Again, although not written out explicitly,
the difference is in the range of $j$ of the summation, c.f., \eqref{eq:Ak12})

We can obtain the $\alpha^{\dots}$ coefficients for BCR-3,4
from those of BCR-1,2.
Instead of taking the squares, $A_k(X,y,z)^2$ and $A_{n-k}(n-X,y,z)^2$,
for BCR-3,4 we have to take
$\frac12 A'_k(X,y,z)\bigl(A'_k(X,y,z)-1\bigr)=\frac12\bigl(A'_k(X,y,z)^2-A'_k(X,y,z)\bigr)$ and
$\frac12 A'_{n-k}(n-X,y,z)\bigl(A'_{n-k}(n-X,y,z)-1\bigr)=\frac12\bigl(A'_{n-k}(n-X,y,z)^2-A'_{n-k}(n-X,y,z)\bigr)$,
so, to get the new $\alpha^{\dots}$ coefficients,
we have to substract the corresponding coefficients of $A'_k(X,y,z)$ and $A'_{n-k}(n-X,y,z)$
(these can be read off from \eqref{eq:Akp34})
from the old ones \eqref{eq:BCR12alpha}, and divide by $2$.
(And not to forget that the summations for $j$ run over a different range.)

\begin{subequations}
\label{eq:BCR34alpha}
\begin{align}
\alpha &=\frac12\Bigl((1\pm k)^2--(1\pm k) \Bigr)= \frac12(k^2\pm3k+2), \\
%
\alpha^{b} &= \frac12\Bigl(\mp 2(1\pm k)-\pm1\Bigr)= -k\mp\frac32, \\
\alpha^{b_{a,1}} &= \frac12\Bigl((1\pm k)2^{j+1}--2^j\Bigr)= (3\pm k)2^{j-1}, \\
\alpha^{b_{a,2}} &= \frac12\Bigl((1+2^l)(2^l\mp2k-1)-(1+2^l)\Bigr)= (1+2^l)(2^{l-1}\mp k-1), \\
%
\alpha^{bb} &= \frac12\bigl(1\bigr)= \frac12, \\
\alpha^{bb_{a,1}} &= \frac12\bigl(\mp2^{j+1}\bigr)= \mp2^j, \\
\alpha^{bb_{a,2}} &= \frac12\bigl(\pm2(1+2^l)\bigr)= \pm(1+2^l), \\
\alpha^{b_ab_{a,1}} &= \frac12\bigl(2^{j+j'}\bigr)= 2^{j+j'-1}, \\
\alpha^{b_ab_{a,2}} &= \frac12\bigl(-(1+2^l)2^{j+1}\bigr)= -(1+2^l)2^{j}.
\end{align}
\end{subequations}

Dictionary: \\
$l\mapsto m$, ($l=m$ auxiliary variables),\\
$x_i\mapsto b_i$, \\
$y_j\mapsto b_{a_j},$
($b_{\text{a},j}$ would be a better choice, $\text{a}$ is a label, $j$ is an index, they should be on the same level.
Also the indexing of the $\alpha^{\dots}$ coefficients could be made more expressive.)\\
$z\mapsto b_{a_m}$, ($b_{\text{a},m}$ would be better)\\
$k\mapsto c$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{SFR-BCR-5}

We begin with the quadratization of f:
\\(Theorem 6, \cite{Boros2018boundsPaper})

\begin{equation}
  \begin{split}
  \left.
  g(x,y,z)
  \right.
  &= \sum_{i=0}^{l-1}\sum_{j=0}^{l-1}r(il+j)y_iz_j
  + 2M\left(1-\sum_{i=0}^{l-1}y_i\right)^2 + 2M\left(1-\sum_{j=0}^{l-1}z_j\right)^2\\
  &+ 2M\left(|x| - \left(l\sum_{i=0}^{l-1}iy_i + \sum_{j=0}^{l-1}jz_j\right)\right)^2
  \end{split}
\end{equation}

After rearranging and substituting $|x| = \sum_ix_i$ we get the equation

\begin{equation}
  \begin{split}
  \left.
  g(x,y,z)
  \right.
  &= \sum_{i,j=1}^{l-1}r((i-1)l+(j-1))y_iz_j
  + 2M\Bigg[\left(1-\sum_{i=0}^{l-1}y_i\right)^2 + \left(1-\sum_{j=0}^{l-1}z_j\right)^2\\
  &+ \left(\sum_ix_i - \left(l\sum_{i=1}^{l-1}(i-1)y_i + \sum_{j=1}^{l-1}(j-1)z_j\right)\right)^2\Bigg]
  \end{split}
\end{equation}

Dictionary: \\
$l\mapsto m + 1$,\\
$x_i\mapsto b_i$, \\
$y_j\mapsto b_{a_j},$\\
$z\mapsto b_{a_{c+i}},$\\
$\lambda\mapsto 2M$\\
$r(x)\mapsto f(x)$

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{SFR-BCR-6}

Lets start with the following quadratization of f:
\\(Theorem 10, \cite{Boros2018QuadratizationsOS})

\begin{equation}
  \begin{split}
  \left.
  g(x,y,z)
  \right.
  &= \sum_{i=1}^{l-1}\sum_{j=1}^{l-1}a_{i,j}\cdot y_i\cdot z_j + M
  + M\cdot(X-Y-1)\cdot(X-Y+1)\\
  &+ M\cdot\sum_{i=1}^{l-2}(1-y_i)\cdot y_{i+1} + M\cdot\sum_{j=1}^{l-2}(1-z_j)\cdot z_{j+1}
  \end{split}
\end{equation}

After factoring out M
\begin{equation}
  \begin{split}
  \left.
  g(x,y,z)
  \right.
  &= \sum_{i,j}^{l-1}a_{ij}y_iz_j + M\Big(1 + \Big((X-Y-1)(X-Y+1)\Big)\\
  &+ \sum_{i=1}^{l-2}(1-y_i)y_{i+1} + \sum_{j=1}^{l-2}(1-z_j)z_{j+1}\Big)
  \end{split}
\end{equation}

\noindent \\Substituting for X, Y, and m using $\sum_i w_ix_i$, $l\left(\sum_{j=1}^{l-1}y_j\right) + \sum_{j=1}^{l-1} z_j$, and $l-1$, respectively, we get
\begin{equation}
  \begin{split}
  \left.
  g(x,y,z)
  \right.
  &= \sum_{i,j}^{m}a_{ij}y_iz_j + M\Big[1 + \Big(\sum_i w_ix_i-(m-1)\sum_{j=1}^{m}y_j + \sum_{j=1}^{m} z_j-1\Big)\\
  &\Big(\sum_i w_ix_i-(m-1)\sum_{j=1}^{m}y_j + \sum_{j=1}^{m} z_j+1\Big)
  + \sum_{i=1}^{m-1}(1-y_i)y_{i+1} + \sum_{j=1}^{m-1}(1-z_j)z_{j+1}\Big]
  \end{split}
\end{equation}

Dictionary: \\
$l\mapsto m + 1$,\\
$x_i\mapsto b_i$, \\
$y_j\mapsto b_{a_j},$\\
$z\mapsto b_{a_{c+i}},$\\
$\lambda\mapsto M$

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{SFR-ABCG-2}

We begin with the following representation of the parity function.
\\(Theorem 4.6, \cite{Anthony2014})

\begin{equation}
  \prod{(x)} = \sum_{j=1}^n x_j + 2\sum_{i=1}^{n-1}(-1)^{i-1}\left[i - \sum_{j=1}^{n}x_j\right]^-
\end{equation}

\noindent Adding $E(l) = l(l-1) + 2\sum_{i=1}^{n-1}\left[i-l\right]^-$ where $l = \sum_{j=1}^n x_j$, we get the quadratization

\begin{equation}
\begin{split}
  \left.
  g(x,y)
  \right.
  &= 2\sum_{i<j}x_ix_j + \sum_{j=1}^n x_j + 4\sum_{\substack{i = 1:\\ i\:odd}}^{n-1}y_i\Big(i - \sum_{j=1}^n x_j\Big)\\
  &= \sum_{i}x_i + 2\sum_{ij}x_ix_j + 4\sum_{2i-1}^{n-1}y_i\Big(2i - 1 - \sum_{j}x_j\Big)
  \end{split}
\end{equation}

\noindent Dictionary:\\
$x_{i,j}\mapsto b_{i,j}$,\\
$y_{i}\mapsto b_{a_i}$\\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{SFR-ABCG-3}
We begin with the complement of the previous function from SFR-ABCG-2:
\\(Theorem 4.6, \cite{Anthony2014})

\begin{equation}
  \overline{\prod}(x) = 1 - \sum_{j=1}^n x_j + 2\sum_{i=1}^{n-1}(-1)^{i}\left[i - \sum_{j=1}^{n}x_j\right]^-
\end{equation}

\noindent Adding $E(l) = l(l-1) + 2\sum_{i=1}^{n-1}\left[i-l\right]^-$ where $l = \sum_{j=1}^n x_j$, we get the quadratization

\begin{equation}
\begin{split}
  \left.
  g(x,y)'
  \right.
  &= 1 + 2\sum_{i<j}x_ix_j - \sum_{i}^n x_i + 4\sum_{\substack{i = 2:\\ i\:even}}^{n-1}y_i\Big(i - \sum_{j=1}^n x_j\Big)\\
  &= 1 + 2\sum_{ij}x_ix_j - \sum_{i} x_i + 4\sum_{2i}^{n-1}y_i\Big(i - \sum_{j} x_j\Big)
  \end{split}
\end{equation}

\noindent Dictionary:\\
$x_{i,j}\mapsto b_{i,j}$,\\
$y_{i}\mapsto b_{a_i}$\\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{SFR-BCR-7}

Beginning with the quadratization of f:
\\(Theorem 9, \cite{Boros2018boundsPaper})

\begin{equation}
  \begin{split}
  \left.
  g(x,y,z)
  \right.
  &= \sum_{i=0}^{l-1}\sum_{j=0}^{l-1}r(il+j)y_iz_j
  + 2M\left(1-\sum_{i=0}^{l-1}y_i\right)^2 + 2M\left(1-\sum_{j=0}^{l-1}z_j\right)^2\\
  &+ 2M\left(|x| - \left(l\sum_{i=0}^{l-1}iy_i + \sum_{j=0}^{l-1}jz_j\right)\right)^2
  \end{split}
\end{equation}

\noindent Finally, we end with the equation
\begin{equation}
  g(x,y) = 1 + 2\sum_{ij}x_ix_j - \sum_{i} x_i + 4\sum_{2i}^{n-1}y_i\Big(i - \sum_{j} x_j\Big)
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{comment}
  \subsection{SFR-BCR-8} THIS WAS JUST SFR-BCR-3/4 FOUND IN THE OTHER PAPER

  Take the quadratization of the exact k-out-of-n function $f_{=k}$:
  \\(Theorem 7, \cite{Boros2018boundsPaper})

  \begin{equation}
  \begin{split}
    \left.
    G_k(x,y,z)
    \right.
    &= \frac{1}{2}A_k(x,y,z)(A_k(x,y,z)-1)\\
    &= \frac{1}{2}\left(A_k(x,y,z)^2-A_k(x,y,z)\right)
  \end{split}
  \end{equation}

  \noindent Where
  \begin{equation}
    A_k(X,y,z) = |x| - (k-2^l)z - (k+1)(1-z) - \sum_{j}^{l-1}2^jy_j.
  \end{equation}

  \noindent We have $A_k(x,y,z)^2$ from \eqref{eq:alpha12} and $A_k(x,y,z)$ from \eqref{eq:Ak1}, thus when we substitute the two equations and collect like terms we end with
  \begin{equation}
  \begin{split}
    \left.
    G_k(x,y,z)
    \right.
    &=\underbrace{\frac{1}{2}(k^2+3k+2)}_{\alpha}
    + \underbrace{-\frac{1}{2}(2k+3)}_{\alpha^{b}} \sum_i x_i
    + \sum_j \underbrace{(k+2)2^{j-1}}_{\alpha^{b_{a,1}}}y_j
    + \underbrace{\frac{1}{2}(1+2^l)(2^l-2k-2)}_{\alpha^{b_{a,2}}}z \\
    &+ \underbrace{\frac{1}{2}}_{\alpha^{bb}} \sum_{i,i'} x_ix_{i'}
    + \sum_{i,j} \underbrace{-2^{j}}_{\alpha^{bb_{a,1}}}x_iy_j
    + \underbrace{(1+2^l)}_{\alpha^{bb_{a,2}}}\sum_i x_iz \\
    &+ \sum_{j,j'} \underbrace{2^{j+j'-1}}_{\alpha^{b_ab_{a,1}}}y_jy_{j'}
    + \sum_j \underbrace{-(1+2^l)2^{j}}_{\alpha^{b_ab_{a,2}}} y_j z
  \end{split}
  \end{equation}

  The coefficients are
  \begin{subequations}
  \begin{align}
  \alpha &= \frac{1}{2}(k^2+3k+2), \\
  %
  \alpha^{b} &= -\frac{1}{2}(2k+3), \\
  \alpha^{b_{a,1}} &= (k+2)2^{j-1}, \\
  \alpha^{b_{a,2}} &= \frac{1}{2}(1+2^l)(2^l-2k-2), \\
  %
  \alpha^{bb} &= \frac{1}{2}, \\
  \alpha^{bb_{a,1}} &= -2^{j}, \\
  \alpha^{bb_{a,2}} &= (1+2^l), \\
  \alpha^{b_ab_{a,1}} &= 2^{j+j'-1}, \\
  \alpha^{b_ab_{a,2}} &= -(1+2^l)2^{j}.
  \end{align}
  \end{subequations}

  Dictionary: \\
  $l\mapsto m$,\\
  $x_i\mapsto b_i$, \\
  $y_j\mapsto b_{a_j}$,\\
  $z\mapsto b_{a_m}$,\\
  $k\mapsto c$.
\end{comment}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{SFR-BCR-8}

Take the quadratization of the at least k-out-of-n function $f_{\geq k}$:
\\(Theorem 8, \cite{Boros2018boundsPaper})

\begin{equation}
  G_k(x,y,z) = \frac{1}{2}A_k(x,y,z)(A_k(x,y,z)-1) + (1-z)
\end{equation}

\noindent With our results from SFR-BCR-8 and adding $(1+z)$ we get\\
\small
\begin{equation}
\begin{split}
  \left.
  G_k(x,y,z)
  \right.
  &=\underbrace{\frac{1}{2}(k^2+3k+4)}_{\alpha}
  + \underbrace{-\frac{1}{2}(2k+3)}_{\alpha^{b}} \sum_i x_i
  + \sum_j \underbrace{(k+2)2^{j-1}}_{\alpha^{b_{a,1}}}y_j
  + \underbrace{\frac{1}{2}((1+2^l)(2^l-2k-2)-2)}_{\alpha^{b_{a,2}}}z \\
  &+ \underbrace{\frac{1}{2}}_{\alpha^{bb}} \sum_{i,i'} x_ix_{i'}
  + \sum_{i,j} \underbrace{-2^{j}}_{\alpha^{bb_{a,1}}}x_iy_j
  + \underbrace{(1+2^l)}_{\alpha^{bb_{a,2}}}\sum_i x_iz \\
  &+ \sum_{j,j'} \underbrace{2^{j+j'-1}}_{\alpha^{b_ab_{a,1}}}y_jy_{j'}
  + \sum_j \underbrace{-(1+2^l)2^{j}}_{\alpha^{b_ab_{a,2}}} y_j z
\end{split}
\end{equation}
\normalsize
The coefficients are
\begin{subequations}
\begin{align}
\alpha &= \frac{1}{2}(k^2+3k+4), \\
%
\alpha^{b} &= -\frac{1}{2}(2k+3), \\
\alpha^{b_{a,1}} &= (k+2)2^{j-1}, \\
\alpha^{b_{a,2}} &= \frac{1}{2}((1+2^l)(2^l-2k-2)-2), \\
%
\alpha^{bb} &= \frac{1}{2}, \\
\alpha^{bb_{a,1}} &= -2^{j}, \\
\alpha^{bb_{a,2}} &= (1+2^l), \\
\alpha^{b_ab_{a,1}} &= 2^{j+j'-1}, \\
\alpha^{b_ab_{a,2}} &= -(1+2^l)2^{j}.
\end{align}
\end{subequations}

Dictionary: \\
$l\mapsto m$,\\
$x_i\mapsto b_i$, \\
$y_j\mapsto b_{a_j}$,\\
$z\mapsto b_{a_m}$,\\
$k\mapsto c$.

\subsection{SFR-BCR-9}

\noindent We begin with the equation for the x-symmetric quadratization of $f_k(x)$
\\(Lemma 2 Eqn 7, \cite{Boros2018QuadratizationsOS})
\begin{equation}
  G(X,y) = \alpha X^2 + X\left(\beta + \sum_{j=1}^m \gamma_jy_j\right) + \left[\sum_{1\leq i < j \leq m} \delta_{ij}y_iy_j + \sum_{j=1}^m \epsilon_jy_j+\phi\right]
\end{equation}

\noindent Using the relationship $X = \sum_i^nx_i$ and rearranging, we get
\begin{equation}
\begin{split}
  \left.
  G(X,y)
  \right.
  &= \alpha\sum_{i,j}^mx_ix_j + \sum_i^mx_i\left(\beta + \sum_{j=1}^m \gamma_jy_j\right) + \left[\sum_{1\leq i < j \leq m} \delta_{ij}y_iy_j + \sum_{j=1}^m \epsilon_jy_j+\phi\right]\\
  &= \alpha\sum_{i,j}^mx_ix_j + \beta\sum_i^mx_i + \sum_{1,j=1}^m \gamma_jx_iy_j + \sum_{ij}^m \delta_{ij}y_iy_j + \sum_{j=1}^m \epsilon_jy_j+\phi
\end{split}
\end{equation}

Dictionary: \\
$x_i\mapsto b_i$, \\
$y_j\mapsto b_{a_j}$\\

\bibliography{SFRBibliography}

\end{document}


\section{Type models and type graphs}
\label{sec:transformation_framework:type_models_and_type_graphs}

In this section, the proposed framework structure is applied to type models and type graphs. First, the general structure and its requirements are discussed. Then the required definitions and theorems are given.

\begin{figure}
    \centering
    \begin{tikzpicture}
    \path
    (-3,4) node[circle,draw,minimum size=10mm,inner sep=0pt](ME) {$T$}
    (-4.5,2) node[circle,draw,minimum size=10mm,inner sep=0pt](MA) {$Tm_A$}
    (-1.5,2) node[circle,draw,minimum size=10mm,inner sep=0pt](MB) {$Tm_B$}
    (-3,0) node[circle,draw,minimum size=10mm,inner sep=0pt](MAB) {$Tm_{AB}$}

    (3,4) node[circle,draw,minimum size=10mm,inner sep=0pt](GN) {$N$}
    (1.5,2) node[circle,draw,minimum size=10mm,inner sep=0pt](GA) {$TG_A$}
    (4.5,2) node[circle,draw,minimum size=10mm,inner sep=0pt](GB) {$TG_B$}
    (3,0) node[circle,draw,minimum size=10mm,inner sep=0pt](GAB) {$TG_{AB}$};

    \path[]
    (ME) [-, black, out=240, in=90] edge node[above] {} (MA)
    (ME) [-, black, out=300, in=90] edge node[above] {} (MB)

    (MA) [-{Latex[width=5]}, black, out=270, in=90] edge node[above] {} (MAB)
    (MB) [-{Latex[width=5]}, black, out=270, in=90] edge node[above] {} (MAB)

    (GN) [-, black, out=240, in=90] edge node[above] {} (GA)
    (GN) [-, black, out=300, in=90] edge node[above] {} (GB)

    (GA) [-{Latex[width=5]}, black, out=270, in=90] edge node[above] {} (GAB)
    (GB) [-{Latex[width=5]}, black, out=270, in=90] edge node[above] {} (GAB)

    (ME) [-{Latex[width=5]}, black, out=25, in=155] edge node[above] {$f$} (GN)
    (GN) [-{Latex[width=5]}, black, out=165, in=15] edge node[above] {} (ME)

    (MA) [-{Latex[width=5]}, black, out=35, in=145] edge node[above] {$f_A$} (GA)
    (GA) [-{Latex[width=5]}, black, out=155, in=25] edge node[above] {} (MA)

    (MB) [-{Latex[width=5]}, black, out=35, in=145] edge node[above] {$f_B$} (GB)
    (GB) [-{Latex[width=5]}, black, out=155, in=25] edge node[above] {} (MB)

    (MAB) [-{Latex[width=5]}, black, out=25, in=155] edge node[above] {$f_{A} \sqcup f_{B}$} (GAB)
    (GAB) [-{Latex[width=5]}, black, out=165, in=15] edge node[above] {} (MAB)
    ;
    \end{tikzpicture}
    \caption{Structure for transforming between type models and type graphs}
    \label{fig:transformation_framework:type_models_and_type_graphs:structure_type_models_graphs}
\end{figure}

\cref{fig:transformation_framework:type_models_and_type_graphs:structure_type_models_graphs} shows an alternation of the structure proposed in \cref{sec:transformation_framework:structure} applied to type models and type graphs. As before, type model $Tm_A$ represents the partially build model which corresponds to type graph $TG_A$ under the transformation function $f_A$. Type model $Tm_B$ represents the next building block to add to this model. It corresponds to type graph $TG_B$ under the bijective transformation function $f_B$.

Type models $Tm_A$ and $Tm_B$ are entirely distinct except for a set types $T$, which means $T \subseteq Type_{Tm_A} \land T \subseteq Type_{Tm_B}$. In a similar way, type graphs $TG_A$ and $TG_B$ are entirely distinct except for a set of node types $N$, so $N \subseteq NT_{TG_A} \land N \subseteq NT_{TG_B}$.

Type models $Tm_A$ and $Tm_B$ are combined into type model $Tm_{AB}$ using \cref{defin:transformation_framework:type_models_and_type_graphs:combining_type_models:combine}. In a similar way type graphs $TG_A$ and $TG_B$ are combined into type graph $TG_{AB}$ using \cref{defin:transformation_framework:type_models_and_type_graphs:combining_type_graphs:combine}. \cref{defin:transformation_framework:type_models_and_type_graphs:combining_type_models:tmod_combine_merge_correct} and \cref{defin:transformation_framework:type_models_and_type_graphs:combining_type_graphs:tg_combine_merge_correct} respectively show that $Tm_{AB}$ and $TG_{AB}$ are valid. Then \cref{defin:transformation_framework:type_models_and_type_graphs:combining_transformation_functions:combination_transformation_function_type_model_type_graph} and \cref{defin:transformation_framework:type_models_and_type_graphs:combining_transformation_functions:combination_transformation_function_type_graph_type_model} can be used to merge the transformation functions $f_A$ and $f_B$ into $f_{A} \sqcup f_{B}$, where \cref{defin:transformation_framework:type_models_and_type_graphs:combining_transformation_functions:tg_combine_mapping_correct} and \cref{defin:transformation_framework:type_models_and_type_graphs:combining_transformation_functions:tg_combine_mapping_function_correct} show that $f_{A} \sqcup f_{B}$ is again a valid transformation function transforming $Tm_{AB}$ to $TG_{AB}$. Similarly, \cref{defin:transformation_framework:type_models_and_type_graphs:combining_transformation_functions:tmod_combine_mapping_correct} and \cref{defin:transformation_framework:type_models_and_type_graphs:combining_transformation_functions:tmod_combine_mapping_function_correct} show that the inverse function of $f_{A} \sqcup f_{B}$ is again a valid transformation function transforming $TG_{AB}$ to $Tm_{AB}$.

\input{tex/04_transformation_framework/03_type_models_and_type_graphs/01_combining_type_models.tex}
\input{tex/04_transformation_framework/03_type_models_and_type_graphs/02_combining_type_graphs.tex}
\input{tex/04_transformation_framework/03_type_models_and_type_graphs/03_combining_transformation_functions.tex}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    INSTITUTE OF PHYSICS PUBLISHING                                   %
%                                                                      %
%   `Preparing an article for publication in an Institute of Physics   %
%    Publishing journal using LaTeX'                                   %
%                                                                      %
%    LaTeX source code `ioplau2e.tex' used to generate `author         %
%    guidelines', the documentation explaining and demonstrating use   %
%    of the Institute of Physics Publishing LaTeX preprint files       %
%    `iopart.cls, iopart12.clo and iopart10.clo'.                      %
%                                                                      %
%    `ioplau2e.tex' itself uses LaTeX with `iopart.cls'                %
%                                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% First we have a character check
%
% ! exclamation mark    " double quote
% # hash                ` opening quote (grave)
% & ampersand           ' closing quote (acute)
% $ dollar              % percent
% ( open parenthesis    ) close paren.
% - hyphen              = equals sign
% | vertical bar        ~ tilde
% @ at sign             _ underscore
% { open curly brace    } close curly
% [ open square         ] close square bracket
% + plus sign           ; semi-colon
% * asterisk            : colon
% < open angle bracket  > close angle
% , comma               . full stop
% ? question mark       / forward slash
% \ backslash           ^ circumflex
%
% ABCDEFGHIJKLMNOPQRSTUVWXYZ
% abcdefghijklmnopqrstuvwxyz
% 1234567890
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\documentclass[12pt]{iopart}
%\newcommand{\gguide}{{\it Preparing graphics for IOP Publishing journals}}
%Uncomment next line if AMS fonts required
%\usepackage{iopams}
\usepackage[thinlines]{easytable}
\begin{document}

\title[Drift velocities in plasmas]{Drift velocities in plasmas}

\author{John "Jack" Brooks}

%\address{}
\ead{jwbrooks0@gmail.com}
\vspace{10pt}
\begin{indented}
\item[]\today
\end{indented}

%\begin{abstract}
%\end{abstract}

%
% Uncomment for keywords
%\vspace{2pc}
%\noindent{\it Keywords}: XXXXXX, YYYYYYYY, ZZZZZZZZZ
%
% Uncomment for Submitted to journal title message
%\submitto{\JPA}
%
% Uncomment if a separate title page is required
%\maketitle
%
% For two-column output uncomment the next line and choose [10pt] rather than [12pt] in the \documentclass declaration
%\ioptwocol
%

\section{Motion in a uniform $\mathbf{B}$ field}

Plasma consists of a collection of positively and negatively charged particles (ions and electrons).  When a uniform magnetic field is applied, $\mathbf{B}$, the plasma is considered magnetized, and the charged particles (plasma) oscillate around the magnetic field lines.  For this work, we define the unit vector associated with the uniform $\mathbf{B}$ to be the parallel, $||$, direction, and everything else as being the cross-field, $\perp$, direction.

The equation of motion of moving charged particles in a magnetic field is
\begin{equation}
\label{eq:eom_simple}
m \dot{\mathbf{v}} = q \mathbf{v} \times \mathbf{B}
\end{equation}
We note that the only non-trivial solution of $\mathbf{v}$ is the cross-field velocity, $\mathbf{v}_\perp$, and therefore we replace $\mathbf{v}$ with $\mathbf{v}_\perp$.

To solve for $\mathbf{v}_\perp$, we take the time derivative of Eq.~\ref{eq:eom_simple} and plug Eq.~\ref{eq:eom_simple} into it.  This provides
\begin{equation*}
\label{eq:eom_simple_2}
\ddot{\mathbf{v}}_\perp = \frac{q^2}{m^2} \left (\mathbf{v}_\perp \times \mathbf{B}  \right) \times \mathbf{B}.
\end{equation*}
Expanding the triple product using BAC-CAB triple product rule provides
\begin{equation*}
\label{eq:eom_simple_3}
\ddot{\mathbf{v}}_\perp = -\frac{q^2}{m^2} \left (B^2 \mathbf{v}_\perp - \left( \mathbf{B} \cdot \mathbf{v}_\perp \right) \mathbf{B} \right).
\end{equation*}
Recognizing that $\mathbf{B} \cdot \mathbf{v}_\perp = 0$, this simplifies to
\begin{equation*}
%\label{eq:eom_simple_3}
\ddot{\mathbf{v}}_\perp = -\left(\frac{qB}{m}\right)^2 \mathbf{v}_\perp
\end{equation*}
and therefore the velocity has the solution
\begin{equation}
\label{eq:velocity_solved}
\mathbf{v}_\perp = v_\perp(0) e^{i \left(qB/m\right) t}.
\end{equation}

Integrating Eq.~\ref{eq:velocity_solved}, we can solve for the position to be

\begin{equation}
%\label{eq:eom_simple_3}
\mathbf{x}_\perp = \frac{v_\perp(0) m}{|q|B} e^{i \left(qB/m\right) t}.
\end{equation}
From this equation, we can identify the gryo-radius (the radius of the circle created by the gyrating particle) to be

\begin{equation}
%\label{eq:gyro-radius}
r_{gyro} = \frac{v_\perp m}{|q|B}
\end{equation}
and the gyro-frequency (the frequency of gyration) to be

\begin{equation}
%\label{eq:gyro-radius}
\omega_{gyro} = \frac{qB}{m}.
\end{equation}
This derivation informs us that the plasma rotates around the magnetic field line and is effectively trapped to the magnetic field without the presence of external forces.

\section{External forces cause a net drift velocity on the plasma}

When an external force is applied to the plasma, it develops an net drift motion (a drift velocity) orthogonal to $\mathbf{B}$ and the external force.  Example forces include gravity, Coulomb force, kinetic pressure, etc.

To calculate the impact of a force on the plasma, we include an unspecified force, $\mathbf{F}$, to our equation of motion (EoM),
\begin{equation*}
%\label{eq:eom_simple}
m \dot{\mathbf{v}} = q \mathbf{v}_\perp \times \mathbf{B} + \mathbf{F}.
\end{equation*}
In addition, we ignore gyromotion and focus on steady-state ``drift'' velocities by time-averaging the EoM over a time much larger than a full gyro-rotation.  The EoM therefore simplifies to
\begin{equation*}
%\label{eq:eom_simple}
0 = q \mathbf{v}_\perp \times \mathbf{B} + \mathbf{F}.
\end{equation*}

We next solve for $\mathbf{v}_\perp (\mathbf{F})$ by multiplying the cross product of $\mathbf{B}$ throughout, performing the BAC-CAB triple product expansion rule, and recognizing that $\mathbf{B} \cdot \mathbf{v}_\perp = 0$.  We finally arrive at
\begin{equation}
\label{eq:drift_velocity_generic}
\mathbf{v}_\perp = \frac{\mathbf{F} \times \mathbf{B}}{q \left | B \right|^2}
\end{equation}

Most drift velocities (perhaps all) can be solved as a special case of the generalized velocity, Eq.~\ref{eq:drift_velocity_generic}, and I personally find it easier to conceptualize each of these drift velocities by considering the force that originally created them.  However, many plasma references prefer a more traditional approach and instead derive them from scratch.

\section{Specific drift velocities}

Now that we have a generic expression for how forces create a net plasma drift velocity (Eq.~\ref{eq:drift_velocity_generic}), the next step is to identify common forces and plug them in.  I've done this for you in Table~\ref{tab:drift_summary} which shows examples of several common forces and their resulting drift velocities.  As an example, the Coulomb force results in the $\mathbf{E} \times \mathbf{B} $ drift that has a net motion that is orthogonal to both $\mathbf{E}$ and $\mathbf{B}$.  Because it has no dependence of charge, $q$, and both ions and electrons therefore move in the same direction when exposed to a net electric field.  The other drift velocities, for example the gravitational drift, are dependent on the plasma charge, $q$, and this means that most forces result in ions and electrons traveling in opposite directions.

\renewcommand{\arraystretch}{1.5} %% Increases row height in the table
\begin{table}[h]
\centering
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{\textbf{Force}, $\mathbf{F}$}    & \multicolumn{2}{c|}{\textbf{Resulting drift velocity}, $\mathbf{v}_\perp$} \\ \hline
\textbf{Name}       & \textbf{Equation} & \textbf{Name}       & \textbf{Equation}      \\ \hline
Gravity             & $m\mathbf{g}$                &      Gravitational drift               &           $\frac{m \mathbf{g} \times \mathbf{B}}{qB^2}$             \\ \hline
Coulomb force      & $q\mathbf{E}$                &       E ``cross'' B drift              &      $\frac{ \mathbf{E} \times \mathbf{B}}{B^2}$                      \\ \hline
Kinetic pressure    &          $\frac{ - \nabla P}{n}$         &        Diamagnetic drift             &     $-\frac{ \nabla P \times \mathbf{B}}{nqB^2}$                       \\ \hline
Magnetic dipole restoring force     &   $-\frac{mv_\perp^2}{2B} \nabla B$                &     ``Grad'' B drift                &           $\frac{m v_\perp^2}{2qB}\frac{\mathbf{B} \times \nabla B}{B^2}$             \\ \hline
Centripetal force          & $\frac{mv_{||}^2}{\mathbf{r}}$          &         Curvature drift            &       $ m v_{||}^2 \frac{\mathbf{r}\times\mathbf{B}}{qB^2r^2}$              \\ \hline
Oscillating E field & $-\frac{m\mathbf{B}\times\mathbf{\dot{E}}}{B^2}$      &    Polarization drift                 &  $\frac{m \mathbf{\dot{E}}}{qB^2} $                      \\ \hline
\end{tabular}
\caption{\label{tab:drift_summary}List of forces that can act on a plasma and their resulting drift velocity. }
\end{table}

Several of the other forces are somewhat nuanced and require a little more context.  The diamagnetic drift is due to resulting force from kinetic pressure (i.e. collisions between particles) in the plasma.  The $\nabla B$ drift is created charged particle gyro-motion around a  non-uniform $\mathbf{B}$ which results in a restoring magnetic dipole force.  Curvature drift occurs when the magnetic field line is curved and the plasma particle (which is mostly trapped to the field line) is forced to bend along with the field line.

The force responsible for the polarization drift is less straight forward.  In this case, the electric field, $\mathbf{E}$, is changing as a function of time.  The resulting force can be derived from the lorentz Force,

\begin{equation*}
\mathbf{E} = - \mathbf{v} \times \mathbf{B},
\end{equation*}
taking the time derivative and multiplying my the mass,
\begin{equation*}
\mathbf{m\mathbf{\dot{E}}} = - m\mathbf{\dot{v}} \times \mathbf{B} = - \mathbf{F} \times \mathbf{B},
\end{equation*}
crossing both sides with $\mathbf{B}$ and using the BAC-CAB rule to get
\begin{equation}
\mathbf{F} = - \frac{m \mathbf{B} \times \mathbf{\dot{E}}}{B^2}.
\end{equation}

The drift velocities discussed here are merely the motions that are typically covered by plasma physics textbooks, and more complicated motions exist.  These include those from radiation pressure and more complex collision models.  In addition, higher order corrections to the drift velocities discussed here exist when the various forces are not constant in time and space.

\section{Plasma currents}

Because of the $q$ dependence in most of the drift velocities in Table~\ref{tab:drift_summary}, ions and electrons move in opposite directions under these forces.  Therefore, these drift velocities also create electrical current in the plasma.  The exception to this is the $\mathbf{E} \times \mathbf{B}$ drift velocity.

\section*{References}
\begin{thebibliography}{widest entry}
\bibitem{deBlank} de Blank, H.J., ``Guiding Center Motion'', Personal notes: $https://juser.fz-juelich.de/record/283631/files/DeBlank_BT-1-2.pdf$
\end{thebibliography}

\end{document}


%!TEX root=../../../template.tex
\subsection{The Lambertian Hypothesis}%
\label{sub:methods_lambertian_hypothesis}

As stated in this chapter's introductory notes, this thesis main body of
work revolves around two base assumptions, our hypothesis. The first
one, about the information capturing by the idealised system, was
addressed in Subsection~\ref{sub:methods_collection}. The second, more
physical in nature, is the subject matter of this section. Our
hypothesis states that the light absorption between points $A$ and $B$
(let's call it $A_{AB}$) should be equal to the difference of the
absorptions in $A$ and $B$. We can write this, in a \emph{Lambertian}
manner as in Equation~\ref{eq:lambertian_hypothesis}.

\begin{equation}
    \label{eq:lambertian_hypothesis}
    I_B = I_A \cdot \exp \bigg[-AB \cdot \sum_i \sigma_{ABi} \cdot
    c_{ABi}\bigg]
\end{equation}

This is to say that the light intensity reaching point $B$ is given by
the intensity reaching $A$, exponentially decreased by the absorbers at
interval $AB$. The intensities at $A$ and $B$ are written as in
Equation~\ref{eq:intensityAtAAndB}.

\begin{equation}
    \begin{aligned}
        \label{eq:intensityAtAAndB}
        I_B = I_0 \cdot \exp \bigg[ -L_B \cdot \sum_i \sigma_{Bi} \cdot
        c_{Bi} \bigg]\\
        I_A = I_0 \cdot \exp \bigg[ -L_A \cdot \sum_i \sigma_{Ai} \cdot
        c_{Ai} \bigg]
    \end{aligned}
\end{equation}

If we join all this information in the same expression, the equation is
transformed into its final form, presented in
Equation~\ref{eq:hypothesis_final_form}.

\begin{equation}
    \small
    \label{eq:hypothesis_final_form}
    I_0 \cdot \exp \bigg[ -L_B \cdot \sum_i \sigma_{Bi} \cdot
            c_{Bi} \bigg] = I_0 \cdot \exp \bigg[ -L_A \cdot \sum_i \sigma_{Ai} \cdot
            c_{Ai} \bigg] \cdot \bigg[-AB \cdot \sum_i \sigma_{ABi} \cdot
            c_{ABi}\bigg]
\end{equation}

Equation~\ref{eq:hypothesis_final_form} can be greatly simplified: we
take the natural logarithm of both sides and we state that $\sum_i
\sigma_{Xi} \cdot c_{Xi} = S_i$. These operations result in the
simplified form of Equation~\ref{eq:final_form_simplified}.

\begin{equation}
    \label{eq:final_form_simplified}
    L_B \cdot S_B = L_A \cdot S_A + L_{AB} \cdot S_{AB}
\end{equation}

Now, $L_X \cdot S_X$ can be thought of as the wavelength dependent light
absorption in path $X$. In this case, the wavelength interval is always
the same. We can therefore conclude that, theoretically, our
hypothesis is valid: light absorption between points $A$ and $B$ can be
expressed in terms of the absorption on both these points and
corresponds to their difference.

Although mathematically this seems clear-cut, in the real world things
can become more problematic, since we have to deal with the
imperfections that characterise a real physical system. Noise,
instrumental limitations, adverse environmental effects, etc.. The
experiment we describe in the next few paragraphs aimed at determining
target trace gas concentration in a set analysis field. This field is
dimension-wise compatible with those that would be employed in the final
working system. This experiment is represented in
Figure~\ref{fig:experiment_map}.

\begin{figure}[htpb]
    \centering
    \includegraphics[width=0.8\linewidth]{img/png/experimentMap.png}
    \caption{Location of observer points for the physical experiment.}
    \label{fig:experiment_map}
\end{figure}

The goal of the experiment was to compare passive and active \gls{DOAS}
measurements performed with a very short time difference between them.
The passive measurement would employ the same acquisition strategy as
the drone is expected to use. This comparison will be used to test our
second hypothesis.

Finding two appropriate experiment sites proved to be the first
difficulty: both telescopes should see sky on the back of the other
telescope. Otherwise, the contribution from the terrain's reflection
would have to be taken into account and the experiment conditions would
be very different from the ones the drone will have. There are not many
site pairs that provide this, and most of the ones that exist are
private and authorisations are not easy to obtain. In the end, we
managed to run the experiment in the facilities of \emph{\gls{iep}} and
the \emph{Cristo-Rei} sanctuary, near our own base.


\startreport{Dense Neural Networks}
\reportauthor{Ramesh Subramonian}

\section{Computational Considerations}
We start by describing the dense neural network (DNN) computation. In
Section~\ref{the_math}, we will discuss the underyling mathematics.
\subsection{Data Structure of DNN}
\label{data_struct}
We start by presenting the data structure of the DNN
\begin{figure}
\centering
% TODO: Following should be auto-generated not hard-coded
\verbatiminput{dnn_types.h}
\end{figure}

We now explain each element of the
\be
\item \(n\) --- {\tt nl}, number of layers. The simplest DNN has 3 layers ---
an input layer, a hidden layer and an output layer.
\item \(p\) -- {\tt npl}, number of neurons per layer. \(p_l\) is number of
neurons in layer \(l\), where \(0 \leq l < n\). The network of
Figure~\ref{sample_network} would have \(n = 3, p = \{3,4,1\}\)
\item \(W\) --- the weights on the edges.
\(W_i\) contains the edges from layer \(i-1\) to layer \(i\).
Hence,
\be
\item \(W_0 = \bot\)
\item \(W_i = \) edges from layer \(i-1\) to layer \(i\). Note that
\(0 < i < n\)
\item \(W_{i,j} = \) edges from neuron \(j\) of layer \(i-1\) to layer \(i\).
Note that \( 0 \leq < p_{i-1}\).
\item \(W_{i,j,k} = \) edge from neuron \(j\) of layer \(i-1\) to
neuron \(k\) of layer \(i\).
Note that \(0 \leq k < p_i\).
\ee
\item \(b\) --- the bias of the neurons.
\(b_i\) contains the bias of neurons in layer \(i\).
Note that \(0 < i < n\).
Hence,
\be
\item \(b_0 = \bot\)
\item \(b_i = \) biases of neurons in layer \(i\)
\item \(b_{i,j} = \) bias of neuron \(j\) of layer \(i\),
where \(0 \leq j < p_i\)
\ee
\item \(z\) --- the intermediate output of a neuron
\(z_i\) contains the intermediate output of neurons in layer \(i\).
Hence,
\be
\item \(z_0 = \bot\)
\item \(z_i = \) intermediate output of neurons in layer \(i\)
Note that \(0 < i < n\).
\item \(z_{i,j} = \) intermediate output of neuron \(j\) of layer \(i-1\)
\ee
\item \(a\) --- the output output of a neuron --- after intermediate output has been
passed through activation function.
\(a_i\) contains the output of neurons in layer \(i\).
Hence,
\be
\item \(a_0 = \bot\)
\item \(a_i = \) output of neurons in layer \(i\)
Note that \(0 < i < n\).
\item \(a_{i,j} = \) output of neuron \(j\) of layer \(i-1\)
\ee
The observant reader would have noticed that the above description indicates
that \(z, a\) are 2-dimensional arrays whereas they have been defined as {\tt
float ***}. This is because the network is evaluated in batches, explained in
Section~\ref{batching}
\ee

\subsection{Batching}
What is batching? Assume that we have \(n\) instances and a batch size of \(m <
n\). Then, it means that an epoch consists of performing the forward pass
for \(m\) instances, then the back propagation, then doing the same for the next
\(m\) instances and so on.

This makes \(z, a\) 3-dimensional arrays such that
\(a_{i,j, k} = \) output of neuron \(j\) of layer \(i-1\) for \(k^{th}\)
instance.

\section{The Math}

\subsection{Dropouts}
Dropouts are explained in \cite{Srivastava14}.

\TBC
\newpage
\bibliographystyle{alpha}
\bibliography{../../../DOC/Q_PAPER/ref}


\section{Summary and Outlook} \label{sec:summary}
By analyzing the SED of a galaxy, we can infer detailed physical properties
such as its stellar mass, star formation rate, metallicity, and dust content.
These properties serve as the building blocks of our understanding of how
galaxies form and evolve.
State-of-the-art SED modeling methods use MCMC sampling to perform Bayesian
statistical inference.
They derive posterior probability distributions of galaxy properties given
observation that accurately estimate uncertainties and parameter degeneracies
to enable more rigorous statistical analyses.
%Posteriors also enable marginalization over any nuisance parameters.
For the dimensionality of current SED models, deriving a posterior requires
${\gtrsim}100,000$ model evaluations and take ${\gtrsim}10-100$ CPU hours per
galaxy.
Upcoming galaxy surveys, however, will observe \emph{billions} of galaxies
using \emph{e.g.} DESI, PFS, Rubin observatory, James Webb Space Telescope, and
the Roman Space Telescope.
Analyzing all of these galaxies with current Bayesian SED models is infeasible
and would require hundreds of billions of CPU hours.
Even with recently proposed emulators, which accelerate model evaluations by
three to four orders to magnitude, the computation cost of SED modeling would
remain a major bottleneck for galaxy studies.

We demonstrate in this work that Amortized Neural Posterior Estimation (ANPE)
provides an alternative \emph{scalable} approach for Bayesian inference in SED
modeling.
ANPE is a simulation-based inference method that formulates Bayesian inference
as a density estimation problem and uses neural density estimators (NDE) to
approximate the posterior over the full space of observations.
The NDE is trained using parameter values drawn from the prior and mock
observations simulated with these parameters.
Once trained, a posterior can be obtained from the NDE by providing the
observations as the conditional variables without any additional model
evaluations.

In this work, we present {\sc SEDflow}, a galaxy SED modeling method using ANPE
and PROVABGS, a flexible SED model that uses a compact non-parameteric SFH and
ZH prescriptions and was recently validated in \cite{hahn2022}.
Furthermore, we apply {\sc SEDflow} to optical photometry from the NASA-Sloan
Atlas as demonstration and validation of our ANPE approach.
We present the following key results from our analysis. \vspace{2mm}
\begin{compactitem}
    \item We train {\sc SEDflow} using a data set of ${\sim}1$ million SED
        model parameters and forward model synthetic SEDs.
        The parameters are drawn from a prior and the forward model is based on
        the PROVABGS and noise models.
        We design the ANPE to estimate $p(\btheta | f_X, \sigma_X, z)$, where
        $f_X$, $\sigma_X$, and $z$ are the photometry, photometric uncertainty,
        and redshift, respectively.
        For its architecture, we use a MAF normalizing flow with 15 MADE blocks
        each with 2 hidden layers and 500 hidden units.
        Training {\sc SEDflow} requires roughly 1 day on a single CPU.
        Once trained, deriving posteriors of galaxy properties for a galaxy
        takes ${\sim}1$ second, $10^5\times$ faster than traditional MCMC sampling.
    \item Posteriors derived using {\sc SEDflow} show excellent agreement with
        posteriors derived from MCMC sampling.
        We further validate the accuracy of the posteriors by applying  {\sc
        SEDflow} to synthetic observations with known true parameter values.
        Based on statistical metrics used in the literature (p-p plot and SBC),
        we find excellent agreement between the {\sc SEDflow} and the true
        posteriors.
    \item Lastly, we demonstrate the advantages of {\sc SEDflow} by applying it
        to the NASA-Sloan Atlas.
        Estimating the posterior of ${\sim}33,000$ galaxies takes $\sim$12 CPU
        hours.
        We make the catalog of posteriors publicly available at
        \url{https://changhoonhahn.github.io/SEDflow/}.
        For each galaxy, the catalog contains posteriors of all 12 PROVABGS
        SED model parameters as well as the galaxy properties: $M_*$,
        average SFR over 1Gyr, mass-weighted metallicity, and mass-weighted
        stellar age. \vspace{2mm}
\end{compactitem}

This work highlights the advantages of using an ANPE approach to Bayesian SED
modeling.
Our approach can easily be extended beyond this application.
For instance, we can include multi-wavelength photometry at ultra-violet or
infrared wavelengths.
We can also modify \sedflow~to infer redshift from photometry.
In \sedflow, we include redshift as a conditional variable, since NSA provides
spectroscopic redshifts.
However, redshift can be included as an inferred variable rather than a
conditional one.
Then, we can apply \sedflow~to infer galaxy properties from photometric data
sets without redshift measurements while marginalizing over the redshift
prior.
If we do not require spectroscopic redshifts, \sedflow~can be extended to much
larger data sets that span fainter and broader galaxy samples.
Conversely, we can use \sedflow~to infer more physically motivated photometric
redshifts, where we marginalize over our understanding of galaxies rather than
using templates.

The ANPE approach to SED modeling can also be extended to galaxy spectra.
Constructing an ANPE for the full data space of spectra would requires
estimating a dramatically higher dimensional probability distribution.
SDSS spectra, for instance, have ${\sim}3,600$ spectral elements.
In our approach we include the uncertainties of observables as conditional
variables, which doubles the curse of dimensionality.
Recent works, however, have demonstrated that galaxy spectra can be represented
in a compact low-dimensional space using autoencoders~\citep[][Melchior \&
Hahn, in prep.]{portillo2020}.
In \cite{portillo2020}, they demonstrate that SDSS galaxy spectra can be
compressed into 7-dimensional latent variable space with little loss of
information.
Such spectral compression dramatically reduces the dimensionality of the
conditional variable space to dimensions that can be tackled by current ANPE
methods.
We will explore SED modeling of galaxy spectrophotometry using ANPE and
spectral compression in a following work.