\documentclass[a4paper,UKenglish]{lipics-v2016}
\usepackage{microtype}
\usepackage{bussproofs}
\usepackage{stmaryrd}

\newcommand{\new}{\mathsf{new}}
\newcommand{\for}{\mathrm{for }}
\newcommand{\Ncal}{\mathcal{N}}
\newcommand{\interp}[1]{\llbracket #1 \rrbracket}
\newcommand{\interpp}[1]{\{\!| #1 |\!\}}
\newcommand{\from}{\leftarrow}
\newcommand{\maps}{\colon}
\newcommand{\Th}{\mathrm{Th}}
\newcommand{\Gph}{\mathrm{Gph}}
\newcommand{\FinSet}{\mathrm{FinSet}}
\newcommand{\FPGphCat}{\mathrm{FPGphCat}}
\newcommand{\Set}{\mathrm{Set}}
\newcommand{\Cat}{\mathrm{Cat}}
\newcommand{\Calc}{\mathrm{Calc}}
\newcommand{\Mon}{\mathrm{Mon}}
\newcommand{\op}{\mathrm{op}}
\newcommand{\NN}{\mathbb{N}}
\newcommand{\pic}{$\pi$-calculus}

\title{Representing operational semantics with enriched Lawvere theories}
\author[1]{
Michael Stay
}
\author[2]{
L.\ G.\ Meredith
}
\affil[1]{
  Pyrofex Corp., Kirkland, WA, USA\\
  {\tt stay@pyrofex.net}
}
\affil[2]{
{RChain Cooperative, Seattle, WA, USA}\\
{\tt greg@rchain.coop}
}
\Copyright{Michael Stay, Lucius Gregory Meredith}
\subjclass{F.1.2 Modes of computation, F.3.2 Semantics of Programming Languages, F.4 Mathematical Logic and Formal Languages, D.1.3 Concurrent Programming, D.3.1 Formal Definitions and Theory, D.3.3 Language Constructs and Features}
\keywords{Concurrent combinator, nominal, pi calculus}

%Editor-only macros:: begin (do not touch as author)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\EventEditors{John Q. Open and Joan R. Acces}
\EventNoEds{2}
\EventLongTitle{42nd Conference on Very Important Topics (CVIT 2016)}
\EventShortTitle{CVIT 2016}
\EventAcronym{CVIT}
\EventYear{2016}
\EventDate{December 24--27, 2016}
\EventLocation{Little Whinging, United Kingdom}
\EventLogo{}
\SeriesVolume{42}
\ArticleNo{23}
% Editor-only macros::end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\maketitle
\begin{abstract}
  \noindent
  Many term calculi, like $\lambda$-calculus or {\pic}, involve
  binders for names, and the mathematics of bound variable names is
  subtle.  Sch\"onfinkel introduced the SKI combinator calculus in
  1924 to clarify the role of quantified variables in intuitionistic
  logic by eliminating them. Yoshida demonstrated how to
  eliminate the bound names coming from the input prefix in the
  asynchronous {\pic}, but her combinators still depend on the $\new$
  operator to bind names.  Recently, Meredith and Stay
  showed how to modify Yoshida's combinators by replacing $\new$ and
  replication with reflective operators to provide the first
  combinator calculus with no bound names into which the asynchronous
  {\pic} has a faithful embedding. Here we provide an alternative set
  of combinators built from $\mathsf{SKI}$ plus reflection that also
  eliminates all nominal phenomena, yet provides a faithful
  embedding  of a reflective higher-order pi calculus.
  We show that with the nominal features effectively
  eliminated as syntactic sugar, multisorted Lawvere theories enriched
  over graphs suffice to capture the operational semantics of the
  calculus.
\end{abstract}
\EnableBpAbbreviations

\section{Introduction}
Many term calculi, like $\lambda$-calculus or {\pic}, involve binders
for names, and the mathematics of bound variable names is subtle.
Sch\"onfinkel introduced the SKI combinator calculus in 1924 to
clarify the role of quantified variables in intuitionistic logic by
eliminating them \cite{finkel}. Yoshida demonstrated how to eliminate
the bound names coming from the input prefix in the asynchronous
{\pic}, but her combinators still depend on the $\new$ operator to
bind names. Curry developed Sch\"onfinkel's ideas much
further. Recently, Meredith and Stay \cite{Rhocomb} showed how to
modify Yoshida's combinators by replacing $\new$ and replication with
reflective operators to provide the first combinator calculus with no
bound names into which the asynchronous {\pic} has a faithful
embedding of a reflective higher-order pi calculus.
Here we provide an alternative set of combinators built
from $\mathsf{SKI}$ plus reflection that also eliminates all nominal
phenomena, yet provides a faithful embedding.

The recent work by Jamie Gabbay and Andrew Pitts
\cite{DBLP:journals/fac/GabbayP02} and others
\cite{DBLP:journals/jcss/Clouston14} on nominal set theory has put the
study of bound names and substitution on a much nicer foundation, at
the cost of an increase in the complexity of the semantic
framework. It interprets nominal phenomena in terms of atoms in
Fraenkl-Mostowski set theory. Clouston's work in particular makes
evident the additional machinery needed to interpret nominal phenomena
as Lawvere theories. On the other hand, with the nominal features
effectively eliminated as syntactic sugar, we show that multisorted
Lawvere theories enriched over graphs suffice to capture the
operational semantics of the calculus.

\section{Previous work}

There is a long history and an enormous body of work on modeling term rewriting and operational semantics with various notions of category enriched over category-like structures; we only have room here for a sampling.  L\"uth and Ghani \cite{DBLP:conf/ctcs/LuethG97} use poset-enriched categories to study the modularity of strong normalization.  One approach to nominality is the one we mentioned in the introduction; a different approach deals with nominal issues by allowing ``funtion types'' in the signature:  Seely \cite{DBLP:conf/lics/Seely87}
% R. A. G. Seely, Modeling computations: a 2-categorical framework, in Proc. Symp. Logic Comp. Sci. 1987, Computer Society of the IEEE, pp. 65–71. Also available at http://www.math.mcgill.ca/rags/WkAdj/LICS.pdf.
suggested using 2-categories for modeling the denotational semantics of lambda calculus in Scott domains to capture the adjunction between $\beta$ reduction and $\eta$ conversion;  Hilken \cite{DBLP:journals/tcs/Hilken96}
% B. Hilken, Towards a proof theory of rewriting: the simply-typed 2λ- calculus, Theor. Comp. Sci. 170 (1996), 407–444. Also available at http://math.ucr.edu/home/baez/qg-winter2007/hilken_2lambda_calculus.ps
expands Seely's work by exploring the proof theory using categorical logic; and Hirschowitz \cite{DBLP:journals/corr/Hirschowitz13}
% Tom Hirschowitz, Cartesian closed 2-categories and permutation equivalence in higher-order rewriting, Logical Methods in Computer Science, IfCoLog (Interna- tional Federation of Computational Logic), 9 (3) (2013), 10–28. Also available at https://hal.archives-ouvertes.fr/hal-00540205/file/macy.pdf
generalizes algebraic signatures to cartesian closed 2-signatures.  A third approach is to model substitution explicitly: Stell \cite{Stell}
% http://www.comp.leeds.ac.uk/jgs/caen.ps
considered sesquicategories for term rewriting; in his system, objects are finite sets of variables, morphisms are substitutions, and 2-morphisms are roughly rewrite rules.

\section{Gph-enriched categories}
Here we review some standard definitions and results in enriched category theory; see \cite{CIS-335497}, \cite{Power99EnrichedLawvereTheories}, \cite{DBLP:journals/acs/LackR11}, and \cite{Trimble} for more details.

A {\bf directed multigraph with self loops}, hereafter {\bf graph}, consists of a set $E$ of edges, a set $V$ of vertices, two functions $s,t\maps E \to V$ picking out the source and target of each edge, and a function $a\maps V \to E$ such that $s\circ a$ and $t \circ a$ are both the identity on $V$---that is, $a$ equips each vertex in $V$ with a chosen self loop.  There are no constraints on $E, V, s,$ or $t$, so a graph may have infinitely many vertices and infinitely many edges between any pair of vertices.  A {\bf graph homomorphism} from $(E, V, s, t, a)$ to $(E', V', s', t', a')$ is a pair of functions $(\epsilon\maps E \to E', \upsilon\maps V \to V')$ such that $\upsilon\circ s = s' \circ \epsilon$ and $\upsilon\circ t = t' \circ \epsilon$.  {\bf Gph} is the category of graphs and graph homomorphisms.  Gph has finite products: the terminal graph is the graph with one vertex and one loop, while the product of two graphs $(E, V, s, t, a) \times (E', V', s', t', a')$ is $(E \times E', V \times V', s \times s', t\times t', a \times a').$

A {\bf Gph-enriched category} consists of
\begin{itemize}
  \item a set of objects;
  \item for each pair of objects $x, y,$ a graph $\hom(x,y);$
  \item for each triple of objects $x, y, z,$ a composition graph homomorphism $\circ\maps \hom(y, z) \times \hom(x, y) \to \hom(x, z);$ and
  \item for each object $x,$ a vertex of $\hom(x, x),$ the identity on $x,$
\end{itemize}
such that composition is associative, and composition and the identity obey the unit laws.  A Gph-enriched category has finite products if the underlying category does.

Any category is trivially Gph-enrichable by treating the elements of the hom sets as vertices and adjoining a self loop to each vertex.  The category Gph is nontrivially Gph-enriched: Gph is a topos, and therefore cartesian closed, and therefore enriched over itself.  Given two graph homomorphisms $F, F'\maps (E, V, s, t, a) \to (E', V', s', t', a'),$ a {\bf graph transformation} assigns to each vertex $v$ in $V$ an edge $e'$ in $E'$ such that $s'(e') = F(v)$ and $t'(e') = F'(v).$  Given any two graphs $G$ and $G',$ there is an exponential graph $G'^G$ whose vertices are graph homomorphisms between them and whose edges are graph transformations.

A {\bf Gph-enriched functor} between two Gph-enriched categories $C, D$ is a functor between the underlying categories such that the graph structure on each hom set is preserved, {\em i.e.} the functions between hom sets are graph homomorphisms between the hom graphs.

Let $S$ be a finite set, $\FinSet$ be a skeleton of the category of finite sets and functions between them, and $\FinSet/S$ be the category of functions into $S$ and commuting triangles.  A {\bf multisorted Gph-enriched Lawvere theory}, hereafter {\bf Gph-theory} is a Gph-enriched category with finite products Th equipped with a finite set $S$ of {\bf sorts} and a Gph-enriched functor $\theta\maps \FinSet^{\op}/S \to \Th$ that preserves products strictly.  Any Gph-theory has an underlying multisorted Lawvere theory given by forgetting the edges of each hom graph.

A {\bf model} of a Gph-theory Th is a Gph-enriched functor from Th to Gph that preserves products up to natural isomorphism.  A {\bf homomorphism of models} is a braided Gph-enriched natural transformation between the functors.  Let FPGphCat be the 2-category of small Gph-enriched categories with finite products, product-preserving Gph-functors, and braided Gph-natural transformations.  The forgetful functor $U\maps \FPGphCat[\Th, \Gph] \to \Gph$ that picks out the underlying graph of a model has a left adjoint that picks out the free model on a graph.

Gph-enriched categories are part of a spectrum of 2-category-like structures.  A strict 2-category is a category enriched over Cat with its usual product.  Sesquicategories are categories enriched over Cat with the ``funny'' tensor product \cite{Lack2010}; a sesquicategory can be thought of as a 2-category where the interchange law does not hold.  A Gph-enriched category can be thought of as a sesquicategory where 2-morphisms (now edges) cannot be composed.  Any strict 2-category has an underlying sesquicategory, and any sesquicategory has an underlying Gph-enriched category; these forgetful functors have left adjoints.

\section{Gph-theories as models of computation}

Lawvere theories and their generalizations are categories with infinitely many objects and morphisms, but most theories of interest are finitely generated.  A presentation of the underlying multisorted Lawvere theory of a finitely-generated Gph-theory is a signature for a term calculus, consisting of a set of sorts, a set of term constructors, and a set of equations, while the edges in the hom graphs of the theory encode the reduction relation.

Here is a presentation of the SKI combinator calculus as a Gph-theory:
\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
  \[\begin{array}{rl}
    S&:1 \to T\\
    K&:1 \to T\\
    I&:1 \to T\\
    (-\; -)&: T^2 \to T\\
  \end{array}\]
  \item structural congruence (no equations)
  \item rewrites
  \[\begin{array}{rl}
    \sigma&:(((S\; x)\; y)\; z) \Rightarrow ((x\; z)\; (y\; z))\\
    \kappa&:((K\; y)\; z) \Rightarrow y\\
    \iota&:(I\; z) \Rightarrow z\\
  \end{array}\]
\end{itemize}
where in the rewrites we have used expressions like $((K\; y)\; z)$ as shorthand for
\[ T\times T \xrightarrow{\mbox{\tiny left}^{-1}} 1\times T \times T \xrightarrow{K \times T \times T} T\times T \times T \xrightarrow{(-\;-)\times T} T\times T \xrightarrow{(-\;-)} T. \]

A model $M$ of this Gph-theory in Gph picks out a graph $M(T)$ of terms and rewrites.  It picks out three special vertices $S,K,$ and $I$ of $M(T)$; it equips $M(T)$ with a graph homomorphism from $M(T)^2$ to $M(T)$ that says for every pair of vertices $(u,v),$ there is a vertex $(u\;v)$, and similarly for edges; and it equips $M(T)$ with graph transformations asserting the existence of an edge out of a reducible expression to the term it reduces to.

That this Gph-theory captures the operational semantics of the SKI calculus is almost definitional: there is an edge between distinct vertices in the free model on the empty graph if and only if the source vertex is reducible to the target vertex in a single step.

It is straightforward to verify that Gph-theories suffice to capture the operational semantics of any calculus where every context is a reduction context.  This restriction on reduction contexts is a consequence of the fact that models map term constructors to graph homomorphisms: given a model $M$, a graph homomorphism $F\maps M(T) \to M(T)$, and an edge $e\maps t_1 \to t_2,$ there is necessarily an edge $F(e)\maps F(t_1) \to F(t_2).$

\section{Gph-theory for SKI with the weak head normal form evaluation strategy}
\label{whnf}
In modern programming languages, many contexts are {\em not} reduction contexts.  In Haskell, for instance, there are no reductions under a lambda abstraction: even if $t_1$ reduces to $t_2$ as a program, the term $\backslash x \to t_1$ does not reduce to $\backslash x \to t_2.$

Gph-theories can still capture the operational semantics of calculi with restrictions on reduction contexts by introducing term constructors that explicitly mark the reduction contexts.  For example, suppose that we want an evaluation strategy for the SKI calculus that only reduces the leftmost combinator when it has been applied to sufficiently many arguments, {\em i.e.} we want the {\em weak head normal form}; we can accomplish this by introducing a term constructor $R\maps T \to T$ that explicitly marks the reduction contexts.  We then add a structural congruence rule for propagating the context and modify the existing reduction rules to apply only to marked contexts.

\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
  \[\begin{array}{rl}
    S&:1 \to T\\
    K&:1 \to T\\
    I&:1 \to T\\
    (-\; -)&: T^2 \to T\\
    R&:T \to T\\
  \end{array}\]
  \item structural congruence
  \[\begin{array}{rl}
    R(x\; y) &= (Rx\; y)\\
  \end{array}\]
  \item rewrites
  \[\begin{array}{rl}
    \sigma&:(((RS\; x)\; y)\; z) \Rightarrow ((Rx\; z)\; (y\; z))\\
    \kappa&:((RK\; y)\; z) \Rightarrow Ry\\
    \iota&:(RI\; z) \Rightarrow Rz\\
  \end{array}\]
\end{itemize}

\begin{theorem}
  Let $t$ be a term in which $R$ does not appear.  Then $Rt$ reduces to $Rt',$ where $t'$ is the weak head normal form of $t.$
\end{theorem}

\begin{proof}
If we form the term $Rt$ where $t$ contains no uses of $R$, no reductions will ever take place in the right-hand argument of an application: the structural congruence and rewrite rules enforce that the $R$ context can only move to the left term in an application, never the right.  The result follows by induction on the number of steps to reach $t'.$
\end{proof}

\section{Explicit reduction contexts as gas}
The Ethereum \cite{wood2014ethereum} and RChain \cite{RChain} projects are building virtual machines on the blockchain.  Both use the concept of a linear resource called ``gas'' (as in gasoline) that is consumed as the virtual machine executes.  Gph-theories can capture the operational semantics of a calculus where reduction contexts are consumable, and thus play a role similar to that of gas \cite{DBLP:journals/corr/StayM15}.

\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
  \[\begin{array}{rl}
    S&:1 \to T\\
    K&:1 \to T\\
    I&:1 \to T\\
    (-\; -)&: T^2 \to T\\
    R&:T \to T\\
  \end{array}\]
  \item structural congruence
  \[\begin{array}{rl}
    R(x\; y) &= (Rx\; y)\\
  \end{array}\]
  \item rewrites
  \[\begin{array}{rl}
    \sigma&:(((RS\; x)\; y)\; z) \Rightarrow ((x\; z)\; (y\; z))\\
    \kappa&:((RK\; y)\; z) \Rightarrow y\\
    \iota&:(RI\; z) \Rightarrow z\\
  \end{array}\]
\end{itemize}

\begin{theorem}
  Let $t$ be a term in which $R$ does not appear; let $t'$ be the weak head normal form of $t$; let $m$ be the number of steps by which $Rt$ reduces to $Rt'$ in the calculus of section \ref{whnf}; and let $n\ge m$.  Then in this calculus, $R^n t$ reduces to $R^{n-m}t'$ in $m$ steps.
\end{theorem}

\begin{proof}
As before, if we form the term $Rt$ where $t$ contains no uses of $R$, no reductions will ever take place in the right-hand argument of an application.  Each application of the reduction rules reduces the number of $R$s by one, and structural equivalence preserves the number of $R$s.  The result follows by induction on the number of steps to reach $t'.$
\end{proof}

\section{Gph-theory for a pi calculus variant}
\label{rhocomb}
Gph-theories can capture the operational semantics of concurrent calculi as well as serial calculi like SKI above.

  Meredith and Radestock \cite{DBLP:journals/entcs/MeredithR05} describe a reflective higher-order variant of pi calculus we call the RHO calculus.  Rather than the usual replication and $\new$ operators, they have quoting and unquoting operators.  Quoting turns a process into a name and unquoting does the opposite; freshness of names is obtained using a type discipline.  They prove that there is a faithful embedding of the monadic asynchronous pi calculus into the RHO calculus.

\subsection{The RHO calculus}
\subsubsection{Syntax}
\[\begin{array}{rlr}
  P, Q &::= 0 & \mbox{the stopped process}\\
  &| \quad \for(y \from x)P & \mbox{input guarded process} \\
  &| \quad x!P & \mbox{output process}\\
  &| \quad P \;|\; Q & \mbox{parallel composition}\\
  &| \quad *x & \mbox{deference}\\
  &\\
  x, y &::= \&P & \mbox{quotation}\\
\end{array}\]

Note that in the original rho-calculus papers the notation was
somewhat different. The quotation and dereference constructions were
originally written, $\ulcorner P \urcorner$ and $\urcorner x \ulcorner$,
respectively. Here we have adopted a more programmer
friendly style employing the $\&$ and $*$ of the $\mathsf{C}$ programming
language for reference (quotation) and dereference, respectively. Input
guards which were written with a whimper $?$ in more traditional process calculi style  are now written in for-comprehension style as adopted
in languages like $\mathsf{Scala}$; e.g. $x?(y)P$ is written
here $\mathsf{for}( y \from x )P$.

\subsubsection{Free and bound names}
\[\begin{array}{rl}
FN(0) &= \emptyset \\
FN(\for(y \from x)P) &= \{x\}\cup (FN(P)\backslash \{y\}) \\
FN(x!P) &= \{x\}\cup FN(P) \\
\end{array}\quad\quad
\begin{array}{rl}
FN(P|Q) &= FN(P)\cup FN(Q) \\
FN(*x) &= \{x\}
\end{array}\]
\subsubsection{Structural congruence}
Structural (process) congruence is the smallest congruence $\equiv$ containing $\alpha$-equivalence and making $(|, 0)$ into a commutative monoid.
\subsubsection{Name equivalence}
Name equivalence is the smallest equivalence relation $\equiv_N$ on names such that
\begin{center}
  \AXC{} \UIC{$\&*x \equiv_N x$} \DP and \AXC{$P \equiv Q$} \UIC{$\&P \equiv_N \&Q$} \DP.
\end{center}
\subsubsection{Substitution}
Syntactic substitution:
\[\begin{array}{rl}
  (0)\{\&Q/\&P\} &= 0\\
  (\for (y \from x) R)\{\&Q/\&P\} &= \for (z \from (x\{\&Q/\&P\})) (R\{z/y\}\{\&Q/\&P\})\\
  (x!R)\{\&Q/\&P\} &= (x\{\&Q/\&P\})!(R\{\&Q/\&P\})\\
  (R|S)\{\&Q/\&P\} &= (R\{\&Q/\&P\}) \;|\; (S\{\&Q/\&P\})\\
  (*x)\{\&Q/\&P\} &= \left\{\begin{array}{rl}
    *\&Q & \mbox{when } x \equiv_N \&Q\\
    *x & \mbox{otherwise,}
  \end{array}\right.
\end{array}\]
where
\[ x\{\&Q/\&P\} = \left\{\begin{array}{rl}
                  \&Q & \mbox{if } x\equiv_N \&P \\
                  x & \mbox{ otherwise}
                \end{array}\right.\]
and, in the rule for input, $z$ is chosen to be distinct from $\&P, \&Q,$ the free names in $Q,$ and all the names in $R.$

Semantic substitution, for use in $\alpha$-equivalence:
\[ (*x)\{\&Q/\&P\} = \left\{\begin{array}{rl}
  Q & \mbox{when } x \equiv_N \&Q\\
  *x & \mbox{otherwise}
\end{array}\right. \]

\subsubsection{Reduction rules}
We use $\to$ to denote single-step reduction.
\begin{center}
\AXC{$x_0 \equiv_N x_1$}
\UIC{$\for(y \from x_1)P \;|\; x_0!Q \quad \to\quad P\{\&Q / y\}$} \DP \quad \quad
\end{center}

\begin{center}
\AXC{$P\to P'$}
\UIC{$P\;|\; Q \quad \to \quad P' \;|\; Q$} \DP
\end{center}

\begin{center}
\AXC{$P\equiv P'$} \AXC{$P' \to Q'$} \AXC{$Q' \equiv Q$}
\TIC{$P\to Q$} \DP
\end{center}
\subsection{RHO combinators}
We can define an embedding $\interp{-}$ of closed RHO calculus terms into a set of RHO combinators.  We follow Milner \cite{milner91polyadicpi} in thinking of an input-prefixed process $\for(x \from y)P$ as consisting of two parts: the first names the channel $y$ on which the process is listening, while the second describes the continuation $\lambda x.P$ in terms of an abstracted name.  The right hand side of the communication rule, in effect, applies the continuation to the name to be substituted.  Since the only bound names in the RHO calculus come from input prefixing, we can completely eliminate bound names by using abstraction elimination on the continuation.  Like the weak head normal form SKI calculus above, this combinator calculus uses a linear resource $C$ to reify reduction contexts.

A Gph-theory for the operational semantics of these combinators has:
\begin{itemize}
  \item one sort $T$, for terms
  \item term constructors
    \[\begin{array}{rl}
      C &: 1 \to T \\
      0 &: 1 \to T \\
      | &: 1 \to T \\
      \for &: 1 \to T \\
      ! &: 1 \to T \\
      \& &: 1 \to T \\
    \end{array}\quad\quad
    \begin{array}{rl}
      * &: 1 \to T \\
      S &: 1 \to T \\
      K &: 1 \to T \\
      I &: 1 \to T \\
      () &: T \times T \to T \\
    \end{array}\]
  \item structural congruence rules
    \[\begin{array}{rll}
      ((|\; 0)\; P) &= P & \mbox{unit law}\\
      ((|\; ((|\; P)\; Q))\; R) &= ((|\; P)\; ((|\; Q)\; R) & \mbox{associativity}\\
      ((|\; P)\; Q) &= ((|\; Q)\; P) &\mbox{commutativity}\\
    \end{array}\]
  \item reduction rules
    \[\begin{array}{ll}
      \sigma\maps (((S\; P)\; Q)\; R) \Rightarrow ((P\; R)\; (Q\; R)) & \mbox{action of }S \\
      \kappa\maps ((K\; P)\; Q) \Rightarrow P & \mbox{action of }K\\
      \iota\maps (I\; P) \Rightarrow P & \mbox{action of }I\\
      \xi\maps ((|\; C)\; ((|\; ((\for\; (\&\; P))\; Q))\; ((!\; (\&\; P))\; R))) \Rightarrow ((|\; C)\; (Q\; (\&\; R))) & \mbox{communication}\\
      \epsilon\maps ((|\; C)\;(*\; (\&\; P))) \Rightarrow ((|\; C)\; P) & \mbox{evaluation} \\
    \end{array}\]
\end{itemize}

\subsection{Embeddings}
We define an interpretation function $\interp{-}$ from RHO calculus terms into RHO combinators by

\[\begin{array}{rl}
  \interp{0} &= 0 \\
  \interp{\for(x \from \&P)Q} &= ((for\; (\&\; \interp{P}))\; \interp{Q}_x)\\
  \interp{\&P!Q} &= ((!\; (\&\; \interp{P}))\; \interp{Q}) \\
  \interp{P|Q} &= ((|\; \interp{P})\; \interp{Q})\\
  \interp{*\&P} &= (*\; (\&\; \interp{P}))
\end{array}\]
where $\interp{-}_x$ eliminates the free name $x:$
\[\begin{array}{rl}
  \interp{P}_x &= (K\; \interp{P}) \mbox{ where $x$ is not free in } P \\
  \interp{\for(y \from \&P)Q}_x &= ((S\; ((S\; (K for))\; ((S\; (K\; \&))\; \interp{P}_x)))\; \interp{\interp{Q}_y}_x) \\
  \interp{\&P!Q}_x &= ((S\; ((S\; (K\; !))\; ((S\; (K\; \&))\; \interp{P}_x)))\; \interp{Q}_x) \\
  \interp{P|Q}_x &= ((S\; ((S\; (K\; |))\; \interp{P}_x))\; \interp{Q}_x) \\
  \interp{*\&P} &= \left\{\begin{array}{ll}
    ((S\; (K\; *))\; I) & \mbox{when } \&P \equiv_N x\\
    ((S\; (K\; *))\; ((S (K\; \&))\; \interp{P}_x) & \mbox{otherwise.}
  \end{array}\right.
\end{array}\]

Consider the following sorting on RHO combinators:
\[\begin{array}{rl}
  C &: W\\
  0 &: W\\
  | &: W \Rightarrow W \Rightarrow W\\
  \for &: N \Rightarrow (N \Rightarrow W) \Rightarrow W\\
  ! &: N \Rightarrow W \Rightarrow W\\
\end{array}\quad\quad
\begin{array}{rl}
  \& &: W \Rightarrow N\\
  * &: N \Rightarrow W\\
  S &: \forall X,Y,Z.(Z \Rightarrow Y \Rightarrow X) \Rightarrow (Z \Rightarrow Y) \Rightarrow Z \Rightarrow X\\
  K &: \forall X,Y.X \Rightarrow Y \Rightarrow X\\
  I &: \forall X.X \Rightarrow X\\
\end{array}\]

The left- and right-hand sides of each of the structural congruence and rewrite rules have the sort $W,$ the interpretation of any RHO calculus term has the sort $W,$ and the result of eliminating an abstraction has the sort $N \Rightarrow W.$

We define an interpretation function $\interpp{-}$ from $W$-sorted RHO combinators not containing $C$ into the RHO calculus by
\[\begin{array}{rl}
  \interpp{0} &= 0\\
  \interpp{P\;|\;Q} &= \interpp{P} \;|\; \interpp{Q}\\
  \interpp{((\for\; (\&\; P))\; Q)} &= for (\&\interpp{R} \leftarrow \&\interpp{P})\interpp{(Q\; (\&\; R))}\\
  \interpp{((!\; (\&\; P))\; Q)} &= \&\interpp{P}!\interpp{Q}\\
  \interpp{(*(\&\; P))} &= *\&\interpp{P}\\
  \interpp{(((S\; P)\; Q)\; R)} &= \interpp{((P\; R)\; (Q\; R))}\\
  \interpp{((K\; P)\; Q)} &= \interpp{P}\\
  \interpp{(I\; P)} &= \interpp{P}
\end{array}\]
where $R$ is any $W$-sorted RHO combinator.

Some simple calculation shows that
\begin{theorem}
\label{roundtrip}
  $\interpp{\interp{P}}$ is $\alpha$-equivalent to $P$, $Q$ is reducible to $\interp{\interpp{Q}}$ without using the rewrite $\xi,$ and $\interp{\interpp{-}}$ is idempotent.
\end{theorem}
See the appendix for more details.

\subsection{Barbed bisimilarity}
An {\bf observation relation} $\downarrow_\Ncal$ over a set of names $\Ncal$ is the smallest relation satisfying
\begin{center}
  \AXC{$y \in \Ncal$} \AXC{$x \equiv_N y$} \BIC{$x!P \downarrow_\Ncal x$} \DP $\quad$ and $\quad$ \AXC{$P  \downarrow_\Ncal x \mbox{ or } Q \downarrow_\Ncal x$} \UIC{$P\;|\;Q  \downarrow_\Ncal x$} \DP
\end{center}
for the RHO calculus or
\begin{center}
  \AXC{$y \in \Ncal$} \AXC{$x \equiv_N y$} \BIC{$((!\; x)\; P) \downarrow_\Ncal x$} \DP $\quad$ and $\quad$ \AXC{$P  \downarrow_\Ncal x \mbox{ or } Q \downarrow_\Ncal x$} \UIC{$((|\; P)\; Q)  \downarrow_\Ncal x$} \DP.
\end{center}
for the RHO combinators.

We denote eventual reduction by $\to^*$ and write $P \downarrow^*_\Ncal x$ if there exists a process $Q$ such that $P \to^* Q$ and $Q \downarrow_\Ncal x.$

An {\bf $\Ncal$-barbed bisimulation} over a set of names $\Ncal$ is a symmetric binary relation $S_\Ncal$ between agents such that $P \mathop{S_\Ncal} Q$ implies
\begin{enumerate}
  \item if $P \to P'$ then $Q \to^* Q'$ and $P' \mathop{S_\Ncal} Q',$ and
  \item if $P \downarrow_\Ncal x,$ then $Q \downarrow^*_\Ncal x.$
\end{enumerate}
$P$ is $\Ncal$-barbed bisimilar to $Q,$ written $P \approx Q,$ if $P \mathop{S_\Ncal} Q$ for some $\Ncal$-barbed bisimulation $S_\Ncal.$

\subsection{Faithfulness}

\begin{theorem}
  $P \approx_{\mbox{\tiny calc}} Q \iff  ((|\; C)\; \interp{P}) \approx_{\mbox{\tiny comb}} ((|\; C)\; \interp{Q})$.
\end{theorem}

\begin{proof}[Proof sketch]
The only occurrence of $C$ on the right is at the topmost context and the rewrite rules preserve the location of $C$, so the only reduction context is the topmost one.  The rest follows from the two interpretation functions and theorem \ref{roundtrip}.  In particular, while the only reduction rule in the RHO calculus is synchronizing on a name, there are extra reduction rules for the RHO combinators; however, these extra reduction rules never send or receive on a name and never prevent sending or receiving on a name.  Therefore, each synchronization in the evaluation of a RHO calculus term corresponds to a synchronization in the corresponding RHO combinator term and some number of reductions of $S,K,I,$ or evaluating a quoted process.
\end{proof}

In fact, we believe a much stronger property than bisimilarity should hold: since $S,K,$ and $I$ are only used for eliminating dummy variables and the $\epsilon$ reduction plays the role of semantic substitution, $\interp{\interpp{-}}$ should pick out a normal form for a RHO combinator.  We should get a set of normal-form equivalence classes of $W$-sorted RHO combinators that is isomorphic to the set of $\alpha$-equivalence classes of RHO calculus terms.  Then we should get
\[ P \xrightarrow{\mbox{\tiny comm}} P' \quad \iff \quad \interp{P} \xrightarrow{\xi} \interp{P'}\]
and
\[ Q \xrightarrow{\xi} Q' \quad \iff \quad \interpp{Q} \xrightarrow{\mbox{\tiny comm}} \interpp{Q'}, \]
where we now regard the left and right sides as being equivalence classes.

\section{Conclusion and future work}
This paper is part of a pair of papers demonstrating that reflection
provides a powerful technique for treating nominal phenomena as
syntactic sugar, thus paving the way for simpler semantic treatments
of richly featured calculi, such as the {\pic} and other calculi of
concurrency. We illustrated the point by providing faithful semantics
of both the $\lambda$-calculus and the {\pic} in terms of graph-enriched
Lawvere theories. This work may be considered preparatory for a more
elaborate study of logics for concurrency in which the nominal
phenomena have logical status, but may be treated in a technically
simpler fashion.

\section{Appendix: abstraction elimination calculations}
\[\begin{array}{rl}
  & ((K\; \interp{P})\; x) \\
  = & \interp{P}\\
  \\

  & (((S\; ((S\; (K\; for))\; ((S\; (K\; \&))\; \; \interp{P}_x)))\; \interp{\interp{Q}_y}_x)\; x)\\
  =&  ((((S\; (K\; for))\; ((S\; (K\; \&))\; \; \interp{P}_x))\; x)\; (\interp{\interp{Q}_y}_x\; x))\\
  =&  ((((K\; for)\; x)\; (((S\; (K\; \&))\; \; \interp{P}_x)\; x))\; \interp{Q}_y)\\
  =&  ((for\; (((K\; \&)\; x)\; (\interp{P}_x\; x)))\; \interp{Q}_y)\\
  =&  ((for\; (\&\; \interp{P}))\; \interp{Q}_y)\\
  \\
  & (((S\; ((S\; (K\; !))\; ((S\; (K\; \&))\; \interp{P}_x)))\; \interp{Q}_x)\; x)\\
  =&  (((((S\; (K\; !))\; ((S\; (K\; \&))\; \interp{P}_x)))\; x)\; (\interp{Q}_x\; x))\\
  =&  ((((K\; !)\; x)\; (((S\; (K\; \&))\; \interp{P}_x)\; x))\; \interp{Q})\\
  =&  ((!\; (((K\; \&)\; x)\; (\interp{P}_x\; x)))\; \interp{Q})\\
  =&  ((!\; (\&\; \interp{P}))\; \interp{Q})\\
  \\
  & (((S\; ((S\; (K\; |))\; \interp{P}_x))\; \interp{Q}_x)\; x)\\
  =& ((((S\; (K\; |))\; \interp{P}_x)\; x)\; (\interp{Q}_x\; x))\\
  =& ((((K\; |)\; x)\; (\interp{P}_x\; x))\; \interp{Q})\\
  =& (|\; \interp{P})\; \interp{Q})\\
  \\
  & (((S\; (K\; *))\; I)\; x)\\
  =&  (((K\; *)\; x)\; (I\; x))\\
  =&  (*\; x)\\
  \\
  & (((S\; (K\; *))\; ((S\; (K\; \&))\; \interp{P}_x))\; x)\\
  =&  (((K\; *)\; x)\; (((S\; (K\; \&))\; \interp{P}_x)\; x))\\
  =&  (*\; (((K\; \&)\; x)\; (\interp{P}_x\; x)))\\
  =&  (*\; (\&\; \interp{P}))\\
\end{array}\]

\bibliographystyle{plainurl}
\bibliography{calco}
\end{document}

Representing operational semantics with enriched Lawvere theories calco2017/calco_orig.tex


\subsection{Quadcopter}
\subsubsection{Defination}
A quadcopter, also called a quadrotor helicopter, quadrocopter, quadrotor, is a multicopter that is lifted and propelled by four rotors.\cite{cite1}
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/250px-ReuseumMakerFaireQuadrotor.JPG}
    \caption{A Maker Faire quadcopter in Garden City, Idaho\cite{cite1}}\\
\end{figure}
\subsubsection{Flight Control}
\begin{figure}[h!]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/flying.eps}
    \caption{The forces effected on the quadcopter while making stable movement.}\\
\end{figure}
%\newpage
\textbf{[ Hover ]}
\begin{figure}[H]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/Quadrotor_yaw_torque.png}
    \caption{The example model of quadcopter.}\\
\end{figure}

Each rotor produces both thrust and torque. If all the rotors are spinning at the same angular velocity, and as the example shows, with rotor 1,3 spinning clockwise and rotor 2,4 spinning counterclockwise, the angular acceleration on the yaw-axis will be zero.
This is the method to hovering.

\textbf{[ Yaw ]}\\
\begin{figure}[H]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/yaw.png}
    \caption{Yaw control}
\end{figure}
The method of making yaw control can be done by adjusting the angular velocity of one pair of rotors.\\
\textbf{[ Pitch ]}\\
\begin{figure}[H]

  \centering
    \includegraphics[width=0.3\textwidth]{../Pictures/pitch.png}
    \caption{Pitch control}
\end{figure}
The method of making pitch control can be done by increasing one rotor's spinning velocity and decreasing the opposite rotor's spinning velocity.\\
\textbf{[ Roll ]}\\
\begin{figure}[H]

  \centering
    \includegraphics[width=0.3\textwidth, angle=90]{../Pictures/pitch.png}
    \caption{Roll control}
\end{figure}
The method of making roll control is similar to making pitch control.\\
It can be done by increasing one rotor's spinning velocity and decreasing the opposite rotor's spinning velocity.\\

\subsubsection{Design Principles}

This will probably remain an amateur project. Which means the time spent on this project will be relatively short. Therefore, we really want to keep things simple.

If there is an existing library that implemented a function we need, we can simply include the library in our project. Adoption of \emph{de facto} or \emph{de jure} standards make things not only \emph{simple}, but also \emph{easy to manage}.

After all, \emph{combination counts}.

\subsection{Computer Vision}
The basic computer vision system in this project will be \emph{OpenCV}\footnote{ OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision, developed by Intel, and now supported by Willow Garage and Itseez. \url{http://opencv.org}}.

There are several reasons for using \emph{OpenCV}:
\begin{itemize}
  \item Open source. \emph{OpenCV} is in BSD license. Therefore, our work can be perfectly legal, without any violation of IP laws.
  \item A large, active online community.
  \item The Intel background makes it reliable.
  \item Implemented in C++, the library is really fast.
\end{itemize}
Now, it is high time for discussing CV related issues.
\subsubsection{Quadcopter Identification}
The optimal choice, in this case, is a Quadcopter positioning system involves stereo visions and four colored table tennis balls on each Quadcopter.
The reason for that decision is fairly simple: \emph{simplicity}.
As seen in a youtube video, a research group did successfully implemented a tracking system which requires only mono-colored balls to locate quadcopters. \footnote{Something titled "Machine", "athletic", "Quadcopter". They used reflection markers as the tennis balls in this case.} While this is possible, it is also too complex for a small amateur team like ours to make it. However, multi-colored ball system can simplify the mechanism greatly. With limited time and computing power, this is the only logical choice.


\chapter{Self-organization}
\label{sec:selfOrganization}

This chapter describes the algorithms emplyed by \emph{HyCube} for joining/leaving the network, as well as maintenance and recovery processes, maintaining good routing properties under dynamically changing conditions. The algorithms are based on nodes exchanging information about references stored in routing tables and neighborhood sets. When a node receives a notification about existence of some other peer or receives references maintained by some other node (requested for example in the recovery or joining process, or generated by the notifying node itself), it should analyze the node and check if the reference should be stored in the routing tables or the neighborhood set. Every node is analyzed as a candidate to the neighborhood set and appropriate routing table slots.
For the primary routing table, this is the slot at the level calculated based on the common prefix length with the node ($l - 1 - commonPrefixLength$, $l$ - the number of hierarchy levels) at the position equal to the first different digit (group of $d$ bits, $d$ - the number of dimensions).
For the secondary routing table, the slot corresponds to the lowest-level hypercube containing the analyzed node, adjacent to the hypercube of the analyzing node. A simple check may be performed to verify this condition: nodes $X$ and $Y$ are in adjacent hypercubes at levels $l - 1 - j$ to $l - 1 - i$ if and only if $i$ first digits ($d$-bit groups) of $X$ and $Y$ are equal, and $i$-th to $j$-th digits (numbered starting from 0) differ on one (the same for all these digits) bit - this bit corresponds to the dimension in which the two hypercubes are adjacent, and only the slot at the lowest level should be considered (to prevent placing the same nodes in overlapping routing table slots at different levels). Furthermore, depending on settings, additional rules described in Section \ref{sec:rtOverlapping} may apply to prevent storing nodes in the primary routing table slots overlapping with the lower-level secondary routing table slots.
For selecting nodes within individual routing table slots and within the neighborhood set, the algorithms described in Sections \ref{sec:HyCubeNodeSelection} and \ref{sec:uniformNSDistribution} apply.

\section{Maintenance and recovery}

Maintenance is a process of ensuring good system properties that takes place continuously (ensuring the relevance of routing tables, updating information about neighbors). The maintenance mechanism used in \emph{HyCube} is directly connected with the routing table neighbor selection (described in detail in Section \ref{sec:HyCubeNodeSelectionLNS}). Every node periodically sends keepalive messages to all the nodes in its routing tables, and, depending on whether the keepalive response is received or not, the node's liveness value is updated. Based on the liveness value, the routing table reference may be skipped in the next hop selection (to avoid failed paths), may be replaced, or removed from the routing tables.

Recovery is a process of repairing the network topology and propagating necessary information over the network on account of a topology change (new nodes joining/leaving the system), topology breakdown (node failures) or attacks. The recovery technique, employed by \emph{HyCube}, is a periodic procedure, run every specified time interval (parameter value). The value of the interval should be adjusted depending on the DHT nature. The higher is the level of churn, the recovery should be run more often, while if the system has a very stable nature, this interval might be larger. The recovery algorithm in \emph{HyCube} proceeds in two phases:

\begin{enumerate}

\item In the first phase, the node sends a recovery request to all nodes in its routing tables, which it turn return their routing tables to the requesting node. Upon receiving the responses, the requesting node processes the references returned and updates its routing tables and neighborhood set.

\item In the second recovery step, the node performing the recovery sends a notification (NOTIFY message) about its existence to all nodes in its neighborhood set and routing tables (immediately after sending the recovery requests). The notification is also sent to the routing tables nodes to spread the information about the node to more distant parts of the hypercube. However, to limit the overhead, the maximum number of the routing table nodes to which notification is sent should be limited - the message would be sent to certain maximum number of nodes (random selection from all routing table nodes).

\end{enumerate}

Because such an approach may lead to exchanging a large number of messages and also increase processing overhead at the node level, neighborhood set recovery - a variant of the recovery was introduced. Nodes performing the neighborhood set recovery send the recovery requests only to nodes in their neighborhood sets, which dramatically decreases the network traffic and processing overhead. In addition to the recovery interval, \emph{HyCube} allows defining the recovery plan - determining a sequence of recovery variants (for individual recovery runs, successive recovery types from the recovery plan are performed - following a cyclical pattern). Full recovery has better properties in terms of keeping the routing tables up-to-date. However, especially for larger networks, the overhead is significantly larger. Although there might be situations in which the neighborhood set is completely corrupt, and sending recovery requests to the routing table nodes would return much more nodes, usually it is sufficient to repeat the neighborhood set recovery instead. Therefore, the neighborhood set recovery is the default recovery method used in \emph{HyCube}.

To spread the information about the node in an even greater degree, it is possible to send a notification also to every node to which a reference is returned in recovery responses. However, this approach proved to generate enormous network traffic, as well as very high nodes' processing overhead. Performed simulations proved that notifying all neighborhood set nodes and 16 random routing table nodes during every recovery does not significantly increase the overhead, and, if the recovery procedure is run frequently enough, it is sufficient to properly propagate the information about the node's existence. Furthermore, depending on configuration, nodes may process the recovery request messages as notifications (analyze the sender as a routing table candidate), in which case, a notification message does not have to be later sent to the nodes to which the recovery requests were sent.

\section{Joining the system (connecting to the DHT)}

When a new node joins the existing DHT, it should have knowledge about any node already connected to the system. \emph{HyCube} implements two different approaches for joining the system. One of them is based on routing a JOIN message through the system to the node closest to the new node's ID, and the routing tables are formed by the references returned by intermediate nodes along the path (including also themselves), as well as the closest node. The second method is based on searching for the nodes closest to the new node's ID, which form initial routing tables for a new node. The routing approach requires much less overhead - a smaller number of messages are exchanged. However the search method is not vulnerable to any single node possibly causing the join message to be dropped. This section presents both join techniques.

\subsection{``Route-join'' procedure}

The route-join procedure is based on the joining mechanism presented in \cite{pastry}. To initiate the route-join procedure, the joining node should send a ``join'' request (JOIN message) to any node already participating in the system. The message is routed to the closest possible node to the joining node's ID (however, omitting the exact match, as the message would be possibly routed back to the joining node). All intermediate nodes send back (directly to the joining node) JOIN\_REPLY messages that contain all references stored by them in their routing tables. Every node returned is analyzed by the joining node and its routing tables are updated based on the criteria described in Sections \ref{sec:HyCubeNodeSelection} and \ref{sec:uniformNSDistribution}. As the message gets closer to the new node ID, references returned are more likely to be good candidates for the neighborhood set. Furthermore, as the common prefix length with the joining node ID increases, more and more routing table slots correspond to the same hypercubes as the one of the joining node, so more and more references returned by the intermediate nodes might be also used for building the routing tables. For that reason, it is a good idea to disable the prefix mismatch heuristic for JOIN messages (the behavior is configured by a system parameter).

\subsection{``Search-join'' procedure}

In the search-join procedure, the joining node initially retrieves all references from routing tables from a known node connected to the system. The references returned are used to perform a search (Section \ref{sec:search}) for the closest nodes to the joining node ID with certain values of parameters $\alpha$, $\beta$, $\gamma$: $\alpha_{join}$, $\beta_{join}$ and $\gamma_{join}$ (system parameters), and the $ITN$ (ignore target node) parameter set to \emph{true}. The value of $k$ is not important, because all nodes returned by intermediate nodes are processed (updating the routing tables). Initially, the set $\Gamma$ is filled with $\gamma$ closest (Euclidean) nodes retrieved in the initial phase - from the bootstrap node. Afterwards, the search procedure proceeds as described in Section \ref{sec:search}.

Because the node performing the search is the node whose identifier is being looked for, to allow the use of the Steinhaus transform, the initial values of Steinhaus points should not be set to the joining node ID (Euclidean metric would be then used for local next hop selections), but should be given the values of IDs of the initial nodes themselves (only for the initial nodes returned by the bootstrap nodes - for any nodes added to $\Gamma$ later, the Steinhaus point is updated based on the information contained in the response message that contained the node reference).

\section{Leaving the system (disconnecting from the DHT)}

The maintenance and recovery mechanisms should be able to maintain connection graph consistency and keep routing tables of nodes up-to-date, ensuring good routing and searching properties. However, these algorithms have certain ``inertia'' and react with a certain delay. A simple solution to overcome this problem is sending the leave information (LEAVE message) to all neighbors in the leaving node's neighborhood set. Such LEAVE messages should contain the list of neighborhood set nodes of the leaving node, and the nodes receiving it should remove references to the LEAVE sender from the routing tables and the neighborhood set, and process all the nodes included in the message to immediately fill the lost routing table or/and neighborhood references.

% ex: set tabstop=4 shiftwidth=4 softtabstop=4 noexpandtab fileformat=unix filetype=tex encoding=utf-8 fileencodings= fenc= spelllang=pl,en spell:


\section{Introduction}
\label{sec:background}

%In this chapter we provide background for the 802.11 wireless communications standard, including its 802.11af \ac{TVWS} extensions, with a focus on \ac{MU-MIMO} transmission techniques in 802.11ac/ax and key performance factors for general \ac{MU-MIMO}.
In this chapter we provide background for wireless communications systems with a focus on \ac{MU-MIMO} transmission techniques such as those used in \ac{IEEE} 802.11ac/ax and key performance factors for \ac{MU-MIMO}.

We will also review software-defined radios and their implementation in order to provide context for the innovative \ac{SDR} system design in this thesis.

	%#######################################################
	%\section{Wi-Fi: the IEEE 802.11 Standard}
	%\label{sec:back_80211}
%
%\iftoggle{isready} {
%
	%As the second widespread use of spectrum sharing,\footnote{The IEEE 802.11a standard includes \ac{DFS} sensing and protection of radar signals between [5.260, 5.700]~GHz.}
%
	%\rgnote{Basic background about 802.11, random access, frame structure, terminology. 802.11ac extension for MU-MIMO. This is where \ac{STA} and \ac{AP} get defined}
%
	%%#######################################################
	%\subsection{IEEE 802.11af Standard Extension for TVWS}
	%\label{sec:back_80211af}
%
	%\rgnote{Motivation and constraints of 802.11af, pulling from my IEEE article content. Database vs. sensing approaches--we focus on database-driven approaches.}
%
	%Wifi over TVWS: \cite{bahl2009white}
%
%}{ \rgnote{The WiFi background section is pending.}}

%#######################################################
\section{Multi-user Beamforming}
\label{sec_mubf_back}
	Multi-user beamforming is a multi-antenna transmission technique that allows a transmitter to spatially reuse a wireless channel by transmitting multiple concurrent data streams on the same radio frequency using linear pre-coding.
	This is achieved in two steps: first, the wireless channel is estimated and represented as a complex matrix $\mathbf{H}_c\in \mathbb{C}^{M\times K}$ representing the narrowband scalar channel weights for each path between $M$ transmit antennas and $K$ receive antennas for each of $C$ narrowband subcarriers within the wideband.
	Shown in Figure~\ref{fig_h_matrix}, this wireless channel representation is called the \ac{OFDM} wideband channel representation since it treats each subcarrier independently and is used for \ac{OFDM} transmission techniques \cite{perahia2008}.
	An underlying assumption for this channel model is that the bandwidth of the subcarriers is chosen such that a complex scalar coefficient, $h_{mkc}$, is sufficient to accurately represent the magnitude and phase of the channel state of subcarrier $c$.
	For the remainder of this thesis, we will forgo the subscript $c$ when discussing the wireless $\mathbf{H}$ matrix channel representation.

	\begin{figure}[h]
\centering
  \includegraphics[width=4.5in]{figs/general/beamforming_h_matrix}
    \caption{H-matrix representation of the physical wireless channel.}
\label{fig_h_matrix}
\end{figure}

	The second step for multi-user beamforming is the application of downlink beam-steering weights.
	Each data stream for each of $K$ receive antennas, $\mathbf{S}\in \mathbb{C}^{K\times 1}$, is left-multiplied by a matrix of complex steering weights $\mathbf{W}\in \mathbb{C}^{M\times K}$  resulting in $\mathbf{X} = \mathbf{W}\cdot \mathbf{S} \in\mathbb{C}^{1\times M}$ pre-coded signals containing a linear combination of each data stream to transmit from each of the \ac{AP}'s $M$ antennas.

	The $K$ antennas may each be on a single separate \ac{STA} or may be in any combination where a subset of $K$ is on a single receiver \ac{STA}.
	When all $K$ are on a single \ac{STA} this reduces to a simple single-user \ac{MIMO} transmission.
	The key distinction is that \acp{STA} are independent receivers in \ac{MU-MIMO} transmissions and do not share receive information between each other.

%#######################################################
\subsection{Pre-coding Weight Selection}
	The linear pre-coding weights $\textbf{W}$, also called ``beam-steering weights,'' are chosen such that the interference between the parallel streams is minimal.
	To compute these weights, the transmitter must first measure the channel state matrix $\textbf{H}$.
	The optimal method of constructing the steering matrix is \acf{DPC} \cite{costa1983dpc}; however, its computational complexity makes it impractical to implement in full.

	Instead, a practical method for calculating $\mathbf{W}$ from $\mathbf{H}$ that approaches optimal performance is \ac{ZFBF}\cite{goldsmith2006zf}.
 Zero-forcing drives interference between spatial streams to zero, and can be inefficient when users' \ac{CSI} is not sufficiently orthogonal \cite{aryafar2010design} or in low \ac{SNR} environments.
 \ac{ZFBF} requires calculation of the $\mathbf{H}$ matrix's Moore-Penrose pseudo-inverse:
\begin{equation}
\mathbf{W} = \mathbf{H}^\dagger =  (\mathbf{H}^\textsc{H} \mathbf{H})^{-1} \mathbf{H}^\textsc{H}, \label{eq:zf}
\end{equation}
where $(\cdot)^\textsc{H}$ represents the matrix conjugate transpose and $(\cdot)^\dagger$ is the Moore-Penrose pseudo-inverse.
%When the transmitter precodes with perfect \ac{ZFBF} weights, $\mathbf{W}$, signals ideally cancel the effects of the wireless channel at the receiver, allowing each user to receive their own, independent streams.

	A key element of \ac{ZFBF} is the zero-interference condition which is a direct result of the pseudo-inverse.
	Because $\textbf{W}=\textbf{H}^\dagger$, then $\textbf{H}\cdot\textbf{W}$ is the identity matrix, meaning that the interference from the data stream from user $k=i$ on the data stream for user $k=j$ is nulled and vice versa.
	\ac{ZFBF} pre-codes the transmitted data streams such that the combined wireless channel between the transmitter and the receivers is separated.
	If \ac{ZFBF} works perfectly, we can express the pre-coded transmission $\mathbf{X}$ as:
\begin{align}
\mathbf{X} = \mathbf{W} \cdot \mathbf{S} \xrightarrow[\mathbf{H}]{\text{Transmit}} & \mathbf{H}\cdot (\mathbf{W} \cdot \mathbf{S}) \notag \\
 = & \cancel{\mathbf{H}} \cdot (\cancel{\mathbf{W}} \cdot \mathbf{S}) = \mathbf{S}
\end{align}

	There are additional ways to calculate the pre-coding weights for digital beamforming systems; two of the most notable alternatives are conjugate beamforming \cite{yang2013performance} and \ac{MMSE} beamforming \cite{joham2005linear}, both of which are optimal under certain conditions.
	In our work, due to the computational complexity of \ac{MMSE} beamforming implementation, we focus on the zero-forcing beamforming technique for \ac{MU-MIMO}, though most of the insights and systems could be easily applied to alternative types of beamforming.

%#######################################################
\subsection{MIMO Channel Correlation and Temporal Correlation Function}
%\rgnote{Beamforming Performance Limits Clean this notation and discusion up}
	The key to the success of the precoding operation is that $\textbf{H}\cdot \textbf{W}$ is designed to reduce to the identity matrix so the transmitted streams are received separately at each receiver.
	We are generally interested in two characteristics of $\textbf{H}$ that can degrade the performance of this precoding operation: an ill-conditioned $\textbf{H}$ \cite{zhong2011distribution} or an out-dated $\textbf{H}$ \cite{kaltenberger2008correlation}, each related to different statistical correlation concepts.

\textbf{MIMO Channel Correlation.}
	An ill-conditioned $\textbf{H}$ matrix renders matrix inversion inaccurate \cite{greenbaum2012numerical, peel2005vector} which is required to calculate the pseudo-inverse in Equation~\ref{eq:zf}.
	This results in $\textbf{H}\cdot \textbf{W}$ becoming far less likely to equal $\textbf{I}$ and causes ``inter-stream interference'' as a result of beamformed data streams interfering and degrading the received signal strength of a data stream to its intended receiver \cite{kaltenberger2008correlation}.
	Ill-conditioned $\textbf{H}$ matrices can be a result of \ac{MIMO} channel correlation, a characteristic of the physical arrangement of the \ac{MIMO} antenna array or the wireless environment that measures how similar the independent paths between \ac{MIMO} antennas are to each other by relating their individual path random processes \cite{loyka2001channel}.
	With sufficiently high \ac{MIMO} channel correlation, any particular realization of the matrix channel random process is likely to not be full-rank, which makes it non-invertible or singular \cite{peel2005vector}.
	Increasing channel correlation in general results in a decrease in \ac{SNR} for a \ac{MIMO} system \cite{loyka2001channel}, and can be interpreted as a lack of physically independent information channels in the environment or radio array available to convey information.

\textbf{Channel Correlation Function.}
	Channel correlation is a property of the wireless \ac{MIMO} channel matrix $\textbf{H}$ and is not to be confused with the channel \emph{temporal correlation} function, which is a property of the individual channel random process $h_{mk}$ and indicates how correlated the channel condition is as a function of time.
	Temporal correlation is the expected autocorrelation between channel snapshots at varying intervals of time calculated as described in \cite{wallace2003experimental}.
	An intrinsic property of the channel random process, an empirical temporal correlation function can be estimated from time series of channel state observations.
	The empirical correlation coefficient for a single channel $\rho$ at time interval $\ell$ is defined as:
\begin{align}
\rho_\ell = \frac{\mathbb{E}\big[h_{m}[k]h^{*}_{mn}[k+\ell]\big]}{\mathbb{E}\big[h_{mn}[k]h^{*}_{mn}[k]\big]}
\label{eq_corr_coeff}
\end{align}
where the expectation is calculated for all combinations of transmit antenna $m$, receive antenna $n$ and starting time sample $k$.
 %Assuming each channel path is generated by the same random process, the resulting empirical correlation functions for each \ac{MIMO} channel path can be averaged together for an overall estimate of channel temporal performance.

	Out-dated $\textbf{H}$ matrices are a direct result of the latency between the measurement of the $\textbf{H}$ matrix and the transmission of the $\textbf{W}$ precoded data streams.
	Increased time between the measurement of $\textbf{H}$ and the transmission of $\textbf{W}\cdot \textbf{S}$, results in a higher probability of incorrect transmit precoding.
	Essentially, the transmitter measures $\textbf{H}_{t}$ and then calculates $\textbf{W}_t = \textbf{H}_t^\dagger$.
However, the subsequent precoded transmission is $\textbf{H}_{t+\Delta}\cdot \textbf{W}_{t}$, which may not equal $\textbf{I}$ since the channel has changed since estimation at time $t$.
	The temporal correlation function is a way of estimating how much the channel will change over time.
	Whether or not $\textbf{H}_{t}=\textbf{H}_{t+\Delta}$ is based on environmental variability and user mobility; and, like channel conditioning, is also an environment and frequency dependent characteristic.

	While a large number of studies have characterized the indoor and outdoor propagation environment for the purpose of network planning and algorithm design, few are applicable to evaluating \ac{MU-MIMO} performance and most have focused on a single frequency band \cite{boyer2007mimo, hammons2008cooperative, jung2011multipath}.
	This makes measurement studies of different frequencies and radio technologies difficult to compare.

At the same time, the implementation complexity and computation required for real-time implementation of multi-carrier \ac{MU-MIMO} has traditionally been prohibitive for software-defined radio platforms \cite{aryafar2010design}, thus providing a challenge to empirical measurement of \ac{MU-MIMO} performance.

%\subsection{Systemic CSI Estimation Error}
%\label{sec_systemic_csi_error}
%
%\rgnote{review this section--I wanted to include it because I constantly have to explain this and it's not immediately clear until you actually prove it with math. This might get cut.}
%
%Recall that given a channel estimate, $\Hb$, the precoding weights for the zero-forcing transmitter are $\mathbf{W} = \mathbf{H}^\dagger =  (\mathbf{H}^\textsc{H} \mathbf{H})^{-1} \mathbf{H}^\textsc{H}$.
	%Arbitrary magnitude and phase offsets may be present in any one estimate of the \ac{CSI} due to receive gain control or timing estimation jitter, respectively.
	%These are normal effects in \ac{OFDM} transceivers that are compensated by the receive \ac{OFDM} channel equalization, yet become part of the channel estimation at the receiver \cite{park2003timing, breit2009coherencetime}.
	%While not a new finding, it is useful to discuss the effect that these types of systemic \ac{CSI} estimation error components and how they differ from estimation error due to stale \ac{CSI}.
	%
	%We define a systemic \ac{CSI} estimation error as any error matrix $\Phi \in \mathbb{C}^{M\times K}$ that can be represented as a diagonal matrix: $\Phi = diag(\alpha_1,\ldots,\alpha_M)$.
%
	%It is a well-known property of beamforming that the input \ac{CSI}, $\Hb$, can be perturbed by an arbitrary diagonal matrix $\Phi = diag(\alpha_1,\ldots,\alpha_M)$ such that the \ac{CSI} used for precoding is $\hat{\Hb} = \Hb\Phi$.
	%The resulting original beamformed streams, $\mathbf{Y} = \mathbf{W}\Hb$, and the received perturbed beamformed streams, $\mathbf{Y'} = \hat{\Hb}^\dagger\Hb$, still preserve the orthogonal separation of the user streams owing to the diagonality of $\Phi$.
	%In other words, $\mathbf{Y'} = (\Phi)^{-1}\mathbf{Y}$, and beamforming performance is not significantly impacted by such measurement artifacts.
%
	%This is a powerful result \rgnote{finish this section when I'm less tired}

%#######################################################
%\section{Many-Antenna and Massive MIMO}
%\label{sec_mami_back}
%
%\iftoggle{isready} {
%
%
%}{ \rgnote{The Massive MIMO background section is pending.}}

%#######################################################
\section{Software Defined Radio Architecture}
\label{sec_sdr_back}

A \acf{SDR} is a radio that replaces analog signal processing components with programmable digital logic.
This radio architecture increases flexibility by replacing analog signal processing components with digital logic, enabling the use of more flexible and complex waveforms such as \ac{OFDM} and simplifying cross-layer system design.

\begin{figure}[ht] % Idealized AGC block diagram
\centering
\includegraphics[width=1\linewidth]{./figs/agc/generic_sdr}
\caption{Block diagram of a generalized Software-Defined Radio system.}
\label{fig_sdr_ideal}
\end{figure}

We present a generalized block diagram model of a single-radio \ac{SDR} in Figure~\ref{fig_sdr_ideal}.
While implementations of \acp{SDR} vary, our model captures key components (from left to right) considered in this work and generalizes well to most commercial \ac{SDR} architectures:
\begin{itemize}
	\item \textbf{Network stack} - signal processing components with non time-critical functions; for example: user scheduling, priority queuing, or layer-2 routing.
	\item \textbf{Programmable logic} - signal processing components with time-critical or hardware-accelerated functions, specifically for networks layers 1 and 2; for example: gain control, digital pre-distortion, forward error correction.
	\item \textbf{Analog/Digital interface} - \acp{ADC} and \acp{DAC} provide the interface between digital and analog domains by converting digital samples to analog voltages and vice-versa.
	\item \textbf{Analog Gain Block (1)} - controls the amplitude of the signal entering or leaving the ADC/DAC.
	\item \textbf{Anti-aliasing filter (2)} - provides a low-pass filtering function on the analog signal to limit its information bandwidth to below the Nyquist sampling rate of the digital conversion step in order to avoid aliasing.
	\item \textbf{Analog Gain Block (3)} - controls the amplitude of the signal entering or leaving the analog mixer.
	\item \textbf{Frequency Up/Downconversion Mixing (4)} - a local center frequency, or \textit{carrier}, is multiplied by the analog signal to either up-convert or down-convert the signal to/from the RF frequency.
	\item \textbf{RF Gain Block (5)} - the input and output RF signal is amplified in order to meet the linearity or power budget requirements of the system's components.
	\item \textbf{Receive Band-Pass Filter (6)} - a band-pass filter limits the frequency content of the signal input to the receive radio chain in order to limit noise ingress and harmonic interference from strong out-of-band signals.
	\item \textbf{Transmit/Receive Switch (7)} - shares a signal antenna with the transmit and receive circuits; while this work generally considers \ac{TDD} protocols, this system component can be replaced with a frequency duplexer to support \ac{FDD} protocols, as well.
\end{itemize}

	This is by no means an exhaustive enumeration of the signal conditioning components present in all \ac{SDR} systems, but captures the important features needed for the techniques and systems in this work while being generic enough to apply across a wide range of commercial \ac{SDR} platforms.

	In addition, the division of functions between ``programmable logic'' and ``network stack'' blocks is a soft delineation made for ease of discussion.
	For the purpose of this work, we consider programmable logic to be an \ac{FPGA} or similar digital logic, whereas the network stack is implemented in embedded C or higher layer computer languages on a general-purpose processor.
	This division matches well-known experimental \ac{SDR} platforms like those mentioned in Section~\ref{sec_related_sdr}.

\subsection{Direct Sampling RF}
	As digital circuit integration and speed increases, a new \ac{SDR} paradigm is emerging in recent years that further reduces the amount of analog components necessary, shifting the dividing line between digital and analog domains in Figure~\ref{fig_sdr_ideal} nearly all the way to the antenna.
	``Direct sampling RF'' systems first used a combination of filtering and aliased sampling to directly sample RF signals without a down-conversion mixing step \cite{psiaki2005design}.
	More recently, increases in the integration and speed of digital circuits now permit direct sampling \acp{ADC} at RF frequencies without the drawback of aliased sampling \cite{xilinx2017rfsoc}.
	Although widespread use of direct sampling architectures will obviate the need for some of the analog components discussed in this thesis, key contributions of \ac{AGC}, environmental \ac{MU-MIMO} studies, and our protocol design remain unchanged.

%#######################################################
\section{Two Port Scattering Parameters}
\label{sec_scattering}

	In order to present some of our circuit design techniques, it will be necessary to be familiar with the definition and notation of normalized scattering, or S-parameters.
	The following discussion is a summary of the discussion and derivation by Yarman \cite{yarman2010design} and Orfanidis \cite{orfanidis2002electromagnetic} to describe a lossless two-port device.

\begin{figure}[h]
\centering
  \includegraphics[width=0.4\linewidth]{figs/matching/general_2_port}
    \caption{General diagram of a lossless linear 2-port device with scattering parameters.}
\label{fig_general_2_port}
\end{figure}

	Consider Figure~\ref{fig_general_2_port}, a linear two-port microwave device that is excited by a complex source or ``generator'' impedance $Z_G$ and terminated in a complex load impedance $Z_L$.
	Assuming it it lossless, it is possible to describe the two-port system by describing the relationship of any incident and reflected voltage wave characterized by the wave variables $a_i, b_i$, where the current is defined in the direction of the corresponding $a_i$ vector and the characteristic reference impedance of the system is $Z_0$.
\begin{align}
a_1 &= \frac{V_1+Z_0 I_1}{2\sqrt{Z_0}}  &a_2 = \frac{V_2+Z_0 I_2}{2\sqrt{Z_0}} \\
b_1 &= \frac{V_1-Z_0 I_1}{2\sqrt{Z_0}}  &b_2 = \frac{V_2-Z_0 I_2}{2\sqrt{Z_0}}
\end{align}
This results in a relationship between incident and reflected traveling waves:
\begin{align}
	b_1 &= S_{11}a_1 + S_{12}a_2 \\
	b_2 &= S_{21}a_1 + S_{22}a_2.
\end{align}
	The system of traveling wave equations forms a scattering matrix, $\mathbf{S}\in\mathbb{C}^{2\times2}$ for a two-port device, $\mathbf{S}\in\mathbb{C}^{4\times4}$ for a four-port device, etc, that completely characterizes the device's linear, lossless behavior.

	There are two important assumptions related to the use of S-parameters; the first is that each set of empirical S-parameter is measured within a system with arbitrary characteristic impedance $Z_0$.
	As a matter of convention, nearly all S-parameters are measured in reference to 50~$\Omega$ and we will assume that in the rest of this thesis.

	Second, S-parameters only capture \emph{linear} steady-state device behavior, and for this reason they are generally described as ``small-signal'' parameters. For example, as the input signal power to a two-port amplifier is increased close to the IP1dB point, or the point where the amplifier becomes saturated and the output power decreases by 1~dB from ideal, non-linear behavior will be observed, requiring ``large-signal'' design techniques like load-pull analysis.
	Quaglia and Cripps present an excellent discussion of recent developments and models for power amplifiers operating in the saturated regime \cite{quaglia2017reappraisal}.
	We instead focus on linear operating conditions and small-signal designs for our system in order to preserve the simplicity of each radio chain within a complex many-antenna system, leaving energy efficiency optimization to future work.

For the system in Figure~\ref{fig_general_2_port}, it is also useful to define generator and load \emph{reflection coefficients} that describe the portion of a reflected incident wave with characteristic reference impedance $Z_0$ into a load.
\begin{equation}
\Gamma_G = \frac{Z_G - Z_0}{Z_G + Z_0},~~~ \Gamma_L = \frac{Z_L - Z_0}{Z_L + Z_0}
\end{equation}

	In this work, we will use the $\hat{(\cdot)}$ notation to indicate a reflection coefficient that is cascaded into a non-reference impedance.
	For example, $\hat{S}_{11}$ in Figure~\ref{fig_general_2_port} is the reflection coefficient of the active two-port terminated in an arbitrary load impedance $Z_L$ and can be written:
\begin{equation} \label{eq_}
\hat{S}_{11}=S_{11}+\frac{S_{12}S_{21}\Gamma_L}{1-S_{22}\Gamma_L},
\end{equation}
where in the case of $Z_L = Z_0 = 50~\Omega$, we see that this reduces to $\hat{S}_{11} = S_{11}$.
	Similarly, we can see that:
\begin{equation}
\hat{S}_{22}=S_{22}+\frac{S_{21}S_{12}\Gamma_G}{1-S_{11}\Gamma_G}.
\end{equation}

	We now have the tools needed to address the broadband power transfer network design problems presented in Section~\ref{sec_wurc_pa_design}.


\documentclass[]{article}
\usepackage{graphicx}
\usepackage{caption}
\graphicspath{ {./images/} }

% Minted
\usepackage[cache=false]{minted}
	\usemintedstyle{vs}
	\usepackage{xcolor}
		\definecolor{light-gray}{gray}{0.97}

\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{comment}

\title{Data Mining: Exercise 4-1}
\author{Brandon Hosley}
\date{\today}

\begin{document}
\maketitle

\section*{Assignment 1: First Method Classification}

\subsection*{Preparing Data}
\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
data = spark.read.format("libsvm").load("/user/data/CSC533DM/sample_libsvm_data.txt")
from pyspark.ml.feature import StringIndexer
labelIndexer = StringIndexer(inputCol="label",outputCol="indexedLabel").fit(data)
labelIndexer.transform(data).show(5)

from pyspark.ml.feature import VectorIndexer
featureIndexer = VectorIndexer(inputCol="features", outputCol="indexedFeatures",maxCategories=4).fit(data)
featureIndexer.transform(data).show(5)

(trainingData, testData) = data.randomSplit([0.7, 0.3])

trainingData.show(5)
testData.show(5)
\end{minted}
\includegraphics[width=\linewidth]{image1.1}
\includegraphics[width=\linewidth]{image1.2}
\includegraphics[width=\linewidth]{image1.3}
\includegraphics[width=\linewidth]{image1.4}

\subsection*{Training/Testing}
\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
from pyspark.ml.classification import DecisionTreeClassifier
dt = DecisionTreeClassifier(labelCol="indexedLabel",featuresCol="indexedFeatures")
from pyspark.ml import Pipeline
pipeline = Pipeline(stages=[labelIndexer, featureIndexer, dt])
model = pipeline.fit(trainingData)
predictions = model.transform(testData)
#predictions.show(5)
predictions.select("prediction", "indexedLabel", "features").show(5)
\end{minted}
\includegraphics[width=\linewidth]{image2}

\subsection*{Evaluating Your Model}
\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
evaluator = MulticlassClassificationEvaluator(labelCol="indexedLabel",predictionCol="prediction", metricName="accuracy")
accuracy = evaluator.evaluate(predictions)
evaluator.setMetricName("weightedPrecision")
precision = evaluator.evaluate(predictions)
evaluator.setMetricName("weightedRecall")
Recall = evaluator.evaluate(predictions)
evaluator.setMetricName("f1")
f1Measure = evaluator.evaluate(predictions)

print("Accuracy: %g " % accuracy)
print("Precision: %g " % precision)
print("Recall: %g " % recall)
print("F1: %g " % f1Measure)
\end{minted}
\includegraphics[width=\linewidth]{image3}

\subsection*{Explain the Above Metric}
\emph{What they are. Explain based on your results/outputs, e.g. how they are calculated
Why some of their names are different and explain the difference, e.g. precision vs. weighted precision} \vspace{2em}

The definitions for the methods are provided by Spark documentation:
\begin{align*}
	ACC &= \frac{TP}{TP+FP} = \frac{1}{N}\sum_{i=0}^{N-1} \hat{\delta}(\hat{y}_i - y_i) \\
	PPV_w &= \frac{1}{N} \sum_{\ell \in L} \frac{
		\sum_{i=0}^{N-1} \hat{\delta} (\hat{y_i}-\ell) \cdot \hat{\delta} (y_i-\ell)
	}{\sum_{i=0}^{N-1} \hat{\delta} (\hat{y_i}-\ell)}
	\cdot \sum_{i=0}^{N-1} \hat{\delta} (y_i -\ell) \\
	TPR_w &= \frac{1}{N} \sum_{\ell \in L} \sum_{i=0}^{N-1} \hat{\delta} (\hat{y_i}-\ell) \cdot \hat{\delta} (y_i-\ell)\\
	F_w(\beta) &=
		\frac{1}{N}
		\sum_{\ell \in L} (1+\beta^2) \cdot
		(\frac{PPV(\ell) \cdot TPR(\ell)}{\beta^2 \cdot PPV(\ell)+TPR(\ell)}) \cdot
		\sum_{i=0}^{N-1} \hat{\delta} (y_i -\ell) \\
\end{align*}

In Other words:\\
Accuracy is the ratio of true positives to true and false positives reported. \\
Precision is the same. \\
Recall is the ratio of true positives to all known positives. \\
When weighted these measurements are reported proportionately to the frequency of their corresponding labels. \\
The F-score is the harmonic mean of precision and recall. Which is the number of labels multiplied by the precisions and recalls of the label and divided by the sum of the precision and recalls. \\

\section*{Assignment 2: Second Method Classification}

\subsection*{Evaluate using Dataframe and RDD-Method}
\emph{
To use RDD-based metrics, convert predictions (DataFrame) into predictionAndLabel (RDD), see References [7]}

\emph{
Use below metrics, see References [6] \\
	Accuracy \\
	Precision, Recall, F-1 measure (by label) \\
	Precision, Recall, F-1 measure (weighted)} \\

\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
from pyspark.mllib.evaluation import MulticlassMetrics
predictionAndLabels = predictions.select("prediction", "indexedLabel").rdd
metrics = MulticlassMetrics(predictionAndLabels)

# Statistics by class
#labels = predictionAndLabels.map(lambda lp: lp.label).distinct().collect()
labels = [0.0,1.0]
for label in sorted(labels):
print("Class %s precision = %s" % (label, metrics.precision(label)))
print("Class %s recall = %s" % (label, metrics.recall(label)))
print("Class %s F1 Measure = %s" % (label, metrics.fMeasure(label, beta=1.0)))

# Weighted stats
print("Weighted recall = %s" % metrics.weightedRecall)
print("Weighted precision = %s" % metrics.weightedPrecision)
print("Weighted F(1) Score = %s" % metrics.weightedFMeasure())
print("Weighted F(0.5) Score = %s" % metrics.weightedFMeasure(beta=0.5))
print("Weighted false positive rate = %s" % metrics.weightedFalsePositiveRate)
\end{minted}
\includegraphics[width=\linewidth]{image4.1}
\includegraphics[width=\linewidth]{image4.2}

\subsection*{Explain the differences of label and weighted based on your results.}

As above, the labeled measurements are based on their corresponding label. The weighted results are the values in proportion to the frequency of their true label in the dataset.

\textbf{Note:} The fact that all three measures are reported as identical is uncommon but indicates that false positives and false negatives are equal. Checking the confusion matrix verifies that this is the case and may not indicate an error in the script.

\includegraphics[width=\linewidth]{image6}

\end{document}

\begin{minted}[breaklines,bgcolor=light-gray,fontsize=\footnotesize]{shell-session}
\end{minted}
\includegraphics[width=\linewidth]{image1}


\section{Methodology} \label{method}
% what is TIMES
\subsection{TIMES Model Description}
\gls{TIMES} models dynamic energy systems and simulates transition scenarios as a mixed-integer linear optimisation problem that is subject to a primary objective function and additional constraints \cite{loulou_etsap-tiam_2008}. The generation, refinement, supply, storage, and trade of energy commodities are modelled across multiple sectors and multiple regions using a wide variety of in-built commodity and process types. Emissions can be associated with energy commodities or processes as an emission coefficient per unit commodity produced or consumed.

%basic features of model
We outline salient features of our model in this section, while the data used for our simulations are in \ref{Appendix}. The relevant input files can be accessed online \cite{chaube_arfci2cner_2021}. The objective function in our single-region model is the overall cost of the transition. The major constraints in our simulations are the demand for electricity (Table \ref{demand}), emission constraints on the electricity-generation sector based on Japan's \gls{INDC} (Table \ref{co2-limits}), and feasible nameplate capacity deployment limits (Table \ref{caplim}). Miscellaneous assumptions are summarised in Table \ref{misc-assump}. In summary, our model minimises the transition cost while meeting the increasing electricity demand and achieving the required emission cuts using a combination of generation and storage technologies.

\begin{table}[H]
\centering
	\caption{Electricity demand increase over the simulation time frame.}
	\vspace{0.1in}
	\begin{tabularx}{0.4\textwidth}{p{0.15\textwidth} p{0.25\textwidth}}
		\hline
\textbf{Year} & \textbf{Annual demand} \\
 & \textbf{increase} \\
\hline
2017-2030 & 1.7 \% \cite{noauthor_electricity_2019} \\
2031-2050 & 1.0 \% \\
2051-2070 & 0.5 \% \\
2070-2100 & 0.0 \% \\
\hline
	\end{tabularx}
\label{demand}
\end{table}

\begin{table}[H]
\centering
	\caption{Emission constraints.}
	\vspace{0.1in}
	\begin{tabularx}{0.6\textwidth}{p{0.05\textwidth} p{0.2\textwidth}p{0.05\textwidth} p{0.3\textwidth}}
		\hline
\textbf{Year} & \textbf{Emission limit} & \textbf{Base} & \textbf{Reduction} \\
 & & \textbf{year} & \textbf{from base year} \\
\hline
2030 & 438 Mt CO$_2$-eq. & 2013 & 26 \% \\
2050 & 75 Mt CO$_2$-eq. & 1990 & 80 \% \\
2100 & 75 Mt CO$_2$-eq. & 2050 & 0 \% \\
\hline
	\end{tabularx}
\label{co2-limits}
\end{table}

While Japanese electricity demand is expected to grow in the near future  \cite{noauthor_electricity_2019}, long-term electricity demand in Japan is expected to plateau, or even decrease, due to Japan's ageing population. However, precisely quantifying this rate of decrease is challenging, as this reduction in population will likely be accompanied by increased electrification of transportation and industrial sectors. Hence, post-2030, we have assumed a demand curve based on increased electrification driving increasing demand, which eventually plateaus due to the aforementioned expected demographic changes. The model captures the initial condition of the post-Fukushima Japanese electricity supply system using The Energy Data and Modelling Centre's data from 2013-2016 \cite{the_institute_of_energy_economics_japan_energy_2018}. Long term impacts of factors such as the retirement of the existing nuclear reactor fleet and the deployment of emerging technology is assessed by simulating the system until 2100. We account for the carbon cost of each technology using an emission coefficient that incorporates both direct emissions and life cycle emissions averaged over the entire operating lifetime for each technology. Using \gls{TIMES} day-night and seasonal time periods \cite{loulou_etsap-tiam_2008}, the daily and seasonal variability of renewables is incorporated. The availability of renewables varies during these time periods based on the annually averaged capacity factors of renewables in Japan \cite{the_institute_of_energy_economics_japan_energy_2018, irena_renewable_2020}.

%scenario description
To explore possible pathways to curbing \gls{GHG} emissions, we simulated five transition scenarios of varying likelihoods, with different sets of technologies enabled for deployment, as described in Table \ref{scen-table}. The first set includes conventional technologies such as  \gls{USC}, \gls{lng}, solar photovoltaic, wind energy (with onshore, offshore-fixed, and offshore-floating considered separately), and utility-scale lithium-ion battery storage. New deployments of oil-fuelled power plants are disabled due to the declining use of oil for electricity generation in accordance with Japan's goal of energy security and independence, as per the Basic Energy Plan. The second set of technologies considered includes emerging carbon-neutral technologies that are already commercialised or close to commercialisation, namely emerging solar photovoltaic (modelled as a composite of perovskites and CdTe solar cells), \gls{CCS}, and utility-scale hydrogen power. For hydrogen power, steam reforming, steam reforming with \gls{CCS}, \glspl{AEC}, \glspl{PEMEC}, \glspl{PEMFC}, and \glspl{SOFC} were incoporated based on their technological potential. Along with these two technology groups, we also explore the potential impact of nuclear energy. Nuclear power has significant advantages over renewables including long operational lifetimes, extremely low life-cycle emissions, and high capacity factors. However, nuclear power faces extremely low public acceptance in Japan after the Fukushima-Daiichi accident, therefore its future in Japan is highly uncertain. Hence, transition scenarios with and without new nuclear reactor deployment must be juxtaposed to assess the importance of the role of nuclear in emission reduction. Finally, the long-term impact of nascent hydrogen technologies on the hydrogen economy is assessed in an additional scenario. In this scenario, the potential commercialisation of \gls{SOEC} and \gls{PWS} post-2050 is explored in the absence of new nuclear power.

\begin{table}[H]
\centering
	\caption{Electricity supply transition scenario definition based on enabled technologies.}
	\vspace{0.1in}
	\begin{tabularx}{0.65\textwidth}{p{0.1\textwidth} p{0.18\textwidth} p{0.15\textwidth} p{0.18\textwidth}}
\hline
\textbf{Scenario}& \textbf{Emerging} & \textbf{New} & \textbf{Nascent}\\
 & \textbf{tech.} & \textbf{nuclear} & \textbf{tech.}\\

%                 & \textbf{enabled} & \textbf{enabled} & \textbf{enabled}\\
                  \hline
%1               &   \xmark       &      \xmark     &   \xmark     \\
%2               &   \xmark       &      \greencheck     &   \xmark     \\
%3               &   \greencheck       &      \xmark     &   \xmark     \\
%4               &   \greencheck       &      \greencheck     &   \xmark     \\
%5               &   \greencheck       &      \xmark     &   \greencheck     \\
1               &  No       &         No     &     No  \\
2               &   No       &      Yes     &     No  \\
3               &   Yes     &         No      &     No   \\
4               &   Yes     &      Yes     &     No  \\
5               &   Yes     &      No     &     Yes  \\
\hline
	\end{tabularx}
\label{scen-table}
\end{table}

%misc assumptions
Exogenous variables such as economic data, emission coefficients, nameplate capacity limits, and growth rates are detailed in Tables \ref{eco}, \ref{caplim}, and \ref{growrate} respectively. Prices and projections for fossil fuels and nuclear fuel are incorporated \cite{wittenstein_projected_2015, world_bank_commodity_2016, international_energy_agency_world_2019}. Learning curves for costs and life-cycle emissions are compiled from existing data (Table \ref{eco}) based on expected scaling of manufacturing, availability of manufacturing materials, and the use of clean energy for manufacturing energy system components. These learning curves are modelled as piecewise linear functions interpolated between the available data points, with the curve plateauing at the latest value for a given parameter, as detailed in Table \ref{eco}. Capacity limits of renewables and \gls{PWS} are based on their land-use requirements. The maximum annual capacity growth rates for existing technologies are held constant. The growth rate of nuclear power is based on historic trends and current pressure vessel manufacturing limitations \cite{iaea_pris_nodate}. The reactor size assumed in this study is 1165 MWe, based on Watts-Bar Unit 2 \cite{iaea_pris_nodate-1}. Due to a projected increase in the share of renewables, nuclear power plants must be able to load-follow to a certain extent, which is approximated in our model based on French reactors' range of capacity factors. The growth rates of all emerging technologies are modelled on the rates observed for solar photovoltaic technology, with rapid initial growth followed by gradual reduction, eventually reaching a moderate maximum attainable growth rate. One notable exception is the maximum growth rate of emerging solar technologies, which we have assumed to be the same as that of existing solar photovoltaic technologies. We assume that these technologies, some of which are already commercialised or close to commercialisation, will benefit immensely from the already streamlined solar photovoltaic manufacturing and supply chain. Therefore, they could be deployed as rapidly as conventional solar photovoltaic cells.

All hydrogen storage devices are operated with a maximum availability factor of 90\%, making them extremely flexible for load-following. Long-term storage of hydrogen is also available using hydrogen tanks with appropriate loss factors \cite{iea_technology_2015}. For hydrogen electrolysers and fuel cells, life-cycle emissions from just the stack are considered, as balance-of-plant emissions from utility scale hydrogen depend strongly on the type of plant and the source of energy used for electrolysis. Our assumptions about the reduction in the investment costs and life-cycle emissions of batteries are conservative due to the rising cost of cobalt and nickel, and lithium-ion battery manufacturing being concentrated in high \gls{GHG}-emitting nations, respectively \cite{oliveira_environmental_2015,emilsson_lithium-ion_2019,turcheniuk_ten_2018,simon_potential_2015}.

\subsection{Sensitivity analysis}
%approach, goal, what are we hoping to learn
%variables, sampling approach, median, range, which variables, which base case scenario, why
While the aforementioned scenarios identify potential pathways that are likely to result in deep emission cuts, many parameters, such as the investment cost, life cycle emissions, and lifetimes, are highly uncertain for novel technologies like \gls{CCS} and hydrogen generation and conversion technologies. Therefore, our sensitivity analysis is focused on investigating the impact of such parameters (Table \ref{sa-vars}). We analyse the sensitivity of the share of each of these technologies in the electricity-generation mix and of the system transition cost with respect to these variables.

\begin{table}[H]
\centering
	\caption{Candidate parameters and their variation in our sensitivity analysis.}
	\vspace{0.1in}
	\begin{tabularx}{0.9\textwidth}{p{0.3\textwidth} p{0.15\textwidth} p{0.15\textwidth}p{0.3\textwidth}}
\hline
\textbf{Technology}  & \textbf{Sampled} & \textbf{Distribution}& \textbf{Distribution}\\
\textbf{Parameter} & \textbf{Distribution} & \textbf{Mean}& \textbf{Range}\\
\hline
\gls{PWS} Investment Cost       & Gaussian    & 3088 \$/kW & $\pm$20\%. \\
\gls{SOEC} Investment Cost      & Gaussian    & 1388 \$/kW & $\pm$20\%.\\
\gls{PEMEC} Investment Cost     & Gaussian    & 3800 \$/kW & $\pm$20\%.\\
\gls{SOFC} Investment Cost      & Gaussian    & 7399 \$/kW & $\pm$20\%.\\
\gls{PEMFC} Investment Cost     & Gaussian    & 7399 \$/kW & $\pm$20\%.\\
\gls{CCS} Gas Investment Cost   & Gaussian    & 2626 \$/kW & $\pm$20\%.\\
\gls{CCS} Coal Investment Cost  & Gaussian    & 5252 \$/kW & $\pm$20\%.\\
\gls{PWS} Emission Coefficient  & Triangular  & 1.08 g/kWh & 0.2-5.405 g/kWh. \\
\gls{SOEC} Emission Coefficient & Triangular  & 1.08 g/kWh & 0.2-5.405 g/kWh. \\
\gls{PWS} Efficiency            & Triangular  & 0.525 & 0.5-0.58. \\
\hline
	\end{tabularx}
\label{sa-vars}
\end{table}

While there is significant uncertainty in the investment cost of nuclear power plants \cite{lovering_historical_2016}, it varies for individual plants and not for the technology as a whole. Preliminary simulations also demonstrated that nuclear power dominates the energy mix, and its share is fairly insensitive to perturbations over known ranges of investment costs \cite{lovering_historical_2016} due to its low life cycle emissions. Consequently, we eliminated nuclear power's investment cost as a candidate for sensitivity analysis. Furthermore, Japan has been exploring low-emission alternatives to nuclear since the Fukushima Daiichi disaster. In order to assess these potential alternatives which would otherwise be eliminated from the energy mix if new nuclear reactors were deployed, we chose Scenario 5 (Table \ref{scen-table}) as our base scenario for sensitivity analysis. Ten model parameters were sampled 30 times from appropriate distributions (Table \ref{sa-vars}). The parameter value at the first year of deployment (Table \ref{eco}) was varied, but the parameter's learning curve was held constant throughout all scenarios. For example, a 20\% change in the deployment year (2030) investment cost of \gls{SOFC}s with respect to the base case scenario reduces their investment cost in 2050 by 20\% as well. This makes learning-based cost-reductions proportionate across all sensitivity analysis runs. These parameters were randomly co-varied in 30 simulations. The share of each electricity generation technology (as the ratio of cumulative technology output to the cumulative electricity demand) and output of hydrogen technologies was plotted versus each varying parameter to correlate the effect of these parameters with the penetration of these technologies into the mix. A similar approach was also used to correlate these parameters with the system's transition cost.