\section{Truncations}

Truncation is a universal way of turning an arbitrary type into a $k$-truncated type. We have already seen the propositional truncation of a type $X$ in \cref{chap:image}, which is the proposition that $X$ is merely inhabited, and the set truncation of $X$ in \cref{sec:set-truncation}, which is the set of connected components of $X$. The $k$-truncation is a generalization of the propositional truncation and the set truncation to an arbitrary truncation level $k$.

We construct the truncations by recursion on $k$. The base case $k\jdeq -2$ is just the operation that sends a type $X$ to the unit type $\unit$, because up to equivalence there is only one contractible type. For the inductive step, we need to construct the $(k+1)$-truncation assuming that the $k$-truncation of an arbitrary type in a fixed universe $\UU$ exists. Our construction of the $(k+1)$-truncation is a direct generalization of the construction of the set truncation as a set quotient, where we quotient out the equivalence relation
\begin{equation*}
  (x\sim y)\defeq \trunc{-1}{x=y}.
\end{equation*}
The idea is simple: if $\trunc{k+1}{X}$ is to be the universal $(k+1)$-truncated type equipped with a map $\tproj{k+1}{\blank}:X\to \trunc{k+1}{X}$, then it has to be the case that
\begin{equation*}
  (\tproj{k+1}{x}=\tproj{k+1}{x'})\simeq \trunc{k}{x=y}.
\end{equation*}
We prove that this is indeed the case in \cref{thm:trunc_id}.

The construction of the $(k+1)$-truncation as a quotient is different than the construction of the $(k+1)$-truncation that appears in \cite{hottbook} as a higher inductive type. This construction is based on the observation that a type $X$ is $k$-truncated if and only if every map $\sphere{k+1}\to X$ is constant. In other words, for every map $f:\sphere{k+1}\to X$ into a $k$-type $X$, there is a point $x:X$ and a family of paths $p(t):x=f(t)$. If we think of $f$ as a `wheel' in $X$, then $x$ is the hub at the center of the wheel, and the paths $p(t)$ are the spokes. This leads to defining the $k$-truncation of a type $X$ by the \emph{hubs-and-spokes method}. In \cref{sec:hubs-and-spokes} we show that the $k$-truncation of a type is such a higher inductive type.

\subsection{The universal property of the truncations}

\begin{defn}\label{defn:is_truncation}
Let $X$ be a type. A map $f:X\to Y$ into an $k$-type $Y$ is said to satisfy the \define{universal property of the $k$-truncation of $X$} if the precomposition map
\begin{equation*}
\blank\circ f: (Y\to Z)\to (X\to Z)
\end{equation*}
is an equivalence for every $k$-type $Z$.
\end{defn}

\begin{rmk}
A map $f:X\to Y$ into an $k$-type $Y$ satisfies the universal property of $k$-truncation if of for every $g:X\to Z$ the type of extensions
\begin{equation*}
\begin{tikzcd}
X \arrow[dr,"g"] \arrow[d,swap,"f"] \\
Y \arrow[r,densely dotted] & Z
\end{tikzcd}
\end{equation*}
is contractible. Indeed, the type of such extensions is the type
\begin{equation*}
\sm{h:Y\to Z} h\circ f\htpy g,
\end{equation*}
which is equivalent to the fiber of the precomposition map $\blank\circ f$ at $g$.
\end{rmk}

In the following proposition we show that if a map $f:X\to Y$ into a $k$-type $Y$ satisfies the universal property of the $k$-truncation of $X$, then $f$ also satisfies the dependent elimination property.

\begin{prp}\label{thm:trunc_dup}
  Suppose the map $f:X\to Y$ into an $k$-type $Y$. The following are equivalent:
  \begin{enumerate}
  \item The map $f$ satisfies the universal property of $k$-truncation.
  \item For any family $P$ of $k$-types over $Y$, the precomposition map
    \begin{equation*}
      \blank\circ f : \Big(\prd{y:Y}P(y)\Big)\to \Big(\prd{x:X}P(f(x))\Big)
    \end{equation*}
    is an equivalence. This property is also called the \define{dependent universal property} of the $k$-truncation.
%  \item For any family $P$ of $k$-types over $Y$, the precomposition map
%    \begin{equation*}
%      \blank\circ f : \Big(\prd{y:Y}P(y)\Big)\to\Big(\prd{x:X}P(f(x))\Big)
%    \end{equation*}
%    has a section.
  \end{enumerate}
\end{prp}

\begin{proof}
  % The fact that (ii) implies (i) and (iii) is immediate, so we only have to show that both (i) and (iii) imply (ii).
  The fact that (ii) implies (i) is immediate, so we only have to prove the converse.

  % To prove that (i) implies (ii),
  Suppose $P$ is a family of $k$-truncated types over $Y$.
  Then we have a commuting square
  \begin{equation*}
    \begin{tikzcd}
      \Big(Y\to\sm{y:Y}P(y)\Big) \arrow[r,"\blank\circ f"] \arrow[d,swap,"\proj 1 \circ\blank"] & \Big(X\to \sm{y:Y}P(y)\Big) \arrow[d,"\proj 1\circ \blank"] \\
      \Big(Y\to Y\Big) \arrow[r,swap,"\blank\circ f"] & \Big(X\to Y)
    \end{tikzcd}
  \end{equation*}
  Since the total space $\sm{y:Y}P(y)$ is again $k$-truncated by \cref{ex:istrunc_sigma}, it follows by the universal property of the $k$-truncation that the top map is an equivalence, and by the universal property the bottom map is an equivalence too. It follows from \cref{cor:pb_equiv} that this square is a pullback square, so it induces equivalences on the fibers by \cref{cor:pb_fibequiv}. In particular we have a commuting square
  \begin{equation*}
    \begin{tikzcd}
      \Big(\prd{y:Y}P(y)\Big) \arrow[r] \arrow[d] & \Big(\prd{x:X}P(f(x))\Big) \arrow[d] \\
      \fib{(\proj 1\circ \blank)}{\idfunc[Y]} \arrow[r] & \fib{(\proj 1 \circ \blank)}{f}
    \end{tikzcd}
  \end{equation*}
  in which the left and right maps are equivalences by \cref{ex:pi_sec}, and the bottom map is an equivalence as we have just established. Therefore the top map is an equivalence, so we conclude that $f$ satisfies the dependent universal property.
%
%  To see that (iii) implies (ii), consider a family $P$ of $k$-types over $Y$. Our goal is to show that the precomposition function
%  \begin{equation*}
%    \blank\circ f : \Big(\prd{y:Y}P(y)\Big)\to\Big(\prd{x:X}P(f(x))\Big)
%  \end{equation*}
%  is an equivalence. It has a section by assumption, for which we will write $\varphi$. It therefore remains to show that $\varphi(h\circ f)\htpy h$ for any dependent function $h:\prd{y:Y}P(y)$. Note that the precomposition function
%  \begin{equation*}
%    \blank\circ f : \Big(\prd{y:Y}\varphi(h\circ f)(y)=h(y)\Big)\to\Big(\prd{x:X}\varphi(h\circ f)(f(x))=h(x)\Big)
%  \end{equation*}
%  has a section, so it suffices to show that $\varphi(h\circ f)\circ f\htpy h\circ f$. This follows again from the assumption that $\varphi$ is a section of $\blank\circ f$.
\end{proof}

Just as for pullbacks, pushouts, and the many other types characterized by a universal property, the $k$-truncation of a type is unique once it exists. We prove this in the following proposition and its corollary.

\begin{prp}
  Consider a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=tiny]
      \phantom{Y'} & X \arrow[dl,swap,"f"] \arrow[dr,"{f'}"] \\
      Y \arrow[rr,swap,"h"] & & Y'
    \end{tikzcd}
  \end{equation*}
  where $Y$ and $Y'$ are assumed to be $k$-types. If any two of the following three properties hold, so does the third:
  \begin{enumerate}
  \item The map $f:X\to Y$ satisfies the universal property of the $k$-truncation of $X$.
  \item The map $f':X\to X'$ satisfies the universal property of the $k$-truncation of $X$.
  \item The map $h$ is an equivalence.
  \end{enumerate}
\end{prp}

\begin{proof}
  The claim follows by the 3-for-2 property of equivalences, since we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=tiny]
      Z^{Y'} \arrow[rr,"\blank\circ h"] \arrow[dr,swap,"{\blank\circ f'}"] & & Z^Y \arrow[dl,"\blank\circ f"] \\
      & Z^X & \phantom{Z^{Y'}}
    \end{tikzcd}
  \end{equation*}
  for any $k$-type $Z$.
\end{proof}

\begin{cor}
  Consider two maps $f:X\to Y$ and $f':X\to Y'$ into $k$-types $Y$ and $Y'$, and suppose that both $f$ and $f'$ satisfy the universal property of the $k$-truncation of $X$. Then the type of equivalences $e:Y\simeq Y'$ equipped with a homotopy witnessing that the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=tiny]
      \phantom{Y'} & X \arrow[dl,swap,"f"] \arrow[dr,"{f'}"] \\
      Y \arrow[rr,swap,"e"] & & Y'
    \end{tikzcd}
  \end{equation*}
  commutes is contractible.
\end{cor}

\subsection{The construction of the  \texorpdfstring{$(k+1)$}{(k+1)}-truncation as a quotient}

\begin{defn}
  Consider a universe $\UU$. We say that $\UU$ \define{has $k$-truncations} if for every type $X:\UU$ there is a map $f:X\to Y$ into a $k$-type $Y:\UU$ that satisfies the universal property of the $k$-truncation of $X$.
\end{defn}

\begin{rmk}
  Note that the universal property of $k$-truncations is formulated with respect to all $k$-types, and not only with respect to the $k$-types in $\UU$.
\end{rmk}

We will use the following proposition to prove the universal property of the $(k+1)$-truncation. In fact, the converse of the following proposition also holds, and we prove it below in \cref{thm:is-truncation}.

\begin{prp}\label{prp:is-truncation}
  Consider a map $f:X\to Y$ into a $(k+1)$-type $Y$. If $f$ is surjective, and its action on paths
  \begin{equation*}
    \apfunc{f}:(x=x')\to(f(x)=f(x'))
  \end{equation*}
  satisfies the universal property of the $k$-truncation of $x=x'$, then $f$ satisfies the universal property of the $(k+1)$-truncaton of $X$.
\end{prp}

\begin{proof}
  Consider a map $g:X\to Z$ into a $(k+1)$-type $Z$. Our goal is to show that $g$ extends uniquely along $f$ to a map $h:Y\to Z$. We claim that for any $y:Y$, the type of extensions
  \begin{equation*}
    \begin{tikzcd}
      \fib{f}{y} \arrow[d] \arrow[dr,"g\circ\proj 1"] \\
      \unit \arrow[r,densely dotted] & Z
    \end{tikzcd}
  \end{equation*}
  is contractible. In other words, on each of the fibers of $f$, the map $g$ is constant in a unique way. Since $f$ is assumed to be surjective, it follows by \cref{prp:surjective} that it suffices to prove the above extension property for $y\jdeq f(x)$, for each $x:X$. In other words, we have to show that the type
  \begin{equation*}
    \sm{z:Z}\prd{x':X}(f(x)=f(x'))\to (z=g(x'))
  \end{equation*}
  is contractible for each $x:X$.

  Note that the type $z=g(x')$ is $k$-truncated, and that the map $\apfunc{f}$ is assumed to satisfy the universal property of the $k$-truncation of $x=x'$. Therefore it is equivalent to show that the type
  \begin{equation*}
    \sm{z:Z}\prd{x':X}(x=x')\to (z=g(x'))
  \end{equation*}
  is contractible. This is immediate by the universal property of the identity type (\cref{thm:yoneda}), and the fact that $\sm{z:Z}z=g(x')$ is contractible (\cref{cor:contr_path}).

  It follows by \cref{thm:funext_wkfunext} that the product
  \begin{equation*}
    \prd{y:Y}\sm{z:Z}\prd{x:X}(y=f(x))\to (z=g(x))
  \end{equation*}
  is contractible. Since $\Pi$ distributes over $\Sigma$ by \cref{thm:choice}, we obtain that the type of functions $h:Y\to Z$ equipped with a homotopy $h\circ f\htpy g$ is contractible.
\end{proof}

Before we show that any universe has $k$-truncations for arbitrary $k$, we prove a truncated version of the type theoretic Yoneda lemma under the assumption that $\UU$ has $k$-truncations for a given $k$.

\begin{lem}\label{lem:truncated-yoneda}
  Suppose $\UU$ is a universe that has $k$-truncations
  \begin{equation*}
    \tproj{k}{\blank}:X\to\trunc{k}{X}
  \end{equation*}
  for a given $k\geq-2$, and consider a family $P$ of types over $X$. We make two claims:
  \begin{enumerate}
  \item The evaluation function
    \begin{equation*}
      \Big(\prd{y:X}\trunc{k}{x=y}\to\trunc{k}{P(y)}\Big)\to \trunc{k}{P(x)}
    \end{equation*}
    given by $h\mapsto h_x(\tproj{k}{\refl{x}})$, is an equivalence.
  \item If the total space of $P$ is contractible, then the evaluation function
    \begin{equation*}
      \Big(\prd{y:X}\trunc{k}{x=y}\simeq\trunc{k}{P(y)}\Big)\to \trunc{k}{P(x)}
    \end{equation*}
    given by $e\mapsto e_x(\tproj{k}{\refl{x}})$, is an equivalence.
  \end{enumerate}
\end{lem}

\begin{proof}
  The first claim follows immediately by the universal property of the $k$-truncation of $x=y$ and the type theoretical Yoneda lemma (\cref{thm:yoneda}).

  To prove the second claim, we first observe that the inclusion of equivalences into all maps induces an embedding that fits in a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      \Big(\prd{y:X}\trunc{k}{x=y}\simeq\trunc{k}{P(y)}\Big) \arrow[rr,hook] \arrow[dr,swap,"\ev_{\tproj{k}{\refl{x}}}"] & & \Big(\prd{y:X}\trunc{k}{x=y}\to\trunc{k}{P(y)}\Big) \arrow[dl,"\ev_{\tproj{k}{\refl{x}}}"] \\
      & \trunc{k}{P(x)}.
    \end{tikzcd}
  \end{equation*}
  The evaluation map on the right is an equivalence, and we have to show that if the total space $\sm{y:X}P(y)$ is contractible, then the evaluation map on the left is an equivalence. We do this by showing that the top map is an equivalence.

  To see this, note that we have a commuting diagram
    \begin{equation*}
    \begin{tikzcd}
      \Big(\prd{y:X}(x=y)\simeq P(y)\Big) \arrow[d,swap,"e\mapsto\lam{y}\trunc{k}{e_y}"] \arrow[r,hook] & \Big(\prd{y:X}(x=y)\to P(y)\Big) \arrow[r,"\evrefl"] \arrow[d,swap,"h\mapsto\lam{y}\trunc{k}{h_y}"] &[2em] P(x) \arrow[d] \\
      \Big(\prd{y:X}\trunc{k}{x=y}\simeq\trunc{k}{P(y)}\Big) \arrow[r,hook] & \Big(\prd{y:X}\trunc{k}{x=y}\to\trunc{k}{P(y)}\Big) \arrow[r,swap,"\ev_{\tproj{k}{\refl{x}}}"] & \trunc{k}{P(x)}
    \end{tikzcd}
  \end{equation*}
  In the top row of this diagram we have a concatenation of equivalences: the first map is an equivalence by the fundamental theorem of identity types, and the second map is an equivalence by \cref{thm:yoneda}. The second map in the bottom row is an equivalence by the first claim of this lemma. Therefore it follows that the vertical map in the middle satisfies the universal property of the $k$-truncation. Since the type at the bottom left is $k$-truncated, we obtain by the universal property of the $k$-truncation a section of the embedding in the bottom row, which proves the claim.
\end{proof}

\begin{thm}
  Any univalent universe $\UU$ that is closed under pushouts has $k$-truncations, for every truncation level $k$. We will write
  \begin{equation*}
    \tproj{k}{\blank}:X\to\trunc{k}{X}
  \end{equation*}
  for the $k$-truncation of $X$.
\end{thm}

\begin{proof}
  It is easy to see that the terminal projection $X\to\unit$ is a $(-2)$-truncation, for any type $X$. Thus, any universe has $(-2)$-truncations.

  We will proceed by induction on $k$. Our inductive hypothesis is that $\UU$ has $k$-truncations, and our goal is to show that $\UU$ has $(k+1)$-truncations. The idea of the construction is very similar to the construction of the set quotient by an equivalence relation. Consider the type-valued relation $R_k:X\to (X\to \UU)$ given by
  \begin{equation*}
    R_k(x,x') \defeq \trunc{k}{x=x'}.
  \end{equation*}
  Analogous to the definition of set quotients, we define
  \begin{equation*}
    \trunc{k+1}{X}\defeq \im(R_k),
  \end{equation*}
  which comes equipped with a surjective map $q:X\to\trunc{k+1}{X}$. Note that the image of $R:X\to(X\to\UU)$ is (essentially) small by \cref{thm:replacement}. To see that $q:X\to\trunc{k+1}{X}$ satisfies the universal property of the $(k+1)$-truncation of $X$, we apply \cref{prp:is-truncation}. Therefore it remains to show that the action on paths
  \begin{equation*}
    \apfunc{q}:(x=x')\to (q(x)=q(x'))
  \end{equation*}
  satisfies the universal property of the $k$-truncation of $x=x'$. Since $q$ is the surjective map in the image factorization of $R_k$, it is equivalent to show that the action on paths
  \begin{equation*}
    \apfunc{R}:(x=x')\to (R_k(x)=R_k(x'))
  \end{equation*}
  satisfies the universal property of the $k$-truncation of $x=x'$. Note that we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=-2em]
      \phantom{\prd{y:X}\trunc{k}{x=y}\simeq\trunc{k}{x'=y},} & (x=x') \arrow[dl,swap,"\apfunc{R_k}"] \arrow[dr] \\
      \Big(R_k(x)=R_k(x')\Big) \arrow[rr,swap,"\simeq"] & & \Big(\prd{y:X}\trunc{k}{x=y}\simeq\trunc{k}{x'=y}\Big),
    \end{tikzcd}
  \end{equation*}
  where the bottom map is an equivalence by function extensionality and the univalence axiom. Therefore it suffices to show that the map on the right of this triangle, which is the unique map that sends $\refl{x}$ to the family of identity equivalences, satisfies the universal property of the $k$-truncation of $x=x'$.

  This map fits in a commuting square
  \begin{equation*}
    \begin{tikzcd}[column sep=huge]
      (x=x') \arrow[d] \arrow[r,"\tproj{k}{\blank}"] & \trunc{k}{x=x'} \arrow[d,"\trunc{k}{\invfunc}"] \\
      \Big(\prd{y:X}\trunc{k}{x=y}\simeq\trunc{k}{x'=y}\Big) \arrow[r,swap,"\ev_{\tproj{k}{\refl{x}}}"] & \trunc{k}{x'=x}.
    \end{tikzcd}
  \end{equation*}
  The map on the right is an equivalence because $\invfunc:(x=x')\to(x'=x)$ is an equivalence. The bottom map is an equivalence by \cref{lem:truncated-yoneda}. The top map satisfies the universal property of the $k$-truncation of $x=x'$, hence so does the map on the left, which completes the proof.
\end{proof}

\begin{thm}\label{thm:is-truncation}
  Consider a map $f:X\to Y$ into a $(k+1)$-truncated type $Y$. Then the following are equivalent:
  \begin{enumerate}
  \item The map $f$ satisfies the universal property of the $(k+1)$-truncation of $X$.
  \item The map $f$ is surjective, and for each $x,x':X$ the map
    \begin{equation*}
      \apfunc{f} : (x=x')\to(f(x)=f(x'))
    \end{equation*}
    satisfies the universal property of the $k$-truncation of $x=x'$.
  \end{enumerate}
\end{thm}

\begin{proof}
  The fact that (ii) implies (i) was established in \cref{prp:is-truncation}, so it suffices to show that (i) implies (ii).

  Suppose first that the map $f$ satisfies the universal property of the $(k+1)$-truncation, and let $x:X$. Recall from \cref{ex:istrunc_UUtrunc} that the universe of $k$-truncated types is itself $(k+1)$-truncated. Therefore it follows that the map $x'\mapsto \trunc{k}{x=x'}$ has a unique extension
  \begin{equation*}
    \begin{tikzcd}
      X \arrow[dr,"{x'\mapsto\trunc{k}{x=x'}}"] \arrow[d,swap,"f"] \\
      Y \arrow[r,densely dotted,swap,"P"] & \UU_{\leq k}.
    \end{tikzcd}
  \end{equation*}
  In other words, we obtain a unique family $P$ of $k$-types over $Y$ equipped with equivalences
  \begin{equation*}
    e_{x'}:P(f(x'))\simeq \trunc{k}{x=x'}
  \end{equation*}
  indexed by $x':X$. In particular, $P$ comes equipped with a point $p_0:P(f(x))$ such that $e_x(p_0)=\tproj{k}{\refl{x}}$. Hence we obtain a family of maps
  \begin{equation*}
    \prd{y:Y} (f(x)=y)\to P(y).
  \end{equation*}
  We claim that this is a family of equivalences. By the fundamental theorem of identity types, \cref{thm:id_fundamental}, it suffices to show that the total space
  \begin{equation*}
    \sm{y:Y}P(y)
  \end{equation*}
  is contractible. We have $(f(x),p_0)$ at the center of contraction, so we have to construct a contraction
  \begin{equation*}
    \prd{y:Y}{p:P(y)} (f(x),p_0)=(y,p).
  \end{equation*}
  Now we observe that the type $\sm{y:Y}P(y)$ is $(k+1)$-truncated, using the fact that any $\Sigma$-type of a family of $k$-types over a $(k+1)$-type is again $(k+1)$-truncated (\cref{ex:istrunc_sigma}). It follows that the type $(f(x),p_0)=(y,p)$ is $k$-truncated for each $y:Y$ and each $p:P(y)$. Now we use the dependent universal property of the $k$-truncation of $X$, which was proven in \cref{thm:trunc_dup}, so it suffices to show that
  \begin{equation*}
    \prd{x':X}{p:P(f(x'))} (f(x),p_0)=(f(x'),p).
  \end{equation*}
  Since we have an equivalence $e_{x'}:P(f(x'))\simeq \trunc{k}{x=x'}$ for each $x':X$, it is equivalent to show that
  \begin{equation*}
    \prd{x':X}{p:\trunc{k}{x=x'}}(f(x),p_0)=(f(x'),e_{x'}^{-1}(p)).
  \end{equation*}
  Again, we use that the type of paths $(f(x),p_0)=(f(x'),e_{x'}^{-1}(p))$ is a $k$-type, so we use \cref{thm:trunc_dup} to conclude that it suffices to show that
  \begin{equation*}
    \prd{x':X}{p:x=x'}(f(x),p_0)=(f(x'),e_{x'}^{-1}(p)).
  \end{equation*}
  This is immediate by path induction on $p:x=x'$. This proves the claim that the canonical map
  \begin{equation*}
    h_y:(f(x)=y)\to P(y)
  \end{equation*}
  is an equivalence for each $y:Y$. Now observe that we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      \phantom{\trunc{k}{x=x'}} & (x=x') \arrow[dl,swap,"\apfunc{f}"] \arrow[dr,"\tproj{k}{\blank}"] & \phantom{(f(x)=f(x'))} \\
      (f(x)=f(x')) \arrow[rr,swap,"h_{f(x')}"] & & \trunc{k}{x=x'}
    \end{tikzcd}
  \end{equation*}
  for each $x':X$. Therefore it follows that $\apfunc{f}:(x=x')\to(f(x)=f(x'))$ satisfies the universal property of the $k$-truncation of $x=x'$.
\end{proof}

\begin{cor}\label{thm:trunc_id}
For any $x,y:X$, there is an equivalence
\begin{equation*}
\eqv{(\tproj{k+1}{x}=\tproj{k+1}{y})}{\trunc{k}{x=y}}.
\end{equation*}
\end{cor}

\subsection{The truncations as recursive higher inductive types}\label{sec:hubs-and-spokes}

Recall from \cref{thm:trunc_ap} that a map $f:A\to B$ is $(k+1)$-truncated if and only if the action on paths
\begin{equation*}
  \apfunc{f}:(x=y)\to(f(x)=f(y))
\end{equation*}
is a $k$-truncated map, for each $x,y:A$. Moreover, in \cref{ex:trunc_diagonal_map} we established that the fibers of the diagonal map $\delta_f:A\to A\times_BA$ are equivalent to the fibers of the maps $\apfunc{f}$, so it is also the case that $f$ is $(k+1)$-truncated if and only if the diagonal $\delta_f$ is $k$-truncated.

In the following theorem, we add yet another equivalent characterization to the truncatedness of a map. We will use this theorem in two ways. First, a simple corollary gives a useful characterization of $k$-truncated types. Second, we will use this theorem to derive an elimination principle of the $(k+1)$-sphere that can be applied to families of $k$-types

\begin{thm}
  Consider a map $f:A\to B$. Then the following are equivalent:
  \begin{enumerate}
  \item The map $f$ is $k$-truncated.
  \item The commuting square
    \begin{equation*}
      \begin{tikzcd}[column sep=large]
        A \arrow[r,"f"] \arrow[d,swap,"\lam{x}\const_x"] & B \arrow[d,"\lam{y}\const_y"] \\
        A^{\sphere{k+1}} \arrow[r,swap,"f^{\sphere{k+1}}"] & B^{\sphere{k+1}}
      \end{tikzcd}
    \end{equation*}
    is a pullback square.
  \end{enumerate}
\end{thm}

\begin{proof}
  We prove the claim by induction on $k\geq-2$. The base case is clear, because the map $A^{\sphere{-1}}\to B^{\sphere{-1}}$ is a map between contractible types, hence an equivalence. Therefore the square
  \begin{equation*}
    \begin{tikzcd}
        A \arrow[r] \arrow[d] & B \arrow[d] \\
        A^{\sphere{-1}} \arrow[r] & B^{\sphere{-1}}
    \end{tikzcd}
  \end{equation*}
  is a pullback square if and only if $A\to B$ is an equivalence.

  For the inductive step, assume that for any map $g:X\to Y$, the map $g$ is $k$-truncated if and only if the square
  \begin{equation*}
    \begin{tikzcd}
      X \arrow[r] \arrow[d] & Y \arrow[d] \\
      X^{\sphere{k+1}} \arrow[r] & Y^{\sphere{k+1}}
    \end{tikzcd}
  \end{equation*}
  is a pullback square, and consider a map $f:A\to B$. Then $f$ is $(k+1)$-truncated if and only if $\apfunc{f}:(x=y)\to(f(x)=f(y))$ is $k$-truncated for each $x,y:A$. By the inductive hypothesis this happens if and only if the square
  \begin{equation*}
    \begin{tikzcd}
      (x=y) \arrow[r] \arrow[d] & (f(x)=f(y)) \arrow[d] \\
      (x=y)^{\sphere{k+1}} \arrow[r] & (f(x)=f(y))^{\sphere{k+1}}
    \end{tikzcd}
  \end{equation*}
  is a pullback square, for each $x,y:A$. Now we observe that this is the case if and only if the square on the left in the diagram
  \begin{equation*}
    \begin{tikzcd}[column sep=small]
      \sm{x,y:A}x=y \arrow[r] \arrow[d] & \sm{x,y:A}(f(x)=f(y)) \arrow[d] \arrow[r] & \sm{x,y:B}x=y \arrow[d] \\
      \sm{x,y:A}(x=y)^{\sphere{k+1}} \arrow[r] & \sm{x,y:A}(f(x)=f(y))^{\sphere{k+1}} \arrow[r] & \sm{x,y:B}(x=y)^{\sphere{k+1}}
    \end{tikzcd}
  \end{equation*}
  is a pullback square. The square on the right is a pullback square, so the square on the left is a pullback if and only if the outer rectangle is a pullback. By the universal property of $\sphere{k+2}$ it follows that the outer rectangle is a pullback if and only if the square
  \begin{equation*}
    \begin{tikzcd}
      A \arrow[r] \arrow[d] & B \arrow[d] \\
      A^{\sphere{k+2}} \arrow[r] & B^{\sphere{k+2}}
    \end{tikzcd}
  \end{equation*}
  is a pullback.
\end{proof}

\begin{thm}\label{thm:truncated}
  Consider a type $A$. Then the following are equivalent:
  \begin{enumerate}
  \item The type $A$ is $k$-truncated.
  \item The map
    \begin{equation*}
      \lam{x}\const_x:A\to (\sphere{k+1}\to A)
    \end{equation*}
    is an equivalence.
  \end{enumerate}
\end{thm}

\begin{proof}
  We prove the claim by induction on $k\geq-2$. The base case is clear, because the map $A^{\sphere{-1}}$ is contractible.

  For the inductive step, assume that any type $X$ is $k$-truncated if and only if the map
  \begin{equation*}
    \lam{x}\const_x:X\to (\sphere{k+1}\to X)
  \end{equation*}
  is an equivalence.
  Then $A$ is $(k+1)$-truncated if and only if its identity types $x=y$ are $k$-truncated, for each $x,y:A$. By the inductive hypothesis this happens if and only if
  \begin{equation*}
    (x=y)\to (\sphere{k+1}\to (x=y))
  \end{equation*}
  is a family of equivalences indexed by $x,y:A$. This is a family of equivalences if and only if the induced map on total spaces
  \begin{equation*}
    \Big(\sm{x,y:A}x=y\Big)\to\Big(\sm{x,y:A}(x=y)^{\sphere{k+1}}\Big)
  \end{equation*}
  is an equivalence. Note that we have a commuting square
  \begin{equation*}
    \begin{tikzcd}
      A \arrow[r] \arrow[d] & A^{\sphere{k+2}} \arrow[d] \\
      \Big(\sm{x,y:A}x=y\Big) \arrow[r] & \Big(\sm{x,y:A}(x=y)^{\sphere{k+1}}\Big)
    \end{tikzcd}
  \end{equation*}
  in which both vertical maps are equivalences. Therefore the top map is an equivalence if and only if the bottom map is an equivalence, which completes the proof.
\end{proof}

\begin{proof}
  Immediate from the fact that $A$ is $k$-truncated if and only if the map $A\to\unit$ is $k$-truncated.
\end{proof}

\begin{defn}
  Consider a type $X$. A \textbf{$k$-truncation} of $X$ consist of a $k$-type $Y$, and a map $f:X\to Y$ satisfying the \textbf{universal property of $k$-truncation}, that for every $k$-type $Z$ the precomposition map
  \begin{equation*}
    \blank\circ f: (Y\to Z)\to (X\to Z)
  \end{equation*}
  is an equivalence.
\end{defn}

We define $\trunc{k}{X}$ by the `hubs-and-spokes' method, as a higher inductive type. The idea is to force any map $\sphere{k}{X}\to \trunc{k}{X}$ to be homotopic to a constant function by including enough points (the hubs) for the values of these constant functions, and enough paths (the spokes) for the homotopies to these constant functions.

\begin{defn}
  For any type $X$ we define a type $\trunc{k}{X}$ as a higher inductive type, with constructors
  \begin{align*}
    \eta & : X \to \trunc{k}{X}. \\
    \mathsf{hub} & : (\sphere{k+1}\to\trunc{k}{X})\to\trunc{k}{X} \\
    \mathsf{spoke} & : \prd{f:\sphere{k+1}\to\trunc{k}{X}}\prd{t:\sphere{k+1}}f(t)=\mathsf{hub}(f).
  \end{align*}
\end{defn}

\begin{rmk}
  The induction principle for $\trunc{k}{X}$ asserts that for any family $P$ of types over $\trunc{k}{X}$, if we have a dependent function $\alpha : \prd{x:X}P(\eta(x))$ and a dependent function
  \begin{equation*}
    \beta : \prd{f:\sphere{k+1}\to\trunc{k}{X}}\Big(\prd{t:\sphere{k+1}}P(f(t))\Big)\to P(\mathsf{hub}(f))
  \end{equation*}
  equipped with an identification
  \begin{equation*}
    \gamma(f,g,t):\tr_P(\mathsf{spoke}(f,t),g(t))=\beta(f,g),
  \end{equation*}
  for every $f:\sphere{k+1}\to\trunc{k}{X}$, $g:\prd{t:\sphere{k+1}}P(f(t))$, and every $t:\sphere{k+1}$, then we obtain a dependent function
  \begin{equation*}
    h:\prd{x:\trunc{k}{X}}P(\eta(x))
  \end{equation*}
  equipped with an identification $H(x):h(\eta(x))=\alpha(x)$ for any $x:X$.
\end{rmk}

\begin{prp}
  For any type $X$, the type $\trunc{k}{X}$ is $k$-truncated.
\end{prp}

\begin{proof}
  By \cref{thm:truncated} it suffices to show that the map
  \begin{equation*}
    \delta\defeq\lam{x}\const_x:\trunc{k}{X}\to (\sphere{k+1}\to\trunc{k}{X})
  \end{equation*}
  is an equivalence. Note that the inverse of this map is simply the map
  \begin{equation*}
    \mathsf{hub}: (\sphere{k+1}\to\trunc{k}{X})\to \trunc{k}{X},
  \end{equation*}
  which is a section of $\delta$ by the homotopy $\mathsf{spoke}$. Therefore it remains to show that
  \begin{equation*}
    \mathsf{hub}(\const_x)=x.
  \end{equation*}
  for every $x:\trunc{k}{X}$. Note that $\mathsf{spoke}(\const_x,\mathsf{hub}(\const_x))^{-1}$ is such an identification.
\end{proof}

Recall that the $(k+1)$-sphere is $k$-connected in the following sense.

\begin{lem}\label{lem:sphere-connected}
  For any family $P$ of $k$-types over $\sphere{k+1}$, the evaluation map at the base point
  \begin{equation*}
    \ev_\ast : \Big(\prd{t:\sphere{k+1}}P(t)\Big)\to P(\ast)
  \end{equation*}
  is an equivalence.
\end{lem}

\begin{thm}
  For any family $P$ of $k$-types over $\trunc{k}{X}$, the function
  \begin{equation*}
    \blank\circ\eta:\Big(\prd{x:\trunc{k}{X}}P(x)\Big)\to\Big(\prd{x:X}P(\eta(x))\Big)
  \end{equation*}
  is an equivalence.
\end{thm}

\begin{proof}
  We first show that for any family $P$ of $k$-types over $\trunc{k}{X}$, the function
  \begin{equation*}
    \blank\circ\eta:\Big(\prd{x:\trunc{k}{X}}P(x)\Big)\to\Big(\prd{x:X}P(\eta(x))\Big)
  \end{equation*}
  has a section. To see this, we apply the induction principle of $\trunc{k}{X}$. For any function $\alpha:\prd{x:X}P(\eta(x))$ we need to construct a function $h:\prd{x:\trunc{k}{X}}P(x)$ such that $h\circ\eta\htpy\alpha$, so it suffices to show that the $k$-truncatedness of the types in the family $P$ imply the existence of the terms $\beta$ and $\eta$ of the induction principle of $\trunc{k}{X}$. In other words, we need to show that for every $f:\sphere{k+1}\to\trunc{k}{X}$ and every $g:\prd{t:\sphere{k+1}}P(f(t))$ there are
  \begin{align*}
    \beta(f,g) & : P(\mathsf{hub}(f)) \\
    \gamma(f,g) & : \prd{t:\sphere{k+1}}\tr_P(\mathsf{spoke}(f,t),g(t))=\beta(f,g).
  \end{align*}
  Since we have already shown that $\trunc{k}{X}$ is $k$-truncated, it suffices to show the above for $f\defeq\const_x$, for any $x:\trunc{k}{X}$. Now the type of $g$ is just the function type $\sphere{k+1}\to P(x)$, so by the truncatedness of $P(x)$ it suffices to construct
  \begin{align*}
    \beta(\const_x,\const_y) & : P(\mathsf{hub}(\const_x)) \\
    \gamma(\const_x,\const_y) & : \prd{t:\sphere{k+1}}\tr_P(\mathsf{spoke}(\const_x,t),y)=\beta(\const_x,\const_y)
  \end{align*}
  for any $x:X$ and $y:P(x)$. Now we simply define
  \begin{equation*}
    \beta(\const_x,\const_y) \defeq \tr_P(\mathsf{spoke}(\const_x,\ast),y).
  \end{equation*}
  Then it remains to construct an identification
  \begin{equation*}
    \tr_P(\mathsf{spoke}(\const_x,t),y)=\tr_P(\mathsf{spoke}(\const_x,\ast),y)
  \end{equation*}
  for any $t:\sphere{k+1}$, but this follows at once from \cref{lem:sphere-connected}, because the identity types of a $k$-truncated type is again $k$-truncated. This completes the proof that the precomposition function
  \begin{equation*}
    \blank\circ\eta:\Big(\prd{x:\trunc{k}{X}}P(x)\Big)\to\Big(\prd{x:X}P(\eta(x))\Big)
  \end{equation*}
  has a section $s$ for every family $P$ of $k$-types over $\trunc{k}{X}$.

  To show that it is an equivalence, we have to show that $s$ is also a retraction of the precomposition function $\blank\circ\eta$, i.e., we have to show that
  \begin{equation*}
    s(h\circ\eta)= h
  \end{equation*}
  for any $h:\prd{x:\trunc{k}{X}}P(x)$. By function extensionality, it is equivalent to show that
  \begin{equation*}
    \prd{x:\trunc{k}{X}}s(h\circ\eta)(x)=h(x).
  \end{equation*}
  Now we observe that the type $s(h\circ\eta)(x)=h(x)$ is a $k$-type, and therefore we already know that the function
  \begin{equation*}
    \blank\circ\eta:\Big(\prd{x:\trunc{k}{X}}s(h\circ\eta)(x)=h(x)\Big)\to \Big(\prd{x:X}s(h\circ\eta)(\eta(x))=h(\eta(x))\Big)
  \end{equation*}
  has a section. In other words, it suffices to construct a dependent function of type
  \begin{equation*}
    \prd{x:X}s(h\circ\eta)(\eta(x))=h(\eta(x)).
  \end{equation*}
  Here we simply use that $s$ is a section $\blank\circ\eta$, and we are done.
\end{proof}

\begin{cor}\label{cor:k-type-is-reflective-subuniverse}
  For any type $X$, the map $\eta:X\to\trunc{k}{X}$ satisfies the universal property of $k$-truncation.
\end{cor}

\begin{comment}
\subsection{The join extension and connectivity theorems}

\begin{defn}\label{defn:local}
For a given type $M$, a type $A$ is said to be \define{$M$-null} if the map
\begin{equation*}
\lam{a}{m}a : A \to (M \to A)
\end{equation*}
is an equivalence.
\end{defn}

In other words, the type $A$ is $M$-null if each $f:M\to A$ has a unique extension along the
map $M\to\unit$, as indicated in the diagram
\begin{equation*}
\begin{tikzcd}
M \arrow[r,"f"] \arrow[d] & A \\
\unit. \arrow[ur,densely dotted]
\end{tikzcd}
\end{equation*}
Note that being $M$-null in the above sense is a proposition, so that the
type of all $M$-null types in $\UU$ is a subuniverse of $\UU$.

\begin{eg}
By \cref{ex:sphere_null}, a type is $\sphere{n+1}$-null precisely when it is $n$-truncated,
for each $n\geq -2$ (taking the $(-1)$-sphere to be the empty type).
\end{eg}

The notion of $M$-connected type is in a sense dual to the notion of $M$-null types.

\begin{defn}
A type $A$ is said to be \define{$M$-connected} if every $M$-null
type is $A$-null. That is, if for every $M$-null type $B$, the map
\begin{equation*}
\lam{b}{a}{b} : B \to (A \to B)
\end{equation*}
is an equivalence. A map is said to be \define{$M$-connected} if its fibers are $M$-connected.
\end{defn}

Thus in particular, $M$ itself is $M$-connected, and the unit type $\unit$ is $M$-connected for every $M$.

\begin{defn}
Let $M$ be a type. We say that a type $X$ has the \define{$M$-extension property}
with respect to a map $F:A\to B$, if the map
\begin{equation*}
\lam{g}{a} g(F(a)) : (B\to X)\to (A\to X)
\end{equation*}
is $M$-null. In the case $M\jdeq\unit$, we say that $X$ is \define{$F$-local}.
\end{defn}

\begin{lem}\label{lem:equivalent-extension-problems}
For any three types $A$, $A'$ and $B$, the type $B$ is $(\join{A}{A'})$-null
if and only if for any any $f:A\to B$, the type
\begin{equation*}
\sm{b:B}\prd{a:A}f(a)=b
\end{equation*}
is $A'$-null.
\end{lem}

