\documentclass[letterpaper]{article}
\usepackage{pythontex}
\usepackage[]{amsmath}
\usepackage{amssymb}
\usepackage{acronym}
\usepackage[]{graphicx}
\usepackage[margin=1.2in]{geometry}
\graphicspath{{./pythontex-files-coloring_sat/}}

\acrodef{CNF}{Conjunctive Normal Form}

\let\vel\vee
\let\at\wedge

\title{Generating Closed Modular Colorings of Graphs Using a SAT Solver}

\author{Jan Hlavacek and Garry L.~Johns\\Saginaw Valley State University}

\begin{document}
\maketitle
\section{Introduction}
We start with a graph $G$.  To keep things simple, we will assume
that $G$ has no true twins.  We color all vertices with colors from $0$ to $k-1$.  Then we
calculate the closed modular coloring:  for each vertex you add the colors of
all its neighbours, add it to its color, and assign that number mod $k$ as its
new color.

A coloring is proper if no two adjacent vertices have the same color.

We are trying to see if it is possible to color a graph $G$ so that the
resulting closed modular coloring is proper.

\section{Description using logic}

We will have $k$ colors, $0,1,\cdots,k-1$.  We try to describe the condition of
having a closed modular coloring using logic formulas, so we can
use a SAT-solver to find out if the condition is satisfiable for
the graph G. We will need to introduce a number of logical
variables and write a logic formula using these variables that
will be equivalent to $G$ having a proper closed modular coloring.

\subsection{Vertices}

\subsubsection{Coloring a vertex}

For each vertex $v$, there will be $k$ color variables $c_v^i$, $i =
0,\dots,k-1$.  Exactly one of those will be true at a time, since a vertex cannot
have more than one color:
\begin{gather}
   c_v^1 \vel c_v^2 \vel c_v^3 \vel \dots \vel c_v^k \label{eq:colors1}\\
   \neg c_v^i \vel \neg c_v^j \text{ for $i \neq j$\label{eq:colors2}}
\end{gather}

\subsubsection{Modular coloring}

Now we need to figure out what will be the new color of the vertex in the
modular coloring.  That will depend on the neighbouring vertices, as well as on
the vertex itself.

We will take a list of all the neighbours of the vertex $v$, including the
vertex $v$ itself, and go through all
the possible colorings of this list.  Each such coloring $C_s$ will be assigned
a variable $n_v^s$, that will be equivalent to all the neighbouring vertices
having exactly the colors from $C_s$.

For example, if the list of neighbours of $v$ is $[v,w,y,x,z]$, then for coloring
$C_s = [2,1,3,2,4]$ of those vertices the variable $n_v^s$ will satisfy
\begin{equation}
 n_v^s \equiv c_v^2 \at c_w^1 \at c_y^3 \at c_x^2 \at c_z^4
   \label{eq:neighbours}
\end{equation}

For each color $j$, there will be possibly several colorings $C_{s_1}, C_{s_2},
\dots, C_{s_{t_j}}$ of the neighbouring vertices that will produce $j$ as the
modular coloring of the vertex $v$.  We introduce $k$ variables $m_v^j$ for $j
= 1, 2, \dots, k$, satisfying
\begin{equation}
   m_v^j \equiv n_v^{s_1} \vel n_v^{s_2} \vel \cdots \vel n_v^{s_{t_j}}
   \label{eq:modular}
\end{equation}

\subsection{Edges}

The rules for edges will be simple enough.  All we need to assure is that the
resulting modular coloring is proper, that is for each edge $(v,w)$, the
modular coloring of the vertex $v$ is different than the modular coloring of
the vertex $w$.   That means that for each color $j = 0,\dots,k-1$, $m_v^j$ and
$m_w^j$ cannot be both true:
\begin{equation}
   \neg m_v^j \vel \neg m_w^j \text{ for $j = 0,\dots,k-1$}
   \label{eq:edge}
\end{equation}

\section{Translation to CNF}

Satisfiability solvers typically require their input to be in the \ac{CNF}, that is
a written as a conjunction of clauses, where each clause is a disjunction of
single variables or their negations.  We see that equations \eqref{eq:colors1},
\eqref{eq:colors2} and \eqref{eq:edge} already are in \ac{CNF}, while
\eqref{eq:neighbours} and \eqref{eq:modular} need to be translated.

The formula $a \equiv (b \at c)$ can be translated into \ac{CNF} as
\begin{equation}
   \left(c \vee \neg a\right) \wedge \left(b \vee \neg a\right) \wedge \left(a \vee \neg b \vee \neg c\right)
   \label{eq:trans1}
\end{equation}
while the formula $a \equiv (b \vel c)$ can be translated to
\begin{equation}
   \left(a \vee \neg c\right) \wedge \left(a \vee \neg b\right) \wedge \left(b \vee c \vee \neg a\right)
   \label{eq:trans2}
\end{equation}

Equation \eqref{eq:neighbours} will then translate to
\begin{gather}
   \label{eq:neighbour_trans_1} (c_v^2 \vel \neg n_v^s) \at (c_w^1 \vel \neg n_v^s) \at (c_y^3 \vel \neg
   n_v^s) \at (c_x^2 \vel \neg n_v^s) \at (c_z^4 \vel \neg n_v^s)\\
   \label{eq:neighbour_trans_2} n_v^s \vel \neg c_v^2 \vel \neg c_w^1 \vel \neg c_y^3 \vel \neg
   c_x^2 \vel \neg c_z^4
\end{gather}
In general, for each coloring $C_s$ of the neighbours of vertex $v$, we will have the conjunction of
\begin{equation}
   c_w^j \vel \neg n_v^s
   \label{eq:general_neighbour_1}
\end{equation}
where $w$ runs through all the neighbors of $v$, including $v$, and for each $w$, $j$ is the
color of $w$ in the coloring $C_s$, with
\begin{equation}
   n_v^s \vel \neg c_{w_1}^{j_1} \vel \neg c_{w_2}^{j_2} \vel \cdots
   \label{eq:general_neighbour_2}
\end{equation}
where $w_i$ are all the neighbors of $v$, including $v$, and $j_i$ are their corresponding
colors in $C_s$.

The formula \eqref{eq:modular} will translate to
\begin{gather}
   \label{eq:modular_general_1} m_v^j \vel \neg n_v^{s_i}\\
   \label{eq:modular_general_2} n_v^{s_1} \vel n_v^{s_2} \vel \cdots \vel
   n_v^{s_{t_j}} \vel \neg m_v^j
\end{gather}
where $j$ runs through all the colors $1$ to $k$, and $s_i$ runs through all
indices $s$ such that the corresponding coloring $C_s$ of all the neighbours of
$v$ will produce the modular coloring of $v$ with the color $j$.

\section{Implementation}

\subsection{Imports and Setup}

We will use the \pyv|pycosat| module, which provides a Python interface to the
picosat SAT solver.  We will also need few functions from the \pyv|itertools|
module. We will use the \pyv|graph_tool| module to handle the graph
stuff.

\begin{pyblock}
import pycosat
from itertools import count, islice, product
import graph_tool as gt
\end{pyblock}

The \pyv|pycosat| module requires variables to be represented by integers. Each
variable will have to be associated with a unique integer.  To keep track which
integers we already used, we will use the \pyv|count()| function from
the \pyv|itertools| module.

\subsection{Assigning Variables}

Given a graph, we will need to assign the initial variables for original colors
($c_v^j$) and for colors of a modular coloring ($m_v^j$).  We need to create two property maps for the
given graph, one for original colors, and one for the closed modular coloring.
Each property will be a vector of integers of length $k$, where $k$ is the
number of colors.  The following function will create and populate one such map

\begin{pyblock}
def assign_color_vars(g, no_of_colors, vars_generator):
   """
   Create property map for graph g, containing variable numbers
   for colors.

   Args:
      g: A graph
      no_of_colors: the number of colors used for graph coloring.
      vars_generator: generator that produces numbers for logical
         variables, to be used in pycosat clauses.

   Returns:
      A vertex property map of 'vector<int>' type, filled with
      numbers of pycosat variables corresponding to possible
      colors for each vertex.
   """
   colors_map = g.new_vertex_property("vector<int>")
   for v in g.vertices():
      colors_map[v] = list(islice(vars_generator, no_of_colors))
   return colors_map
\end{pyblock}

We will call this function twice for each graph, first time to generate
variable numbers for all $c_v^j$ variables, the second time to generate numbers
for all $m_v^j$ variables.

\subsection{Generating Clauses}

Now we will write some functions that will generate clauses for the SAT solver.
Each clause will be a list of positive or negative integers.  Positive number
means that the variable with the number appear in the conjunction, while a
negative number means that the negation of the variable with the opposite
number appears in the conjunction.

\subsubsection{Clauses from equations \eqref{eq:colors1} and \eqref{eq:colors2}}

These clauses are grouped by individual vertices.  We will write a function
that will receive a vertex and generate all these clauses for that vertex.  For
the equation \eqref{eq:colors2}, we will only need the clauses for $j < i$,
since disjunction is commutative.

\begin{pyblock}
def vertex_color_clauses(v, color_map):
   """
   Return a list of clauses dealing with individual vertex
   initial color.

   Args:
      v: a graph vertex
      color_map: a vertex property map that assigns variable
      numbers to colors of individual vertices.

   Returns:
      A list of lists, each of them representing one clause.
   """
   l = []
   colors = color_map[v]
   l.append(list(colors))
   for i in range(1,len(colors)):
      for j in range(i):
         l.append([-colors[i],-colors[j]])
   return l
\end{pyblock}

\subsubsection{Clauses from equation \eqref{eq:edge}}

There will be a group of clauses for each edge, assuring that the resulting
modular coloring is proper.  The function will receive an edge, and return a
list of clauses. It will need the lists of modular coloring colors for each end
of the edge, and create a list of clauses assuring that the colors on the ends
of the edge are not the same.

\begin{pyblock}
def edge_clauses(e, modular_color_map):
   """
   Return a list of clauses assuring that the colors of the
   modular coloring on the ends of the edge are different.

   Args:
      e: an edge of a graph
      modular_color_map: a vertex property map that assigns
      variable numbers to colors in modular coloring of
      individual vertices.

   Returns:
      A list of lists, each of which correspond to one clause.
   """
   l = []
   mc1 = modular_color_map[e.source()]
   mc2 = modular_color_map[e.target()]
   for i,j in zip(mc1,mc2):
      l.append([-i,-j])
   return l
\end{pyblock}

\subsubsection{Clauses from equations \eqref{eq:general_neighbour_1},
\eqref{eq:general_neighbour_2}, \eqref{eq:modular_general_1} and
\eqref{eq:modular_general_2}}

This is the trickiest part.  For a given vertex $v$, we take the list of all
the neighbours of $v$, including $v$.  Then we will go through all the possible colorings of
this list, for each coloring we allocate a new variable, and make it equivalent
to a conjunction of the vertex color variables corresponding to the colors in
the coloring.  At the same time, we will check what color would a modular
coloring produced by this coloring of neighbours of $v$ produce at $v$.   We
will add the variable we allocated to this coloring to a list of variables for
the obtained modular color. At the end we will go through all the modular color
variables for vertex $v$, and generate clauses that will make each equivalent
to the disjunction of all the variables added to their lists.

We will start with two auxiliary functions.  Each will receive a variable
number as the first argument, and a list of variable numbers as its second
argument.  The first will generate clauses making the first variable equivalent
to the conjunction of all the variables in the list, the second one will do the
same for a disjunction of all the variables on the list.

\begin{pyblock}
def equiv_to_conjunction(a,vars):
   """
   Return a list of clauses that assure that the variable a is
   equivalent to the conjunction of all the variables in the list
   vars.

   Args:
      a: integer, a number of a variable
      vars: a list of integers, each corresponding to a variable

   Returns:
      a list of lists of integers.  Each list of integers
      corresponds to one clause.  The conjunction of all the
      clauses will be a CNF version of the equivalence of the
      variable a with the conjunction of variables in l.
   """
   l = [[a] + [-b for b in vars]]
   for b in vars:
      l.append([b,-a])
   return l

def equiv_to_disjunction(a,vars):
   """
   Return a list of clauses that assure that the variable a is
   equivalent to the disjunction of all the variables in the list
   vars.

   Args:
      a: integer, a number of a variable
      vars: a list of integers, each corresponding to a variable

   Returns:
      a list of lists of integers.  Each list of integers
      corresponds to one clause.  The conjunction of all the
      clauses will be a CNF version of the equivalence of the
      variable a with the disjunction of variables in l.
   """
   l = [[-a] + vars]
   for b in vars:
      l.append([-b,a])
   return l
\end{pyblock}

The next function will receive a vertex $v$, and produce all the clauses from
equations \eqref{eq:general_neighbour_1}, \eqref{eq:general_neighbour_2},
\eqref{eq:modular_general_1} and \eqref{eq:modular_general_2}.

\begin{pyblock}
def modular_coloring_clauses(v,color_map,modular_color_map,
                             vars_generator):
   """
   Return the list of clauses that will assure that the colors in
   the modular_coloring_map are created as a closed modular
   coloring from the colors in the color_map.

   Args:
      v: a vertex of a graph
      color_map: a vertex property map that assigns variable
         numbers to colors of individual vertices.
      modular_color_map: a vertex property map that assigns
         variable numbers to colors in modular coloring of
         individual vertices.
      vars_generator: generator that produces numbers for logical
         variables, to be used in pycosat clauses.

   Returns:
      A list of lists, each of which correspond to one clause.

   Notes:
      May allocate new pycosat variables using the vars_generator.
   """
   modular_colors = list(modular_color_map[v])
   k = len(modular_colors) # number of colors
   m_lists = {m:[] for m in modular_colors}
   neighbours = list(v.out_neighbours()) + [v]
   colorings = product(range(k),repeat=len(neighbours))
   l = []
   for cs in colorings:
      new_var = next(vars_generator)
      modular_color = sum(cs) % k
      m_lists[modular_colors[modular_color]] += [new_var]
      neighbour_color_vars = []
      for w,c in zip(neighbours,cs):
         neighbour_color_vars.append(color_map[w][c])
      l += equiv_to_conjunction(new_var,neighbour_color_vars)

   for m,s in m_lists.items():
      l += equiv_to_disjunction(m,s)

   return l
\end{pyblock}

\subsubsection{Putting all clauses together}

Now we need to put all the clause generating functions together. The following
function will take a graph and a number of colors, and generate all the clauses
for the graph.  The result can then be fed directly to the pycosat solver.

The steps will be:
\begin{enumerate}
   \item Create a new variable generator.
   \item Assign color and modular color variables to all vertices.
   \item For each vertex:
      \begin{enumerate}
         \item Generate all the clauses for equations \eqref{eq:colors1} and
            \eqref{eq:colors2}.
         \item Generate all the clauses for equations
            \eqref{eq:general_neighbour_1},
            \eqref{eq:general_neighbour_2},
            \eqref{eq:modular_general_1} and
            \eqref{eq:modular_general_2}.
      \end{enumerate}
   \item For each edge, generate all clauses for equation \eqref{eq:edge}.
   \item Return a complete list of all the generated clauses.  Also return the
      two vertex property maps, since we will need them to decode the result.
\end{enumerate}

\begin{pyblock}
def graph_clauses(g,num_of_colors):
   """
   Return the complete lists of all the clauses describing
   conditions for a proper closed modular coloring of g.

   Args:
      g: a graph_tool graph
      num_of_colors: number of colors for the modular coloring

   Returns:
      a list of lists of integers: each list of integers
      corresponds to a disjunction of variables or their
      negations, and two vertex property maps containing
      the colors for the original coloring and the corresponding
      modular coloring.
   """
   l = []
   vars_generator = count(1)
   color_map = assign_color_vars(g,num_of_colors,vars_generator)
   modular_color_map = assign_color_vars(g,num_of_colors,
                                         vars_generator)

   for v in g.vertices():
      l += vertex_color_clauses(v,color_map)
      l += modular_coloring_clauses(v,color_map,modular_color_map,
                                    vars_generator)

   for e in g.edges():
      l +=  edge_clauses(e, modular_color_map)

   return l, color_map, modular_color_map
\end{pyblock}

\subsection{Making sense of the result}

The pycosat solver returns a long list of positive or negative integers, one
for each variable.  Each number corresponds to one variable, and is positive if
the corresponding variable is true, and negative if the variable is false.  We
are only interested in the variables on the beginning, the ones that correspond
to the color variables and modular color variables for each vertex. Out of
these, we only want to know, for each vertex, which of its color variables and
which of its modular color variables has a positive number in the result.

A safe way (see below for some ideas that could make things easier if we could
rely on some assumptions) would be:
\begin{enumerate}
   \item Discard all negative entries from the list
   \item Create a set from the positive entries, to make lookup faster
   \item Iterate through vertices of the graph:
      \begin{enumerate}
         \item For each vertex, iterate through the colors, asking if each is
            in the set, until you find one
         \item Do the same for modular colors
      \end{enumerate}
   \item For each vertex, remember the found color and modular color.
\end{enumerate}

If we could assume that:
\begin{itemize}
   \item Iteration through vertices of a graph always happens in the same
      order. (that is probably true)
   \item The variables returned by pycosat solver always come ordered by
      absolute value. (that is also probably true.  We could also sort the list
      ourselves)
   \item The first (number of colors)*(number of vertices) variables correspond
      to the colors, the next to modular colors (that is true, but it would be
      too hackish to rely on it.  Changing one part of the code could
      completely mess up the other part, introducing messy hard to detect
      errors.)
\end{itemize}
we could simply discard all negative variables, zip together the first (number
of vertices) of the remaining numbers with the vertices of the graph to get the
colors, and zip together the next (number of vertices) of the numbers with the
vertices to get the modular coloring.

\begin{pyblock}
def extract_colors(g,vertex_map,result_list):
   """
   Extract colors of each vertex from the result list.

   Args:
      g: a graph_tool graph
      vertex_map: a vertex property map.  Each vertex has a list
         of its color variables. Could be original color map or
         modular map.
      result_list: the list of variable values returned by the
         pycosat solver.

   Returns:
      a new vertex property map: one integer for each vertex, the
      color of the vertex in the coloring found by the solver.
   """
   s = set(i for i in result_list if i > 0)
   coloring = g.new_vertex_property('int')
   for v in g.vertices():
      for i,n in enumerate(vertex_map[v]):
         if n in s:
            coloring[v] = i
            break

   return coloring
\end{pyblock}

We will try to plot the graph to verify that the solver indeed found a coloring
with the right properties.  We can either use colors to show the original
coloring and numbers to show the modular coloring (default), or numbers to show
the original coloring and colors to show the modular coloring (set
\pyv|color_orig| to \pyv|False|).  The default setting will make it easier to
see patterns in the original coloring that lead to a closed modular coloring,
while the alternative setting will make it easier to quickly verify that the
resulting coloring is indeed a proper closed modular coloring generated by the
original coloring.

\begin{pyblock}
from graph_tool.draw import graph_draw

def plot_graph(g,coloring,modular_coloring,
               color_names,layout,
               filename,size=(600,600),
               color_orig=True):
   fcolors = g.new_vertex_property('string')
   labels = g.new_vertex_property('string')
   if color_orig:
      for v in g.vertices():
         fcolors[v] = color_names[coloring[v]]
         labels[v] = str(modular_coloring[v])
   else:
      for v in g.vertices():
         fcolors[v] = color_names[modular_coloring[v]]
         labels[v] = str(coloring[v])

   graph_draw(g,vertex_fill_color=fcolors,
              vertex_text=labels,
              pos=layout,
              output_size=size,
              output=filename)
\end{pyblock}

\section{Check the coloring}

Just to make sure, we will make an independent check if
\begin{enumerate}
   \item the produced coloring is indeed a closed modular coloring obtained
      from the original coloring.
   \item the produced coloring is proper.
\end{enumerate}

These can be done the following way:
\begin{enumerate}
   \item Loop through vertices, calculating modular coloring and comparing it
      with the assigned modular color.
   \item Loop through edges, making sure the two colors at the ends of the edge
      are different.
\end{enumerate}

\begin{pyblock}
def is_modular(g,coloring,modular_coloring,k):
   """
   Check if the coloring described by modular_coloring is really a
   modular coloring.

   Args:
      g: a graph-tool graph
      coloring: a vertex property map describing the original
         coloring
      modular_coloring: a vertex property map describing the
         coloring that is tested for modularity
      k: an integer, the number of colors available

   Returns:
      True if the coloring in modular_coloring is really modular,
      False otherwise
   """
   for v in g.vertices():
      neighbours = list(v.out_neighbours()) + [v]
      true_modular_color = sum(coloring[w]
                               for w in neighbours) % k
      if modular_coloring[v] != true_modular_color:
         return False

   return True

def is_proper(g,coloring):
   """
   Check if the coloring of g described by coloring is proper

   Args:
      g: a graph-tool graph
      coloring: a vertex property map describing a coloring of g

   Return:
      True if the coloring is proper, False otherwise
   """
   for e in g.edges():
      if coloring[e.source()] == coloring[e.target()]:
         return False

   return True
\end{pyblock}

\subsection{Test a given graph}

Now we take the generated clauses and feed them into the
SAT-solver.  Then we decode the result, and return a tuple whose
first element will be \pyv|True| if we found a solution, or
\pyv|False| if we did not.

In the case a solution was found, the second element will be the
string \pyv|'SAT'|, and the third and fourth element will be the
original coloring and the modular coloring, respectively.

If the solution was not found, the second element of the tuple
will be a string explaining what happened:
\begin{description}
   \item[UNSAT] the solver decided that there is no solution.
   \item[UNKNOWN] the solver was not able to find a solution, and
      was not able to prove that there is none.
   \item[NOT MODULAR] the solver found a solution, but the
      supposedly modular coloring returned by the solver is not a
      closed modular coloring generated by the original coloring
      returned by the solver. This should \emph{never} happen!
   \item[NOT PROPER] the solver found a solution, but the
      supposedly modular coloring returned by the solver is not a
      proper. This should \emph{never} happen!
\end{description}

\begin{pyblock}
def g_has_pcmc(g,k):
   l, c_m, m_c_m = graph_clauses(g,k)
   result = pycosat.solve(l)

   if (result == 'UNSAT') or (result == 'UNKNOWN'):
      return (False,result)

   coloring = extract_colors(g,c_m,result)
   modular_coloring = extract_colors(g,m_c_m,result)

   if not is_modular(g,coloring,modular_coloring,k):
      return(False,'NOT MODULAR',coloring,modular_coloring)
   if not is_proper(g,modular_coloring):
      return(False,'NOT PROPER',coloring,modular_coloring)

   return(True,'SAT',coloring,modular_coloring)
\end{pyblock}

\subsection{Iteration over all colorings}

The Pycosat solver makes it possible to iterate over all solutions, using the
\pyv|itersolve| function.  We will use this to attempt to find all colorings of
a given graph that produce a proper closed modular coloring.  For graphs that
have a large number of such colorings, this can be rather lengthy process, and,
especially for larger graphs, will require a large amount of memory.  We will
therefore provide an option to limit the number of coloring found.  We will
also provide an option to check if the resulting colorings are proper and
closed modular.

\begin{pyblock}
def g_all_pcmc(g,k,limit=0,check=True):
   l, c_m, m_c_m = graph_clauses(g,k)
   if limit==0:
      results = list(pycosat.itersolve(l))
   else:
      results = list(islice(pycosat.itersolve(l),limit))

   colorings = [extract_colors(g,c_m,r) for r in results]
   modular_colorings = [extract_colors(g,m_c_m,r) for r in results]

   combined = zip(colorings, modular_colorings)

   return [(c,m) for c,m in combined if (not check) or
      (is_modular(g,c,m,k) and is_proper(g,m))]
\end{pyblock}

\section{Try it on $K_5$}

Since $K_5$ has true twins, and since we do not check for true
twins, there will not be a proper modular coloring of $K_5$.

\begin{pyblock}
g = gt.Graph(directed=False)

vs = [g.add_vertex() for i in range(5)]
for v in vs:
   for w in vs:
      if v != w:
         g.add_edge(v,w)

res = g_has_pcmc(g,3)

if not res[0]:
   print(res[1])
else:
   print('Modular coloring is proper')
\end{pyblock}

The result was \printpythontex.

\section{Grid Graphs}

We will see if the solver can find closed modular colorings for
some grid graphs.

We will start by some imports and some code for properly laying
out grid graphs. Grid graphs are available in graph-tool in the
\pyv|graph_tool.generation| module, and are called 'lattice'
graphs.  Automatic layout algorithms do not work well for them, so
we define our own layout.
\begin{pyblock}
from graph_tool.generation import lattice

def grid_layout(g,rows,cols):
   grid_pos = g.new_vertex_property("vector<float>")

   for i in range(cols):
      for j in range(rows):
         grid_pos[g.vertex(rows*i + j)] = [i,j]

   return grid_pos
\end{pyblock}

The following function will receive a list of pairs $(m,n)$ and an integer $k$,
and return a dictionary containing, for each pair $(m,n)$, the result of
\pyv|g_has_pcmc| for the grid graph $G_{m,n}$. It will only return the Boolean
result and the string result, without the actual colorings.
\begin{pyblock}
def check_grids(pairs,k):
   return {p:g_has_pcmc(lattice(p),k)[0:2] for p in pairs}
\end{pyblock}

Since the graphs $G_{m,n}$ and $G_{n,m}$ are equivalent from our point of view,
we only need to check $G_{m,n}$ for $n\le m$.  The following function will
generate all such pairs:
\begin{pyblock}
def triangle(K):
   return ((i,j) for i in range(2,K) for j in range(2,i+1))
\end{pyblock}

The following function will then check all the grid graphs $G_{m,n}$ for $2 \le
m,n < K$ (using the above mentioned symmetry), and print the results.
\begin{pyblock}
def check_all_grids(K,k):
   def format_g(pair):
      return '$G_{{{:d},{:d}}}$'.format(*pair)

   results = check_grids(triangle(K),k)
   graphs_without = [p for p in results if not results[p][0]]
   if len(graphs_without) == 0:
      print('All of the graphs have proper {:d}-modular coloring'.format(k))
   else:
      print('Proper {:d}-modular coloring not found for '.format(k) +
         ', '.join(format(g) for g in sorted(graphs_without)))
\end{pyblock}

\subsection{$k = 3$}

First we will look at grid graphs $G_{m,n}$ for $2 \le m,n < 21$:
\begin{pyblock}
check_all_grids(21,3)
\end{pyblock}
\printpythontex

\subsection{$k = 2$}

Now we will try with $k=2$:
\begin{pyblock}
check_all_grids(21,2)
\end{pyblock}

\printpythontex

\subsection{All Colorings for $3\times m$}
We will look at coloring by 2 colors for $3\times m$ graphs.

\begin{pyblock}
def all_for_grid(m,n,k,color_names=['black','white']):
   g = lattice([m,n])
   results = g_all_pcmc(g,k)
   print('There are {} proper closed modular colorings mod {} for the graph $G_{{{:d},{:d}}}$.'.format(len(results),k,m,n))

   for i,r in enumerate(results):
      filename = 'grid_{:d}_{:d}_{:d}_{:d}.pdf'.format(m,n,k,i)
      plot_graph(g,r[0],r[1],
                    color_names,
                    grid_layout(g,m,n),
                    filename,size=(50*(n-1),50*m))
      frm = 'width=\\linewidth' if n > 10 else ''
      print('\n\n\\includegraphics[' + frm + ']{' + filename + '}\n\n')
\end{pyblock}

\printpythontex

\begin{pyblock}
for n in range(2,21):
   print('\n\\subsection{{Colorings for $G_{{3,{:d}}}$}}'.format(n))
   all_for_grid(3,n,2)
\end{pyblock}

\printpythontex

\subsection{All Colorings for $4\times m$}
The same code can be used to generate all coloring by 2 colors for $4\times m$ graphs,
for $m \le 20$:

\begin{pyblock}
for n in range(2,21):
   print('\n\\subsection{{Colorings for $G_{{4,{:d}}}$}}'.format(n))
   all_for_grid(4,n,2)
\end{pyblock}

\printpythontex

\subsection{All Colorings for $5\times m$}
The same code can be used to generate all coloring by 2 colors for $5\times m$ graphs,
for $m \le 40$:

\begin{pyblock}
for n in range(2,41):
   print('\n\\subsection{{Colorings for $G_{{5,{:d}}}$}}'.format(n))
   all_for_grid(5,n,2)
\end{pyblock}

\printpythontex

\subsection{All Colorings for $6\times m$}
The same code can be used to generate all coloring by 2 colors for $6\times m$ graphs,
for $m \le 40$:

\begin{pyblock}
for n in range(2,41):
   print('\n\\subsection{{Colorings for $G_{{6,{:d}}}$}}'.format(n))
   all_for_grid(6,n,2)
\end{pyblock}

\printpythontex

\subsection{A Matrix}

Next we will calculate how many closed 2-modular colorings are there for grid
graphs $G_{m,n}$ for $2 \le m,n < 18$.  Since the calculation is very long for
graphs that have large number of such colorings, we will stop after finding 33
colorings for each graph.  We will list these as ``$>32$'' in the table.

\begin{pyblock}
k = 2
L = 18
M = [[0]*(L-2) for i in range(2,L)]
for m in range(2,L):
   for n in range(2,m+1):
      g = lattice([m,n])
      l, c_m, m_c_m = graph_clauses(g,k)
      M[m-2][n-2] = len(list(islice(pycosat.itersolve(l),33)))
      M[n-2][m-2] = M[m-2][n-2]
\end{pyblock}

\begin{pyblock}
print('\\begin{tabular}{r|' + 'c'*(L-2) + '}\n')
print('&' + '&'.join(str(i) for i in range(2,L)) + '\\\\\\hline\n')
for i,R in enumerate(M):
   print(str(i+2) + '&' + '&'.join(str(j) if j < 33 else '$>32$' for j in R) + '\\\\\n')
print('\\end{tabular}')
\end{pyblock}

\printpythontex

\subsection{All Colorings for $10\times m$}

\begin{pyblock}
for n in range(2,41):
   print('\n\\subsection{{Colorings for $G_{{10,{:d}}}$}}'.format(n))
   all_for_grid(10,n,2)
\end{pyblock}

\printpythontex

\subsection{All Colorings for $14\times m$}

\begin{pyblock}
for n in range(2,41):
   print('\n\\subsection{{Colorings for $G_{{14,{:d}}}$}}'.format(n))
   all_for_grid(14,n,2)
\end{pyblock}

\printpythontex
\end{document}


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

\begin{document}

% --------------------------------------------------------------------------------------------
\section*{Exercise 1.1 Verify symmetry of $\Gamma^{a}{}_{bc}$}

\begin{cadabra}
   {a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices.

   g_{a b}::Metric.

   \partial{#}::PartialDerivative.

   Gamma := \Gamma^{a}_{b c} -> (1/2) g^{a d} (  \partial_{b}{g_{d c}}
                                               + \partial_{c}{g_{b d}}
                                               - \partial_{d}{g_{b c}} ).

   diff := \Gamma^{a}_{b c} - \Gamma^{a}_{c b}.   # cdb (ex-0101.101,diff)

   substitute    (diff, Gamma)                    # cdb (ex-0101.102,diff)
   distribute    (diff)                           # cdb (ex-0101.103,diff)
   canonicalise  (diff)                           # cdb (ex-0101.104,diff)
\end{cadabra}

\begin{align*}
   \cdb{ex-0101.101} &= \cdb{ex-0101.102}\\
                     &= \cdb{ex-0101.103}\\
                     &= \cdb{ex-0101.104}
\end{align*}

\end{document}


\section{Second Order Linear Equations - Method of Undetermined Coefficients}{}{}\label{sec:second order linear equations}
Now we consider second order equations of the form $\ds ay''+by'+cy=f(t)$,
with $a$, $b$, and $c$ constant. Of course, if $a=0$ this
is really a first order equation, so we assume $a\not=0$.
%Also, much
%as in exercise~\xrefn{exer:second order really first order} of
%section~\xrefn{sec:second order homogeneous},
Also, if $c=0$ we can solve
the related first order equation $\ds ah'+bh=f(t)$, and then solve
$\ds h=y'$ for $y$. So we will only examine examples in which
$c\not=0$.

Suppose that
$\ds y_1(t)$ and $\ds y_2(t)$ are solutions to $\ds ay''+by'+cy=f(t)$,
and consider the function $\ds h=y_1-y_2$. We substitute
this function into the left hand side of the differential equation and
simplify:
$$
a(y_1-y_2)''+b(y_1-y_2)'+c(y_1-y_2)=ay_1''+by_1'+cy_1 -
(ay_2''+by_2'+cy_2)=f(t)-f(t)=0.
$$
So $h$ is a solution to the homogeneous equation $\ds ay''+by'+cy=0$.
Since we know how to find all such $h$, then with
just one particular solution $\ds y_2$ we can express all possible
solutions $\ds y_1$, namely, $\ds y_1=h+y_2$, where now $h$ is the
general solution to the homogeneous equation. Of course, this is
exactly how we approached the first order linear equation.

To make use of this observation we need a method to find a single
solution $y_2$. This turns out to be somewhat more difficult than the
first order case, but if $f(t)$ is of a certain simple form, we can
find a solution using the
\dfont{method of undetermined coefficients}, sometimes
more whimsically called the
\dfont{method of judicious guessing}.

\begin{example}{Second Order Linear Equation}{Second Order Linear Equation 1}\label{Second Order Linear Equation 1}
 Solve the differential equation
$y''-y'-6y=18t^2+5.$
\end{example}

\begin{solution}
The general solution of the homogeneous equation is
$\ds Ae^{3t}+Be^{-2t}$. We guess that a solution to the
non-homogeneous equation might look like $f(t)$ itself, namely,
a quadratic $\ds y=at^2+bt+c$. Substituting this guess into the
differential equation we get
$$
y''-y'-6y = 2a-(2at+b)-6(at^2+bt+c) = -6at^2+(-2a-6b)t+(2a-b-6c).
$$
We want this to equal $18t^2+5$, so we need
\begin{eqnarray*}
-6a&=&18\cr
-2a-6b&=&0\cr
2a-b-6c&=&5
\end{eqnarray*}
This is a system of three equations in three unknowns and is not hard
to solve: $a=-3$, $b=1$, $c=-2$. Thus the general solution to the
differential equation is $\ds Ae^{3t}+Be^{-2t}-3t^2+t-2$.
\end{solution}

So the ``judicious guess'' is a function with the same form as $f(t)$
but with undetermined (or better, yet to be determined)
coefficients. This works whenever $f(t)$ is a polynomial.

\begin{example}{Mass-Spring System with No Damping}{Mass-Spring System with No Damping}\label{Mass-Spring System with No Damping}
Analyze the initial value problem $\ds my'' +ky=-mg$,
$y(0)=2$, $\ds y'(0)=50$.
\end{example}

\begin{solution}
The left hand side represents a mass-spring
system with no damping, i.e., $b=0$. Unlike the homogeneous case, we
now consider the force due to gravity, $-mg$, assuming the spring is
vertical at the surface of the earth, so that $g=980$. To be specific,
let us take $m=1$ and $k=100$. The general solution to the homogeneous
equation is $\ds A\cos(10t)+B\sin(10t)$. For the solution to the
non-homogeneous equation we guess simply a constant $y=a$, since $-mg=-980$
is a constant. Then $\ds y''+100y= 100a$ so $a=-980/100=-9.8$. The
desired general solution is then $\ds A\cos(10t)+B\sin(10t)-9.8$.
Substituting the initial conditions we get
\begin{eqnarray*}
2&=&A-9.8\cr
50&=&10B
\end{eqnarray*}
so $A=11.8$ and $B=5$ and the solution is $\ds 11.8\cos(10t)+5\sin(10t)-9.8$.
\end{solution}

More generally, this method can be used when a function similar to
$f(t)$ has derivatives that are also similar to $f(t)$; in the
examples so far, since $f(t)$ was a polynomial, so were its derivatives.
The method will work if $f(t)$ has the form $p(t)e^{\alpha t}\cos(\beta t)+
q(t)e^{\alpha t}\sin(\beta t)$, where $p(t)$ and $q(t)$ are
polynomials; when $\alpha=\beta=0$ this is simply $p(t)$, a
polynomial. In the most general form it is not simple to describe the
appropriate judicious guess; we content ourselves with some examples
to illustrate the process.

\begin{example}{Solving a Second Order Linear Equation}{Solving a Second Order Linear Equation 2}\label{Solving a Second Order Linear Equation 2}
 Find the general solution to $\ds y''+7y'+10y=e^{3t}.$
\end{example}

\begin{solution}
The characteristic equation is $r^2+7r+10=(r+5)(r+2)$,
so the solution to the homogeneous equation is
$Ae^{-5t}+Be^{-2t}$. For a particular solution to the inhomogeneous
equation we guess $Ce^{3t}$. Substituting we get
$$
9Ce^{3t}+21Ce^{3t}+10Ce^{3t}=e^{3t}40C.
$$
When $C=1/40$ this is equal to $f(t)=e^{3t}$, so the solution is
$Ae^{-5t}+Be^{-2t}+(1/40)e^{3t}$.
\end{solution}

\begin{example}{Solving a Second Order Linear Equation}{Solving a Second Order Linear Equation 3}\label{Solving a Second Order Linear Equation 3}
 Find the general solution to
$\ds y''+7y'+10y=e^{-2t}.$
\end{example}

\begin{solution}
Following the last example we might guess
$Ce^{-2t}$, but since this is a solution to the homogeneous equation
it cannot work. Instead we guess $Cte^{-2t}$. Then
$$
(-2Ce^{-2t}-2Ce^{-2t}+4Cte^{-2t})+7(Ce^{-2t}-2Cte^{-2t})+10Cte^{-2t}
=e^{-2t}(-3C).
$$
Then $C=-1/3$ and the solution is $Ae^{-5t}+Be^{-2t}-(1/3)te^{-2t}$.
\end{solution}

In general, if $f(t)=e^{kt}$ and $k$ is one of the roots of the
characteristic equation, then we guess $Cte^{kt}$ instead of
$Ce^{kt}$. If $k$ is the only root of the characteristic equation,
then $Cte^{kt}$ will also not work, so we must guess $Ct^2e^{kt}$.

\begin{example}{Solving a Second Order Linear Equation}{Solving a Second Order Linear Equation 4}\label{Solving a Second Order Linear Equation 4}
 Find the general solution to
$\ds y''-6y'+9y=e^{3t}.$
\end{example}

\begin{solution}
The characteristic equation is
$\ds r^2-6r+9=(r-3)^2$, so the general solution to the homogeneous
equation is $Ae^{3t}+Bte^{3t}$. Guessing $Ct^2e^{3t}$ for the
particular solution, we get
$$
(9Ct^2e^{3t}+6Cte^{3t}+6Cte^{3t}+2Ce^{3t})-6(3Ct^2e^{3t}+2Cte^{3t})+9Ct^2e^{3t}
=e^{3t}2C.
$$
Thus, the solution is $\ds Ae^{3t}+Bte^{3t}+(1/2)t^2e^{3t}$.
\end{solution}

It is common in various physical systems to encounter an $f(t)$ of the
form $\ds a\cos(\omega t)+b\sin(\omega t)$.

\begin{example}{Solving a Second Order Linear Equation}{Solving a Second Order Linear Equation 5}\label{Solving a Second Order Linear Equation 5}
 Find the general solution to
$\ds y''+6y'+25y=\cos(4t).$
\end{example}

\begin{solution}
The roots of the characteristic equation are
$-3\pm 4i$, so the solution to the homogeneous equation is
$\ds e^{-3t}(A\cos(4t)+B\sin(4t))$. For a particular solution, we
guess $C\cos(4t)+D\sin(4t)$. Substituting as usual:
$$(-16C\cos(4t)+-16D\sin(4t))+6(-4C\sin(4t)+4D\cos(4t))+25(C\cos(4t)+D\sin(4t))$$
$$=(24D+9C)\cos(4t)+(-24C+9D)\sin(4t).$$
To make this equal to $\cos(4t)$ we need
\begin{eqnarray*}
24D+9C&=&1\cr
9D-24C&=&0
\end{eqnarray*}
which gives $C=1/73$ and $D=8/219$. The full solution is then
$\ds e^{-3t}(A\cos(4t)+B\sin(4t))+(1/73)\cos(4t)+(8/219)\sin(4t)$.

The function $\ds e^{-3t}(A\cos(4t)+B\sin(4t))$ is a damped
oscillation as in example~\ref{Mass-Spring System with No Damping},
while $\ds(1/73)\cos(4t)+(8/219)\sin(4t)$ is a simple undamped
oscillation. As $t$ increases, the sum $\ds
e^{-3t}(A\cos(4t)+B\sin(4t))$ approaches zero, so the solution
$$e^{-3t}(A\cos(4t)+B\sin(4t))+(1/73)\cos(4t)+(8/219)\sin(4t)$$
becomes more and more like the simple oscillation
$\ds(1/73)\cos(4t)+(8/219)\sin(4t)$---notice that the initial
conditions don't matter to this long term behavior. The damped portion
is called the
\dfont{transient} [part of the]
\dfont{solution}, and the simple oscillation is called the
\dfont{steady state} [part of the] \dfont{solution}.
A physical example is a mass-spring system. If the only force on the
mass is due to the spring, then the behavior of the system is a damped
oscillation. If in addition an external force is applied to the mass,
and if the force varies according to a function of the form
 $\ds a\cos(\omega t)+b\sin(\omega t)$, then the long term behavior
will be a simple oscillation determined by the steady state portion of the
general solution; the initial position of the mass will not matter.
\end{solution}

As with the exponential form, such a simple guess may not work.

\begin{example}{Solving a Second Order Linear Equation}{Solving a Second Order Linear Equation 6}\label{Solving a Second Order Linear Equation 6}
 Find the general solution to $\ds y''+16y=-\sin(4t).$
\end{example}

\begin{solution}
The roots of the characteristic equation are $\pm4i$, so the
solution to the homogeneous equation is $A\cos(4t)+B\sin(4t)$. Since
both $\cos(4t)$ and $\sin(4t)$ are solutions to the homogeneous
equation,  $C\cos(4t)+D\sin(4t)$ is also, so it cannot be a solution
to the non-homogeneous equation. Instead, we guess
$Ct\cos(4t)+Dt\sin(4t)$. Then substituting:
$$(-16Ct\cos(4t)-16D\sin(4t)+8D\cos(4t)-8C\sin(4t)))+16(Ct\cos(4t)+Dt\sin(4t))$$
$$=8D\cos(4t)-8C\sin(4t).$$
Thus $C=1/8$, $D=0$, and the solution is
$\ds C\cos(4t)+D\sin(4t)+(1/8)t\cos(4t)$.
\end{solution}

In general, if $f(t)=a\cos(\omega t)+b\sin(\omega t)$, and $\pm \omega
i$ are the roots of the characteristic equation, then instead of
$C\cos(\omega t)+D\sin(\omega t)$ we guess $Ct\cos(\omega t)+Dt\sin(\omega t)$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:second order linear equations}}

\begin{enumialphparenastyle}

Find the general solution to the differential equation.

%%%%%%%%%%
\begin{ex}
 $\ds y'' -10y'+25y=\cos t$
\begin{sol}
 $Ae^{5t}+Bte^{5t}+(6/169)\cos t-(5/338)\sin t$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+2\sqrt2y'+2y=10$
\begin{sol}
 $\ds Ae^{-\sqrt2t}+Bte^{-\sqrt2t}+5$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+16y=8t^2+3t-4$
\begin{sol}
 $\ds A\cos(4t)+B\sin(4t)+ (1/2)t^2+(3/16)t-5/16$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+2y=\cos(5t)+\sin(5t)$
\begin{sol}
 $\ds A\cos(\sqrt2t)+B\sin(\sqrt2t)-(\cos(5t)+\sin(5t))/23$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''-2y'+2y=e^{2t}$
\begin{sol}
 $\ds e^{t}(A\cos t+B\sin t)+e^{2t}/2$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''-6y+13=1+2t+e^{-t}$
\begin{sol}
 $\ds Ae^{\sqrt6t}+Be^{-\sqrt6t}+2-t/3-e^{-t}/5$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+y'-6y=e^{-3t}$
\begin{sol}
 $\ds Ae^{-3t}+Be^{2t}-(1/5)te^{-3t}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''-4y'+3y=e^{3t}$
\begin{sol}
 $\ds Ae^t+Be^{3t}+(1/2)te^{3t}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+16y=\cos(4t)$
\begin{sol}
 $\ds A\cos(4t)+B\sin(4t)+(1/8)t\sin(4t)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y'' +9y=3\sin(3t)$
\begin{sol}
 $\ds A\cos(3t)+B\sin(3t)-(1/2)t\cos(3t)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+12y'+36y=6e^{-6t}$
\begin{sol}
 $\ds Ae^{-6t}+Bte^{-6t}+3t^2e^{-6t}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''-8y'+16y=-2e^{4t}$
\begin{sol}
 $\ds Ae^{4t}+Bte^{4t}-t^2e^{4t}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+6y'+5y=4$
\begin{sol}
 $\ds Ae^{-t}+Be^{-5t}+(4/5)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''-y'-12y=t$
\begin{sol}
 $\ds Ae^{4t}+Be^{-3t}+(1/144)-(t/12)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+5y=8\sin(2t)$
\begin{sol}
 $\ds A\cos(\sqrt5t)+B\sin(\sqrt5t)+8\sin(2t)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''-4y=4e^{2t}$
\begin{sol}
 $\ds Ae^{2t}+Be^{-2t}+te^{2t}$
\end{sol}
\end{ex}

\noindent Solve the initial value problem.

%%%%%%%%%%
\begin{ex}
 $\ds y''-y=3t+5$, $y(0)=0$, $\ds y'(0)=0$
\begin{sol}
 $\ds 4e^{t}+e^{-t}-3t-5$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+9y=4t$, $y(0)=0$, $\ds y'(0)=0$
\begin{sol}
 $\ds -(4/27)\sin(3t)+(4/9)t$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y'' +12y' +37y=10e^{-4t}$, $y(0)=4$, $\ds y'(0)=0$
\begin{sol}
 $\ds e^{-6t}(2\cos t+20\sin t)+2e^{-4t}$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 $\ds y''+6y'+18y=\cos t-\sin t$, $y(0)=0$, $\ds y'(0)=2$
\begin{sol}
 $\ds
\left(-{23\over 325}\cos(3t)+{592\over 975}\sin(3t)\right)+
{23\over325}\cos t-{11\over325}\sin t$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the solution for the mass-spring equation
$\ds y''+4y'+29y=689\cos(2t)$.
\begin{sol}
 $\ds e^{-2t}(A\sin(5t)+B\cos(5t))+8\sin(2t)+25\cos(2t)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the solution for the mass-spring equation
$\ds3y''+12y'+24y=2\sin t$.
\begin{sol}
 $\ds e^{-2t}(A\sin(2t)+B\cos(2t))+(14/195)\sin t-(8/195)\cos t$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Consider the differential
equation $\ds my''+by'+ky=\cos(\omega t)$,
with $m$, $b$, and $k$ all positive and $\ds b^2<2mk$; this equation
is a model for a damped mass-spring system with external
driving force $\cos(\omega t)$.
Show that the steady state part of the solution has amplitude
$${1\over \sqrt{(k-m\omega^2)^2+\omega^2b^2}}.$$
Show that this amplitude is largest when
$\ds \omega={\sqrt{4mk-2b^2}\over 2m}$. This is the
\dfont{resonant frequency} of the system.
\end{ex}

\end{enumialphparenastyle}


\documentclass[12pt]{article}
\usepackage{fullpage,latexsym,amsmath,graphicx}
\setlength{\textheight}{25cm}
\pagestyle{empty}
\title{on random hex (draft)}
\author{RBH etc}
\date{April 2013}
\begin{document}
\maketitle
\section{intro}
Consider a game of random Hex on an $n$$\times$$n$ board.
Black and White alternate moves, and the first player to connect their two opposing sides wins. Each move is uniformly randomly selected
from the set of all available moves (i.e., empty cells) at that point.
For each player, and for each positive integer $t$,
we are interested in $w_n^t$, the probability that the player wins the game with their $t$'th move.

Hex on an $n$$\times$$n$ board has certain symmetries that simplify the computation of $w$.

Define $f_n^t$ as the probability that,
a uniformly randomly chosen
$t$-subset of the $n$$\times$$n$ cell locations yields a winning configuration
for a fixed player --- say Black.
By the symmetry of the $n$$\times$$n$ board, and since
each filled board yields a win for exactly one player (draws are not possible in Hex),
there is a bijection between $t$-subsets that
yield a win for Black and $t$-subsets that yield a win for White.
Thus $f_n^t$ is the same for each player.

Since each move is uniform random,
$f_n^t$ is just the cumulative distribution of $w_n^t$,
namely
\[ f_n^t = \sum_{j=1}^t w_n^j \: .  \]

%\input{code/data/gn/winrate.11}
\ \hfill \includegraphics{code/data/gn/winrate.21.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.20.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.19.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.18.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.17.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.16.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.15.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.13.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.11.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.9.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.7.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.5.eps} \hfill \

\ \hfill \includegraphics{code/data/gn/winrate.3.eps} \hfill \

\section{random hex win prob \ \ (draft ??)}
define $b(n)$ (resp.\ $w(n)$)
as probability that first (second) player wins $n$$\times$$n$ hex

Once one player has a connection, filling any unoccupied cells
does not change the outcome.
So $b(n)$ is the fraction
of final board positions (exactly $\lceil n^2/2 \rceil$ black cells)
that connect black's two sides,
$w(n)$ is the fraction
of final board positions (exactly $\lfloor n^2/2 \rfloor$ white cells)
that connect white's two sides.
Flipping colors and reflecting through the main
diagonal maps a winning position to a winning position,
and so $b(n)=w(n)$ if $n$ is even.

\section{n odd}
For $n=2t+1$, we are interested in $b(n)-w(n)$.
Any corresponding position corresponds
to a game that black wins on the last move;
after the initial $n-1$ stones have been played, the
black subset is not winning, and neither is the white.
The white subset is a non-winning $t$-subset,
the black subset is a non-winning $t$-subset,
which the final stone transforms into a winning $t+1$-subset.
Thus the last stone is a cut vertex of the final winning
black connecting set.
So this difference can be found by finding the
ratio $c(n)$ of cut vertices to total vertices over
all black winning subsets.
\[ c(n) = \frac{b(n)-w(n)}{b(n)}
\: \: \: \: \: \mbox{\rm and } \: \: \: \: \:
b(n) + w(n) = 1
\: \: \: \: \: \mbox{\rm so }  \: \: \: \: \:
b(n) = \frac{1}{2 - c(n)}\]
so $b(n)$ goes to 0.5 if $c(n)$ goes to 0.

\vfill

\noindent{\bf Data generated by counting subsets}

\begin{tabular}{ccccccc}
$n$ & subsets & w-win & b-win & $w(n)$ & $b(n)$ & $c(n)$ \\
1   &  1      &  0    & 1     & 1         & 0     &  1     \\
3   &  126    & 42    & 84    & .333333... & .666666... & .5 \\
5   &  5200300 &  2219059 & 2981241 & .426717... & .573282... & .255659...
\end{tabular}

\vfill

\noindent{\bf Data generated by $10^6$ pseudorandom samples, error about .001}

\begin{verbatim}
 3 .332755 .667051
 5 .427192 .573638
 7 .456181 .544021
 9 .469588 .530459
11 .476214 .522408
13 .482711 .519231
15 .485096 .515169
17 .487105 .512194
19 .489548 .511278
21 .491369 .510176
23 .491933 .508733
25 .492264 .507082
\end{verbatim}
\newpage
\vspace*{-3cm}
\input{4subs}
\newpage
\section{probs}
using sampling, we approximate the prob that a particular
cell is the black winning move (namely, that the game ends with that move)

eg. on 3x3, prob a1 wins?
\begin{verbatim}
winning path
! . .
 x . .
  x . .
wins on move 3 prob (1/(9 choose 2)) * 1/7
wins on move 4 prob (5/(9 choose 3)) * 1/6
wins on move 5 prob (9/(9 choose 4)) * 1/5
! . .        (not the above => bottom left cell not black)
 x x .
  . x .
wins on move 4 prob (1/(9 choose 3)) * 1/6
wins on move 5 prob (2/(9 choose 4)) * 1/5
! . .
 x x x
  . . x
wins on move 5 prob (1/(9 choose 4)) * 1/5

total
(1/(9 choose 2))*1/7 + (1/(9 choose 3)) + (12/5)(1/(9 choose 4)) =
11/(9*7*5) = 0.0349206349...

by contrast, the prob that black wins after playing at a1
is 12/126 = 0.095238...
\end{verbatim}

prob game goes to last move is 1 - 2*prob(n2-1)/2 stones connect
= prob (n2-1)/2 stones connect in neither direction

\newpage
\ \hfill \includegraphics[scale=1]{draw/bigboard.eps} \hfill \

\end{document}


\documentclass[paper.tex]{subfiles}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{algpseudocode}
\usepackage{algorithm}

% Add vertical spacing to tables
\renewcommand{\arraystretch}{1.4}

% Begin Document
\begin{document}

\section{Approximation Approach}

As the Minimum Domating Set requires a brute force approach, sufficiently large graphs can have enormous run times.
To counter these problems, approximation algorithms are developed.
These are designed to find an answer that is often considered good enough for the task at hand, while having a much lower complexity.

We explored one such approach.
This approach uses a greedy technique for finding a dominating set.
While the set is not covered, it selects the node with the highest out-degree of nodes that are not yet dominated.
This node gets added to the current list of dominating nodes, and repeats until the entire graph is covered.
This greatly improves our runtime complexity to $O(n^2)$.

To put this concept into pseudocode:

\begin{algorithm}[H]

    \caption{Minimum Dominating Set approximation algorithm}

    \begin{algorithmic}[1]
        \Procedure{Approximate Dominating Set}{}
            \While{the graph is not dominated}
                \For{each node not yet domianted}
                    \State Find its out-degree of nodes not yet dominated
                \EndFor
                \State Get the node with the highest out-degree
                \State Set all of its neighbors to be dominated
            \EndWhile
        \EndProcedure
    \end{algorithmic}

\end{algorithm}

As evident, this algorithm is much simpler.
However, there is no guarantee that this algorithm will produce a minimal set, or even a set that is close to minimal.
Depending on the task at hand, this approximate solution using a greedy technique may be preferred to a guaranteed minimal set.

We show some results of our experimental data in the next section.

\end{document}


\section{Moments}
\label{sec:appendix:moments}

\subsection{Overview}
\subsubsection{Moments and initialization for addition}
The desired properties for initialization are according to Glorot et al. \cite{glorot-initialization}:
\begin{equation}
\begin{aligned}
E[z_{h_\ell}] &= 0 & E\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &= 0 \\
Var[z_{h_\ell}] &= Var\left[z_{h_{\ell-1}}\right] &
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &= Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell}}}\right]
\end{aligned}
\end{equation}

\subsubsection{Initialization for addition}

Glorot initialization can not be used for $\mathrm{NAC}_{+}$ as $W_{h_{\ell-1},h_{\ell}}$ is not sampled directly. Assuming that $\hat{W}_{h_\ell, h_{\ell-1}} \sim \mathrm{Uniform}[-r, r]$ and $\hat{M}_{h_\ell, h_{\ell-1}} \sim \mathrm{Uniform}[-r, r]$, then the variance can be derived (see proof in Appendix \ref{sec:appendix:moments:weight-matrix-construction}) to be:
\begin{equation}
Var[W_{h_{\ell-1},h_{\ell}}] = \frac{1}{2r} \left(1 - \frac{\tanh(r)}{r}\right) \left(r - \tanh\left(\frac{r}{2}\right)\right)
\end{equation}
One can then solve for $r$, given the desired variance ($Var[W_
{h_{\ell-1},h_{\ell}}] = \frac{2}{H_{\ell-1} + H_{\ell}}$) \cite{glorot-initialization}.

\subsubsection{Moments and initialization for multiplication}
Using second order multivariate Taylor approximation and some assumptions of uncorrelated stochastic variables, the expectation and variance of the $\mathrm{NAC}_{\bullet}$ layer can be estimated to:
\begin{equation}
\begin{aligned}
f(c_1, c_2) &= \left(1 + c_1 \frac{1}{2} Var[W_{h_\ell, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2\right)^{c_2\ H_{\ell-1}} \\
E[z_{h_\ell}] &\approx f\left(1, 1\right) \\
Var[z_{h_\ell}] &\approx f\left(4, 1\right) - f\left(1, 2\right) \\
E\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &= 0 \\
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell}}}\right] H_{\ell}\ f\left(4, 1\right)\ Var[W_{h_{\ell}, h_{\ell-1}}] \\
&\cdot \left(\frac{1}{\left(|E[z_{h_{\ell-1}}]| + \epsilon\right)^2} + \frac{3}{\left(|E[z_{h_{\ell-1}}]| + \epsilon\right)^4} Var[z_{h_{\ell-1}}]\right)
\end{aligned}
\end{equation}

This is problematic because $E[z_{h_\ell}] \ge 1$, and the variance explodes for $E[z_{h_{\ell-1}}] = 0$. $E[z_{h_{\ell-1}}] = 0$ is normally a desired property \cite{glorot-initialization}. The variance explodes for $E[z_{h_{\ell-1}}] = 0$, and can thus not be initialized to anything meaningful.

For our proposed NMU, the expectation and variance can be derived (see proof in Appendix \ref{sec:appendix:moments:nmu}) using the same assumptions as before, although no Taylor approximation is required:
\begin{equation}
\begin{aligned}
E[z_{h_\ell}] &\approx \left(\frac{1}{2}\right)^{H_{\ell-1}} \\
E\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx 0 \\
Var[z_{h_\ell}] &\approx \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1}} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1}} - \left(\frac{1}{4}\right)^{H_{\ell-1}} \\
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] H_\ell \\
&\cdot \left(
\left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1}} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1} - 1}\right)
\end{aligned}
\end{equation}

These expectations are better behaved. It is unlikely that the expectation of a multiplication unit can become zero, since the identity for multiplication is 1. However, for a large $H_{\ell-1}$ it will be near zero.

The variance is also better behaved, but do not provide a input-independent initialization strategy. We propose initializing with $Var[W_{h_{\ell-1},h_\ell}] = \frac{1}{4}$, as this is the solution to $Var[z_{h_\ell}] = Var[z_{h_{\ell-1}}]$ assuming $Var[z_{h_{\ell-1}}] = 1$ and a large $H_{\ell-1}$ (see proof in Appendix \ref{sec:appendix:moments:nmu:initialization}). However, more exact solutions are possible if the input variance is known.

\subsection{Expectation and variance for weight matrix construction in NAC layers}
\label{sec:appendix:moments:weight-matrix-construction}

The weight matrix construction in NAC, is defined in scalar notation as:
\begin{equation}
W_{h_\ell, h_{\ell-1}} = \tanh(\hat{W}_{h_\ell, h_{\ell-1}}) \sigma(\hat{M}_{h_\ell, h_{\ell-1}})
\end{equation}

Simplifying the notation of this, and re-expressing it using stochastic variables with uniform distributions this can be written as:
\begin{equation}
\begin{aligned}
W &\sim \tanh(\hat{W}) \sigma(\hat{M}) \\
\hat{W} &\sim ~ U[-r, r] \\
\hat{M} &\sim ~ U[-r, r]
\end{aligned}
\end{equation}

Since $\tanh({\hat{W}})$ is an odd-function and $E[\hat{W}] = 0$, deriving the expectation $E[W]$ is trivial.
\begin{equation}
\mathrm{E}[W] = \mathrm{E}[\tanh(\hat{W})]\mathrm{E}[\sigma(\hat{M})] = 0 \cdot \mathrm{E}[\sigma(\hat{M})] = 0
\end{equation}

The variance is more complicated, however as $\hat{W}$ and $\hat{M}$ are independent, it can be simplified to:
\begin{equation}
\mathrm{Var}[W] = \mathrm{E}[\tanh(\hat{W})^2] \mathrm{E}[\sigma(\hat{M})^2] - \mathrm{E}[\tanh(\hat{W})]^2 \mathrm{E}[\sigma(\hat{M})]^2 = \mathrm{E}[\tanh(\hat{W})^2] \mathrm{E}[\sigma(\hat{M})^2]
\end{equation}

These second moments can be analyzed independently. First for $\mathrm{E}[\tanh(\hat{W})^2]$:
\begin{equation}
\begin{aligned}
\mathrm{E}[\tanh(\hat{W})^2] &= \int_{-\infty}^{\infty} \tanh(x)^2 f_{U[-r, r]}(x)\ \mathrm{d}x \\
&= \frac{1}{2r} \int_{-r}^{r} \tanh(x)^2\ \mathrm{d}x \\
&= \frac{1}{2r} \cdot 2 \cdot (r - \tanh(r)) \\
&= 1 - \frac{\tanh(r)}{r}
\end{aligned}
\end{equation}

Then for $\mathrm{E}[\tanh(\hat{M})^2]$:
\begin{equation}
\begin{aligned}
\mathrm{E}[\sigma(\hat{M})^2] &= \int_{-\infty}^{\infty} \sigma(x)^2 f_{U[-r, r]}(x)\ \mathrm{d}x \\
&= \frac{1}{2r} \int_{-r}^{r} \sigma(x)^2\ \mathrm{d}x \\
&= \frac{1}{2r} \left(r - \tanh\left(\frac{r}{2}\right)\right)
\end{aligned}
\end{equation}

Which results in the variance:
\begin{equation}
\mathrm{Var}[W] = \frac{1}{2r} \left(1 - \frac{\tanh(r)}{r}\right) \left(r - \tanh\left(\frac{r}{2}\right)\right)
\end{equation}

\subsection{Expectation and variance of \texorpdfstring{$\mathrm{NAC}_{\bullet}$}{NAC-mul}}
\label{sec:appendix:moments:nac-mul}
\subsubsection{Forward pass}

\paragraph{Expectation} Assuming that each $z_{h_{\ell-1}}$ are uncorrelated, the expectation can be simplified to:
\begin{equation}
\begin{aligned}
E[z_{h_\ell}] &= E\left[\exp\left(\sum_{h_{\ell-1}=1}^{H_{\ell-1}} W_{h_{\ell}, h_{\ell-1}} \log(|z_{h_{\ell-1}}| + \epsilon) \right)\right] \\
&= E\left[\prod_{h_{\ell-1}=1}^{H_{\ell-1}} \exp(W_{h_{\ell}, h_{\ell-1}} \log(|z_{h_{\ell-1}}| + \epsilon)) \right] \\
&\approx \prod_{h_{\ell-1}=1}^{H_{\ell-1}} E[\exp(W_{h_{\ell}, h_{\ell-1}} \log(|z_{h_{\ell-1}}| + \epsilon))] \\
&= E[\exp(W_{h_{\ell}, h_{\ell-1}} \log(|z_{h_{\ell-1}}| + \epsilon))]^{H_{\ell-1}} \\
&= E\left[(|z_{h_{\ell-1}}| + \epsilon)^{W_{h_{\ell}, h_{\ell-1}}}\right]^{H_{\ell-1}} \\
&= E\left[f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})\right]^{H_{\ell-1}}
\end{aligned}
\end{equation}

Here we define $g$ as a non-linear transformation function of two independent stochastic variables:
\begin{equation}
f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}}) = (|z_{h_{\ell-1}}| + \epsilon)^{W_{h_{\ell}, h_{\ell-1}}}
\end{equation}

We then apply second order Taylor approximation of $f$, around $(E[z_{h_{\ell-1}}], E[W_{h_{\ell}, h_{\ell-1}}])$.
\begin{equation}
\begin{aligned}
&E[f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})] \approx
E\Bigg[\\
&f(E[z_{h_{\ell-1}}], E[W_{h_{\ell}, h_{\ell-1}}])\\
&+ \begin{bmatrix}
z_{h_{\ell-1}} - E[z_{h_{\ell-1}}] \\ W_{h_{\ell}, h_{\ell-1}} - E[W_{h_{\ell}, h_{\ell-1}}]
\end{bmatrix}^T \begin{bmatrix}
\frac{\partial f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial z_{h_{\ell-1}}} \\
\frac{\partial f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial W_{h_{\ell}, h_{\ell-1}}}
\end{bmatrix} \Bigg\rvert_{
\begin{cases}
z_{h_{\ell-1}} = E[z_{h_{\ell-1}}] \\
W_{h_{\ell}, h_{\ell-1}} = E[W_{h_{\ell}, h_{\ell-1}}]
\end{cases}
} \\
&+ \frac{1}{2} \begin{bmatrix}
z_{h_{\ell-1}} - E[z_{h_{\ell-1}}] \\ W_{h_{\ell}, h_{\ell-1}} - E[W_{h_{\ell}, h_{\ell-1}}]
\end{bmatrix}^T \\
&\bullet \begin{bmatrix}
\frac{\partial^2 f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial^2 z_{h_{\ell-1}}} & \frac{\partial^2 f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial z_{h_{\ell-1}} \partial W_{h_{\ell}, h_{\ell-1}}} \\
\frac{\partial^2 f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial z_{h_{\ell-1}} \partial W_{h_{\ell}, h_{\ell-1}}} & \frac{\partial^2 f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial^2 W_{h_{\ell}, h_{\ell-1}}}
\end{bmatrix} \Bigg\rvert_{
\begin{cases}
z_{h_{\ell-1}} = E[z_{h_{\ell-1}}] \\
W_{h_{\ell}, h_{\ell-1}} = E[W_{h_{\ell}, h_{\ell-1}}]
\end{cases}
} \\
&\bullet \begin{bmatrix}
z_{h_{\ell-1}} - E[z_{h_{\ell-1}}] \\ W_{h_{\ell}, h_{\ell-1}} - E[W_{h_{\ell}, h_{\ell-1}}]
\end{bmatrix}\Bigg]
\end{aligned}
\end{equation}

Because $E[z_{h_{\ell-1}} - E[z_{h_{\ell-1}}]] = 0$, $E[W_{h_{\ell}, h_{\ell-1}} - E[W_{h_{\ell}, h_{\ell-1}}]] = 0$, and $Cov[z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}}] = 0$. This simplifies to:
\begin{equation}
\begin{aligned}
&E[g(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})] \approx
g(E[z_{h_{\ell-1}}], E[W_{h_{\ell}, h_{\ell-1}}])\\
&+ \frac{1}{2} Var\begin{bmatrix}
z_{h_{\ell-1}} \\ W_{h_{\ell}, h_{\ell-1}}
\end{bmatrix}^T \begin{bmatrix}
\frac{\partial^2 g(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial^2 z_{h_{\ell-1}}} \\
\frac{\partial^2 g(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})}{\partial^2 W_{h_{\ell}, h_{\ell-1}}}
\end{bmatrix} \Bigg\rvert_{
\begin{cases}
z_{h_{\ell-1}} = E[z_{h_{\ell-1}}] \\
W_{h_{\ell}, h_{\ell-1}} = E[W_{h_{\ell}, h_{\ell-1}}]
\end{cases}
}
\end{aligned}
\end{equation}

Inserting the derivatives and computing the inner products yields:
\begin{equation}
\begin{aligned}
&E[f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})] \approx
(|E[z_{h_{\ell-1}}]| + \epsilon)^{E[W_{h_{\ell}, h_{\ell-1}}]} \\
&+ \frac{1}{2} Var[z_{h_{\ell-1}}] (|E[z_{h_{\ell-1}}]| + \epsilon)^{E[W_{h_{\ell}, h_{\ell-1}}] - 2} E[W_{h_{\ell}, h_{\ell-1}}] (E[W_{h_{\ell}, h_{\ell-1}}] - 1) \\
&+ \frac{1}{2} Var[W_{h_{\ell}, h_{\ell-1}}] (|E[z_{h_{\ell-1}}]| + \epsilon)^{E[W_{h_{\ell}, h_{\ell-1}}]} \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2 \\
&=1 + \frac{1}{2} Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2
\end{aligned}
\label{eq:appendix:nac:forward-pass:expectation:taylor}
\end{equation}

This gives the final expectation:
\begin{equation}
\begin{aligned}
E[z_{h_\ell}] &= E\left[g(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})\right]^{H_{\ell-1}} \\
&\approx\left(1 + \frac{1}{2} Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2\right)^{H_{\ell-1}}
\end{aligned}
\end{equation}

We evaluate the error of the approximation, where $W_{h_{\ell}, h_{\ell-1}} \sim U[-r_w,r_w]$ and $z_{h_{\ell-1}} \sim U[0, r_z]$. These distributions are what is used in the  arithmetic dataset. The error is plotted in figure \ref{fig:nac-mul-expectation-estimate}.
\begin{figure}[h]
\centering
\includegraphics[width=\linewidth]{graphics/nac-mul-expectation-estimate.pdf}
\caption{Error between theoretical approximation and the numerical approximation estimated by random sampling of $100000$ observations at each combination of $r_z$ and $r_w$.}
\label{fig:nac-mul-expectation-estimate}
\end{figure}

\paragraph{Variance} The variance can be derived using the same assumptions as used in ``expectation'', that all $z_{h_{\ell-1}}$ are uncorrelated.
\begin{equation}
\begin{aligned}
Var[z_{h_\ell}] &= E[z_{h_\ell}^2] - E[z_{h_\ell}]^2 \\
&= E\left[\prod_{h_{\ell-1}=1}^{H_{\ell-1}} (|z_{h_{\ell-1}}| + \epsilon)^{2 \cdot W_{h_{\ell}, h_{\ell-1}}} \right]
- E\left[\prod_{h_{\ell-1}=1}^{H_{\ell-1}} (|z_{h_{\ell-1}}| + \epsilon)^{W_{h_{\ell}, h_{\ell-1}}}\right]^2 \\
&= E\left[f(z_{h_{\ell-1}}, 2 \cdot W_{h_{\ell}, h_{\ell-1}}) \right]^{H_{\ell-1}}
- E\left[f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})\right]^{2\cdot H_{\ell-1}}
\end{aligned}
\end{equation}

We already have from the expectation result in \eqref{eq:appendix:nac:forward-pass:expectation:taylor} that:
\begin{equation}
E\left[f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})\right] \approx 1 + \frac{1}{2} Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2
\end{equation}

By substitution of variable we have that:
\begin{equation}
\begin{aligned}
E\left[f(z_{h_{\ell-1}}, 2 \cdot W_{h_{\ell}, h_{\ell-1}})\right] &\approx 1 + \frac{1}{2} Var[2 \cdot W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2 \\
&\approx 1 + 2 \cdot Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2
\end{aligned}
\end{equation}

This gives the variance:
\begin{equation}
\begin{aligned}
Var[z_{h_\ell}] &= E\left[g(z_{h_{\ell-1}}, 2 \cdot W_{h_{\ell}, h_{\ell-1}}) \right]^{H_{\ell-1}}
- E\left[f(z_{h_{\ell-1}}, W_{h_{\ell}, h_{\ell-1}})\right]^{2\cdot H_{\ell-1}} \\
&\approx \left(1 + 2 \cdot Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2\right)^{H_{\ell-1}} \\
&- \left(1 + \frac{1}{2} \cdot Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2\right)^{2\cdot H_{\ell-1}}
\end{aligned}
\end{equation}

\subsubsection{Backward pass}

\paragraph{Expectation} The expectation of the back-propagation term assuming that $\delta_{h_{\ell+1}}$ and $\frac{\partial z_{h_{\ell+1}}}{\partial z_{h_\ell}}$ are mutually uncorrelated:
\begin{equation}
E[\delta_{h_\ell}] = E\left[\sum_{h_{\ell+1}=1}^{H_{\ell+1}} \delta_{h_{\ell+1}} \frac{\partial z_{h_{\ell+1}}}{\partial z_{h_\ell}}\right] \approx H_{\ell+1} E[\delta_{h_{\ell+1}}] E\left[\frac{\partial z_{h_{\ell+1}}}{\partial z_{h_\ell}}\right]
\end{equation}

Assuming that $z_{h_{\ell+1}}$, $W_{h_{\ell+1},h_\ell}$, and $z_{h_\ell}$ are uncorrelated:
\begin{equation}
E\left[\frac{\partial z_{h_{\ell+1}}}{\partial z_{h_\ell}}\right] \approx E[{z_{h_{\ell+1}}}] E[W_{h_{\ell+1}, h_{\ell}}] E\left[ \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right] = E[z_{h_{\ell+1}}] \cdot 0 \cdot E\left[ \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_\ell}| + \epsilon}\right] = 0
\end{equation}

\paragraph{Variance} Deriving the variance is more complicated:
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial z_{h_{\ell+1}}}{\partial z_{h_\ell}}\right] &= Var\left[z_{h_{\ell+1}} W_{h_{\ell+1}, h_{\ell}} \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right]
\end{aligned}
\end{equation}

Assuming again that $z_{h_{\ell+1}}$, $W_{h_{\ell+1},h_\ell}$, and $z_{h_\ell}$ are uncorrelated, and likewise for their second moment:
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial z_{h_{\ell+1}}}{\partial z_{h_\ell}}\right] & \approx E[z_{h_{\ell+1}}^2] E[W_{h_{\ell+1}, h_{\ell}}^2] E\left[\left( \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right)^2\right] \\
&- E[z_{h_{\ell+1}}]^2 E[W_{h_{\ell+1}, h_{\ell}}]^2 E\left[ \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right]^2 \\
&= E[z_{h_{\ell+1}}^2] Var[W_{h_{\ell+1}, h_{\ell}}] E\left[\left( \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right)^2\right] \\
&- E[z_{h_{\ell+1}}]^2 \cdot 0 \cdot E\left[ \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right]^2 \\
&= E[z_{h_{\ell+1}}^2] Var[W_{h_{\ell+1}, h_{\ell}}] E\left[\left( \frac{\mathrm{abs}'(z_{h_{\ell}})}{|z_{h_{\ell}}| + \epsilon}\right)^2\right]
\end{aligned}
\end{equation}

Using Taylor approximation around $E[z_{h_{\ell}}]$ we have:
\begin{equation}
\begin{aligned}
E\left[\left(\frac{\mathrm{abs}'(z_{h_{\ell}})}{|z| + \epsilon}\right)^2\right] &\approx\frac{1}{\left(|E[z_{h_{\ell}}]| + \epsilon\right)^2} + \frac{1}{2} \frac{6}{\left(|E[z_{h_{\ell}}]| + \epsilon\right)^4} Var[z_{h_{\ell}}] \\
&= \frac{1}{\left(|E[z_{h_{\ell}}]| + \epsilon\right)^2} + \frac{3}{\left(|E[z_{h_{\ell}}]| + \epsilon\right)^4} Var[z_{h_{\ell}}]
\end{aligned}
\end{equation}

Finally, by reusing the result for $E[z_{h_\ell}^2]$ from earlier the variance can be expressed as:
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell}}}\right] H_{\ell}\ \left(1 + 2 \cdot Var[W_{h_{\ell}, h_{\ell-1}}] \log(|E[z_{h_{\ell-1}}]| + \epsilon)^2\right)^{H_{\ell-1}} \\
&\cdot Var[W_{h_{\ell}, h_{\ell-1}}] \left(\frac{1}{\left(|E[z_{h_{\ell-1}}]| + \epsilon\right)^2} + \frac{3}{\left(|E[z_{h_{\ell-1}}]| + \epsilon\right)^4} Var[z_{h_{\ell-1}}]\right)
\end{aligned}
\end{equation}

\subsection{Expectation and variance of NMU}
\label{sec:appendix:moments:nmu}
\subsubsection{Forward pass}
\paragraph{Expectation} Assuming that all $z_{h_{\ell-1}}$ are independent:
\begin{equation}
\begin{aligned}
E[z_{h_\ell}] &= E\left[\prod_{h_{\ell-1}=1}^{H_{\ell-1}} \left(W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell} \right)\right] \\
&\approx E\left[W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell} \right]^{H_{\ell-1}} \\
&\approx \left(E[W_{h_{\ell-1},h_\ell}] E[z_{h_{\ell-1}}] + 1 - E[W_{h_{\ell-1},h_\ell}] \right)^{H_{\ell-1}}
\end{aligned}
\end{equation}

Assuming that $E[z_{h_{\ell-1}}] = 0$ which is a desired property and initializing $E[W_{h_{\ell-1},h_\ell}] = \nicefrac{1}{2}$, the expectation is:
\begin{equation}
\begin{aligned}
E[z_{h_\ell}] &\approx \left(E[W_{h_{\ell-1},h_\ell}] E[z_{h_{\ell-1}}] + 1 - E[W_{h_{\ell-1},h_\ell}] \right)^{H_{\ell-1}} \\
&\approx\left(\frac{1}{2}\cdot0 + 1 - \frac{1}{2}\right)^{H_{\ell-1}} \\
&=\left(\frac{1}{2}\right)^{H_{\ell-1}}
\end{aligned}
\end{equation}

\paragraph{Variance} Reusing the result for the expectation, assuming again that all $z_{h_{\ell-1}}$ are uncorrelated, and using the fact that $W_{h_{\ell-1},h_\ell}$ is initially independent from $z_{h_{\ell-1}}$:
\begin{equation}
\begin{aligned}
Var[z_{h_\ell}] &= E[z_{h_\ell}^2] - E[z_{h_\ell}]^2 \\
&\approx E[z_{h_\ell}^2] - \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&= E\left[\prod_{h_{\ell-1}=1}^{H_{\ell-1}}\left(W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}\right)^2\right] - \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&\approx E[\left(W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}\right)^2]^{H_{\ell-1}}- \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&= \Big(E[W_{h_{\ell-1},h_\ell}^2] E[z_{h_{\ell-1}}^2] - 2 E[W_{h_{\ell-1},h_\ell}^2] E[z_{h_{\ell-1}}]+ E[W_{h_{\ell-1},h_\ell}^2] \\
&\quad\quad + 2 E[W_{h_{\ell-1},h_\ell}] E[z_{h_{\ell-1}}] - 2 E[W_{h_{\ell-1},h_\ell}] + 1\Big)^{H_{\ell-1}}- \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}}
\end{aligned}
\end{equation}

Assuming that $E[z_{h_{\ell-1}}] = 0$, which is a desired property and initializing $E[W_{h_{\ell-1},h_\ell}] = \nicefrac{1}{2}$, the variance becomes:
\begin{equation}
\begin{aligned}
Var[z_{h_\ell}] &\approx \left(E[W_{h_{\ell-1},h_\ell}^2] \left(E[z_{h_{\ell-1}}^2] + 1\right)\right)^{H_{\ell-1}}- \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&\approx \left(\left(Var[W_{h_{\ell-1},h_\ell}] + E[W_{h_{\ell-1},h_\ell}]^2\right) \left(Var[z_{h_{\ell-1}}] + 1\right)\right)^{H_{\ell-1}}- \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&= \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1}} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1}} - \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}}
\end{aligned}
\end{equation}

\subsubsection{Backward pass}

\paragraph{Expectation} For the backward pass the expectation can, assuming that $\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}$ and $\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}$ are uncorrelated, be derived to:
\begin{equation}
\begin{aligned}
E\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right]
&= H_\ell E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}} \frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] \\
&\approx H_\ell E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] E\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] \\
&= H_\ell E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] E\left[\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}} W_{h_{\ell-1},h_\ell}\right] \\
&= H_\ell E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] E\left[\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right]E\left[W_{h_{\ell-1},h_\ell}\right]
\end{aligned}
\end{equation}

Initializing $E[W_{h_{\ell-1},h_\ell}] = \nicefrac{1}{2}$, and inserting the result for the expectation $E\left[\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right]$.
\begin{equation}
\begin{aligned}
E\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx H_\ell E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] \left(\frac{1}{2}\right)^{H_{\ell-1}-1} \frac{1}{2} \\
&= E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] H_\ell \left(\frac{1}{2}\right)^{H_{\ell-1}}
\end{aligned}
\end{equation}

Assuming that $E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] = 0$, which is a desired property \cite{glorot-initialization}.
\begin{equation}
\begin{aligned}
E\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx 0 \cdot H_\ell \cdot \left(\frac{1}{2}\right)^{H_{\ell-1}} \\
&= 0
\end{aligned}
\end{equation}

\paragraph{Variance} For the variance of the backpropagation term, we assume that $\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}$ is uncorrelated with $\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}$.
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &= H_\ell Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}} \frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] \\
&\approx H_\ell \Bigg(Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] E\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right]^2 + E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right]^2 Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] \\
&+ Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right]\Bigg)
\end{aligned}
\end{equation}

Assuming again that $E\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] = 0$, and reusing the result $E\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] = \left(\frac{1}{2}\right)^{H_{\ell-1}}$.

\begin{equation}
\begin{aligned}
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] H_\ell \left(\left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} + Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right]\right)
\end{aligned}
\end{equation}

Focusing now on $Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right]$, we have:
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] &= E\left[\left(\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right)^2\right] E[W_{h_{\ell-1},h_\ell}^2] \\
&- E\left[\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right]^2 E[W_{h_{\ell-1},h_\ell}]^2
\end{aligned}
\end{equation}

Inserting the result for the expectation $E\left[\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right]$ and Initializing again $E[W_{h_{\ell-1},h_\ell}] = \nicefrac{1}{2}$.
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] &\approx E\left[\left(\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right)^2\right] E[W_{h_{\ell-1},h_\ell}^2] \\
&- \left(\frac{1}{2}\right)^{2 \cdot \left(H_{\ell-1}-1\right)} \left(\frac{1}{2}\right)^2 \\
&= E\left[\left(\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right)^2\right] E[W_{h_{\ell-1},h_\ell}^2] \\
&- \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}}
\end{aligned}
\end{equation}

Using the identity that $E[W_{h_{\ell-1},h_\ell}^2] = Var[W_{h_{\ell-1},h_\ell}] + E[W_{h_{\ell-1},h_\ell}]^2$, and again using $E[W_{h_{\ell-1},h_\ell}] = \nicefrac{1}{2}$.
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] &\approx E\left[\left(\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right)^2\right] \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right) \\
&- \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}}
\end{aligned}
\end{equation}

To derive $E\left[\left(\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right)^2\right]$ the result for $Var[z_{h_\ell}]$ can be used, but for $\hat{H}_{\ell-1} = H_{\ell-1} - 1$, because there is one less term. Inserting $E\left[\left(\frac{z_{h_\ell}}{W_{h_{\ell-1},h_\ell} z_{h_{\ell-1}} + 1 - W_{h_{\ell-1},h_\ell}}\right)^2\right] = \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1} - 1} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1} - 1}$, we have:
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right] &\approx \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1} - 1} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1} - 1} \\
&\cdot \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right) - \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&= \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1}} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1} - 1} - \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}}
\end{aligned}
\end{equation}

Inserting the result for $Var\left[\frac{\partial z_{h_\ell}}{\partial z_{h_{\ell-1}}}\right]$ into the result for $Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right]$:
\begin{equation}
\begin{aligned}
Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_{\ell-1}}}\right] &\approx Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] H_\ell \Bigg(
\left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}} \\
&+ \left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1}} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1} - 1} - \left(\frac{1}{2}\right)^{2 \cdot H_{\ell-1}}\Bigg) \\
&= Var\left[\frac{\partial \mathcal{L}}{\partial z_{h_\ell}}\right] H_\ell \\
&\cdot \left(
\left(Var[W_{h_{\ell-1},h_\ell}] + \frac{1}{4}\right)^{H_{\ell-1}} \left(Var[z_{h_{\ell-1}}] + 1\right)^{H_{\ell-1} - 1}\right)
\end{aligned}
\label{eq:appendix:nmu:variance:result}
\end{equation}

\subsubsection{Initialization}
\label{sec:appendix:moments:nmu:initialization}

The $W_{h_{\ell-1},h_\ell}$ should be initialized with $E[W_{h_{\ell-1},h_\ell}] = \frac{1}{2}$, in order to not bias towards inclusion or exclusion of $z_{h_{\ell-1}}$. Using the derived variance approximations \eqref{eq:appendix:nmu:variance:result}, the variance should be according to the forward pass:

\begin{equation}
Var[W_{h_{\ell-1},h_\ell}] = \left((1 + Var[z_{h_\ell}])^{-H_{\ell-1}}Var[z_{h_\ell}] + (4 + 4Var[z_{h_\ell}])^{-H_{\ell-1}}\right)^{\frac{1}{H_{\ell-1}}} - \frac{1}{4}
\end{equation}

And according to the backward pass it should be:
\begin{equation}
Var[W_{h_{\ell-1},h_\ell}] = \left( \frac{ \left(Var[z_{h_\ell}] + 1\right)^{1 - H_{\ell-1}} }{H_{\ell}} \right)^{\frac{1}{H_{\ell-1}}} - \frac{1}{4}
\end{equation}

Both criteria are dependent on the input variance. If the input variance is know then optimal initialization is possible. However, as this is often not the case one can perhaps assume that $Var[z_{h_{\ell-1}}] = 1$. This is not an unreasonable assumption in many cases, as there may either be a normalization layer somewhere or the input is normalized. If unit variance is assumed, the variance for the forward pass becomes:
\begin{equation}
Var[W_{h_{\ell-1},h_\ell}] = \left(2^{-H_{\ell-1}} + 8^{-H_{\ell-1}}\right)^{\frac{1}{H_{\ell-1}}} - \frac{1}{4} = \frac{1}{8} \left(\left(4^{H_{\ell-1}} + 1\right)^{H_{\ell-1}} - 2\right)
\end{equation}

And from the backward pass:
\begin{equation}
Var[W_{h_{\ell-1},h_\ell}] = \left( \frac{ 2^{1 - H_{\ell-1}} }{H_{\ell}} \right)^{\frac{1}{H_{\ell-1}}} - \frac{1}{4}
\end{equation}

The variance requirement for both the forward and backward pass can be satisfied with  $Var[W_{h_{\ell-1},h_\ell}] = \frac{1}{4}$ for a large $H_{\ell-1}$.


\subsection{Producer nodes selection}
The selection of producers among the worker pool can be achieved for each ledger cycle using a randomised approach. Since a producer generates a ledger state update for a ledger cycle based on transactions collected during the previous ledger cycle(s), such assignment to a node should be revealed at least one cycle ahead. In fact, we use a method that reveals at the beginning of a ledger cycle $\mathcal{C}_n$ the list of nodes selected to be producers for a ledger cycle $\mathcal{C}_{n+1}$ using information available one cycle ahead ($\mathcal{C}_{n-1}$).\\

At the beginning of a ledger cycle $\mathcal{C}_n$, at time $t = t_{n,0}$, a pseudo-random number $r_{n+1}$ is drawn using the Merkel tree root of the ledger state update produced during the the cycle $\mathcal{C}_{n-1}$, as seed to the pseudo-random number generator. The random number $r_{n+1}$ is then used to define the list of workers selected to become producers for the next cycle $\mathcal{C}_{n+1}$ in the following way: for each worker node identifier $Id_i$ the quantity $u_i = Id_i \oplus r_{n+1}$ is defined, where $\oplus$ is an XOR function (for binary-based modulus addition). The list of new identifiers $\{u_i \}_{i=1,...,N}$ ($N$ is the total number of nodes in the worker pool) is sorted in ascending order and the first $P$ identifiers in that list are the identifiers of the nodes selected to be producers for the next cycle $\mathcal{C}_{n+1}$.

\subsection{Worker nodes selection}

In a large network, it can be anticipated that a large number of nodes have available resources to be used to manage the ledger database and try to join the worker pool (which translates as a high demand for work). The size of the worker pool must however be determined by security as well as economical factors. Indeed, it must be profitable for a node to join the worker pool. Said otherwise, the average number of tokens earned by a producer over a period of time should at the very least cover its operational cost. As there might be more nodes willing to work than required for the worker pool, nodes may join a secondary pool, called \textit{worker queue}, and wait to be called to join the worker pool. In order for these nodes to join the worker pool, there must be a mechanism that limits the time period during which a node can persist in the worker pool. The approach considered is to grant nodes joining the worker pool a worker pass which is valid for a limited period of time. \\

\begin{comment}
For security reasons explained in section~\ref{Sec:ConSec}, rather than defining a strict expiry time, a decay rate is used to determine the validity of a worker pass. Similarly to unstable nuclear elements the work pass has a 50\% chance to remain valid after the worker sits in the worker pool for a period of time equal to the its worker pass mean lifetime (see section~\ref{Sec:PassVal}). The decay rate is not fixed over time but instead can vary depending on the quality of work performed by the worker when acting as a producer. It can also vary to account for the demand for work, \textit{i.e.} the length of the worker queue, although a threshold is considered to mitigate the risk of a malicious entity (or group of entities) trying to simulate a highest demand for work than reality.

The list of identifiers of nodes in the worker pool can be maintained in a hash table ($DHT_w$) distributed across the network. Such table also stores the decay rate of each node worker pass. At the end of a ledger cycle, nodes in the network can therefore deduce the list of worker passes which validity has expired. Nodes on the network can update the table, freeing some slots that can be occupied by the nodes sitting in the worker queue. \\
\end{comment}

The list of identifiers of nodes in the worker pool is maintained in a hash table, $DHT_w$, distributed across the network. Such table also stores the time of issuance of the node worker pass. At the end of a ledger cycle, nodes in the network will be able to verify which worker passes are no longer valid and have expired. Nodes on the network can update the table, freeing some slots that can be occupied by the nodes sitting in the worker queue. \\

By providing proof of their available resource to the network \cite{coremark,pos}, nodes can freely apply to become workers. These nodes join the worker queue before joining the worker pool. Nodes on the network store such proof alongside the node identifier in a secondary distributed hash table, $DHT_q$. As worker nodes leave the worker pool, some nodes listed in $DHT_q$ join the worker pool. A logic described below can be implemented such that nodes with identifiers at the top of the list of nodes in $DHT_q$ are the first ones to access the worker pool and be listed in $DHT_w$. Such an approach could however be seen as potentially accommodating Sybil-identity attacks~\cite{sybil}, nevertheless expensive, if an entity controls a large number nodes at the top of the worker queue (at least equal to half the worker pool size $N/2$) and frequently adds many nodes to the worker queue such that the size of the worker queue is large enough to create an impression of a large demand for work. %The peer identification protocol on Catalyst network is designed to limit the number nodes on the same IP address entering a pool of nodes.
We therefore adapt our approach to define the dynamic of nodes leaving the worker queue and joining the worker pool that both prevents Sybil attack and incentivise nodes to join the worker queue during periods of low demand for work. We propose a method to sort out the nodes listed in the worker queue.\\

A score (or ranking) is given to a node when it joins the worker queue. The nodes in the worker queue are then ordered based on their score in descending order, \textit{i.e.} the nodes with the lowest score are at the top of the queue and are the first ones to leave the worker queue and be selected to join the worker pool when some slots are freed in the worker pool. The method to assign a score to a node joining the worker queue is not purely chronological-based. It depends on the volume of nodes trying to join the worker queue during an allotted time period $\Delta t$. Assuming $S_t$ nodes apply to join the worker queue during a window of time $[t, t+\Delta t]$. The $S_t$ nodes first register to a temporary queue, represented by a third hash table $DHT_s$. At the end of the time window, a fixed and limited number of nodes listed in $DHT_s$, $z \leq S_t$, are randomly selected. $z$ is equal to the number of nodes who left the worker pool during the previous time window $[t-\Delta t, t]$. These $z$ nodes are given a score drawn from a normal distribution centred around $R_q$, which is a predetermined threshold of the worker queue length. This means that some selected nodes may obtain a score lower than nodes currently at the bottom of the worker queue. The rest of the nodes in the temporary queue ($S_t-z$) are given a score drawn from a normal distribution centred around $R_l = R_q + s$, where $s$ is a shift proportional to the volume of nodes in $DHT_s$. Figure~\ref{fig:NSM} summarises the process of score allocation for nodes joining the worker queue. \\
%The rotation rate of nodes in the worker pool then depends on the size of the worker queue but only take into account the number of nodes with a ranking higher than a given threshold.

\newpage
\begin{landscape}
\begin{figure}
\centering
\includegraphics[width=22cm,height=42cm,keepaspectratio]{Figures/Work_Queue_Management}
\caption{\label{fig:NSM}Illustration of the process followed by Catalyst network to add nodes to the worker queue.}
\end{figure}
\end{landscape}


\documentclass[en]{elegantpaper}

\begin{document}
\section*{1}
\noindent Because $\mu_i=\beta_1+\beta_2z_i$, then
\begin{align*}
    f(y_i|\mu_i,\sigma)&=\frac{1}{\sqrt{2\pi}\sigma}\exp\left(-\frac{(y_i-(\beta_1+\beta_2z_i))^2}{2\sigma^2}\right)\\
    &=\frac{1}{\sqrt{2\pi}\sigma}\exp\left(-\frac{1}{2\sigma^2}y_i^2+\frac{\beta_1}{\sigma^2}y_i+\frac{\beta_2z_i}{\sigma^2}y_i-\frac{(\beta_1+\beta_2z_i)^2}{\sigma^2}\right).
\end{align*}
$\eta=(\frac{\beta_1}{\sigma^2}, \frac{\beta_2z_i}{\sigma^2}, -\frac{1}{\sqrt{2\pi}\sigma})$. So, $\mathcal{E}=\mathbb{R}\times\mathbb{R}\times(-\infty,0)$.
\section*{2}
    \noindent Because normal distribution is exponential family, $\bar{X}, S^2$ is the sufficient and complete (because the interior of the natural parameter space is not empty) statistics for $\mu$ and $\sigma^2$. And $\bar{X}\sim N(\mu, \sigma^2/n)$,
    \[
        Var(\bar{X})=\mathbb{E}(\bar{X}^2)-(\mathbb{E}X)^2,
    \]
    \[
        \mathbb{E}(\bar{X}^2)=\mu^2+\frac{\sigma^2}{n}.
    \]
    So, \[
        \mathbb{E}\left(\bar{X}^2-\frac{S^2}{n}\right)=\mu^2.
    \]
    From \emph{Lehman-Scheffe theorem}, we can know that $T(X)=\bar{X}^2-S^2/n$ is UMVU of $\mu^2$.
    \section*{3}
    \noindent For Beta($\alpha. \alpha$),
    \[
        f(x|\alpha)=\mathbf{1}_{0<x<1}\exp\left((\alpha-1)\log(x(1-x))-\log\left(\frac{\Gamma^2(\alpha)}{\Gamma(2\alpha)}\right)\right).
    \]
    Let $\eta=\alpha-1$, then $A(\eta)=\log\frac{\Gamma^2(\eta+1)}{\Gamma(2\eta+2)}$. And we know that $T(X)=\log(X(1-X))$ is a sufficient statistics. So,
    \[
        \begin{aligned}
            \mathbb{E}(T(X))=A'(\eta){}&=\log(\Gamma^2(\eta+1))-\log(\Gamma(2\eta+2))\\&=\frac{2\Gamma(\eta+1)\Gamma'(\eta+1)}{\Gamma^2(\eta+1)}-\frac{2\Gamma'(2\eta+2)}{\Gamma(2\eta+2)}\\
            &=\frac{2\Gamma(\alpha)\Gamma'(\alpha)}{\Gamma^2(\alpha)}-\frac{2\Gamma'(2\alpha)}{\Gamma(2\alpha)}
        \end{aligned}
    \]
    where $\Gamma'(n)=(n-1)!\left(\sum_{i=1}^{n-1}\frac{1}{i}-\gamma\right)$, $\gamma$ is Euler's constant.
    \section*{4}
    \noindent For Gamma distribution,
    \[
        f(\mathbf{x}|\alpha_0, \lambda)=\frac{\prod{x_i}^{\alpha_0-1}}{\Gamma^n(\alpha_0)}\exp\left(-\lambda\sum^n x_i+n\alpha_0\log(\lambda)\right).
    \]
    So, $T(X)=\bar{X}$ is a sufficient and complete statistics for $\lambda$. Next, we need to prove that $X_1/\bar{X}$ is an ancillary statistics.

    Let $Z\sim\Gamma(\alpha_0, 1)$, then
    \[
        X\sim\lambda^{\alpha_0}e^{-\lambda}Z, \quad \bar{X}\sim \lambda^{\alpha_0}e^{-\lambda}\bar{Z}.
    \]
    So,
    \[
        \frac{X_1}{\bar{X}}\sim\frac{\lambda^{\alpha_0}e^{-\lambda}Z_1}{\lambda^{\alpha_0}e^{-\lambda}\bar{Z}}=\frac{Z_1}{\bar{Z}}.
    \]
    This ratio is independent on $\lambda$, i.e., it is an ancillary statistics. So, from \emph{Basu theorem}, $X_1/\bar{X}$ is independent with $\bar{X}$.
    \section*{5}
    \noindent When $\eta=1$,
    \[
        f(\mathbf{x})=1\Big/\prod(x_i)\exp\left(\log\theta\sum_{i=1}^nx_i-n\theta\right),
    \]
    for $\mathbf{y}\neq\mathbf{x}$,
    \[
        \frac{f(\mathbf{x})}{f(\mathbf{y})}=\frac{\prod y_i}{\prod x_i}\exp\left(\log\theta \left(\sum_{i=1}^nx_i-\sum_{i=1}^ny_i\right)\right).
    \]
    So, $T_1(X)=\sum_{i=1}^nX_i$ is the minimal sufficient statistic for $\theta$.

    \,

    \noindent When $\eta=2$,
    \[
        f(\mathbf(x))=\theta^{\sum x_i}(1-\theta)^{n-\sum x_i},
    \]
    for $\mathbf{y}\neq\mathbf{x}$,
    \[
        \frac{f(\mathbf{x})}{f(\mathbf{y})}=\theta^{\sum x_i-\sum y_i}(1-\theta)^{\sum y_i-\sum x_i}.
    \]
    So, $T_2(X)=\sum_{i=1}^nX_i$ is a minimal sufficient statistic for $\theta$.

    \,

    \noindent Overall, $T(X)=\sum_{i=1}^{n}X_i$ is the minimal sufficient statistic for $(\theta, \eta)$.
    \section*{6}
    \noindent  For Poisson distribution,
    \[
        f(x=k|\lambda)=\frac{\lambda^k}{k!}e^{-\lambda}=1/k!\exp\left(k\log\lambda-\lambda\right),
    \]
    \[
        f(X=K|\lambda)=1\Big/\prod k_i!\exp\left(\log\lambda\sum_{i=1}^n(k_i)-n\lambda\right).
    \]
    So, \(T(X)=\sum_{i=1}^{n} X_{i}\) is sufficient and complete for \(\lambda\). Let $\log\lambda=\theta$,
    \[
        \mathbb{E}(T)=\left(ne^\theta\right)'=n\lambda.
    \]
    Hence, UMVU for $\lambda$ is $T/n$. Let $h(t)$ be UMVU for $\lambda^r$, then
    \[
        \mathbb{E}(h(T))=\sum_{i=0}^\infty\frac{h(i)n^i}{i!}\lambda^i=e^{n\lambda}\lambda^r=\sum_{i=0}^\infty\frac{(n\lambda)^i}{i!}\lambda^r.
    \]
    Then, compare the coefficients for both sides, when $t<r$,
    \[
        h(t)=0.
    \]
    But when $t\geqslant r$,
    \[
        h(t)=\frac{t!}{n^r(t-r)!}.
    \]
\end{document}


\section{Operators on Complex Vector Spaces}
\subsection{Generalized Eigenvectors and Nilpotent Operators}
  \paragraph{3.}
  \begin{proof}
    Suppose $\dim V= N$.
    \begin{align*}
      v\in G(T\inv, \lambda\inv)
      &\Leftrightarrow (T\inv v-\lambda\inv I)^nv = 0 \\
      &\Leftrightarrow
        \left(\sum_{k=0}^n\binom{n}{k}T^{-k}(-\lambda\inv)^{n-k}\right)v=0 \\
      &\Leftrightarrow
        T^n(-\lambda)^n
        \left(\sum_{k=0}^n\binom{n}{k}T^{-k}(-\lambda)^{k-n}\right)v=0 \\
      &\Leftrightarrow
        \left(\sum_{k=0}^n\binom{n}{k}T^{n-k}(-\lambda)^{k}\right)v=0 \\
      &\Leftrightarrow (T-\lambda I)^nv = 0 \\
      &\Leftrightarrow v\in G(T,\lambda).
    \end{align*}
  \end{proof}

  \paragraph{5.}
    Intuitively, $\nul T,\dots,\nul T^n$ is a sequence of subspaces where the
    preceding ones are contained by succeeding ones. And $T^{k}v$ lies in the
    additional part between two successive subspaces.
  \begin{proof}
    Note that $T^{m-1}v\ne 0$ but $T^mv=0$ implies for $k=1,\dots,m$
    \[
      T^{m-k}v\in\nul T^k,\quad T^{m-k}v\notin\nul T^{k-1}.
    \]
    Therefore, $T^{m-k}v\notin\spn(T^{m-1}v,\dots,T^{m-k+1}v)$; otherwise,
    $T^{m-k}v=x_1T^{m-1}v+\cdots+x_{m-k+1}T^{m-k+1}v$, implying that $T^{m-k}v
    \in\nul T^{k-1}$. Hence, $v, \dots, T^{m-1}v$ are linearly independent.
  \end{proof}

  \paragraph{7.}
  \begin{proof}
    It follows immediately from 8.19.
  \end{proof}

  \paragraph{9.}
  \begin{proof}
    Suppose that $v$ is nonzero and $(TS-\lambda I)v=0$. Then
    \[
      (STS-\lambda S)v=0 \quad\Rightarrow\quad
      (ST-\lambda I)Sv=0.
    \]
    If $Sv=0$, then $0=(TS-\lambda I)v=T(Sv)-\lambda v$ implies $\lambda=0$. If
    $Sv\ne 0$, then $\lambda$ is an eigenvalue of $ST$ and therefore equals $0$
    by Exercise 7. Hence, in every case, $\lambda=0$. Thus, $TS$ is also
    nilpotent.
  \end{proof}

  \paragraph{13.}
  \begin{proof}
    We are going to show that $N^{k-1}=0$ if $N^k=0$ for $k>1$ to conclude that
    $N=0$. Suppose that $N^k=0$ and let $M=N^{k-1}$. Then for every $v\in V$,
    \[
      \|M^*Mv\|^2 = \langle M^*Mv, M^*Mv\rangle
      = \langle M^*MM^*Mv,v\rangle = \langle M^*M^*MMv,v\rangle.
    \]
    Since $M^2=N^{2k-2}=0$, this implies $M^*M=0$. Hence, with the polar
    decomposition, $M=0$.
  \end{proof}

  \paragraph{15.}
  \begin{proof}
    Suppose $\dim V=n$. Since $\nul N^{n-1}\ne\nul N^n$,
    \[
      0<\dim N < \cdots < \dim N^n.
    \]
    Hence, $\dim\nul N^j=j$ for $0\le j\le n$. Since $\dim\nul N^n=n$, $\nul N^n
    =V$ and therefore $N$ is nilpotent.
  \end{proof}
% end

\subsection{Decomposition of an Operator}
  \paragraph{10.}
  \begin{proof}
    By 8.29, there exists a basis of $V$ with respect to which $\mathcal{M}(T)$
    has the form described in 8.29. Suppose $\mathcal{M}(D)$ be the diagonal
    matrix whose diagonal entries are the ones of $\mathcal{M}(T)$ and
    $\mathcal{M}(N)=\mathcal{M}(T)-\mathcal{M}(D)$. Clear that $N$ is nilpotent
    and
    \[
      \mathcal{M}(N) =
      \begin{bmatrix}
        A_1-\lambda_1 I_1 &        & 0 \\
                          & \ddots &   \\
        0                 &        & A_m-\lambda_m I_m
      \end{bmatrix}
    \]
    where each $I_k$ is the identity matrix with corresponding size. Note that
    $\lambda_k I_k$ and $A_k-\lambda_k I_k$ are commuting. Hence, by Problem 9,
    $\mathcal{M}(N)$ and $\mathcal{M}(D)$ are commuting and so do $D$ and $N$.
  \end{proof}
% end