\begin{proof}
To give $f:A\to B$ and $(f',H):A'\to\sm{b:B}\prd{a:A}f(a)=b$ is equivalent to giving a map $g:\join{A}{A'}\to B$. Concretely, the equivalence is given by substituting in $g:\join{A}{A'}\to B$ the constructors of the join, to obtain $\pairr{g\circ\inl,g\circ\inr,\apfunc{g}\circ\glue}$.

Now observe that the fiber of precomposing with the unique map $!_{\join{A}{A'}} : \join{A}{A'}\to\unit$ at $g : \join{A}{A'}\to B$, is equivalent to
\begin{equation*}
\sm{b:B}\prd{t:\join{A}{A'}}g(t)=b.
\end{equation*}
Similarly, the fiber of precomposing with the unique map $!_{A'} : A'\to\unit$ at $\pairr{g\circ\inr,\apfunc{g}\circ\glue} : A'\to\sm{b:B}\prd{a:A}f(a)=b$ is equivalent to
\begin{equation*}
\sm{b:B}{h:\prd{a:A}g(\inl(a))=b}\prd{a':A'}\pairr{g(\inr(a')),\apfunc{g}(\glue(a,a'))}=\pairr{b,h}.
\end{equation*}
By the universal property of the join, these types are equivalent.
\end{proof}

\begin{lem}\label{lem:join-null}
Suppose $A$ is an $M$-connected type, and that $B$ is an $(\join{M}{N})$-null type. Then $B$ is $(\join{A}{N})$-null.
\end{lem}

\begin{proof}
Let $B$ be a $(\join{M}{N})$-null type. Our goal of showing that $B$ is
$(\join{A}{N})$-null is equivalent to showing that for any $f:N\to B$,
the type
\begin{equation*}
\sm{b:B}\prd{a:A}f(a)=b
\end{equation*}
is $A$-null.
Since $B$ is assumed to be $(\join{M}{N})$-null, we know that this type is
$M$-null. Since $A$ is $M$-connected, this type is also $A$-null.
\end{proof}

\begin{lem}\label{lem:N-extension-simple}
Let $A$ be $M$-connected and let $B$ be $(\join{M}{N})$-null. Then the map
\begin{equation*}
\lam{b}{a}b:B\to B^A
\end{equation*}
is $N$-null.
\end{lem}

\begin{proof}
The fiber of $\lam{b}{a}b$ at a function $f:A\to B$ is equivalent to the type $\sm{b:B}\prd{a:A}f(a)=b$. Therefore, it suffices to show that this type is $N$-null. By \cref{lem:equivalent-extension-problems}, it is equivalent to show that $B$ is $(\join{A}{N})$-null. This is solved in \cref{lem:join-null}.
\end{proof}

\begin{thm}[Join extension theorem]\label{thm:join-extension}
Suppose $f:X\to Y$ is $M$-connected, and let $P:Y\to\UU$ be a family of
$(\join{M}{N})$-null types for some type $N$. Then precomposition by $f$, i.e.
\begin{equation*}
\lam{s}s\circ f : \Big(\prd{y:Y}P(y)\Big)\to\Big(\prd{x:X}P(f(x))\Big),
\end{equation*}
is an $N$-null map.
\end{thm}

\begin{proof}
Let $g:\prd{x:X}P(f(x))$. Then we have the equivalences
\begin{align*}
\hfib{(\blank\circ f)}{g}
& \eqvsym \sm{s:\prd{y:Y}P(y)}\prd{x:X}s(f(x))=g(x) \\
& \eqvsym \sm{s:\prd{y:Y}P(y)}\prd{y:Y}{(x,p):\hfib{f}{y}} s(y)= \trans{p}{g(x)} \\
& \eqvsym \prd{y:Y}\sm{z:P(y)}\prd{(x,p):\hfib{f}{y}} \trans{p}{g(x)}=z \\
& \eqvsym \prd{y:Y}\hfib{\lam{z}{(x,p)}z}{\lam{(x,p)}\trans{p}{g(x)}}.
\end{align*}
Therefore, it suffices to show for every $y:Y$, that $P(y)$ has the $N$-extension property with respect to the unique map of type $\hfib{f}{y}\to\unit$. This is a special case of \cref{lem:N-extension-simple}.
\end{proof}

\begin{thm}\label{thm:simple-join}
Suppose $X$ is an $M$-connected type and $Y$ is an $N$-connected type. Then $\join{X}{Y}$ is an $(\join{M}{N})$-connected type.
\end{thm}

\begin{proof}
It suffices to show that any $(\join{M}{N})$-null type is $(\join{X}{Y})$-null.
Let $Z$ be an $(\join{M}{N})$-null type.
Since $Z$ is assumed to be $(\join{M}{N})$-null, it follows by \cref{lem:join-null} that $Z$ is $(\join{X}{N})$-null. By symmetry of the join, it also follows that $Z$ is $(\join{X}{Y})$-null.
\end{proof}

\begin{thm}[Join connectivity theorem]\label{thm:join-connectivity}
Consider an $M$-connected map $f:A\to X$ and an $N$-connected map $g:B\to X$. Then $\join{f}{g}$ is $(\join{M}{N})$-connected.
\end{thm}

\begin{proof}
This follows from \cref{thm:simple-join} and \cref{defn:join-fiber}.
\end{proof}

\begin{thm}\label{thm:joinconstruction-connectivity}
Consider the factorization
\begin{equation*}
\begin{tikzcd}
A_n \arrow[dr,swap,"f^{\ast n}"] \arrow[r,"q_n"] & \im(f) \arrow[d] \\
& X
\end{tikzcd}
\end{equation*}
of $f^{\ast n}$ through the image $\im(f)$.
Then the map $q_n$ is $(n-2)$-connected, for each $n:\N$.
\end{thm}

\begin{proof}
We first show the assertion that, given a commuting diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[r,"q"] \arrow[dr,swap,"f"] & Y \arrow[d,"m"] & A' \arrow[l,swap,"{q'}"] \arrow[dl,"{f'}"] \\
& X
\end{tikzcd}
\end{equation*}
in which $m$ is an embedding, then $\join{f}{f'}=\join{(m\circ q)}{(m\circ q')}=m\circ (\join{q}{q'})$.
In other words, postcomposition with embeddings distributes over
the join operation.

Note that, since $m$ is assumed to be an embedding, we have an equivalence of
type $\eqv{f(a)=f'(a)}{q(a)=q'(a)}$, for every $a:A$. Hence the pullback of
$f$ and $f'$ is equivalent to the pullback of $q$ along $q'$. Consequently, the
two pushouts
\begin{equation*}
\begin{tikzcd}
A\times_X A' \arrow[r,"\pi_2"] \arrow[d,swap,"\pi_1"] & A' \arrow[d] \\
A \arrow[r] & \join[X]{A}{A'}
\end{tikzcd}
\qquad\text{and}\qquad
\begin{tikzcd}
A\times_Y A' \arrow[r,"\pi_2"] \arrow[d,swap,"\pi_1"] & A' \arrow[d] \\
A \arrow[r] & \join[Y]{A}{A'}
\end{tikzcd}
\end{equation*}
are equivalent. Hence the claim follows.

As a corollary, we get that $q_n=q_f^{\ast n}$. Note that $q_f$ is surjective,
in the sense that $q_f$ is $\bool$-connected, where $\bool$ is the type of booleans%
\footnote{Recall that the $\bool$-null types are precisely the mere propositions.}.
Hence it follows that $q_n$ is $\bool^{\ast n}$-connected.

Now recall that the $n$-th join power of $\bool$ is the $(n-1)$-sphere $\Sn^{n-1}$, and that
a type is $(\Sn^{n-1})$-connected if and only if it is $(n-2)$-connected.
\end{proof}
\end{comment}

\begin{comment}
\subsection{The construction of the $n$-truncation}\label{sec:truncation}

Our goal in this section is to prove the following theorem. Its proof will take up the entire section.

\begin{thm}\label{thm:truncation}
For every $k\geq -2$, there is a $k$-truncation operation
\begin{equation*}
\trunc{k}{\blank} : \UU\to\UU
\end{equation*}
equipped with a fiberwise transformation
\begin{equation*}
\tproj{k}{\blank}:\prd{X:\UU}X\to\trunc{k}{X},
\end{equation*}
such that for each $X:\UU$ the type $\trunc{k}{X}$ is a $k$-type satisfying the (dependent) universal property of $k$-truncation.
\end{thm}

We will define the $k$-truncation operation by induction on $k\geq-2$,
with the trivial operation as the base case. For $k\geq -2$, suppose we have
a $k$-truncation operation as described in the statement of the theorem.

\cref{thm:trunc_id} suggests that we can think of the type $\trunc{k+1}{X}$ is as the quotient of $X$ modulo the
`$(k+1)$-equivalence relation' given by $\trunc{k}{a=b}$.

\begin{defn}
We define the reflexive relation $I_k(A) : A \to (A \to \UU)$ by
\begin{equation*}
I_k(A)(a,b) \defeq \trunc{k}{a=b},
\end{equation*}
and then we define
\begin{align*}
\trunc{k+1}{A} & \defeq \im(I_k(A)) \\
\tproj{k+1}{\blank} & \defeq q_{I_k(A)},
\end{align*}
where $q_{I_k(A)}:A\to \im(I_k(A))$ is the map with which the image comes equipped.
\end{defn}

Note that the codomain $(A\to\UU)$ of $I_k(A)$ is locally small since it is the exponent of
the locally small type $\UU$ by a small type $A$.
Therefore the image of $I_k(A)$ is essentially small by \cref{thm:replacement}.
Since we want the $(k+1)$-truncation to be an operation $\UU\to\UU$, it would be more precise to define $\trunc{k+1}{A}$ as the (unique) type in $\UU$ that is equivalent to $\im(I_k(A))$. Of course, this makes no substantial difference.

\begin{lem}\label{lem:modal_contr}
For every $a,b:A$, we have an equivalence
\begin{equation*}
\eqv{(I_k(A)(a)=I_k(A)(b))}{\trunc{k}{a=b}}.
\end{equation*}
\end{lem}

\begin{proof}
Since $\im(I_k(A))$ is a subtype of $\UU^A$, there is for any $b:A$ a `tautological' family $E_b$ of types over $\im(I_k(A))$, given by
\begin{equation*}
E_b(P) \defeq P(b).
\end{equation*}
Note that $E_b(I_k(A)(a))\jdeq \trunc{k}{a=b}$. Therefore we can prove the claim by showing that the canonical map
\begin{equation*}
\prd{P:\im(I_k(A))} (I_k(A)(b)=P)\to P(b)
\end{equation*}
is a fiberwise equivalence. By \cref{thm:id_fundamental} it suffices to show that for each $b:A$, the total space
\begin{equation*}
\sm{P:\im(I_k(A))}P(b)
\end{equation*}
is contractible.

For the center of contraction we take the pair
$\pairr{I_k(A)(b),\tproj{k}{\refl{b}}}$.
For the contraction we construct a term of type
\begin{equation*}
\prd{P:\im(I_k(A))}{y:P(b)} \pairr{I_k(A)(b),\tproj{k}{\refl{b}}}=\pairr{P,y}.
\end{equation*}
Since $I_k(A)(b,a)\jdeq\trunc{k}{b=a}$, it is equivalent to construct a term of type
\begin{equation*}
\prd{P:\im(I_k(A))}{y:P(b)}\sm{\alpha:\prd{a:A} \eqv{\trunc{k}{b=a}}{P(a)}} \alpha_b(\tproj{k}{\refl{b}})=y.
\end{equation*}
Let $P:\im(I_k(A))$ and $y:P(b)$. Then $P(a)$ is $n$-truncated for any $a:A$. Therefore, to construct a map
$\alpha(P,y)_a:\trunc{k}{b=a}\to P(a)$, it suffices to construct a map of type $(b=a)\to P(a)$. This may be done by
path induction, using $y:P(b)$. Since it follows that $\alpha(P,y)_b(\tproj{k}{\refl{b}})=y$, it only remains to show that each $\alpha(P,y)_a$ is an equivalence.

Note that the type of those $P:\im(I_k(A))$ such that for all $y:P(b)$ and all $a:A$ the map $\alpha(P,y)_a$ is an equivalence, is a subtype of $\im(I_k(A))$, we may use the universal property of the image of $I_k(A)$: it suffices to lift
\begin{equation*}
\begin{tikzcd}
& \sm{P:\im(I_k(A))}\prd{y:P(b)}{a:A}\isequiv(\alpha(P,y)_a) \arrow[d] \\
A \arrow[ur,densely dotted] \arrow[r,swap,"I_k(A)"] & \im(I_k(A)).
\end{tikzcd}
\end{equation*}
In other words, it suffices to show that
\begin{equation*}
\prd{x:A}{y:I_k(A)(x,b)}{a:A}\isequiv(\alpha(I_k(A)(x),y)_a).
\end{equation*}
Thus, we want to show that for any $y:\trunc{k}{x=b}$, the map $\trunc{k}{a=b}\to\trunc{k}{x=b}$ constructed above is an equivalence.
Since the fibers of this map are $n$-truncated, and $\iscontr(X)$ of an $n$-truncated type $X$ is always $n$-truncated, we may assume that $y$ is of the form $\tproj{k}{p}$ for $p:x=b$.
Now it is easy to see that our map of type $\trunc{k}{b=a}\to\trunc{k}{x=a}$ is the unique map which
extends the path concatenation $\ct{p}{\blank}$, as indicated in the diagram
\begin{equation*}
\begin{tikzcd}[column sep=8em]
(b=a) \arrow[r,"\ct{p}{\blank}"] \arrow[d] & (x=a) \arrow[d] \\
\trunc{k}{b=a} \arrow[r,densely dotted,swap,"{\alpha(I_k(A)(x),y)_a}"] & \trunc{k}{x=a}.
\end{tikzcd}
\end{equation*}
Since the top map is an equivalence, it follows that the map $\alpha(I_k(A)(x),y)_a$ is an equivalence.
\end{proof}

\begin{cor}\label{cor:truncated}
The image $\im(I_k(A))$ is an $(n+1)$-truncated type.
\end{cor}

\begin{proof}[Construction]
We will show that $\trunc{n+1}{A}$ is indeed $(n+1)$-truncated in \cref{cor:truncated} of \cref{lem:modal_contr} below. Once this fact is established, it remains to verify the dependent universal property of $(n+1)$-truncation.
By the join extension theorem \cref{thm:join-extension} (using $N\defeq \emptyt$), it suffices to show that the map $\tproj{n+1}{\blank}:A\to\trunc{n+1}{A}$ is $\sphere{n+2}$-connected. Note that $\tproj{n+1}{\blank}$ is surjective, so the claim that $\tproj{n+1}{\blank}$ is $\sphere{n+2}$-connected follows from \cref{lem:ap_connectivity}, where we show that for any surjective map $f:A\to X$, if the action on paths is $M$-connected for any two points in $A$, then $f$ is $\susp(M)$-connected. To apply this lemma, we also need to know that $\tproj{k}{\blank}:A\to\trunc{k}{A}$ is $\sphere{n+1}$-connected. This is shown in Corollary 7.5.8 of \cite{hottbook}.
\end{proof}

Before we are able to show that for any surjective map $f:A\to X$, if the action on paths is $M$-connected for any two points in $A$, then $f$ is $\susp(M)$-connected, we show that a type is $\susp(M)$-connected precisely when its identity types are $M$-connected.

\begin{lem}\label{lem:local_id}
Let $M$ be a type. Then a type $X$ is $(\join{\bool}{M})$-null
if and only if all of its identity types are $M$-null.
\end{lem}

\begin{proof}
The map
\begin{equation*}
\lam{p}{m}p : (x=y)\to (M\to (x=y))
\end{equation*}
is an equivalence if and only if the induced map on total spaces
\begin{equation*}
\lam{\pairr{x,y,p}}\pairr{x,y,\lam{m}p} : \Big(\sm{x,y:X}x=y\Big)\to\Big(\sm{x,y:X}M\to (x=y)\Big)
\end{equation*}
is an equivalence.
Since the map $\lam{x}\pairr{x,x,\refl{x}}:X\to\sm{x,y:X}x=y$ is an equivalence,
the above map is an equivalence if and only if the map
\begin{equation*}
\lam{x}\pairr{x,x,\lam{m}\refl{x}} : X\to\Big(\sm{x,y:X}M\to (x=y)\Big)
\end{equation*}
is an equivalence. For every $x:X$, the triple $\pairr{x,x,\lam{m}\refl{x}}$
induces a map $\susp(M)\to X$. By uniqueness of the universal property,
it follows that this map is the constant map $\lam{m}x$.
Thus we see that $\lam{x}\pairr{x,x,\lam{m}\refl{x}}$ is an equivalence if
and only if the map
\begin{equation*}
\lam{x}{m}x : X \to (\susp(M)\to X)
\end{equation*}
is an equivalence.
\end{proof}

\begin{lem}\label{lem:ap_connectivity}
Suppose $f:A\to X$ is a surjective map, with the property that for every
$a,b:A$, the map
\begin{equation*}
\mapfunc{f}(a,b):(a=b)\to (f(a)=f(b))
\end{equation*}
is $M$-connected. Then $f$ is $\susp(M)$-connected.
\end{lem}

\begin{proof}
We have to show that $\fib{f}{x}$ is $\susp(M)$-connected for each $x:X$.
Since this is a mere proposition, and we assume that $f$ is surjective, it
is equivalent to show that $\fib{f}{f(a)}$ is $\susp(M)$-connected for each $a:A$.
Let $Y$ be a $\susp(M)$-null type.
For every $g:\fib{f}{f(a)}\to Y$ be a map we have the point $\theta(g)\defeq g(a,\refl{f(a)})$ in $Y$,
so we obtain a map
\begin{equation*}
\theta : (\fib{f}{f(a)}\to Y)\to Y
\end{equation*}
It is clear that $\theta(\lam{\pairr{b,p}}y)=y$, so it remains to show that
for every $g:\fib{f}{f(a)}\to Y$ we have $\lam{\pairr{b,p}}\theta(g)=g$.
That is, we must show that
\begin{equation*}
\prd{b:A}{p:f(a)=f(b)} g(a,\refl{f(a)})=g(b,p).
\end{equation*}
Using the assumption that $Y$ is $\susp(M)$-connected, it follows from
\cref{lem:local_id} that the type $g(a,\refl{f(a)})=g(b,p)$ is $M$-connected,
for every $b:A$ and $p:f(a)=f(b)$.
Therefore it follows, since the map $\mapfunc{f}(a,b):(a=b)\to(f(a)=f(b))$ is connected, that our goal is equivalent to
\begin{equation*}
\prd{b:A}{p:a=b} g(a,\refl{f(a)})=g(b,\mapfunc{f}(a,b,p)).
\end{equation*}
This follows by path induction.
\end{proof}
\end{comment}

\subsection{Theorems not to forget}

\begin{thm}
  Consider a type $X$ and a family $P$ of $(k+n)$-truncated types over $\trunc{k}{X}$. Then the precomposition map
  \begin{equation*}
    \blank\circ\eta : \Big(\prd{y:\trunc{k}{X}}P(y)\Big)\to\Big(\prd{x:X}P(\eta(x))\Big)
  \end{equation*}
  is $(n-2)$-truncated.
\end{thm}

\begin{exercises}
\exercise Consider an equivalence relation $R:A\to (A\to\prop)$. Show that the map $\tproj{0}{\blank}\circ \inl:A\to \trunc{0}{A\sqcup^{R} A}$ satisfies the universal property of the quotient $A/R$, where $A\sqcup^{R} A$ is the canonical pushout
\begin{equation*}
\begin{tikzcd}
\sm{x,y:A}R(x,y) \arrow[r,"\pi_2"] \arrow[d,swap,"\pi_1"] & A \arrow[d,"\inr"] \\
A \arrow[r,swap,"\inl"] & A\sqcup^{R} A.
\end{tikzcd}
\end{equation*}
\exercise Consider the trivial relation $\unit\defeq\lam{x}{y}\unit:A\to (A\to\prop)$. Show that the set quotient $A/\unit$ is a proposition satisfying the universal property of the propositional truncation.
\exercise Show that the type of pointed $2$-element sets
\begin{equation*}
\sm{X:\UU_{\bool}}X
\end{equation*}
is contractible.
\exercise Define the type $\mathbb{F}$ of finite sets by
\begin{equation*}
\mathbb{F}\defeq \im(\fin),
\end{equation*}
where $\fin:\N\to\UU$ is defined in \cref{defn:fin}.
\begin{subexenum}
\item Show that $\eqv{\mathbb{F}}{\sm{n:\N}\UU_{\fin(n)}}$.
\item Show that $\mathbb{F}$ is closed under $\Sigma$ and $\Pi$.
\end{subexenum}
\exercise
\begin{subexenum}
\item A type $Y$ is called \define{$k$-separated} if for every type $X$ the map
  \begin{equation*}
    (\trunc{k}{X}\to Y)\to(X\to Y)
  \end{equation*}
  is an embedding. Show that $Y$ is $k$-separated  if and only if it is $(k+1)$-truncated.
\item A type $Y$ is called \define{$n$-fold $k$-separated} if for every type $X$ the map
  \begin{equation*}
    (\trunc{k}{X}\to Y)\to (X\to Y)
  \end{equation*}
  is $(n-2)$-truncated. Show that $Y$ is $n$-fold $k$-separated if and only if it is $(k+n)$-truncated.
\end{subexenum}
\exercise Consider a map $f:A\to B$. Show that the square
\begin{equation*}
  \begin{tikzcd}[column sep=large]
    A \arrow[r,"f"] \arrow[d,swap,"\lam{x}\const_x"] & B \arrow[d,"\lam{y}\const_y"] \\
    A^{\sphere{k+1}} \arrow[r,swap,"f^{\sphere{k+1}}"] & B^{\sphere{k+1}}
  \end{tikzcd}
\end{equation*}
is a pullback square if and only if its gap map has a section.
\exercise Consider a map $f:X\to Y$ into a $k$-truncated type $Y$. Show that the following are equivalent:
\begin{enumerate}
\item For any family $P$ of $k$-types over $Y$, the precomposition map
  \begin{equation*}
    \blank\circ f : \Big(\prd{y:Y}P(y)\Big)\to\Big(\prd{x:X}P(f(x))\Big)
  \end{equation*}
  is an equivalence.
\item For any family $P$ of $k$-types over $Y$, the precomposition map
  \begin{equation*}
    \blank\circ f : \Big(\prd{y:Y}P(y)\Big)\to\Big(\prd{x:X}P(f(x))\Big)
  \end{equation*}
  has a section.
\end{enumerate}
\exercise Show that for each type $X$, the map
\begin{equation*}
  \trunc{k+1}{X}\to\UU^X
\end{equation*}
given by $y\mapsto\lam{x}(y=\eta'(x))$ is an embedding.
\end{exercises}


\section{Trees}

\frame{
{Part 3: Trees}

\tableofcontents[currentsection,hideallsubsections, firstsection=1, sections={1-4}]
}

\begin{frame}{Trees and Connectivity}

  {\larger
    \begin{itemize}
    \item \structure{Trees} are connected Graphs with \alert{no cycles}.
    \item Every tree 1-edge connectivity, 1-vertex connectivity.
    \item Chromatic Number = 2 (trees can always be bi-colored)

      \bigskip

    \item Trees come up all the time:
      \begin{columns}
        \column{0.5\textwidth}
        \begin{itemize}
        \item Family Trees;
        \item Search Trees;
        \item Game Trees;
        \item Parse Trees;
        \end{itemize}
        \column{0.5\textwidth}
        \begin{itemize}
        \item Spanning Trees;
        \item Rooted Trees;
        \item Ordered Trees;
        \item Binary Trees;
        \item etc...
        \end{itemize}
      \end{columns}
    \end{itemize}
  }
\end{frame}

\begin{frame}{Trees and Connectivity}

  {\larger
    \begin{itemize}
    \item \structure{Cut Edge}: An edge is a cut edge if
      removing it makes two vertices disconnected.

      \bigskip

    \item {\bf Lemma:} An edge is not a cut edge if it is on a
      cycle.

      \bigskip

    \item A tree is a \structure{connected graph} where
      \structure{every edge is a cut edge}

      \bigskip

    \item This implies that a tree is a connected graph which
      is {\bf\structure{Edge Minimal}}
      \begin{itemize}
      \item A tree has the minimum number of edges necessary to
        connect a set of vertices.
      \end{itemize}
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Tree Coloring}

  {\larger
    \begin{itemize}
    \item A tree is a graph with a \structure{unique path}
      between every pair of vertices.

    \item As a consequence, $\chi(\text{tree}) = 2$

    \item {\bf Constructive Demonstration}

      \begin{columns}
        \column{0.7\textwidth}
        \begin{itemize}
        \item Pick any node in the tree to be the {\bf root}, color
          it ``blue''.
        \item Color nodes ``odd'' length from the root as ``red''
        \item Color nodes ``even'' length from the root as ``blue''
        \item This is the algorithm for 2-coloring on general graphs
        \end{itemize}
        \column{0.3\textwidth}

        \begin{tikzpicture}[scale=.8,auto,swap]
          %\tikzset{edge/.style = {->,>=latex'}}
          \node[blue vertex] (a) at (1,0) {};
          \node[red vertex] (a1) at (0,1) {};
          \node[red vertex] (b1) at (1,1) {};
          \node[blue vertex] (a2) at (0,2) {};
          \node[blue vertex] (b2) at (1,2) {};
          \node[blue vertex] (c2) at (2,2) {};
          \node[red vertex] (a3) at (0,3) {};
          \node[red vertex] (b3) at (1,3) {};
          \node[red vertex] (c3) at (2,3) {};
          \node[red vertex] (d3) at (3,3) {};
          \draw[edge] (a) to (a1);
          \draw[edge] (a) to (b1);
          \draw[edge] (a1) to (a2);
          \draw[edge] (a1) to (b2);
          \draw[edge] (b1) to (c2);
          \draw[edge] (b2) to (a3);
          \draw[edge] (b2) to (b3);
          \draw[edge] (b2) to (c3);
          \draw[edge] (c2) to (d3);

        \end{tikzpicture}
      \end{columns}

    \end{itemize}

  }
\end{frame}

\subsection{Spanning Trees}

\begin{frame}
  \frametitle{Spanning Trees}

  {\larger

    \begin{itemize}
    \item A \structure{Spanning Subgraph} of G is a subgraph of
      $G$ that has all vertices of $G$ (and some of the edges).
    \item A \structure{Spanning Tree} of G is a spanning graph of
      $G$ that is also a tree.
    \end{itemize}

    \begin{center}
      \begin{tikzpicture}[scale=.8,auto,swap]
        %\tikzset{edge/.style = {->,>=latex'}}
        \node[vertex] (a) at (0,1) {};
        \node[vertex] (a1) at (1,0) {};
        \node[vertex] (b1) at (1,1) {};
        \node[vertex] (a2) at (2,0) {};
        \node[vertex] (b2) at (2,1) {};
        \node[vertex] (c2) at (2,2) {};
        \node[vertex] (a3) at (3,0) {};
        \node[vertex] (b3) at (3,1) {};
        \node[vertex] (c3) at (3,2) {};
        \node[vertex] (d3) at (2,3) {};
        \draw[red edge] (a) to (a1);
        \draw[red edge] (a) to (b1);
        \draw[red edge] (a1) to (a2);
        \draw[red edge] (a1) to (b2);
        \draw[red edge] (b1) to (c2);
        \draw[red edge] (b2) to (a3);
        \draw[red edge] (b2) to (b3);
        \draw[red edge] (b2) to (c3);
        \draw[edge] (c2) to (d3);
        \draw[edge] (a) to (d3);
        \draw[edge] (b1) to (d3);
        \draw[edge] (a1) to (b1);
        \draw[red edge] (b1) to (b2);
        \draw[edge] (b2) to (a2);
        \draw[edge] (b2) to (c2);
        \draw[red edge] (a2) to (a3);
        \draw[red edge] (c2) to (c3);
        \draw[edge] (a3) to (b3);
        \draw[edge] (c3) to (d3);
        \draw[edge] (b3) to (c3);
      \end{tikzpicture}
    \end{center}

    \begin{itemize}
    \item One graph can have multiple spanning trees.
    \item Every connected graph has a spanning tree.
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Weighted Spanning Trees}

  {\larger

    The Spanning Tree problem becomes more interesting when we consider
    \structure{weighted edges}.

    \begin{center}
      \begin{tikzpicture}[scale=.8,auto,swap]
        %\tikzset{edge/.style = {->,>=latex'}}
        \node[vertex] (a) at (3,5) {};
        \node[vertex] (b) at (0,3) {};
        \node[vertex] (c) at (3,3) {};
        \node[vertex] (d) at (6,3) {};
        \node[vertex] (e) at (2,1) {};
        \node[vertex] (f) at (4,1) {};
        \draw[edge] (a) -- node[weight] {$3$} (b);
        \draw[edge] (a) -- node[weight] {$4$} (d);
        \draw[edge] (b) -- node[weight] {$4$} (c);
        \draw[edge] (c) -- node[weight] {$1$} (d);
        \draw[edge] (b) -- node[weight] {$6$} (e);
        \draw[edge] (c) -- node[weight] {$9$} (e);
        \draw[edge] (c) -- node[weight] {$7$} (f);
        \draw[edge] (d) -- node[weight] {$2$} (f);
        \draw[edge] (e) -- node[weight] {$1$} (f);
      \end{tikzpicture}
    \end{center}

    What is the \structure{minimal cost} structure that
    allows me to connect everything?

  }
\end{frame}

\begin{frame}
  \frametitle{Minimum Spanning Tree Algorithm}

  {\larger
    \begin{center}
      \begin{tikzpicture}[scale=.8,auto,swap]
        %\tikzset{edge/.style = {->,>=latex'}}
        \node[vertex] (a) at (3,5) {};
        \node[vertex] (b) at (0,3) {};
        \node[vertex] (c) at (3,3) {};
        \node[vertex] (d) at (6,3) {};
        \node[vertex] (e) at (2,1) {};
        \node[vertex] (f) at (4,1) {};
        \draw[black edge] (a) -- node[weight] {$3$} (b);
        \draw[edge] (a) -- node[weight] {$4$} (d);
        \draw[black edge] (b) -- node[weight] {$4$} (c);
        \draw[black edge] (c) -- node[weight] {$1$} (d);
        \draw[edge] (b) -- node[weight] {$6$} (e);
        \draw[edge] (c) -- node[weight] {$9$} (e);
        \draw[edge] (c) -- node[weight] {$7$} (f);
        \draw[black edge] (d) -- node[weight] {$2$} (f);
        \draw[black edge] (e) -- node[weight] {$1$} (f);
      \end{tikzpicture}
    \end{center}

    \begin{enumerate}
    \item Start with one arbitrary vertex and add it to the MST.
    \item From all edges connected with the MST, select one with minimum weight;
    \item Add the edge, and vertex, to the MST;
    \item Return to (2)
    \end{enumerate}
  }
\end{frame}


\subsection{Centrality}
  Another important measure is the network centrality. Intuitively, a network that resembles a mesh is preferable over a
  network with a central hub that acts as an intermediary between all other pairs of nodes. This is because in the latter case
  Trust Is Risk does not offer any realistic improvement over the existing centralized marketplaces model (e.g. ebay).
  Nevertheless, the expected indirect trust measure is not always able to distinguish between the two. We thus propose here
  some different measures to that end.

  \subsubsection{Degree Centrality} \ \\

    One possible approach is the degree centrality \cite{freeman}, that can be broken down as in-degree and out-degree
    centrality. We first define the node in-degree centrality.
    \begin{align*}
      C^d_{in}\left(A\right) = \sum\limits_{B \in \mathcal{V} \setminus \{A\}}DTr_{B \rightarrow A}\footnotemark && \mbox{
      (Node in-degree centrality)}
    \end{align*}
    \footnotetext{Maybe indirect trust is more intuitive here than direct trust.}
    Let $A^*$ be the player with the maximum centrality: $A^* = \argmax\limits_{A \in \mathcal{V}}C^d_{in}\left(A\right)$. The
    network in-degree centrality is defined as:
    \begin{align*}
      C^d_{in} = \sum\limits_{A \in \mathcal{V}}\left(C^d_{in}\left(A^*\right) - C^d_{in}\left(A\right)\right) && \mbox{
      (Network in-degree centrality)}
    \end{align*}
    Similarly, for the out-degree centrality we have:
    \addtocounter{footnote}{-1}
    \begin{align*}
      C^d_{out}\left(A\right) = \sum\limits_{B \in \mathcal{V} \setminus \{A\}}DTr_{A \rightarrow B}\footnotemark && \mbox{
      (Node out-degree centrality)}
    \end{align*}
    Let $A^* = \argmax\limits_{A \in \mathcal{V}}C^d_{out}\left(A\right)$. The network out-degree centrality is:
    \begin{align*}
      C^d_{out} = \sum\limits_{A \in \mathcal{V}}\left(C^d_{out}\left(A^*\right) - C^d_{out}\left(A\right)\right) && \mbox{
      (Network out-degree centrality)}
    \end{align*}

    A problem of these centrality measures is that their unit is the currency used (i.e. Bitcoin) and thus may not always have
    an intuitive meaning. We would thus like to have a measure of \textit{centralization} that has no units and can take
    values in the interval $\left[0,1\right]$, with 0 corresponding to a network of no centralization (all nodes are equal,
    e.g.  cycle) and 1 to a network with the maximum centralization possible (there is one central vital node for all, e.g.
    star). A centralization measure that achieves this target is proposed in \cite{freeman}. Here we use the following
    modified form. For a graph $\mathcal{G}$, the in- and out-centralization are defined as:
    \begin{align*}
      Cn^d_{in} &= \frac{C^d_{in}}{\max C^d_{in}} && \mbox{ (in-degree centralization)} \\
      Cn^d_{out} &= \frac{C^d_{out}}{\max C^d_{out}} && \mbox{ (out-degree centralization)} \enspace,
    \end{align*}
    where $\max C^d_{in}$ is defined as the maximum in-degree centrality over all networks with the same number of nodes for
    which the maximum direct\footnote{Maybe indirect trust is more intuitive here than direct trust.} trust is equal to the
    maximum \addtocounter{footnote}{-1}direct\footnotemark \ trust of $\mathcal{G}$; $\max C^d_{out}$ is defined equivalently.

  \subsubsection{maxFlow Centrality} \ \\

    An alternative measure of centrality for a player $A \in \mathcal{V}$ of a network $\mathcal{G}$ can be defined as the
    impact that the removal of $A$ would have on the indirect trust between the rest of the players. More specifically, let
    $\mathcal{G}' = \mathcal{G} \setminus \{A\}$. Then it is:
    \begin{align*}
      C^{mF}\left(A\right) = \sum\limits_{B,C \in \mathcal{V}'}\left(Tr_{\mathcal{G}, B \rightarrow C} - Tr_{\mathcal{G}', B
      \rightarrow C}\right) && \mbox{ (Node maxFlow centrality)} \enspace.
    \end{align*}
    We can now follow the same steps as previously for the relevant network definitions. Let $A^* = \argmax\limits_{A \in
    \mathcal{V}}C^{mF}\left(A\right)$. Then the network maxFlow centrality is:
    \begin{align*}
      C^{mF} = \sum\limits_{A \in \mathcal{V}}\left(C^{mF}\left(A^*\right) - C^{mF}\left(A\right)\right) && \mbox{ (Network
      maxFlow centrality)}
    \end{align*}
    and the centralization:
    \begin{align*}
      Cn^{mF} = \frac{C^{mF}}{\max C^{mF}} && \mbox{ (Network maxFlow centralization)} \enspace,
    \end{align*}
    where $\max C^{mF}$ is the maximum centrality over all networks with the same number of nodes for which the maximum
    direct\footnote{Maybe indirect trust is more intuitive here than direct trust.} trust is equal to the maximum
    \addtocounter{footnote}{-1} direct\footnotemark \ trust of $\mathcal{G}$.


\chapter{Omitted proofs}
\section{Precision bounds for $+,-,\times$}\label{sec:proof-precision}

We first formulate an assumption on the rounding operation $\round()$. In this section, $M$ and $\epsilon$ are positive real constants.
\begin{assum}
The rounding of a value $x$ has a relative error of at most $\epsilon$. Therefore, if $|x| \leq M^d$, as we will always assume of a $d$-dimensional value, then
\[|\round(x)-x| \leq M^d\epsilon\]
\end{assum}

To give a solid formalism to our notions of $d$-dimensional values and ``computed in $n$ operations'', we introduce the following recursive definition.
\begin{mydef}\label{def:valid-computation}
A quadruplets $(x,x',d,n)$ is a \emph{valid computation} if $|x| \leq M^d$ and one of these holds:
\begin{enumerate}[label={(\alph*)}]
\item $x = x'$, $n=0$;
\item $(a,a',d_a,n_a)$ and $(b,b',d_b,n_b)$ are valid computations, $n = n_a+n_b+1$ and either:
    \begin{enumerate}[label={(\roman*)}]
    \item $d=d_a=d_b$, $x=a+b$, $x'=\round(a'+b')$ and $|a'+b'| \leq M^d$;
    \item $d=d_a=d_b$, $x=a-b$, $x'=\round(a'-b')$ and $|a'-b'| \leq M^d$;
    \item $d=d_a+d_b$, $x=ab$, $x'=\round(a'b')$ and $|a'b'| \leq M^d$.
    \end{enumerate}
\end{enumerate}
\end{mydef}

Note that valid computations are strongly limited by the assumptions we place on the magnitude of the results, both theoretical and actual.

{
    \newcommand{\f}[1]{(1+\epsilon)^{#1}-1}
    \newcommand{\mdfn}[2]{M^{#1}\left(\f{#2}\right)}

    \begin{thm}\label{thm:precision}
    If $(x,x',d,n)$ is a valid computation, then \[|x'-x| \leq \mdfn{d}{n}.\]
    \end{thm}

    We will prove the theorem by induction on the structure of valid computations. We will separate the proof into two lemmas: first addition and subtraction together in Lemma~\ref{lemma:precision-add}, then multiplication in Lemma~\ref{lemma:precision-mult}.

    \begin{lemma}\label{lemma:fplusf}
    Let $f(x) = \f{x}$. If $a,b>0$, then $f(a) + f(b) \leq f(a+b)$.
    \end{lemma}
    \begin{proof}
    Clearly, $f$ is convex. From convexity we find
    \begin{align*}
    f(a) &\leq \frac{b}{a+b}\,f(0) + \frac{a}{a+b}\,f(a+b)\\
    f(b) &\leq \frac{a}{a+b}\,f(0) + \frac{b}{a+b}\,f(a+b).\\
    \end{align*}
    Therefore, $f(a) + f(b) \leq f(0) + f(a+b) = f(a+b)$.
    \end{proof}

    \begin{lemma}[addition and subtraction]\label{lemma:precision-add}
    Let operator $\ast$ be either $+$ or $-$.
    If $(a,a',d,n_a)$ and $(b,b',d,n_b)$ are two valid computations for which Theorem~\ref{thm:precision} holds and $(a\ast b,\round(a'\ast b'),d,n_a+n_b+1)$ is a valid computation, then Theorem~\ref{thm:precision} holds for it as well.
    \end{lemma}
    \begin{proof}
    From the hypotheses know that
    \begin{align*}
    |a'-a| &\leq \mdfn{d}{n_a}\\
    |b'-b| &\leq \mdfn{d}{n_b}\\
    |a' \ast b'| &\leq M^d.
    \end{align*}
    We find
    \begin{align*}
    |\round(a'&\ast b') - (a \ast b)| \\
    &= |(\round(a'\ast b') - (a'\ast b')) + ((a'\ast b') - (a\ast b))| \\
    &\leq |\round(a'\ast b') - (a'\ast b')| + |(a'-a) \ast (b'-b)| \\
    &\leq M^d\epsilon + |a'-a| + |b'-b| \\
    &\leq M^d\epsilon + \mdfn{d}{n_a} + \mdfn{d}{n_b} \\
    &= M^d\left[f(1) + f(n_a) + f(n_b)\right] \\
    &\leq M^d\,f(n_a+n_b+1)\\
    &= \mdfn{d}{n_a+n_b+1}
    \end{align*}
    where the step before last follows from two applications of Lemma~\ref{lemma:fplusf}.
    \end{proof}

    \begin{lemma}[multiplication]\label{lemma:precision-mult}
    If $(a,a',d_a,n_a)$ and $(b,b',d_b,n_b)$ are two valid computations for which Theorem~\ref{thm:precision} holds and $(ab,\round(a'b'),d_a+d_b,n_a+n_b+1)$ is a valid computation, then Theorem~\ref{thm:precision} holds for it as well.
    \end{lemma}
    \begin{proof}
    From the hypotheses we know that
    \begin{align*}
    |a| &\leq M^d\\
    |b| &\leq M^d\\
    |a'-a| &\leq \mdfn{d_a}{n_a}\\
    |b'-b| &\leq \mdfn{d_a}{n_b}\\
    |a'b'| &\leq M^{d_a+d_b}.
    \end{align*}
    We find
    \begin{align*}
    |\round(&a'b') - ab)|\\
    =\ &|(\round(a'b') - a'b') + (a'b'-ab)|\\
    \leq\ &|\round(a'b') - a'b'| + |(a'-a)b + (b'-b)a + (a'-a)(b'-b)|\\
    \leq\ &M^{d_a+d_b}\epsilon + |a'-a||b| + |b'-b||a| + |a'-a||b'-b|\\
    \leq\ &M^{d_a+d_b}\epsilon + \mdfn{d_a}{n_a}M^{d_b} + \mdfn{d_b}{n_b}M^{d_a}\\
    &\qquad + \mdfn{d_a}{n_a}\mdfn{d_b}{n_b}\\
    =\ &M^{d_a+d_b}\big[\epsilon + \left(\f{n_a}\right) + \left(\f{n_b}\right)\\
    &\qquad + \left(\f{n_a}\right)\left(\f{n_b}\right)\big]\\
    =\ &M^{d_a+d_b}\left[\epsilon + \f{n_a+n_b}\right]\\
    =\ &M^{d_a+d_b}\left[f(1) + f(n_a+n_b)\right]\\
    \leq\ &M^{d_a+d_b}\,f(n_a+n_b+1)\\
    =\ &\mdfn{d_a+d_b}{n_a+n_b+1}
    \end{align*}
    where the step before last follows from Lemma~\ref{lemma:fplusf}.
    \end{proof}

    \begin{proof}[Proof of Theorem~\ref{thm:precision}]
    By induction on the recursive structure of valid computations (see Definition~\ref{def:valid-computation}). Case (a) is trivial because $|x'-x| = 0$. For case (b), the inductive step for (i) and (ii) follows from Lemma~\ref{lemma:precision-add} while that of (iii) follows from Lemma~\ref{lemma:precision-mult}.
    \end{proof}
}


\chapter{Background}
\label{chap:background}

Monte Carlo codes are vital tools in a diverse number of fields. Based
in simple random number generators, these tools harness the power of
statistics to predict and describe a wide variety of behaviors. In
nuclear engineering, there is a long history of Monte Carlo methods
used to simulate neutral particle propagation. These methods enjoy a
wide variety of applications within the field, from fuel cell
development to full core analysis. A vital part of the simulation is
the propagation and interaction of neutral particles, neutrons. In
this chapter we will describe the history and mathematics of these
neutral particle propagation methods.

\section{The Monte Carlo Method}
\label{sec:monte_carlo}

The history of Monte Carlo methods begins with nuclear science and
engineering. Nicholas Metropolis and S. Ulam~\cite{metropolis1949} at
Los Alamos National Laboratory developed the method to support the
Manhattan Project. Today, Monte Carlo methods have found diverse
applications across the hard and social sciences. In nuclear
engineering, we still use Monte Carlo for its original purpose, the
propagation of neutral particles. Now, its use has expanded from
weapons research to assessing the viability and properties of nuclear
reactors.

When used for particle transport, the Monte Carlo method uses a random
number generator to simulate a particle's history. As the simulation
runs, various parameters are calculated and recorded. This will continue
for a finite number of histories, $N$, after which the mean value of
the parameters of interest are calculated:
\begin{equation*}
  \bar{x} = \frac{1}{N}\sum_{n=1}^Nx_n\:,
\end{equation*}
where $x_n$ is the value from the $n$-th history\cite{lewis1993}. The process
for sampling will be described in general here, and then extended to
particle propagation methods in Sec.~\ref{sec:propagation}.

As described by Lewis and Miller~\cite{lewis1993}, we first must
consider a random variable, $x$. We define the probability that $x$
will have a value between $a$ and $b$: $P\{a \leq x \leq b\}$. We
are interested in the probability that our random variable will have one
specific value. Therefore, we define the probability that our variable
will fall within a differential distance to a given value $x$ in the
limit that the differential distance goes to zero. This is the
\gls{pdf}, the probability that $x'$ will be between $x$ and $x +
\Delta x$:
\begin{equation}
  \label{eq:pdf_initial}
 f(x)\Delta x = \lim_{\Delta x \to 0}  P \{ x \leq x' \leq x + \Delta x \}\:.
\end{equation}
Integrating the \gls{pdf} gives the overall probability for the range:
\begin{equation}
\label{pdf}
  \int_a^bf(x)dx = P\{a \leq x \leq b\}\:.
\end{equation}
If we integrate the \gls{pdf} over the entire range of possible values
of $x$, it must equal unity; there is a 100\% chance that its value is
in the range. Therefore, we must normalize the \gls{pdf}:
\begin{equation*}
  \int_{-\infty}^{\infty}f(x)dx = 1, \quad x \in (-\infty,\infty)\:.
\end{equation*}
We then may define a \gls{cdf}:
\begin{equation}
  \label{eq:cdf}
  F(x) = P \{ x' \leq x\} = \int_{-\infty}^xf(x')dx'\:.
\end{equation}
In the limit where $x \to \infty$, $F(x) \to 1$ and as
$x \to -\infty$, $F(x) \to 0$ from the normalization of the
\gls{pdf}. The \gls{cdf} is uniformly distributed from zero to
unity~\cite{lewis1993}, with each value corresponding to a single
value of $x$. We therefore have a means to generate values of $x$
using a random number generator uniformly distributed from zero
unity. To do so, we set:
\begin{equation*}
  F(x) = \xi\:,
\end{equation*}
where $\xi \in [0,1)$ is a random number. For each random number
sampled, we sample a single value of $x$. This is determined by
inverting the \gls{cdf}:
\begin{equation}
  \label{eq:inverted_cdf}
  F^{-1}(\xi) = x\:.
\end{equation}
We now have a direct way of using a random number generator to
generate a randomly distributed variable $x$. We will now apply this
to particle propagation. There are methods available when the
\gls{cdf} is not invertible. Each \gls{cdf} used for particle
propagation is invertible, so these methods are outside the scope of
discussion.
% you may want to mention that there are many sampling techniques
% for when the CDF is not invertible and that those are outside the
% scope of this discussion.

\section{Particle Propagation Methods}
\label{sec:propagation}

As a particle moves through a medium, multiple forces and events will
affect its trajectory. We will consider neutral elementary particles,
neutrons, so that the effects of gravitational and electromagnetic
forces are negligible. It is therefore a good assumption that a neutron
will travel in a straight line until a physical collision with another
particle. Despite the large number of neutrons present in our
simulation, we will ignore improbable collisions between neutrons
themselves. Neutron collisions with atoms result in an array of
effects, including absorption and scattering. The relative probability
of a collision occurring and the resulting type of collision are
dependent on the atom that the neutron collides with and the energy of the
neutron. Modeling the propagation of neutrons is accomplished through
a variety of algorithms, some of which we describe in this section.

\subsection{Ray Tracing}
\label{sec:ray_tracing}

The basic algorithm for simulating particle transport using Monte Carlo is
ray tracing. This method follows a particle from collision to
collision, assuming that it travels in a straight, statistically-sampled
path. Material properties determine the distance between
collisions, and therefore material boundaries must be considered explicitly.

Particles propagating through a material have an interaction
probability characterized by the material's experimentally-determined
total cross section, a function of position and the incident neutron
energy, $\Sigma_t(\mathbf{r}, E)$. The probability of an interaction
occurring in a differential distance $ds$ is related to the
macroscopic cross-section:
\begin{equation}
  \label{eq:macroscopic}
  \frac{dP}{ds} = \Sigma_t(\mathbf{r}, E)\:.
\end{equation}
We assume that each interaction will remove the neutron from the
incident flux, as it is absorbed or deflected out of the original path
in position, energy, or both.  The medium therefore attenuates
an incident mono-energetic neutron flux $\phi_0$ as a function of
distance:
\begin{equation}
  \label{eq:attenuation}
  \phi(s) = \phi_0 e^{-s\Sigma_t(\mathbf{r})}\:.
  % I don't think you defined \phi_{0}
% It was defined just before the equation, should I make it more
% explicit? -jsr
\end{equation}
The probability that a neutron has its first interaction in
differential distance $ds$ after
traveling a distance $s$ is found by dividing the flux at that
position $\phi(s)$ by the total flux:
\begin{equation}
  \label{eq:PDFfunct}
  f(s)ds = \frac{\phi(s)}{\int_0^\infty\phi(s)ds}ds\:.
\end{equation}
The basis of Eq.~\eqref{eq:PDFfunct} is clarified by an example: if
the flux after a distance $s$ is half the total flux, then half of the
neutrons have undergone an interaction.

We consider the cross-section in a single material region, where we
assume that the material is homogeneous. In this case, the
cross-section is no longer a function of position.  Using
Eqs.~\eqref{eq:attenuation} and \eqref{eq:PDFfunct}, and assuming that
the total cross-section is constant over $\mathbf{r}$\cite{lux1991}:
\begin{equation}
  \label{eq:PDF}
  f(s)ds = \Sigma_te^{-s\Sigma_t}ds\:.
\end{equation}
This is the \gls{pdf} of the distance
traveled before the first collision, $s$. As the distance a neutron travels
increases, the value of $f(s)$ decreases; it is less probable that a
neutron will travel further without collision. The \gls{pdf} gives us the
probability at a point $s$ that a neutron undergoes a collision
exactly there; integrating over the distance $s$ provides us the
probability that a neutron undergoes a collision between 0 and $s$. This
is the \gls{cdf}:
\begin{equation}
  \label{eq:cdf}
  F(s) = \int_0^s f(s')ds' = 1-e^{-s\Sigma_t}\:.
\end{equation}
As expected, increasing $s$ causes the probability of any interaction,
$F(s)$, to approach unity. Plots illustrating the behavior of the
\gls{pdf} and \gls{cdf} are shown in Fig.~\ref{fig:cdf_pdf}.
\begin{figure}[hbt]
  \centering
  \includegraphics[scale=0.75]{images/cdf_pdf}
  \caption{Plots of the \acrshort{cdf} and \acrshort{pdf} for collision probability per distance travelled $s$.}
  \label{fig:cdf_pdf}
\end{figure}

As the \gls{cdf} ranges from zero to unity, we can sample its value by
a uniformly distributed random variable $\xi \in [0,1)$. The distance

a function of this sampled random variable:
\begin{align*}
  F(s) = 1 - e^{-s\Sigma_t} &= \xi \:,\\
  \ln(e^{-s \Sigma_t}) &= \ln(1-\xi)\:, \\
  -\Sigma_t s &= \ln(\xi) \:,\\
  s(\xi) &= \frac{1}{\Sigma_t}\ln(\xi)\:.
\end{align*}

After sampling the path length of the neutron, its position is
updated based on its original position and direction. We assumed that
the cross-section $\Sigma_t$ was constant in a material region, so the
sampled path length is only valid as long as the neutron remains in
that region.  If the neutron reaches the boundary between two material
regions, a new path length must be sampled using the cross-section of
the region it is entering. Each time a path length is sampled, the
distance to the nearest boundary in the direction of motion is
determined, and the neutron is moved to the boundary and path length
is resampled if appropriate. This can become computationally expensive
in complicated geometries and when the probability of crossing
boundaries with each sample path length is high.

\section{Woodcock Delta-tracking}
\label{sec:delta-tracking}

As discussed in Section~\ref{sec:ray_tracing}, the value of $\Sigma_t$
at a given position depends on the material at that point. Therefore,
$\Sigma_t(\vec{r})$ is a piece-wise discontinuous function that varies
arbitrarily with position and the geometry of the
problem~\cite{leppanen2013}. Using the ray tracing method, neutrons
must stop at boundaries to sample a new path length in a new material
region. To avoid the computational inefficiency that arises in
geometrically-complicated regions, a rejection sampling technique
known as Woodcock delta-tracking was developed~\cite{woodcock1965}.

Woodcock delta-tracking introduces the concept of the majorant
cross-section, chosen to be the maximum of all material total
cross-sections in the region of interest:
\begin{equation}
  \label{eq:majorant}
  \Sigma_\mathrm{maj} \equiv \max_{\mathbf{r} \in \mathbb{V}}\{\Sigma_t(\mathbf{r})\}\:,
\end{equation}
where $\mathbb{V}$ is the volume of interest. The majorant concept is shown in a
one-dimensional region in Fig.~\ref{fig:sigma_maj}.
\begin{figure}[hbt]
  \centering
  \includegraphics[scale=0.75]{images/sigma_maj}
  \caption{Total cross-section as a function of position in one
    dimension. The majorant cross-section is the largest value in the
    region of interest and determines the probability of a real collision.}
  \label{fig:sigma_maj}
\end{figure}
% should y axis be \Sigma_{t} for clarity? Updated -jsr

The majorant
cross-section can also be represented as the summation of the total
cross-section and a delta cross-section:
\begin{equation}
  \label{eq:majorant2}
  \Sigma_\mathrm{maj} = \Sigma_\delta(\mathbf{r}) +
  \Sigma_t(\mathbf{r}), \quad\forall \vec{r} \in \mathbb{V}\:.
\end{equation}
Following from the definition of $\Sigma_\mathrm{maj}$ in
Eq.~\eqref{eq:majorant}, the function $\Sigma_\delta(\mathbf{r})$ is
chosen such that $\Sigma_\mathrm{maj}$ is constant for the entire
region of interest. At the position \textbf{r} where the maximum value
of $\Sigma_t(\mathbf{r})$ occurs, the delta cross-section is zero.

The majorant cross-section is constant throughout the entire region of
interest, so we can treat it as a single material. Following the same derivation in
Section~\ref{sec:ray_tracing}, the \gls{pdf} of the first collision occurring after
$s$ in the region of interest using the majorant cross-section is given by:
\begin{align}
 f_{\mathrm{maj}}(s) &= \Sigma_{\mathrm{maj}}e^{-\Sigma_{\mathrm{maj}}s}\\
& = (\Sigma_{\delta}(\mathbf{r}) + \Sigma_{t}(\mathbf{r}))e^{-\Sigma_{\mathrm{maj}}s}\:.
  \label{eq:majorantpdf}
\end{align}

In most of the region of interest, the majorant cross-section is not
the real cross-section. To accurately preserve physics, we must use a technique called rejection
sampling to simulate sampling the real $\Sigma_t(\vec{r})$ while
actually sampling using $\Sigma_\mathrm{maj}$.

\subsubsection{Rejection Sampling}
\label{sec:rejection_sampling}
As described by Lux and
Koblinger~\cite{lux1991}, rejection sampling requires a \gls{pdf} of
interest, $f(x)$, and a second \gls{pdf} $g(x)$ for which:
\begin{equation}
  \label{eq:Mleq}
  f(x) \leq M\cdot g(x), \quad \forall x\:,
\end{equation} where $M \in \mathbb{R}$ is a constant.
Sampling from $M\cdot g(x)$ and accepting these samples with probability:
\begin{equation}
  \label{eq:preal}
  P = \frac{f(x)}{M\cdot g(x)}
\end{equation}
replicates sampling directly from $f(x)$.

For example, consider a
\gls{pdf} of interest $f(x) = x^2$ on the interval $x \in (0,1]$ as shown
in Fig.~\ref{fig:circle_square}.
\begin{figure}[hbt]
  \centering
  \includegraphics[scale=0.75]{images/circle}
  \caption{Example \acrshort{pdf} function $f(x) = x^2$.}
  \label{fig:circle_square}
\end{figure}
As described in the previous section, we could sample $f(x)$ using the
$F(x)$ found by integrating and then inverting. Instead, we can sample from a
different \gls{pdf} that is always majorant of $f(x)$, as defined in
Eq.~\eqref{eq:Mleq}. We can choose $g(x) = M = 1$, as $f(x) \leq 1$ on
the interval of interest. The \gls{cdf} of $g(x)$ is easy to
determine, because it is a constant value, and is just $G(x) =
x$. Therefore, we can sample $g(x)$ by simply sampling a random value $\xi
\in (0,1]$ and taking this as the value of $x$. To replicate sampling
from $f(x)$, we then sample another random value $\xi_2 \in (0,1]$ and
accept our value of $x$ using the probability defined in Eq.~\eqref{eq:preal}:
\begin{equation*}
  \xi_2 \leq \frac{f(x)}{M \cdot g(x)} = x^2\:.
\end{equation*}
% may what to mention that M=1 in this case?
\begin{minipage}{1.0\linewidth}
The following Matlab script replicates this procedure and generates
the histogram shown in Fig.~\ref{fig:pdf_histogram}.
\begin{lstlisting}
a = [];                     % Accepted samples
for i = 1:1000
    x = rand;               % Sample g(x)
    y = rand;               % Gen random number xi2
    if y <= x.^2            % If xi2 <= f(x)/g(x)
        a(end+1) = x;       % Accept sample
    end
end
\end{lstlisting}
\end{minipage}
As expected, the procedure has reproduced the desired \gls{pdf} of
$f(x) = x^2$. Although this requires sampling two random variables, it
can be advantageous if $F(x)$ is difficult or impossible to invert.
\begin{figure}[hbtp]
  \centering
  \includegraphics[scale=0.75]{images/pdf_histogram}
  \caption{Histogram of results from sampling and applying the
    rejection sampling algorithm.}
  \label{fig:pdf_histogram}
\end{figure}
\subsubsection{Application to Delta-tracking}
\label{sec:application_to_delta-tracking}

As we saw in Fig.~\ref{fig:sigma_maj}, the total cross-section
$\Sigma_t$ is a piecewise discontinuous function that depends on the
geometry of our problem. Therefore, each region has a different
\gls{cdf} for sampling path length, and described in
Section~\ref{sec:ray_tracing}. Using rejection sampling, we can sample
the real collision \gls{pdf}, using a different, simpler \gls{pdf}.
In Woodcock delta tracking, the second \gls{pdf}, $g(x)$, is chosen to
be the majorant \gls{pdf}, Eq.~\eqref{eq:majorantpdf}. This is
beneficial because the majorant cross-section is constant over the
entire region.

These functions are both maximized at $x=0$, where the inequality of
Eq.~\eqref{eq:Mleq} is satisfied by setting $M=1$:
\begin{equation}
  \label{eq:cseq}
  \Sigma_t(\mathbf{r}) \leq \Sigma_\mathrm{maj}(\mathbf{r}), \forall
  \mathbf{r} \in \mathbb{V} \:.
\end{equation}
We sample path length using the constant majorant
cross-section:
\begin{equation}
  \label{eq:majorantsample}
  s_\mathrm{maj}(\xi) = -\frac{1}{\Sigma_\mathrm{maj}}\ln(\xi)\:,
\end{equation}
which samples the \gls{pdf} defined in Eq.\eqref{eq:majorantpdf}.
% don't need to repeat equation; I think you mean this eqref?
% Can you clarify this comment? -jsr
We can see that this \gls{pdf} is the sum of two different ones: one
representing actual collisions based on the real total cross-section
$\Sigma_t$ and one representing the non-physical collisions based on
$\Sigma_\delta$. We call these non-physical collisions ``virtual''
collisions, and these should be eliminated by our rejection
sampling. To apply rejection sampling, we therefore pick the \gls{pdf}
we want to sample, $f(x)$, and the \gls{pdf} we will actually sample,
$g(x)$, as such:
\begin{align*}
  f(x) &= \Sigma_t(\vec{r})e^{-\Sigma_\mathrm{maj}s} \:,\\
  g(x) &= \Sigma_\mathrm{maj}e^{-\Sigma_\mathrm{maj}s}\:.
\end{align*}
The equation $g(x)$ is always majorant to $f(x)$, by the definition of
$\Sigma_{\mathrm{maj}}$, so we set $M=1$. We will therefore accept samples with the
probability given in Eq.~\eqref{eq:preal}:
\begin{align}
  \label{eq:prealfinal}
  P_{\mathrm{real}}(\vec{r}) &= \frac{f(x)}{M \cdot g(x)} =
      \frac{\Sigma_t(\mathbf{r})e^{-\Sigma_\mathrm{maj}s}}{\Sigma_\mathrm{maj}e^{-\Sigma_\mathrm{maj}s}}
  = \frac{\Sigma_t(\mathbf{r})}{\Sigma_\mathrm{maj}}\:.
\end{align}
% what's M?
We will refer to this as the probability of a ``real'' collision,
$P_\mathrm{real}$, to differentiate these physical collisions from the
non-physical virtual collisions. It is important to note that the
probability is independent of path length $s$, but is dependent on
position $\vec{r}$. At each collision, the material region at the
neutron position must be determined, but we do not need to explicitly
track boundaries nor calculate their distance each time a path length
is sampled. The algorithm for delta-tracking is shown in
Fig.~\ref{fig:dt}.

\begin{figure}[p]
  \centering
  \begin{algorithm}[H]
\caption{Delta-tracking}\label{alg:dt}
\begin{algorithmic}[1]
  \State \textbf{Sample} path length

  \State \textbf{Look up} location to get $\Sigma_t(\vec{r})$
  \State $P_\mathrm{real} \gets
  \frac{\Sigma_t(\vec{r})}{\Sigma_\mathrm{maj}}$
  \State \textbf{Sample} random number $\xi \in [0,1)$
  \If{$\xi < P_\mathrm{real}$} \Comment{Collision is real}
  \State \textbf{Execute} real collision
    \Else \Comment{Collision is virtual}
    \State \textbf{Execute} virtual collision
  \EndIf
\end{algorithmic}
\end{algorithm}
\includegraphics[scale=0.5]{images/dt}
  \caption{Delta-tracking algorithm and flow chart.}
  \label{fig:dt}
\end{figure}

Now, the path length can be sampled across multiple material regions
of varying $\Sigma_t$ without explicitly stopping the neutron at a
given boundary. This allows us to avoid determining the distance to
the nearest boundary after each path length sampling. Delta-tracking
is also much more efficient in regions where the geometry is complex
or material region thicknesses are on the same order as the path
length. % I'd add a sentence about why this is important--make us see that this is a thing it is
% a good idea to choose sometimes; the imbalance in talking about the negative
% properties leaves me wondering why anyone would choose it.
This method can become computationally inefficient in
regions where the total cross-section is much less than the majorant
cross-section, leading to oversampling of virtual collisions. This is
seen in geometries that include localized absorbers, such as control
rods.

% Another downside is that the \gls{tle} for
% flux cannot be used.
% % Apparently this isn't true. I didn't get the details, but this came up for Kelly at M&C
% % Check with her to see if she found the reference about it.
% The \gls{tle} requires calculating the track-lengths
% within a particular material cell, and therefore does not work when
% the neutron path length can cross one or more material
% boundaries. The \gls{cfe} can be used in its
% place, but often results in inferior statistics as not every track
% length sampled ends in a collision~\cite{leppanen2013}.
% % May want to elaborate on all of this, but it'll likely change
% given previous comment.
%
% I commented it out, I'll email Jaakko about his paper, which I use
% as a reference for it. - JSR

\section{The Serpent 2 Monte Carlo Code}
\label{sec:serpent2}

The Serpent Monte Carlo code was developed at \gls{vtt} as a PhD
thesis project~\cite{leppanen2007}. A second iteration of the code,
Serpent 2, is currently under development. Both versions of Serpent use a combination of surface tracking, Woodcock
delta-tracking, and rejection sampling for non-uniform density
distributions. Serpent 2 selects between surface tracking and
delta-tracking by examining the ratio of total cross-section to
majorant cross-section~\cite{leppanen2010}. In regions where many
virtual collisions would occur, the code preferentially switches to
ray tracing. This is to avoid the computational inefficiency of
processing virtual collisions that provide no statistics. This
selection is determined by a constant $c$ and the inequality in
Eq.~\eqref{eq:s2deltasurface}.
\begin{equation}
  \label{eq:s2deltasurface}
  \frac{\Sigma_t(\vec{r})}{\Sigma_\mathrm{maj}} > 1 - c
\end{equation}

If this inequality is true, delta-tracking is used, otherwise
ray-tracing (referred to as surface-tracking in Serpent 2) is used. By
default, the value of $c$ is 0.9, as this was determined to produce
the best improvement in run time~\cite{leppanen2010}. We note that
this inequality is identical to the value of $P_\mathrm{real}$. Prior to
sampling path length, the code tests this ratio for the current
neutron position and determines if surface tracking or delta-tracking
should be used. If delta-tracking is used, the code then determines if
the collision is virtual or real. The value of $c$ can be set in the
input file using \verb|set dt|.
% \begin{figure}[hbtp]\centering
%   \begin{tikzpicture}[scale=1.5]
%     \draw[thick] (0,0) -- (10.0,0);
%     \foreach \x in {0,1,...,10}
%     {
%       \draw (\x, 0.1) -- (\x, -0.1);
%       \pgfmathsetmacro\result{\x * 0.1}
%       \node [below] at (\x, -0.2) {\small $\pgfmathprintnumber{\result}$};
%     }
%     \node [left] at (0,0) {$P_{\mathrm{real}}$};
%     \draw[ thick, <->] (0,0.2) -- (1,0.2);
%     \draw[ thick, <->] (1,0.2) -- (10,0.2);
%     \node [above] at (0.5, 0.2) {Ray Tracing};
%     \node [above] at (5.5, 0.2) {Delta-tracking};
%   \end{tikzpicture}
%   \caption{Ray-tracing and delta-tracking threshold values in
%     standard Serpent 2.}
%   \label{fig:ray_wdt_normal}
% \end{figure}
% Not sure that this is really illustrating anything.

% \subsubsection{Nonuniform Density Distributions}
% \label{sec:nonuniform}

% Rejection sampling can also be used when the total cross-section is
% not constant within a material region. As discussed by
% Lepp\"{a}nen~\cite{leppanen2013}, Serpent 2 conducts a rejection
% sampling routine similar to Woodcock delta-tracking in these
% regions. Instead of sampling from a majorant cross-section across
% multiple materials, a maximum cross-section for the single material
% region is used. Unlike delta-tracking, this requires stopping the
% neutron at boundaries and resampling path lengths. This algorithm is
% not modified by the implementation of \gls{wdt} and none of the input
% files used to assess \gls{wdt} use this feature.
% Wait, maybe I missed something, but I don't know what's going on this section / paragraph.
%
% This is extraneous and outside the scope of this. Not even sure why
% I put it in here. -JSR

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../masters_report"
%%% End: