\begin{document}

\chapter{Appendix}

\label{chapter:appendix}

This appendix contains plots from the experiments conducted on Scenes 2 and 3.
Note that for Scene 2, the potential fields planner was not able to complete
find a path to the goal on sufficiently many cases and thus results could not
be concluded. This is a consequence of using a potential fields planner, it may
not bring the robot to the goal if it gets stuck in a local minima.

\chapter*{Appendix I\\Results for Scene 2}

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.80\linewidth]{figs/planner_mean_min_distance_1}

    \caption{Plots showing how the minimum distance to any obstacle for a path
        changes as the speed increases for various amounts of obstacle position
        uncertainties.  The horizontal axis represents the speed of the robot
        and the vertical axis represents the minimum distance to obstacles
        along the path. The different lines on each plot represent experiments
    with differing amounts of noise and the error bars represent one standard
deviation.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.80\linewidth]{figs/planner_mean_max_cost_1}

    \caption{Plots showing how the maximum cost for a path changes as the
        speed increases for various amounts of obstacle position uncertainties.
        The horizontal axis represents the speed of the robot and the vertical
        axis represents the maximum cost along the path. The different lines on
    each plot represent experiments with differing amounts of noise and the
error bars represent one standard deviation.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.80\linewidth]{figs/planner_mean_avg_cost_1}

    \caption{Plots showing how the average cost for a path changes as the
        speed increases for various amounts of obstacle position uncertainties.
        The horizontal axis represents the speed of the robot and the vertical
        axis represents the average cost along the path. The different lines on
    each plot represent experiments with differing amounts of noise and the
error bars represent one standard deviation.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.80\linewidth]{figs/planner_std_min_distance_1}

    \caption{Plots showing how the standard deviation for the minimum distance
        along a path changes as the noise injected into the obstacle
        trajectories increases. The horizontal axis represents the amount of
        noise and the vertical axis represents the standard deviation. The
        different lines indicate different speeds that the robot was
    travelling.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.48\linewidth]{figs/planner_std_max_cost_1}
    \includegraphics[width=0.48\linewidth]{figs/planner_std_avg_cost_1}

    \caption{Plots showing how the standard deviation for the maximum cost and
        average cost along a path changes as the noise injected into the
        obstacle trajectories increases. The horizontal axis represents the
        amount of noise and the vertical axis represents the standard
        deviation. The different lines indicate different speeds that the robot
        was travelling. On the left is the graph for Dodger and on the right is
    the graph for the potential fields planner.}

\end{figure}

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.48\linewidth]{figs/planner_mean_times_1}
    \includegraphics[width=0.48\linewidth]{figs/planner_small_mean_times_1}

    \caption{Plots showing how the computational to generate a path changes as
        the speed increases for various amounts of obstacle position
        uncertainties.  The horizontal axis represents the speed of the robot
        and the vertical axis represents the computational time to generate the
        path. The different lines on each plot represent experiments with
        differing amounts of noise and the error bars represent one standard
        deviation.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.80\linewidth]{figs/planner_std_avg_times_1}

    \caption{Plots showing how the standard deviation for the computational
        cost to generate a path changes as the noise injected into the obstacle
        trajectories increases.  The horizontal axis represents the amount of
        noise and the vertical axis represents the standard deviation. The
    different lines indicate different speeds that the robot was travelling.}

\end{figure}

\chapter*{Appendix II\\Results for Scene 3}

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.48\linewidth]{figs/planner_mean_min_distance_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_mean_min_distance_2}

    \caption{Plots showing how the minimum distance to any obstacle for a path
        changes as the speed increases for various amounts of obstacle position
        uncertainties.  The horizontal axis represents the speed of the robot
        and the vertical axis represents the minimum distance to obstacles
        along the path. The different lines on each plot represent experiments
    with differing amounts of noise and the error bars represent one standard
deviation. On the left is the graph for Dodger and on the right is the graph
for the potential fields planner.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.48\linewidth]{figs/planner_mean_max_cost_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_mean_max_cost_2}

    \caption{Plots showing how the maximum cost for a path changes as the
        speed increases for various amounts of obstacle position uncertainties.
        The horizontal axis represents the speed of the robot and the vertical
        axis represents the maximum cost along the path. The different lines on
    each plot represent experiments with differing amounts of noise and the
error bars represent one standard deviation.  On the left is the graph for
Dodger and on the right is the graph for the potential fields planner.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.48\linewidth]{figs/planner_mean_avg_cost_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_mean_avg_cost_2}

    \caption{Plots showing how the average cost for a path changes as the
        speed increases for various amounts of obstacle position uncertainties.
        The horizontal axis represents the speed of the robot and the vertical
        axis represents the average cost along the path. The different lines on
    each plot represent experiments with differing amounts of noise and the
error bars represent one standard deviation.  On the left is the graph for
Dodger and on the right is the graph for the potential fields planner.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.48\linewidth]{figs/planner_std_min_distance_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_std_min_distance_2}

    \caption{Plots showing how the standard deviation for the minimum distance
        along a path changes as the noise injected into the obstacle
        trajectories increases. The horizontal axis represents the amount of
        noise and the vertical axis represents the standard deviation. The
        different lines indicate different speeds that the robot was
    travelling. On the left is the graph for Dodger and on the right is the
graph for the potential fields planner.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.48\linewidth]{figs/planner_std_max_cost_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_std_max_cost_2} \\
    \includegraphics[width=0.48\linewidth]{figs/planner_std_avg_cost_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_std_avg_cost_2}

    \caption{Plots showing how the standard deviation for the maximum cost and
        average cost along a path changes as the noise injected into the
        obstacle trajectories increases. The horizontal axis represents the
        amount of noise and the vertical axis represents the standard
        deviation. The different lines indicate different speeds that the robot
        was travelling. On the left is the graph for Dodger and on the right is
    the graph for the potential fields planner. The top row is for the maximum
cost and the bottom row is the for the average cost.}

\end{figure}

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.48\linewidth]{figs/planner_mean_times_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_mean_times_2} \\
    \includegraphics[width=0.48\linewidth]{figs/planner_small_mean_times_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_small_mean_times_2}

    \caption{Plots showing how the computational to generate a path changes as
        the speed increases for various amounts of obstacle position
        uncertainties.  The horizontal axis represents the speed of the robot
        and the vertical axis represents the computational time to generate the
        path. The different lines on each plot represent experiments with
        differing amounts of noise and the error bars represent one standard
        deviation.  On the left is the graph for Dodger and on the right is the
    graph for the potential fields planner.}

\end{figure}

\begin{figure}[h!]
    \centering

    \includegraphics[width=0.48\linewidth]{figs/planner_std_avg_times_2}
    \includegraphics[width=0.48\linewidth]{figs/pf_std_avg_times_2}

    \caption{Plots showing how the standard deviation for the computational
        cost to generate a path changes as the noise injected into the obstacle
        trajectories increases.  The horizontal axis represents the amount of
        noise and the vertical axis represents the standard deviation. The
    different lines indicate different speeds that the robot was travelling. On
the left is the graph for Dodger and on the right is the graph for the
potential fields planner.}

\end{figure}

\end{document}


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

\chapter{Quantum ElectroDynamics}

\section{QED \(S\)-matrix expansion}

\marginpar{Monday\\ 2020-6-15, \\ compiled \\ \today}

As an explicit example of the theory we developed for an interacting QFT, we consider Quantum ElectroDynamics.

The Lagrangian is written as
%
\begin{align}
\mathscr{L} =
- \frac{1}{4} F^{\mu \nu } F_{\mu \nu }
+ \mathscr{L} _{\text{gauge-fixing}}
+ i q \overline{\psi} \qty(i\slashed{\DD} - M) \psi
\,,
\end{align}
%
where \(q\) is the charge of the fermion --- for instance, electrons have \(q = - \abs{e}\).
This can be decomposed as \(\mathscr{L} = \mathscr{L}_{0} + \mathscr{L} _{\text{int}}\), with
%
\begin{align}
\mathscr{L} _{\text{int}} = - q \overline{\psi} \gamma^{\mu } \psi A_{\mu } = - \mathscr{H} _{\text{int}}
\,.
\end{align}

Notice that the interaction Lagrangian and Hamiltonian are opposite of each other: this holds in general, as long as there are no derivative terms --- basically, we have an interaction \emph{potential}.

We are working in the \textbf{interaction picture}.

The \(S\)-matrix operator \eqref{eq:s-matrix} for QED reads:
%
\begin{align}
S _{\text{QED}}
&= T \qty[\exp(-iq \int \dd[4]{x} N [\overline{\psi}(x) \slashed{A} (x) \psi (x)])]  \\
&= \sum _{n=0}^{ \infty }
\frac{(-iq)^{n}}{n!}
\int \dd[4]{x_1 } \dots \dd[4]{x_{n}}
T \qty[N[\dots]_{x_1} \dots N[\dots]_{x_n}]
\,.
\end{align}

\subsection{Expansion in position space}

\subsubsection{0th order term}

It is just the identity, since it corresponds to no interaction:
%
\begin{align}
S^{(0)} = \mathbb{1}
\,.
\end{align}

\subsubsection{1st order term}

It is given by
%
\begin{align}
S^{(1)} &= - iq \int \dd[4]{x} T \qty[N[\overline{\psi}(x) \slashed{A} (x) \psi (x)]]  \\
&= - iq \int \dd[4]{x} N[\overline{\psi}(x) \slashed{A} (x) \psi (x)]
\,.
\end{align}

In order to remove the time-ordering we have used the corollary of Wick's theorem: we know that
%
\begin{align}
T[N[A(x) B(x)]] = T[A(x) B(x)]_{\text{NCET}}
\,,
\end{align}
%
so we can apply Wick's theorem to the last expression: we only have the first term, since all the possible contractions would be equal-time ones, therefore we get
%
\begin{align}
T[A(x) B(x)]_{\text{NCET}} = N[A(x)B(x)]
\,.
\end{align}

For three operators the reasoning is exactly the same.

We can split \(\overline{\psi} \), \(\slashed{A}\) and \(\psi \) into their \(+\) and \(-\) components: we get eight contributions,
%
\begin{align}
N \qty[
    \qty(\overline{\psi}_{+} + \overline{\psi}_{-})
    \qty(\slashed{A}_{+} + \slashed{A}_{-})
    \qty(\psi_{+} + \psi_{-})
]
= N \qty[
    \sum \overline{\psi}_{\pm} \slashed{A}_{\pm} \psi_{\pm}
]
\,,
\end{align}
%
which we can represent using Feynman diagrams.
All of these diagram only have one vertex at \(x\), and they have a fermion, an antifermion and a photon being created/annihilated there (all the 8 possible combinations). As an example, we show:
%
\begin{align} \label{eq:fermion-fermion-photon-vertex-1}
\overline{\psi}_{+} \slashed{A}_{-} \psi_{+}
= \feynmandiagram[inline=(c.base), horizontal = c to p]{
    c [particle=\(x\)] -- [photon] p [particle=\(\gamma \)],
    a [particle=\(e^{-}\)] -- [fermion] c -- [fermion] b [particle=\(e^{+}\)]
};
\end{align}
%
here we have an electron and a positron annihilating to form a photon.

\todo[inline]{I think this vertex has a wrong sign in the professor's notes.}

In general, particles which are created (so, with subscript \(-\)) go rightward, particles which are annihilated (with subscript \(+\)) come from the left.

These diagrams are \emph{vertices}: they represent a certain interaction in QED, between 2 fermions and a photon.

Notice that the flow of the fermion arrows is continuous in the diagram we wrote: this generalizes to all of them, and it is a manifestation of the \(U(1)\) charge conservation.

Now, all these 8 diagrams represent \textbf{unphysical processes}: due to the fact that the photon is massless, relativistic kinematics forbids them.

Let us show it for the process \eqref{eq:fermion-fermion-photon-vertex-1}, which can be written in more usual notation as
%
\begin{align}
e^{+}(p) + e^{-} (p') \to \gamma (k)
\,.
\end{align}

Relativistic kinematics tells us that \(p + p' = k\).
The fermions are massive, so we can go to their center-of-mass frame before the annihilation: in this frame, we will have \((p + p')_{i} = 0 \), which implies \(k_{i} = 0 \), which is not possible since \(k^{0} = \abs{\vec{k}}\) (and the photon must have positive energy).

Consider another process:
%
\begin{align}
e^{-}(p) \to e^{-} (p') + \gamma (k)
\,,
\end{align}
%
which is represented diagrammatically as
\begin{align} \label{eq:fermion-fermion-photon-vertex-2}
\overline{\psi}_{-} \slashed{A}_{-} \psi_{+}
= \feynmandiagram[inline=(c.base), horizontal = a to c]{
    c [particle=\(x\)] -- [photon] p [particle=\(\gamma \)],
    a [particle=\(e^{-}\)] -- [fermion] c,
    c -- [fermion] b [particle=\(e^{-}\)]
};
\end{align}

This is also forbidden (all of them are). To show this, let us go to the rest frame of the incoming electron: starting from the relation \(p = p' + k\) we get
%
\begin{align}
m_e &= p^{\prime 0} + k^{0}  \\
0 &= \vec{p}' + \vec{k}
\,,
\end{align}
%
so, since for the photon \(k^{0} = \abs{k}\), we can write the first relation as
%
\begin{align}
m_e &= p^{\prime 0} - \abs{\vec{p}'}  \\
m_e^2 &= \qty(p^{\prime 0} - \abs{\vec{p}'})^2  \\
\qty(p^{\prime 0})^2 - \abs{\vec{p}'}^2 &= \qty(p^{\prime 0})^2
- 2 p^{\prime 0} \abs{\vec{p}'} + \abs{\vec{p}'}^2  \\
0 &= - 2 p^{\prime 0} \abs{\vec{p}'} + 2 \abs{\vec{p}'}^2
\,,
\end{align}
%
which means that either \(\vec{p}' = 0\) (unacceptable, it would mean the photon has zero energy) or \(p^{\prime 0} = \abs{\vec{p}'}\), which tells us that the electron is massless, which it is not.

Some of these processes are \textbf{allowed} if the boson is \textbf{massive}: for example, we can have
%
\begin{align}
Z(k) \to e^{+}(p) + e^{-}(p')
\,,
\end{align}
%
where the mass of the \(Z\) boson satisfies \(M^2_Z > (2 m_e)^2\).
This works since we can go to the rest frame of the \(Z\) boson, and if the inequality is satisfied the electron and positron momenta can be real.

\subsubsection{2nd order term}

Since the first order processes are unphysical, we have to go to the second order. The second term in the \(S\)-matrix expansion reads:
%
\begin{align}
S^{(2)} = \frac{(-iq)^2 }{2!}
\int \dd[4]{x} \dd[4]{y}
T \qty[
    N[\overline{\psi} \slashed{A} \psi ]_{x}
    N[\overline{\psi} \slashed{A} \psi ]_{y}
]
\,.
\end{align}

By the corollary of Wick's theorem, we can write
%
\begin{align}
T \qty[
    N[\overline{\psi} \slashed{A} \psi ]_{x}
    N[\overline{\psi} \slashed{A} \psi ]_{y}
]
=
T \qty[
    \qty(\overline{\psi} \slashed{A} \psi) _{x}
    \qty(\overline{\psi} \slashed{A} \psi )_{y}
] _{\text{NCET}}
\,,
\end{align}
%
so when we compute the contractions we can only connect fields computed at \(x\) with ones computed at \(y\). Also, contractions between different types of fields vanish, as do contractions between a complex-valued field and itself. So, we have:
\begin{enumerate}
    \item the 0 contraction term
    %
    \begin{align}
        \underbrace{N \qty[
            \qty(\overline{\psi} \slashed{A} \psi) _{x}
            \qty(\overline{\psi} \slashed{A} \psi )_{y}
        ]}_{\Circled{A}}
    \,,
    \end{align}
    %
    \item the 1 contraction term
    %
    \begin{align}
        \underbrace{N \left[
            \contraction{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right] +
        N \left[
            \contraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right]}_{\Circled{B_1}} +
        \underbrace{N \left[
            \contraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right]}_{\Circled{B_2}}
    \,,
    \end{align}
    %
    \item the 2 contraction term
    %
    \begin{align}
        \underbrace{N \left[
            \bcontraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
            \contraction{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right]+
        N \left[
            \bcontraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
            \contraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right]}_{\Circled{C_1}}+
        \underbrace{N \left[
            \bcontraction{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
            \contraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right]}_{\Circled{C_2}}
    \,,
    \end{align}
    %
    \item and the 3 contraction term
    %
    \begin{align}
        \underbrace{N \left[
            \contraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
            \bcontraction[2ex]{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
            \bcontraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
            (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
        \right]}_{\Circled{D}}
    \,.
    \end{align}
\end{enumerate}

For any of these we can write a specific propagator, whose expression is
%
\begin{align}
S_X = \frac{(-iq)^2}{2!} \int \dd[4]{x} \dd[4]{y} \Circled{X}
\,,
\end{align}
%
where \(\Circled{X}\) is any of the terms we have just written.

\subsubsection{No propagators: \(\Circled{A}\)}

The diagram looks like the product of two disconnected first-order diagrams. Since they were not physically allowed, this is not allowed either.

If our vector were massive, like the \(Z\) boson, this would describe two separate decay processes.

\subsubsection{One fermion propagator: \(\Circled{B_1}\)}

This term looks like
%
\begin{align}
S_{B_1 } &= \frac{(-iq)^2}{2!} \int \dd[4]{x} \dd[4]{y} \Circled{B_1}  \\
&= \frac{- q^2}{2!} \int \dd[4]{x} \dd[4]{y}
N \left[
    \contraction{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
    (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
\right] +
N \left[
    \contraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
    (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
\right]  \\
&= - q^2 \int \dd[4]{x} \dd[4]{y}
N \qty[
    \overline{\psi} (x) \slashed{A}(x)
    S_F(x-y)
    \slashed{A}(y) \psi (y)
]
\,,
\end{align}
%
since
%
\begin{align}
S_F(x-y) = \bcontraction{}{\psi}{ (x) }{\overline{\psi}}
\psi (x) \overline{\psi} (y)
\,,
\end{align}
%
and the two contributions are equal, since inside of the normal ordering the fermion operators anticommute, we need to swap an even number of them, and changing \(x \leftrightarrow y\) is not a problem since they are both integrated away.

Now, we have 4 free fields, which correspond to \(2^{4}= 16\) terms in the \(\pm\) fields.

\begin{claim}
The only physical processes are those in which 2 particles are created and 2 are annihilated.
\end{claim}

\begin{proof}
4-momentum conservation must hold: its \(0\)-th component already disqualifies \(0 \leftrightarrow 4\) processes, since on one side of the process we have zero energy.

As for \(1 \leftrightarrow 3\) processes: if the 1 particle is a fermion, we go to its rest frame, then the \(0\)-th component of the momentum conservation imposes that the sum of the energies of the 3 particles must equal the mass of the fermion. One of the three particles is that kind of fermion, so its energy is \(\geq \) the mass, which implies that the photons must have zero energy.

If the 1 particle is a photon, we go to the center of mass of the three particles, where the three-momentum is zero, and find a contradiction since the photon does not have a rest frame.
\end{proof}

The possible diagrams are then only six. They are:
%
\begin{align}
(\overline{\psi}_{-} \slashed{A}_{-})_{x} S_F (x-y) (\psi_{+} \slashed{A}_+)_{y} =
\feynmandiagram[baseline=(y.base), large, horizontal = y to x]{
    g1 [particle = \(\gamma \)] -- [photon, edge label = {\(\slashed{A}_+(y)\)}] y,
    e1 [particle = \(e^{-}\)] -- [fermion, edge label = {\(\psi_{+} (y)\)}] y,
    y -- [fermion, edge label = {\(S_F(x-y)\)}] x ,
    x -- [photon, edge label = {\(\slashed{A}_{-}(x)\)}] g2 [particle = \(\gamma \)],
    x -- [fermion, edge label = {\(\overline{\psi}_{-}(x)\)}] e2 [particle = {\(e^{-}\)}]
};
\,,
\end{align}
%
which contributes to Compton scattering,
%
\begin{align}
(\overline{\psi}_{-} \slashed{A}_{+})_{x} S_F (x-y) (\psi_{+} \slashed{A}_{-})_{y} =
\begin{tikzpicture}[baseline=(y)]
    \begin{feynman}
        \vertex (y);
        \vertex [below left = of y] (e1) {\(e^{-}\)};
        \vertex [right = 2cm of y] (x);
        \vertex [below right = of x] (e2) {\(e^{-}\)};
        \vertex [above left = of y] (g1) {\(\gamma \)};
        \vertex [above right = of x] (g2) {\(\gamma \)};
        \diagram*{
        (e1) -- [fermion] (y)  -- [fermion] (x) -- [fermion] (e2),
        (y) -- [photon] (g2),
        (g1) -- [photon] (x),
        };
    \end{feynman}
\end{tikzpicture}
\,,
\end{align}
%
which also contributes to Compton scattering, and
%
\begin{align}
(\overline{\psi}_{+} \slashed{A}_{-})_{x} S_F (x-y) (\psi_{+} \slashed{A}_-)_{y} =
\begin{tikzpicture}[baseline=(center)]
    \begin{feynman}
        \vertex (y);
        \vertex [above left = of y] (e1) {\(e^{-}\)};
        \vertex [below = 2cm of y] (x);
        \vertex [below left = of x] (e2) {\(e^{-}\)};
        \vertex [above right = of y] (g1) {\(\gamma \)};
        \vertex [below right = of x] (g2) {\(\gamma \)};
        \node (center) at ($(y)!0.5!(x)$) {};
        \diagram*{
        (e1) -- [fermion] (y)  -- [fermion] (x) -- [fermion] (e2),
        (y) -- [photon] (g1),
        (g2) -- [photon] (x),
        };
    \end{feynman}
\end{tikzpicture}
\,,
\end{align}
which contributes to electron-positron annihilation into two photons.

The other three are the left-to-right mirror symmetric of these: two of them contribute to Compton scattering off a positron, one contributes to pair production from two photons. Their expressions are derived from the ones already written, mapping \(+ \leftrightarrow -\). They are:
%
\begin{align}
&\qty(\overline{\psi}_{+} \slashed{A}_{+})_{x}
  S_F (x-y) \qty(\psi_{-} \slashed{A}_{-})_{y} \\
&\qty(\overline{\psi}_{+} \slashed{A}_{-})_{x}
  S_F (x-y) \qty(\psi_{-} \slashed{A}_{+})_{y} \\
&\qty(\overline{\psi}_{-} \slashed{A}_{+})_{x}
  S_F (x-y) \qty(\psi_{+} \slashed{A}_{-})_{y}
\,.
\end{align}

When we draw these diagrams, particles become antiparticles and creation becomes annihilation: so, we take the \(x\) and \(y\) points and swap their position, but in doing so we must keep the arrows rigidly pointing in the same direction, so the arrow in the propagator switches direction.

As we have seen, different diagrams can contribute to the same physical process. We can have either \emph{equivalent} operators, which are the same if we exchange \(x \leftrightarrow y\), and ones which are not equivalent.

The terms \((\overline{\psi}_{-} \slashed{A}_{-})_x (\psi_{+} \slashed{A}_+)_{y}\) and \((\overline{\psi}_{-} \slashed{A}_{+})_x (\psi_{+} \slashed{A})_{y}\), for example, are different and both contribute to Compton scattering.

\subsubsection{One photon propagator: \(\Circled{B_2 }\)}

The term looks like:
%
\begin{align}
S_{\Circled{B_2 }} &= - \frac{q^2}{2!}
\int \dd[4]{x} \dd[4]{y} N \qty[
    \contraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
    (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
]  \\
&=- \frac{q^2}{2!}
\int \dd[4]{x} \dd[4]{y}
D^{\mu \nu}_{F} (x-y) N \qty[
    \qty(\overline{\psi} \gamma_{\mu } \psi )_x
    \qty(\overline{\psi} \gamma_{\nu } \psi )_y
]
\,.
\end{align}

As before, we have 16 possible combinations of \(+\) and \(-\) when we open the normal-ordered product, but only the ones with two destructions and two creations are kinematically allowed.

When drawing these, notice that the bare field always has the arrow pointing towards the vertex, while for the conjugate field the arrow always points away from it.

% The \(++--\) and \(--++\) terms contribute to \(e^{+}e^{-} \to e^{+}e^{-}\), the \(+-+-\) term contributes to \(e^{-} e^{-} \to e^{-} e^{-}\), the \(-+-+\) term contributes to \(e^{+} e^{+} \to e^{+} e^{+}\).

What are the diagrams contributing to \(e^{+} e^{-} \to e^{+}e^{-}\)?
There are exactly 4, the signs of the fermions being
\begin{enumerate}
    \item \(++--\) and \(--++\);
    \item \(-++-\) and \(+--+\).
\end{enumerate}

The first two are equivalent, as are the second two (since the photon propagator is symmetric): so, we count only one of them twice.
Now, we know that \(D^{\mu \nu } (x-y) = - \eta^{\mu \nu } D_F\) as long as we select \(\xi = 1\). Then, we can write
%
\begin{align}
S_{e^{-}e^{+}} = - q^2 \int \dd[4]{x} \dd[4]{y} D_F^{\mu \nu } (x-y)
\qty(\underbrace{N \qty[
    \qty(\overline{\psi}_{-} \gamma_{\mu } \psi_{-} )_x
    \qty(\overline{\psi_{+}} \gamma_{\nu } \psi_{+} )_y
]}_{\Circled{A}}+
\underbrace{N \qty[
    \qty(\overline{\psi_{+}} \gamma_{\mu } \psi_{-} )_x
    \qty(\overline{\psi}_{-} \gamma_{\nu } \psi_{+} )_y
]}_{\Circled{B}}
)
\,.
\end{align}

The two diagrams are topologically distinct: \Circled{A} is called the \textbf{S-channel contribution}, in which the fermions annihilate into a virtual photon which propagates and then annihilates into fermions again.

On the other hand, \Circled{B} is called the \textbf{T-channel contribution}, in which the electron and positron exchange a virtual photon but are never annihilated.

\subsubsection{One photon and one fermion propagator: \(\Circled{C_1 }\)}

We now consider the term
%
\begin{align}
S_{\Circled{C_1 }} &= - \frac{q^2}{2!} \int \dd[4]{x} \dd[4]{y}
N \left[
    \bcontraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
    \contraction{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
    (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
\right]+
N \left[
    \bcontraction{(\overline{\psi} }{\slashed{A}}{ \psi ) _{x} (\overline{\psi} }{\slashed{A}} % ph-ph
    \contraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
    (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
\right]  \\
&= - q^2 \int \dd[4]{x} \dd[4]{y} D^{\mu \nu }_{F} (x-y)
N \qty[\overline{\psi}(x) \gamma_{\mu } S_F (x-y) \gamma_{\nu } \psi (x)]
\label{eq:fermion-self-energy}
\,,
\end{align}
%
since the two components can be cast into each other by renaming \(x \leftrightarrow y\) (which can always be done since they are integrated away), and permuting 4 (\(\equiv 0 \mod 2\)) fermion operators.

The \(0 \to 2\) and \(2 \to 0\) processes are not allowed (they enforce zero energy for all the particles). So, we have two possibilities: either an electron having a loop and coming back to itself, or a positron doing the same.

\begin{figure}[ht]
\centering
\feynmandiagram[layered layout, horizontal = y to x]{
e1 -- [fermion] y -- [fermion] x -- [fermion] e2,
y -- [photon, half left] x
};
\quad
\feynmandiagram[layered layout, horizontal = y to x]{
e1 -- [anti fermion] y -- [anti fermion] x -- [anti fermion] e2,
y -- [photon, half left] x
};
\caption{Electron loop, positron loop.}
\label{fig:electron-loop-positron-loop}
\end{figure}

This is known as the electron (positron) self-energy.

\subsubsection{Two fermion propagators: \(\Circled{C_2}\)}

This term looks like
%
\begin{align}
S_{\Circled{C_2 }}
&= - \frac{q^2}{2} \int \dd[4]{x} \dd[4]{y}
N \left[
    \bcontraction{}{(\overline{\psi}}{ \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} }{\psi} % psibar-psi
    \contraction{(\overline{\psi} \slashed{A} }{\psi}{ ) _{x} (}{\overline{\psi}} % psi-psibar
    (\overline{\psi} \slashed{A} \psi ) _{x} (\overline{\psi} \slashed{A} \psi ) _{y}
\right]  \\
&= \frac{q^2}{2} \int \dd[4]{x} \dd[4]{y}
\Tr \qty[S_F (y-x) \gamma^{\mu } S_F(x-y) \gamma^{ \nu }] N \qty[A_{\mu } A_{\nu }]
\,.
\end{align}

Why can we write this as a trace? This comes from a permutation of the spinors; in order to make it explicit we restore the spinorial indices of the \(\gamma^{\mu }\) matrices, using the first letters of the Greek alphabet:
%
\begin{align}
&N \left[
    \bcontraction{}{(\overline{\psi}_{\alpha }}{ \gamma^{\mu }_{\alpha \beta } \psi_{\beta } ) _{x} (\overline{\psi}_{\gamma } \gamma^{\nu }_{\gamma \delta }}{ \psi_{ \delta }} % 1-4
    \acontraction{(\overline{\psi}_{\alpha } \gamma^{\mu }_{\alpha \beta } }{\psi_{\beta }}{ ) _{x} (}{\overline{\psi}_{\gamma }}  % 2-3
    (\overline{\psi}_{\alpha } \gamma^{\mu }_{\alpha \beta } \psi_{\beta } ) _{x} (\overline{\psi}_{\gamma } \gamma^{\nu }_{\gamma \delta } \psi_{ \delta } ) _{y}
\right]  \\
&=
(-)^3
\bcontraction{}{\psi_{ \delta }}{}{\overline{\psi}_{\alpha }}
\psi_{ \delta } \overline{\psi}_{\alpha }
\bcontraction{}{\psi_{ \beta }}{}{\overline{\psi}_{\gamma }}
\psi_{ \beta } \overline{\psi}_{\gamma }
\gamma^{\mu }_{\alpha \beta } \gamma^{\nu }_{\gamma \delta }  \\
&= - S_F(y-x)_{\delta \alpha } \gamma^{\mu }_{ \alpha \beta }
S_{F}(x-y) _{\beta \gamma }
\gamma^{\nu }_{\gamma \delta } \\
&= - \Tr(S_F(y-x) \gamma^{\mu }
S_F(x-y)
\gamma^{\nu })
\,,
\end{align}
%
where we are taking the trace since all the indices are contracted, including the first and last.

Now, we only have one diagram: it looks like

\begin{figure}[ht]
\centering
\feynmandiagram[layered layout, horizontal = y to x]{
    a -- [photon] y,
    y -- [half left, fermion] x,
    y -- [half right, anti fermion] x,
    x -- [photon] b
};
\caption{Photon self-energy.}
\label{fig:photon-self-energy}
\end{figure}

This corresponds to \(A^{\mu }_{+}(y) A^{\nu }_{-} (x)\), the only other valid one is the same except for \(x \leftrightarrow y\).

\subsubsection{Three propagators: \(\Circled{D}\)}

There are no uncontracted fields: this kind of process cannot contribute to the physical properties of already-existing particles, but it does alter the vacuum. This becomes relevant when QED is coupled to gravity.

\begin{figure}[ht]
\centering
\feynmandiagram[layered layout, horizontal = y to x]{
    y -- [photon] x,
    y -- [fermion, half left] x,
    y -- [anti fermion, half right] x,
};
\caption{Vacuum diagram.}
\label{fig:vacuum-QED}
\end{figure}

\subsubsection{Third order QED diagrams}

The \(S\)-matrix element looks like:
%
\begin{align}
S = \frac{(-iq)^3}{3!}
\int \dd[4]{x} \dd[4]{y} \dd[4]{z}
T \qty[
    (\overline{\psi} \slashed{A} \psi )_{x}
    (\overline{\psi} \slashed{A} \psi )_{y}
    (\overline{\psi} \slashed{A} \psi )_{z}
]
\,.
\end{align}

None of the propagators with zero, one

\subsection{Expansion in momentum space}

We have seen that even terms which are not allowed kinematically are included in the position-space \(S\)-matrix expansion.

In order to determine which states describe physical processes we need to take the matrix element with the initial and final states:
%
\begin{align}
S_{fi} = \bra{f} S \ket{i}
\,,
\end{align}
%
where \(\ket{f} = \ket{\psi (t \to + \infty )}\) and \(\ket{i} = \ket{\psi (t \to - \infty )}\).
This matrix element is the transition amplitude; the transition probability is given by \(\abs{S_{fi}}^2\).

This works well and is normalized, since if the states \(\ket{f}\) are a basis we have
%
\begin{align}
\sum _{f} \abs{S_{fi}}^2
&= \sum_f \bra{i} S ^\dag \ket{f}\bra{f} S \ket{i}  \\
&= \bra{i} S ^\dag \mathbb{1} S \ket{i}  \\
&= 1
\,,
\end{align}
%
since \(S ^\dag S  = \mathbb{1}\).

Now, when we compute the \(T\)-product we get propagators and uncontracted fields, the latter will act on the initial states.
Let us see how they do so.

\subsubsection{Contractions with \(\ket{i}\)}

As a common QED example, consider
%
\begin{align}
\ket{i} = \ket{e^{-}_{s} (p)}
= (2 \pi )^{3/2}
\sqrt{2 \omega_{p}}
c_s ^\dag (p) \ket{0}
\,.
\end{align}

Let us calculate the applications
\todo[inline]{So ``contractions'' has a different meaning here as opposed to the contractions of field operators, right?}
of the annihilation operators \(\psi_{+}\) and \(\overline{\psi}_{+}\) on this state: we get
%
\begin{align}
\psi_{+} (x) \ket{e^{-}_{s} (p)}
&= \int \dd[3]{k} \sqrt{\frac{2 \omega_{p}}{2 \omega_{k}}}
e^{-ikx} \sum_{r} u_r (k) c_r (k) c_s ^\dag (p) \ket{0}  \\
&= \int \dd[3]{k} \sqrt{\frac{2 \omega_{p}}{2 \omega_{k}}}
e^{-ikx} \sum_{r} u_r (k) \qty(\qty{ c_r (k), c_s ^\dag (p)} - c_s ^\dag (p) c_r (k)) \ket{0}  \\
&= \int \dd[3]{k} \sqrt{\frac{2 \omega_{p}}{2 \omega_{k}}}
e^{-ikx} \sum_{r} u_r (k) \delta_{rs} \delta^{(3)}(\vec{p} - \vec{k}) \ket{0}  \\
&= e^{-ipx} u_s (p) \ket{0}
\,.
\end{align}

The same reasoning applied to \(\overline{\psi}_{+}(x) \ket{e^{-}_{s}(p)}\) yields 0, since \(\qty{d_r, c_s ^\dag }= 0\).
The same holds for any field operator which is unrelated to the state (like a photon annihilation operator).

The useful relations in this sense are:
%
\begin{align}
\psi_{+} (x) \ket{e^{-}_{s} (p)} &= e^{-ipx} u_s (p) \ket{0} \\
\overline{\psi}_{+} (x) \ket{e^{+}_{s} (p)} &= e^{-ipx} \overline{v}_s (p) \ket{0}  \\
A^{\mu }_{+} (x) \ket{\gamma_{\lambda } (p)} &= e^{-ipx} \epsilon^{\mu }_{\lambda } (p)\ket{0}  \\
\varphi ^\dag_{+} (x) \ket{s(p)} &= e^{-ipx} \ket{0}
\,.
\end{align}

\subsubsection{Contractions with \(\bra{f}\)}

We take the adjoint of the example from before:
%
\begin{align}
\bra{e^{-}_{s} (p)} = (2 \pi)^{3/2} \sqrt{2 \omega_{p}}
\bra{0} c_s (p)
\,,
\end{align}
%
and we apply from the right a creation operator \(\overline{\psi}_{-}(x)\):
%
\begin{align}
\bra{e^{-}_{s}(p)}
\overline{\psi}_{-}(x)
&= \bra{0} \int \dd[3]{k}
\sqrt{ \frac{2 \omega_{k}}{2 \omega_{p}}}
e^{ikx} \sum _{r} \overline{u}_{r} (k) c_s (k) c_r ^\dag (p)  \\
&= \bra{0} e^{ipx} \overline{u}_{s} (p)
\,,
\end{align}
%
since \(\qty{c_s (k), c_r ^\dag (p)} = \delta_{rs} \delta^{(3)}(\vec{k} - \vec{p})\). Like before, we have
%
\begin{align}
\bra{e^{-}_{s}(p)} \psi_{-} (x) = 0
\,.
\end{align}

A summary of useful relations:
%
\begin{align}
\bra{e^{-}_{s} (p)} \overline{\psi}_{-} (x) &= \bra{0} e^{ipx} \overline{u}_{s} (p) \\
\bra{e^{+}_{s} (p)} \psi_{-} (x) &= \bra{0} e^{ipx} v_{s} (p) \\
\bra{\gamma_{\lambda } (p)} A^{\mu }_{-} (x) &= \bra{0} e^{ipx} e^{\mu *}_{\lambda }(p) \\
\bra{s(p)} \varphi ^\dag_{-} (x) &= \bra{0} e^{ipx}
\,.
\end{align}

\subsubsection{\(S\)-matrix expansion in momentum space}

At \textbf{0th order} we get \(S^{(0)} = \mathbb{1}\), so the transition amplitude is \(\braket{f}{i}= \delta_{fi} \).

At \textbf{1st order} things get more interesting. Let us consider the process \(e^{-} \to e^{-} \gamma \): so, we are setting
%
\begin{align}
\ket{i} &= \ket{e^{-}_{s}(p)}  \\
\ket{f} &= \ket{e^{-}_{s'} (p') \gamma_{\lambda'} (k')}
\,,
\end{align}
%
so the only term which will contribute to the \(S\)-matrix expansion will be
%
\begin{align}
S^{(1)}_{e^{-} \to e^{-}\gamma }
= -iq \int \dd[4]{x} N \qty[ \overline{\psi}_{-} (x) \slashed{A}_{-} \psi_{+} (x)]
\,,
\end{align}
%
so the matrix element is
%
\begin{align}
S^{(1)}_{fi} = -iq \int \dd[4]{x} \bra{e^{-}_{s'} (p') \gamma_{\lambda'} (k')} \overline{\psi}_{-} (x) \slashed{A}_{-} \psi_{+} (x) \ket{e^{-}_{s}(p)}
\,,
\end{align}
%
where any operator has a specific state it is acting on, if it acts on a state which is not its own the contribution vanishes.
So, using the relations which were written before we have
%
\begin{align}
S^{(1)}_{fi} &= -iq \epsilon^{\mu }_{\lambda' } (k') \overline{u}_{s'} (p') \gamma_{\mu } u_s (p) \int \dd[4]{x} e^{-i(p - p' k')x} \braket{0}{0}  \\
&= (2 \pi )^{4} \delta^{(4)} (p - p' - k') \mathcal{M}^{(1)}_{e^{-} \to e^{-}\gamma }
\,,
\end{align}
%
where the \textbf{Feynman amplitude} \(\mathcal{M}\) is given by
%
\begin{align}
\mathcal{M}^{(1)}_{e^{-} \to e^{-}\gamma } =
-iq \slashed{\epsilon}_{\lambda' } (k') \overline{u}_{s'} (p') \gamma_{\mu } u_s (p)
\,.
\end{align}

Let us make some observations on this result.

\begin{enumerate}
    \item The term \(\delta^{(4)}(p _{i} - p _{f})\) enforces momentum conservation in the matrix element: it removes all kinematically forbidden processes.\footnote{This is related to the fact that we are considering \emph{asymptotic} initial and final states: since they are at a diverging time difference, we are allowed to completely remove the energy uncertainty. }
    \item All the interaction physics is contained in the Feynman amplitude: we have the terms \(\overline{u}_{s'} (p')\) and \(\epsilon^{\mu }_{\lambda '} (k ') u_s (p)\) accounting for the spins and polarizations of the incoming particles, while \(-i q \gamma_{\mu }\) describes the structure of the QED interaction.
    \item With the same conventions used in position space we can draw a diagram for the process in momentum space:
    \begin{figure}[ht]
    \centering
    \feynmandiagram[layered layout, large, horizontal = e to x]{
    e [particle = {\(e^{-}_{s}\)}] -- [fermion, momentum = \(p\)] x [particle = {\(- i q \gamma^{\mu } \)}] -- [fermion, momentum = \(p'\)] e2 [particle = {\(e^{-}_{s'}\)}],
    x -- [photon, momentum = \(k'\)] p [particle = \(\gamma_{\lambda '} \)]
    };
    \caption{Momentum space diagram.}
    \label{fig:eegamma-interaction}
    \end{figure}
    \item We can develop an automatic procedure to derive the Feynman amplitude directly from this diagram.
\end{enumerate}

The rules for this procedure are as follows:
\begin{enumerate}
    \item go to the end of the fermionic arrow and start moving backward;
    \item insert a term \(\overline{u}_{s'} (p')\) based on the momentum and spin of the outgoing electron;
    \item encounter the vertex: insert a term \(-iq \gamma^{\mu }\) since we deal with QED;
    \item encounter the initial electron: insert a term \(u_s (p)\) based on its spin and momentum;
    \item include the polarization of the photon  with a term \(\epsilon^{\mu }_{\lambda '} (k')\).
\end{enumerate}

In order to develop these rules fully, we go to the second order.

\subsubsection{Second order term: \(e^{-} \gamma \to e^{-} \gamma \)}

We consider the process of \textbf{Compton scattering}, so our initial and final states are:
%
\begin{align}
\ket{i} &= \ket{e^{-}_{s} (p) \gamma_\lambda (k)}  \\
\ket{f} &= \ket{e^{-}_{s'} (p') \gamma_{\lambda'} (k')}
\,,
\end{align}
%
and as we saw in the previous section the only diagrams which contribute are:
%
\begin{align}
\begin{split}
S _{\text{Compton}} &= (-iq)^2
\int \dd[4]{x} \dd[4]{y} \\
& \underbrace{N [(\overline{\psi}_{-} \slashed{A}_{-})_{x} S_F (x-y) (\overline{\psi}_{+} \slashed{A}_{+})_{y}]}_{\Circled{A}}
+ \underbrace{N [(\overline{\psi}_{-} \slashed{A}_{+})_{x} S_F (x-y) (\overline{\psi}_{+} \slashed{A}_{-})_{y}]}_{\Circled{B}}
\end{split}
\,.
\end{align}

Let us start with the term \Circled{A}:
%
\begin{align}
S^{A}_{fi} &= \bra{f} S^{A} \ket{i}  \\
&= - q^2 \int \dd[4]{x} \dd[4]{y}
\bra{e^{-}_{s'} (p') \gamma_{\lambda'} (k')}
N [(\overline{\psi}_{-} \slashed{A}_{-})_{x} S_F (x-y) (\overline{\psi}_{+} \slashed{A}_{+})_{y}]
\ket{e^{-}_{s} (p) \gamma_\lambda (k)}  \\
\begin{split}
&= -q^2 \int \frac{ \dd[4]{q}}{(2 \pi )^{4}} \overline{u}_{s'} (p') \slashed{\epsilon }_{\lambda'} (k') \widetilde{S}_{F} (q) \slashed{\epsilon }_{\lambda } (k) u_s (p) \times \\
&\phantom{=}\  \times \int \dd[4]{x} e^{-i (q - p' - k' )x}
\int \dd[4]{y} e^{i (q-p -k) y} \braket{0}{0}
\end{split}  \\
&= \mathcal{M}_{A} (2 \pi )^{4} \delta^{4} (p + k - p' - k')
\,,
\end{align}
%
where
%
\begin{align}
\mathcal{M}_{A} = - q^2 \overline{u}_{s'} (p') \slashed{\epsilon }_{\lambda'} (k') \widetilde{S}_{F} (p+k) \slashed{\epsilon }_{\lambda } (k) u_s (p)
\,.
\end{align}

Note that we have substituted in \(\widetilde{S}_F(q)\), the fermion propagator in momentum space \eqref{eq:fermion-propagator-momentum-space}.
The exponential its definition contained allowed for the connection between the momentum conservation between the initial and final states.

This can be derived from the momentum-space diagram, like before! The diagram is

\begin{figure}[ht]
\centering
\feynmandiagram[layered layout, extra large, horizontal = ia to ib]{
e1 [particle = {\(e^{-}_{s}\)}] -- [fermion, momentum = \(p\)] ia [label = -45: \(-i q \gamma_{\mu }\)] -- [fermion, momentum = {\(q= p+k\)}] ib [label = 235 : {\(-iq \gamma_{\nu }\)}] -- [fermion, momentum = {\(p'\)}] e2 [particle = {\(e^{-}_{s'}\)}],
p1 [particle = {\(\gamma_{\lambda }\)}] -- [photon, momentum' = \(k\)] ia,
ib -- [photon, momentum' = \(k'\)] p2 [particle = {\(\gamma_{\lambda' }\)}]
};
\caption{Momentum  space, first contribution to the Compton effect. }
\label{fig:momentum-space-compton}
\end{figure}

The algorithm to get the amplitude goes as follows:
\begin{enumerate}
    \item Start from the end of the fermion line and move backward;
    \item at the final fermion insert a term \(\overline{u}_{s'} (p')\);
    \item at the QED vertex insert a term \(-iq \gamma_{\nu }\);
    \item for the propagator insert a term \(\widetilde{S}_{F} (q)\);
    \item at the QED vertex insert a term \(-iq \gamma_{\mu }\);
    \item for the initial electron insert a term \(u_s (p)\);
    \item for the initial and final photons insert terms \(\epsilon^{\mu }_{\lambda }(k)\) and \(\epsilon^{\nu }_{\lambda '} (k')\), the index should match that of the QED vertex at which the photon is attached.
\end{enumerate}

This yields:
%
\begin{align}
\mathcal{M}_{A} = \overline{u}_{s'} (p')
\qty(-iq \gamma_{\nu })
\widetilde{S}_{F} (q)
\qty(-iq \gamma_{\mu })
u_s (p)
\epsilon^{\mu }_{\lambda }(k)
\epsilon^{\nu }_{\lambda '} (k')
\,.
\end{align}

Since this will be multiplied by the Dirac delta in the end, we are allowed to use momentum conservation when doing the computation.
Following the fermion arrows ensures we preserve the spinorial structure.

Now, we can do the same for the term \Circled{B}. This reads
\begin{align}
S^{B}_{fi} &= \bra{f} S^{B} \ket{i}  \\
&= - q^2 \int \dd[4]{x} \dd[4]{y}
\bra{e^{-}_{s'} (p') \gamma_{\lambda'} (k')}
N [(\overline{\psi}_{-} \slashed{A}_{+})_{x} S_F (x-y) (\overline{\psi}_{+} \slashed{A}_{-})_{y}]
\ket{e^{-}_{s} (p) \gamma_\lambda (k)}  \\
\begin{split}
&= -q^2 \int \frac{ \dd[4]{q}}{(2 \pi )^{4}} \overline{u}_{s'} (p') \slashed{\epsilon }_{\lambda} (k) \widetilde{S}_{F} (q) \slashed{\epsilon }_{\lambda' } (k') u_s (p) \times \\
&\phantom{=}\  \times \int \dd[4]{x} e^{-i (q - p' + k )x}
\int \dd[4]{y} e^{i (q-p +k') y} \braket{0}{0}
\end{split}  \\
&= \mathcal{M}_{A} (2 \pi )^{4} \delta^{4} (p + k - p' - k')
\,,
\end{align}
%
so now we have momentum conservation like before, but \(q = p - k' = p' - k\).

This diagram is topologically different from the one before: now the outgoing photon is created in the \emph{first} vertex, the incoming photon is destroyed in the \emph{second} vertex.

Applying the same procedure as before, we find
%
\begin{align}
\mathcal{M}_{B} = \overline{u}_{s'} (p') \qty(-iq \gamma_{\mu })
\widetilde{S}_{F} (p - k') \qty(-i q \gamma_{\nu }) u_s (p)
\epsilon^{\lambda }_{\mu } (k) \epsilon^{\lambda '}_{\nu } (k')
\,.
\end{align}

% Note that this is \textbf{different} from \(\mathcal{M}_A\)!
The total probability amplitude is then given by
%
\begin{align}
S^{(2)} (e^{-} \gamma ) = (2\pi )^{4} \delta^{(4)}
(p + k - p' - k') \qty(\mathcal{M_A} + \mathcal{M}_{B})
\,.
\end{align}

\subsubsection{Second order term: photon self-energy}

We have seen that there is a second-order QED term in which both the initial and final states are photons, the \textbf{photon self-energy}:
%
\begin{align}
\ket{i} &= \ket{\gamma_{\lambda } (p)} \\
\ket{f} &= \ket{\gamma_{\lambda' } (p')}
\,,
\end{align}
%
so that we have both a fermion and an antifermion propagator:
%
\begin{align}
S^{(2)}_{\gamma \gamma } =
- \frac{q^2}{2} \int \dd[4]{x} \dd[4]{y}
(-) \Tr \qty(S_F(y-x) \gamma^{\mu } S_F(x-y) \gamma^{\nu })
N[A_{\mu } (x) A_{\nu } (y)]
\,.
\end{align}

As we calculate the matrix element we get a factor two, since the signs of the two \(A_{\mu }\), \(A_{\nu }\) can be either \(+ -\) or \(- +\) equivalently: we have
%
\begin{align}
N[A_{\mu }(x) A_{\nu } (y)] = N \qty[
    A_{\mu +} (x) A_{\nu -} (y) +
    A_{\mu -} (x) A_{\nu +} (y)
]
\,,
\end{align}
%
since there is \emph{a priori} no causal constraint between \(x\) and \(y\). These two terms give an equal contribution (they are symmetric if we substitute \(\mu \leftrightarrow \nu \) ad \(x \leftrightarrow y\)), so we find
%
\begin{align}
\begin{split}
S^{(2)}_{fi} &=
q^2 \int \frac{ \dd[4]{k_1 }}{(2 \pi )^{4}}
\int \frac{ \dd[4]{k_2 }}{(2 \pi )^{4}}
\epsilon^{\mu}_{\lambda }(p) \epsilon^{\nu }_{\lambda '} (p')
\Tr \qty[\widetilde{S}_{F}(k_2 ) \gamma_{\mu } \widetilde{S}_{F} (k_1 )\gamma_{\nu }] \times \\
&\phantom{=}\ \times
\int \dd[4]{x} e^{-i(p - k_2 + k_1 )x}
\int \dd[4]{y} e^{-i (p' - k_2 + k_1 ) x}
\end{split}
\,.
\end{align}

The diagram for this interaction is that shown in figure \ref{fig:photon-self-energy}.
Now, we can do one of the integrals in momentum space, but the other remains: we have
%
\begin{align}
S^{(2)}_{fi} &= q^2 \int \dd[4]{k_1 } \int \dd[4]{k_2 } \delta^{(4)}(p - k_1 + k_2 )
\delta^{(4)} (p' - k_2 + k_1 )
\epsilon^{\nu }_{\lambda }( p) \epsilon^{\mu }_{\lambda '} (p') \Tr[\dots]  \\
&= q^2 \int \dd[4]{k_1 } \delta^{(4)} (p' - (p + k_1 ) + k_1 ) \epsilon^{\nu }_{\lambda }( p) \epsilon^{\mu }_{\lambda '} (p') \Tr[\dots]  \\
&= ( 2 \pi )^{4} \delta^{(4)} (p - p') \underbrace{q^2\int \frac{ \dd[4]{k_1 }}{(2 \pi )^{4}}
\epsilon^{\nu }_{\lambda }( p) \epsilon^{\mu }_{\lambda '} (p') \Tr[\widetilde{S}_{F}(p' + k_1  ) \gamma_{\mu } \widetilde{S}_{F} (k_1 )\gamma_{\nu }]}_{\mathcal{M}}
\,.
\end{align}

Note that inside of \(\mathcal{M}\) we have a factor \(-1\) because of the fact that it is a fermionic loop, so we need to swap an odd number of fields.

Momentum conservation is enforced by the delta, but there is a degree of arbitrariness in the momentum of the loop: \(k_1 \) is free to vary, as long as \(k_2 \) varies with it. We integrate over all of the possible configurations in which this loop can occur.

In general, the number of loops \(L\) is given in terms of the number of fermion propagators \(P_e\), of photon propagators \(P_{\gamma }\), of vertices \(V\) as:
%
\begin{align}
L = P_e - P_\gamma - V + 1
\,.
\end{align}

In this case, we have \(L =1 \).

\subsubsection{Divergences}

Let us give an estimate of this self-energy, assuming \(k \gg m, p\) (so, in the ``ultraviolet'': the loop is extremely energetic).

We then have
%
\begin{align}
S^{(2)}_{fi} \sim \int \dd[4]{k} \frac{\Tr[(\slashed{p+k} +m) \slashed{\epsilon } \qty(\slashed{k} + m) \slashed{\epsilon }]}{(k^2-m^2) \qty((k+p)^2- m^2)} \sim \int \dd[4]{k} \frac{k^2}{k^{4}} \to \infty
\,,
\end{align}
%
where we have used the expression \eqref{eq:fermion-propagator-momentum-space-explicit} for the propagator; in terms of orders of \(k\) we can switch the integral to \(\int \dd[4]{k} \sim \int k^3 \dd{k} \). So, we are left with \(\int k \dd{k}\), which diverges \textbf{quadratically}.

\begin{claim}
    A similar argument can be applied to show that the electron self-energy is divergent as well.
\end{claim}

\begin{proof}
The matrix element comes from the \(S\)-matrix in equation \eqref{eq:fermion-self-energy}; expressing everything in momentum space we find:
%
\begin{align}
\begin{split}
S^{(2)}_{fi} &= - q^2 \int \frac{ \dd[4]{k_{\gamma }}}{(2 \pi )^{4}}
\int \frac{ \dd[4]{k_{F}}}{(2 \pi )^{4}}
\widetilde{D}^{\mu \nu }(k_\gamma )
\overline{u}_{s'} (p') \gamma_{\mu } \widetilde{S}_{F}(k_F) \gamma_{\nu } u_s (p) \\
&\phantom{=}\
\underbrace{\int \dd[4]{x} e^{i (p' -k_\gamma - k_F) x}  }_{(2 \pi )^{4} \delta^{(4)} (p' - k_\gamma - k_F)}
\underbrace{\int \dd[4]{y} e^{-i (p - k_{\gamma } - k_F) y}}_{(2 \pi )^{4} \delta^{(4)} (p - k_\gamma - k_F)}
\end{split}  \\
&= - q^2
( 2\pi )^{4} \delta^{(4)} (p - p')
\int \frac{ \dd[4]{k_{\gamma }}}{(2 \pi )^{4}}
\widetilde{D}^{\mu \nu }(k_\gamma )
\overline{u}_{s'} (p') \gamma_{\mu } \widetilde{S}_{F}(p' - k_{\gamma }) \gamma_{\nu } u_s (p)
\,,
\end{align}
%
so like before we can do power-counting to the integral in \(\dd[4]{k_\gamma }\): we have
%
\begin{align}
\mathcal{M} &= - q^2 \int \frac{ \dd[4]{k_{\gamma }}}{(2 \pi )^{4}}
\widetilde{D}^{\mu \nu }(k_\gamma )
\overline{u}_{s'} (p') \gamma_{\mu } \widetilde{S}_{F}(p' - k_{\gamma }) \gamma_{\nu } u_s (p)   \\
&= - q^2 \int \frac{ \dd[4]{k_{\gamma }}}{(2 \pi )^{4}}
\frac{-i \eta^{\mu \nu }}{k^2+ i \epsilon }
\overline{u}_{s'} (p') \gamma_{\mu }
% \frac{\slashed{p' - k_\gamma } + m}{\qty(p' - k_\gamma )^2 - m}
\frac{(p' - k_\gamma )_{\alpha}\gamma^{\alpha} + m}{\qty(p' - k_\gamma )^2 - m}
\gamma_{\nu } u_s (p)  \\
&\sim \int \dd[4]{k_\gamma } \frac{1}{k^2} \frac{k}{k^2} \sim \int \dd{k_\gamma } k_{\gamma }^3 k_\gamma^{-2} k_\gamma^{-1} \sim \int \dd{k_\gamma }
\,,
\end{align}
%
which still diverges, but this time only linearly.
\end{proof}

\end{document}


\documentclass{article}
\begin{document}
\title{The MPL Programming Language}
\author{Mark Kamradt}
\maketitle
\section{Introduction}
\emph{MPL}\footnote{MPL is an ambigius initialism.
  For most people it could stand for Mathematical Programming Language.
  If you're someone who know's me it stands for Mark's Programming Language.
  To me it's just My Programming Language.}
is a simple impartative mathematical scripting language.
The intent of this language is for writting small mathematical programs.
Programs that are to small to need the facilities of a real programming
language, yet too large for a handheld calculator.

\emph{MPL} is meant to be a scripting lauguage.
The ideal workflow is: find a problem, collect data for input,
write a script in you favorite text editor, run your script,
(maybe debug your script) find your solution.
You could use the interpreter interactivaly, but it doesn't have
any of the niceties of a user friendly interpreter.

If you type a mathematical expression, \emph{MPL} will respond with
the result.  For example,
\begin{verbatim}
(1 + sqrt(5))/2
\end{verbatim}
gives
\begin{verbatim}
1.61803
\end{verbatim}

You can also save the results of an expression into a variable.
\begin{verbatim}
phi = (1 - sqrt(5))/2
\end{verbatim}
And then use that variable in expressions
\begin{verbatim}
-1/phi
\end{verbatim}

You can also define functions. For example, to compute the $n$th Fibonacci
number, define the function:
\begin{verbatim}
function fibonacci(n)
    q = (1 + sqrt(5))/2
    p = (1 - sqrt(5))/2
    return (q^n - p^n)/sqrt(5)
end
\end{verbatim}
and then use that function in an expression
\begin{verbatim}
fibonacci(101)/fibonacci(100)
\end{verbatim}

There are several data type built-in to \emph{MPL}.
In addtion to real (the same as C's double) there are also
complex, real and complex vectors, real and complex matricies,
strings and lists.
The vector and matrix types are \emph{not} general purpose arrays.
The list type can be used as an array.

\section{Running the Interpreter}
A typical \emph{MPL} invocation is
\begin{verbatim}
mpl script <input >output
\end{verbatim}
If no script is given, \verb|mpl| will read code from standard input.
More than one script can be listed on the command line, \verb|mpl| will then
execute each script, in order, as though all the script were a single file.

\section{Lexical}
\emph{MPL} is a line oriented language.
Each statement is on its own line.
If a line is too long you can split it in to two lines by by ending
the first line with a backslash, \verb|\|.
Comments start with a tilde, \verb|~|, and continue to the end of the line.

If a line is too long you can split it into multiple lines by
ending each line, except the last, with a backslash, \verb|\|, character.
Line continuations are considered the same as a space so can only occur
where spaces are allowed.
A string cannot span multiple lines, however you can use line contimuations
and the string concatenation operator, \verb|+|.

Each line is divided into \emph{word symbols}, \emph{numbers},
\emph{strings}, and \emph{character symbols}.
Word symbols start with an alphabetic character and are followed by
zero or more alphanumeric characters.
The underscore, \verb|_|, and the dollar sign, \verb|$|, are treated like as
alphabetic characters.

There are no reserved words in \emph{MPL} but words with special meaning
to the language should be avoided for naming variables and functions.
Also words that begin with an underscore should be avoided.

\section{Types}
There are eight data types in MPL. \emph{Real}, \emph{complex},
\emph{real vector}, \emph{complex vector}, \emph{real matrix}, \emph{complex matrix}, \emph{string}, and \emph{list}. There are no facilities for creating
addition data types.

\subsection{Real}
The \emph{real} data type, internally, is represented by a double-precision floating point value.\footnote{Hopefully IEEE 754.}
Real numbers can be written as a decimal number with an optional exponent.
The following are all valid ways to write real numbers:
\begin{center}
  \begin{tabular}{cc}
    \verb|142857| &  \verb|3.14159| \\
    \verb|6.626e-34| & \verb|6.02214076e23| \\
    \verb|1e+100| \\
  \end{tabular}
\end{center}
You cannot use commas or spaces to format numbers.

The arithmetic operators for \emph{real} are:
\begin{center}
  \begin{tabular}{c}
    \verb|-| \emph{real} \\
    \emph{real} \verb|+| \emph{real} \\
    \emph{real} \verb|-| \emph{real} \\
    \emph{real} \verb|*| \emph{real} \\
    \emph{real} \verb|/| \emph{real} \\
    \emph{real} \verb|div| \emph{real} \\
    \emph{real} \verb|mod| \emph{real} \\
    \emph{real} \verb|^| \emph{real} \\
  \end{tabular}
\end{center}
The \verb|div| operator is a floored division, \verb|a div b| is $\lfloor a/b \rfloor $.
The \verb|mod| operator is the modulus operation, \verb|a mod b| is $b\lfloor a/b\rfloor - a$.
Note that this is different then many other languages.

The comparison operators for reals return a real value of 1 for true and a real value of 0 for false.
\begin{center}
  \begin{tabular}{c}
    \emph{real} \verb|==| \emph{real} \\
    \emph{real} \verb|!=| \emph{real} \\
    \emph{real} \verb|<| \emph{real} \\
    \emph{real} \verb|<=| \emph{real} \\
    \emph{real} \verb|>| \emph{real} \\
    \emph{real} \verb|>=| \emph{real} \\
  \end{tabular}
\end{center}

There are logic operators for real. The operands for the logic operators are non-zero for true and zero for false.
\begin{center}
  \begin{tabular}{c}
    \verb|not| \emph{real} \\
    \emph{real} \verb|and| \emph{real} \\
    \emph{real} \verb|or| \emph{real} \\
  \end{tabular}
\end{center}

\subsection{Complex}
Complex numbers are represented by a pair of double-precision floating-point numbers representing the
real and imaginary parts.
They can be written with parenthesis surrounding the comma sepreated real and imaginary parts.
The number $3 + 4i$ is written
\begin{verbatim}
(3, 4)
\end{verbatim}
The values for the parts of a complex number can be any real valued
expression.
The real and imaginary parts can be any real expression.
\begin{verbatim}
(cos(x)^2, sqrt(y))
\end{verbatim}
The constant \verb|I| is defined as the imanginary unit, $i$. You can use this for a more traditional
representation of a complex number.
\begin{verbatim}
3 + 4*I
\end{verbatim}

To specify a complex number in polar notation there are different options.
\begin{verbatim}
polar(2, PI/6)
2*cis(PI/6)
2*exp(I*PI/6)
\end{verbatim}
all represent the number $2e^{i \pi/6}$.

The arithmetic operators for \emph{complex} are:
\begin{center}
  \begin{tabular}{c}
    \verb|-| \emph{complex} \\
    \emph{complex} \verb|+| \emph{complex} \\
    \emph{complex} \verb|-| \emph{complex} \\
    \emph{complex} \verb|*| \emph{complex} \\
    \emph{complex} \verb|/| \emph{complex} \\
    \emph{complex} \verb|^| \emph{complex} \\
  \end{tabular}
\end{center}
The comparison operators complex numbers are:
\begin{center}
  \begin{tabular}{c}
    \emph{real} \verb|==| \emph{real} \\
    \emph{real} \verb|!=| \emph{real} \\
  \end{tabular}
\end{center}
Operators with mixed real and complex operands act as if the
real operand is complex.
Operators that are not defined for complex types are also
undefined for mix real and complex types.

\subsection{Vector}
The vector type is a Euclidean $n$-space vector.
A vector is written with square brackets surounding a comma seperated
list of real expressions.
\begin{verbatim}
[ 1, 2, 3]
[ sqrt(a), a, a^2 ]
\end{verbatim}
The arithmetic operators for vectors are:
\begin{center}
  \begin{tabular}{c}
    \verb|-| \emph{vector} \\
    \emph{vector} \verb|+| \emph{vector} \\
    \emph{vector} \verb|-| \emph{vector} \\
    \emph{real} \verb|*| \emph{vector} \\
    \emph{vector} \verb|*| \emph{real} \\
    \emph{vector} \verb|*| \emph{vector} \\
    \emph{vector} \verb|/| \emph{real} \\
  \end{tabular}
\end{center}
The \emph{vector} \verb|*| \emph{vertor} operator is a dot product and results in a real.
The comparison operators for vectors are:
\begin{center}
  \begin{tabular}{c}
    \emph{vector} \verb|==| \emph{vector} \\
    \emph{vector} \verb|!=| \emph{vector} \\
  \end{tabular}
\end{center}

\subsection{Complex Vector}
The complex vector is a vector of complex numbers instead of real numbers.
Complex vectors are written the same as real vectors except
at least one of the members must be a complex type.
\begin{verbatim}
[ (1,2), 3 ]
\end{verbatim}

The operators for complex vectors are the same as real vectors.
The dot product for complex vectors produce the Hermitian inner product.
When mixing reals and vectors with complex vectors in expressions the
reals and real vectors are treated like complex and complex vectors.

\subsection{Matrix}
A matrix is a two-dimential array of real numbers.
A matrix is written simular to a vector except different rows of the
matrix are seperated by a vertical bar, \verb$|$.
\begin{verbatim}
[ 1, 2, 3 | 4, 5, 6 | 7, 8, 9]
\end{verbatim}
Matricies can also be written with each row on a seperate line.
\begin{verbatim}
[ 1, 2, 3
  4, 5, 6
  7, 8, 9 ]
\end{verbatim}
There is no way to write a matix with a single row.
If the number of members in each row is different then the number columns is the same as
the row with the most members and missing members are filled with 0.

Like vectors, matrix members can be any real-valued expression.
For example, you could write:
\begin{verbatim}
[ cos(theta), -sin(theta)
  sin(theta), cos(theta) ]
\end{verbatim}

The arithmetic operators for \emph{matrix} are:
\begin{center}
  \begin{tabular}{c}
    \verb|-| \emph{matrix} \\
    \emph{matrix} \verb|+| \emph{matrix} \\
    \emph{matrix} \verb|-| \emph{matrix} \\
    \emph{real} \verb|*| \emph{matrix} \\
    \emph{matrix} \verb|*| \emph{real} \\
    \emph{matrix} \verb|*| \emph{vector} \\
    \emph{matrix} \verb|*| \emph{matrix} \\
    \emph{matrix} \verb|/| \emph{real} \\
  \end{tabular}
\end{center}

The comparison operators for matrix are:
\begin{center}
  \begin{tabular}{c}
    \emph{matrix} \verb|==| \emph{matrix} \\
    \emph{matrix} \verb|!=| \emph{matrix} \\
  \end{tabular}
\end{center}

\subsection{Complex Matrix}
The complex matrix is simular to a matrix but with complex values.
They are bulit the same as a real matrix but at least one member is complex.
The operators for a complex matrix are the same as matrix.
Operators mixing matrix/complex matrix, vector/complex vector and real/complex
will treat the real types as their complex counterpart.
Although complex vectors use a Hermitian inner product, complex matrices do not.

\subsection{String}
A string is an array of zero or more characters.
String are written by surounding characters with double quotes.
\begin{verbatim}
"Hello, world"
\end{verbatim}
The quote character and some control characters can be included
in a string with ``C''-style escape sequences.
\begin{center}
  \begin{tabular}{cl}
    \verb|\a| & alert \\
    \verb|\b| & backspace \\
    \verb|\e| & ASCII escape \\
    \verb|\f| & formfeed \\
    \verb|\n| & newline \\
    \verb|\r| & carriage return \\
    \verb|\t| & tab \\
    \verb|\v| & vertical tab \\
  \end{tabular}
\end{center}

Any character not in the above table following a backslash will be that
character without the backslash. Thus, a \verb|\"| will be a quote
character.
The quotes surounding the string are not printed.

The arithmetic operators for \emph{string} are:
\begin{center}
  \begin{tabular}{c}
    \verb|-| \emph{string} \\
    \emph{string} \verb|+| \emph{string} \\
    \emph{string} \verb|-| \emph{string} \\
    \emph{real} \verb|*| \emph{string} \\
    \emph{string} \verb|*| \emph{real} \\
  \end{tabular}
\end{center}
The negation operator reverses the order of the string.
The \verb|+| operator will concatenate the two string.
The subtraction operator concatenates the first string and the reverse of the second string.
The \verb|*| operator repeates the string the number of times specified by the real value.
If the real value is negative, then it repeates the reverse of the string the number of times
specified by the absolute value of the real value.
\begin{center}
  \begin{tabular}{cc}
    expression & result \\
    \verb|-"abc"| & \verb|"cba"| \\
    \verb|"abc" + "def"| & \verb|"abcdef"| \\
    \verb|"abc" - "def"| & \verb|"abcfed"| \\
    \verb|3*"abc"| & \verb|"abcabcabc"| \\
    \verb|"abc"*-3| & \verb|"cbacbacba"| \\
  \end{tabular}
\end{center}

The comparison operators for string return are:
\begin{center}
  \begin{tabular}{c}
    \emph{string} \verb|==| \emph{string} \\
    \emph{string} \verb|!=| \emph{string} \\
    \emph{string} \verb|<| \emph{string} \\
    \emph{string} \verb|<=| \emph{string} \\
    \emph{string} \verb|>| \emph{string} \\
    \emph{string} \verb|>=| \emph{string} \\
  \end{tabular}
\end{center}
The string comparison operators work only on the character codes for each character in the strings.
So \verb|"Z" < "a"| is true and \verb|"29" > "8"| is false.

\subsection{List}
A list is an array of zero of more objects.
Each member of a list can be any type independent of the other members, including another list.
A list is written with curly braces surounding a comma seperated list of objects.
\begin{verbatim}
{ (1.2, 3.4), [1,2,3|4,5,6], "abc" }
\end{verbatim}

All operators operate on lists in a special way.
Unary operators on a list results in a list a list where that unary operator is
applied to evey member of that list.
\begin{verbatim}
-{5, "abc", [1,-2,3]}
\end{verbatim}
results in\footnote{The quotes around the string are not printed.}
\begin{verbatim}
{-5, "cba", [-1, 2, -3]}
\end{verbatim}

Binary operators have two forms.
On form is both operands are lists.
\begin{center}
  \emph{list} \emph{op} \emph{list}
\end{center}
The result of this form is a list with the operator applies
to each member of both operands. If the operand have different
sizes, the members of the smaller list are repeated.
\begin{verbatim}
{1, 2, 3} + { 1, 2, 3, 4, 5}
\end{verbatim}
results in
\begin{verbatim}
{2, 4, 6, 5, 7}
\end{verbatim}

The other form of binary operators is mixing a list operand with a non-list operand.
In this case the result is a list where the singular value is operated on each member
of the list operand.
\begin{verbatim}
2*{1, 2, 3}
\end{verbatim}
results in
\begin{verbatim}
{2, 4, 6}
\end{verbatim}

\section{Indexing}
Individul members of vectors, matricies, string and list can be accessed
by indexes. Only variables and indexed variables can be indexed, not
general expressions.

\subsection{Simple Index}
A simple index selectes one member from the object.
Simple indexes are written by the variable name followed by an index surrounded
by square brackets.
\begin{verbatim}
a[2]
\end{verbatim}
All indexes are zero based so an index of 1 gives the second member of the object.
A simple index of a matrix variable results in a vector of that row.
A member of a matrix at a given row and column can given by two indexs
\begin{verbatim}
m[r][c]
\end{verbatim}
Or you could use matrix indexes.
When elements of a list can be indexed you can use multiple indexes as well.

\subsection{Matrix Index}
A matrix index can access member of a specific row and column.
These indexes are written using a single pair of square brackets with
a row and column index inside, seperated by a comma.
\begin{verbatim}
m[r,c]
\end{verbatim}
If the row index is blank then a vector of the specified column is returned.
If the column index is blank then the row is returned.

If \verb|M| is a matrix:
\begin{center}
  \begin{tabular}{cccl}
    \verb|M[1][2]| & or & \verb|M[1,2]| & returns the value at row 1 and column 2 \\
    \verb|M[1,]| & or & \verb|M[1]| & returns a vector of row 1 \\
    && \verb|M[,2]| & return a vector of column 2 \\
  \end{tabular}
\end{center}

\subsection{Slices}
Slices are use to specify parts of an indexable type.
For example, given a vertor,
\begin{center}
\verb|v = [1, 2, 3, 4, 5, 6]|
\end{center}
then the following expression results in:
\begin{center}
\begin{tabular}{ccc}
  \verb|v[1:3]| & $\Rightarrow$ & \verb|[2, 3, 4]| \\
  \verb|v[1:3:2]| & $\Rightarrow$ & \verb|[2, 4, 6]| \\
\end{tabular}
\end{center}
The first number in a slice is the initial index for the slice.
The second number is the number of members in a slice.
The optional third number is the stride, i.e., the difference between
adjacent indexes.
If the third number is not present, the stride defaults
to one.
Slices can be used for any type and in any way that regular indexes occure.
The commands:
\begin{verbatim}
a = "abcdef"
b = "ABCDEF"
a[1:3:2] = b[0:3:2]
a
\end{verbatim}
will print out:
\begin{verbatim}
aAcCeE
\end{verbatim}
\section{Expressions}
\begin{center}
  \begin{tabular}{cl}
    \emph{number} & real number \\
    \emph{string} & string \\
    \emph{var} & variable \\
    \emph{var} \verb|[| \emph{expr} \verb|]| & simple index \\
    \emph{var} \verb|[| \emph{expr} \verb|,| \emph{expr} \verb|]| & matrix index \\
    \emph{func}\verb|(| \emph{expr-list} \verb|)| & function \\
    \verb|(| \emph{expr} \verb|,| \emph{expr} \verb|)| & complex number \\

    \verb|[| \emph{expr-list} \verb|]| & vector \\
    \verb|[| \emph{row-list} \verb|]| & matrix \\
    \verb|{| \emph{expr-list} \verb|}| & list \\
  \end{tabular}
\end{center}
\begin{center}
  \begin{tabular}{cll}
    \verb|(| \emph{expr} \verb|)| & grouping  \\ \hline
    \emph{expr} \verb|^| \emph{expr} & exponentation & right \\ \hline
    \verb|-| \emph{expr} & negation & left \\ \hline
    \emph{expr} \verb|*| \emph{expr} & multiplication & left \\
    \emph{expr} \verb|/| \emph{expr} & division & left \\
    \emph{expr} \verb|div| \emph{expr} & floored division & left \\
    \emph{expr} \verb|mod| \emph{expr} & modulus & left \\ \hline
    \emph{expr} \verb|+| \emph{expr} & addition & left \\
    \emph{expr} \verb|-| \emph{expr} & subtraction & left \\ \hline
    \emph{expr} \verb|==| \emph{expr} & equal, $=$ & none \\
    \emph{expr} \verb|!=| \emph{expr} & no equal, $\ne$ & none \\
    \emph{expr} \verb|<| \emph{expr} & less than, $<$ & none \\
    \emph{expr} \verb|<=| \emph{expr} & less than or equal, $\le$ & none \\
    \emph{expr} \verb|>| \emph{expr} & greater than, $>$ & none \\
    \emph{expr} \verb|>=| \emph{expr} & greater than or equal, $\ge$ & none \\ \hline
    \verb|not| \emph{expr} & logical \emph{not} & left \\ \hline
    \emph{expr} \verb|and| \emph{expr} & logical \emph{and} & left \\ \hline
    \emph{expr} \verb|or| \emph{expr} & logical \emph{or} & left \\
  \end{tabular}
\end{center}
\section{Commands}
\subsection{Print Command}
The \emph{print} command is just an expression on a line by itself.
\begin{center}
  \emph{expression}
\end{center}
The expression is evaluated and its value printed.
For formated ouput see the standard library.
\subsection{Assignment Command}
The \emph{assignemnt} command evaluates an expresion and
assigns its value to a variable.
\begin{center}
  \emph{lvalue} = \emph{expression}
\end{center}
The \emph{lvalue} can be either a simple variable or an indexed variable.
If the \emph{lvalue} is a simple variable the previous value and
type of the variable are discarded for the new value.
If the \emph{lvalue} is an indexed variable the type (including sizes)
of the expression must match the lvalue.
\subsection{Include Command}
The \emph{include} command is use to the include a file as if
that file replaces the include command.
\begin{center}
  \verb|include| \emph{expression}
\end{center}
The parameter to the include command can be any expression that results in a string.
\subsection{Function Definition}
A function definition command is a multi-lined command used to define functions.
The first line of a function defintion gives the name and parameters of the function.
\begin{center}
  \verb|function| \emph{name} \verb|(| \emph{argument-list} \verb|)|
\end{center}
If the function take no arguments, the empty parenthisis are still required.
The function definition ends with an \emph{end} statement.
\begin{center}
  \verb|end|
\end{center}
In between the first and last line of a function definition are the statements that make up the function.
\section{Variables}
\emph{MPL} is a loosely typed language.
Variables can be assigned any type.
If a variable is assigned a new value, that value need not be same
same type as the previous value.

There are three kinds of variables. Global variables, local variables and function arguments.
Global variables are defined (given a value) outside a function.
Their scope is from the initial assignment till the end of the prorgram.

Function arguments are defined in the first line of a function definition.
Their scope is till the end of the function.
Function arguments are ``pass by reference'', meaning the if you assign a new value to the argument the new value is reflected in the variable passed to the function.
If the value of a parameter is an expression and not a variable, then reassigning the parameter in a function does not change any variable outside the function.
If the following code as executed:
\begin{verbatim}
function test(x)
    x = "hello, world"
end
a = 5
test(a)
a
\end{verbatim}
then
\begin{verbatim}
"hello, world"
\end{verbatim}
will be printed. However, the following code:
\begin{verbatim}
function test(x)
    x = "hello, world"
end
a = 5
test(a+0)
a
\end{verbatim}
will print
\begin{verbatim}
5
\end{verbatim}

Local variable, like function parameters, have scope limited to the function.
If a variable has not been defined as a global variable, assigning a value
to it in a function defines it as a local variable.
If a local variable is latter defined as a global variable, it remains a local variable within the function.

\section{Statements}
The \emph{print} and \emph{assignment} commands also work as statements in a function.
\subsection{If Statement}
\begin{center}
  \begin{tabular}{l}
    \verb|if| \emph{expression} \verb|then| \\
    \quad\quad \emph{statements} \\
    \verb|else if| \emph{expression} \verb|then| \\
    \quad\quad \emph{statements} \\
    \verb|else| \\
    \quad\quad \emph{statements} \\
    \verb|end| \\
  \end{tabular}
\end{center}
The \emph{if} statement only executes statements following it if \emph{expression} evaluates to true.
An \emph{else if} clause will execute the statements following it if a previous \emph{if} or \emph{else if} clause evaluates to false and the its own expression evaluates to true. An \emph{else} clause only executes the statements following it if the previous \emph{if} or \emph{else if} clause evalutates to false.
The \emph{end} clause end any conditional executation. There can be 0 or more \emph{else if} and \emph{else} parts in an \emph{if} statement.
\subsection{While Statement}
\begin{center}
  \begin{tabular}{l}
    \verb|while| \emph{expression} \\
    \quad\quad \emph{statements} \\
    \verb|end| \\
  \end{tabular}
\end{center}
\subsection{Repeat Statement}
\begin{center}
  \begin{tabular}{l}
    \verb|repeat| \\
    \quad\quad \emph{statements} \\
    \verb|until| \emph{expression} \\
  \end{tabular}
\end{center}
\subsection{For Statement}
\begin{center}
  \begin{tabular}{l}
    \verb|for| \emph{variable} \verb|in| \emph{range-specifier} \\
    \quad\quad \emph{statements} \\
    \verb|end| \\
  \end{tabular}
\end{center}
The code
\begin{verbatim}
for x in {"a", "b", "c"}
    x
end
\end{verbatim}
prints
\begin{verbatim}
a
b
c
\end{verbatim}
The code
\begin{verbatim}
for i in 1:3
    i
end
\end{verbatim}
prints
\begin{verbatim}
1
2
3
\end{verbatim}
The code
\begin{verbatim}
for i in 1:5:2
    i
end
\end{verbatim}
prints
\begin{verbatim}
1
3
5
\end{verbatim}
\subsection{Return Statement}
\begin{center}
  \verb|return| \emph{expression}
\end{center}
The \emph{return} statement ends the execution of a function and specifies the return
value.
The return value is optional.
If the executions of a function reaches the end of the function there is an implicite return statement and the function ends without a return value.
Functions that return without a value can only be used in \emph{print} statements/commands.
\section{Standard Library}
\begin{tabular}{lp{8cm}}
  \verb|ceil(x)| & Ceiling function. Rounds $x$ to smallest integer greater than or equal to $x$. \\
\end{tabular}
\section{Things You're Not Suppose to Know.}
This section is for documenting ``undocumneted'' features of \emph{MPL}.
These are features of the language that may change in the future.
\subsection{Real Numbers}
\emph{Real} numbers can be written in hexadecimal form.
A \verb|#| (pound symbol) followed by hexidecimal digits give the bits
of the floating point number. For example, \verb|#3ff0000000000000|
represents the number 1.

The way the current interpreter reads \emph{real} literals,
a \verb|.| (decimal point) by itself represents the number
zero.

\end{document}


\chapter[Game Theory]{Game Theory}\label{chp:game_theory}

% Introduction
\chapterinitial{M}{ost} of the time when modelling certain situations two
approaches are valid: to make assumptions about the overall behaviour or to make
assumptions about the detailed behaviour. The later can be thought of as
measuring emergent behaviour. One tool used to do this is the study of
interactive decision making: game theory.

\section{Problem}\label{sec:game_theory_problem}

Consider a city council. Two electric taxi companies, company A and company B,
are going to move in to the city and the city wants to ensure that the customers
are best served by this new duopoly. The two taxi firms will be deciding how
many vehicles to deploy: one, two or three. The city wants to encourage them to
both use three as this ensures the highest level of availability to the
population.

Some exploratory data analysis gives the following insights:

\begin{itemize}
    \item if both companies use the same number of taxis then they make the same
        profit which will go down slightly as the number of taxis goes up;
    \item if one company uses more taxis than the other then they make more
        profit.
\end{itemize}

The expected profits for the companies are given in
Table~\ref{tbl:profit-of-taxi-companies}.

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{table}[!hbtp]
    \begin{center}
    \begin{tabular}{cl|P{0.08\textwidth}P{0.08\textwidth}P{0.08\textwidth}}
                                                                  &    & \multicolumn{3}{c}{Company B}\\
                                                                  &    & 1               & 2                 & 3\\
        \toprule
        \multirow{3}{*}{\rotatebox[origin=c]{90}{Company A}} & 1  & 1               & \(\frac{1}{2}\)   & \(\frac{1}{3}\)\\[4.5mm]
                                                                  & 2  & \(\frac{3}{2}\) & \(\frac{19}{20}\) & \(\frac{1}{2}\)\\[4.5mm]
                                                                  & 3  & \(\frac{5}{3}\) & \(\frac{4}{5}\)   & \(\frac{17}{20}\)\\[4.5mm]
    \end{tabular}
    \qquad
    \begin{tabular}{cl|P{0.08\textwidth}P{0.08\textwidth}P{0.08\textwidth}}
                                                                  &    &
                                                                  \multicolumn{3}{c}{Company B}\\
                                                                  &    & 1               & 2                 & 3\\
        \toprule
        \multirow{3}{*}{\rotatebox[origin=c]{90}{Company A}} & 1  & 1 & \(\frac{3}{2}\)   & \(\frac{5}{3}\)\\[4.5mm]
                                                                  & 2  & \(\frac{1}{2}\) & \(\frac{19}{20}\) & \(\frac{4}{5}\)\\[4.5mm]
                                                                  & 3  & \(\frac{1}{3}\) & \(\frac{1}{2}\)   & \(\frac{17}{20}\)\\[4.5mm]
    \end{tabular}
    \end{center}
    \caption{Profits (in GBP per hour) of each taxi company based on
    the choice of vehicle number by all companies. The first table shows the profits for
    company A. The second table shows the profits for company B.}
    \label{tbl:profit-of-taxi-companies}
\end{table}

Given these expected profits, the council wants to understand what is likely to
happen and potentially give a financial incentive to each company to ensure
their behaviour is in the population's interest.
This would take the form of a fixed increase to the companies' profits,
\(\epsilon\), to be found, if they put on three taxis, shown in
Table~\ref{tbl:profit-of-taxi-companies-offset}

\begin{table}[!hbtp]
    \begin{center}
    \begin{tabular}{cl|P{0.08\textwidth}P{0.08\textwidth}P{0.08\textwidth}}
                                                                  &    & \multicolumn{3}{c}{Company B}\\
                                                                  &    & 1               & 2                 & 3\\
        \toprule
        \multirow{3}{*}{\rotatebox[origin=c]{90}{Company A}} & 1  & 1               & \(\frac{1}{2}\)   & \(\frac{1}{3}\)\\[4.5mm]
                                                                  & 2  & \(\frac{3}{2}\) & \(\frac{19}{20}\) & \(\frac{1}{2}\)\\[4.5mm]
                                                                  & 3  & \(\frac{5}{3} + \epsilon\) & \(\frac{4}{5} + \epsilon\) & \(\frac{17}{20} + \epsilon\)\\[4.5mm]
    \end{tabular}
    \qquad
    \begin{tabular}{cl|P{0.08\textwidth}P{0.08\textwidth}P{0.08\textwidth}}
                                                                  &    &
                                                                  \multicolumn{3}{c}{Company B}\\
                                                                  &    & 1               & 2                 & 3\\
        \toprule
        \multirow{3}{*}{\rotatebox[origin=c]{90}{Company A}} & 1  & 1 & \(\frac{3}{2}\)   & \(\frac{5}{3} + \epsilon\)\\[4.5mm]
                                                                  & 2  & \(\frac{1}{2}\) & \(\frac{19}{20}\) & \(\frac{4}{5} + \epsilon\)\\[4.5mm]
                                                                  & 3  & \(\frac{1}{3}\) & \(\frac{1}{2}\) & \(\frac{17}{20} + \epsilon\)\\[4.5mm]
    \end{tabular}
    \end{center}
    \caption{Profits (in GBP per hour) of each taxi company based on
    the choice of vehicle number by all companies. The first table shows the profits for
    company A. The second table shows the profits for company B. The
    council's financial incentive \(\epsilon\) is included.}
    \label{tbl:profit-of-taxi-companies-offset}
\end{table}

For example, from Table~\ref{tbl:profit-of-taxi-companies-offset} it can be seen
that if Company B chooses to use 3 vehicles while Company A chooses to only use
2 then Company B would get \(\frac{17}{20} + \epsilon\) and Company A would get
\(\frac{1}{2}\) profits per hour. The question is: what value of \(\epsilon\)
ensures both companies use 3 vehicles?

\section{Theory}\label{sec:game_theory_theory}

In the case of this city, the interaction can be modelled using a mathematical
object called a normal form game\index{normal form game}, which here requires:

\begin{enumerate}
    \item a given collection of actors that make decisions (players);
    \item options available to each player (actions);
    \item a numerical value associated to each player for every possible
        choice of action made by all the players. This is the utility or reward.
\end{enumerate}

This is called a normal form game and is formally defined by:

\begin{enumerate}
    \item a finite set of \(N\) players;
    \item action spaces for each player: \(\{A_1, A_2, A_3, \dots, A_N\}\);
    \item utility functions\index{utility} that for each player
          \(u_1, u_2, u_3, \dots, u_N\) where
          \(u_i:A_1\times A_2 \times A_3 \dots A_N \to \mathbb{R}\).
\end{enumerate}

When \(N=2\) the utility function is often represented by a pair of
matrices\index{matrix} (1 for each player) with the same number of rows and
columns. The rows correspond to the actions available to the first player and
the columns to the actions available to the second player.

Given a pair of actions (a row and column) we can read the utilities to both
players by looking at the corresponding entry of the corresponding matrix.

For this example, the two matrices would be:

\[
   M =
        \begin{pmatrix}
            1     & 1 / 2   & 1 / 3 \\
            3 / 2 & 19 / 20 & 1 / 2 \\
            5 / 3 & 4 / 5   & 17 / 20\\
        \end{pmatrix}
   \qquad
   N = M ^T =
        \begin{pmatrix}
            1     & 3 / 2   & 5 / 3 \\
            1 / 2 & 19 / 20 & 4 / 5 \\
            1 / 3 & 1 / 2   & 17 / 20\\
        \end{pmatrix}
\]

A diagram of the system is shown in Figure~\ref{fig:taxi-firm-game}

\begin{figure}
\begin{center}
\includestandalone[width=.8\textwidth]{./assets/taxi-firm-game/main}
\end{center}
\caption{Diagrammatic representation of the action sets and payoff matrices for
    the game.}
\label{fig:taxi-firm-game}
\end{figure}

A strategy corresponds to a way of choosing actions, this is represented by a
probability\index{probability} vector\index{vector}. For the \(i\)th player,
this vector \(v\) would be of size \(|A_i|\) (the size of the action space) and
\(v_i\) corresponds to the probability of choosing the \(i\)th action.

Both taxi firms always choosing to use 2 taxis (the second row/column) would
correspond to both taxi firms choosing the strategy: \((0, 1, 0)\).
If both companies use this strategy and the row player (who controls the
rows) wants to improve their outcome it is evident by inspecting the second
column that the highest number is \(19 / 20\): thus the row player has no reason
to change what they are doing.

This is called a Nash equilibrium\index{Nash equilibrium}: when both players are
playing a strategy that is the best response\index{best response} against the
other.

An important fact is that a Nash equilibrium is guaranteed to exist.
This was actually the theoretic result for which John Nash\index{John Nash}
received a Nobel Prize\footnote{
John Nash proved the fact that any game has a Nash equilibrium
in~\cite{nash1950equilibrium}. Discussions and proofs for particular cases
can be found in good game theory text books such as~\cite{maschler2013game}
}.
There are various algorithms that can be used for finding Nash equilibria, they
involve a search through the pairs of spaces of possible strategies until pairs
of best responses are found. Mathematical insight allows this do be done
somewhat efficiently using algorithms that can be thought of as modifications of
the algorithms used in linear programming\index{linear programming} (see
Chapter~\ref{chp:linear_programming}).
One such example is called the Lemke-Howson\index{Lemke-Howson} algorithm.
A Nash equilibrium is not necessarily guaranteed to be arrived at through
dynamic decision making. However, any stable\index{stability} behaviour that
does emerge will be a Nash equilibrium, such emergent processes are the topics
of evolutionary game theory\index{evolutionary game theory}\footnote{
Evolutionary game theory was formalised in~\cite{smith1974theory} although
some of the work of Robert Axelrod\index{Axelrod} is evolutionary in
principle~\cite{axelrod1990evolution}.
},
learning algorithms\index{learning algorithms}\footnote{
An excellent text on learning in games is~\cite{fudenberg1998theory}.
}
and/or agent based\index{agent based} modelling which will be covered in
Chapter~\ref{chp:agent_based_simulation}.

\section{Solving with Python}\label{sec:game_theory_solving-with-python}

The first step is to write a function to create a game using the
matrix of expected profits and any offset. The Nashpy\index{Nashpy}~\cite{knight2018nashpy}
and Numpy\index{Numpy}~\cite{harris2020array} libraries will be used for this.

\begin{pyin}
import nashpy as nash
import numpy as np

def get_game(profits, offset=0):
    """Return the game object with a given offset when 3 taxis are
    provided.

    Args:
        profits: a matrix with expected profits
        offset: a float

    Returns:
        A nashpy game object
    """
    new_profits = np.array(profits)
    new_profits[2] += offset
    return nash.Game(new_profits, new_profits.T)
\end{pyin}

This gives the game for the considered problem:

\begin{pyin}
import numpy as np

profits = np.array(
    (
        (1, 1 / 2, 1 / 3),
        (3 / 2, 19 / 20, 1 / 2),
        (5 / 3, 4 / 5, 17 / 20),
    )
)
game = get_game(profits=profits)
print(game)
\end{pyin}

which gives:

\begin{pyout}
Bi matrix game with payoff matrices:

Row player:
[[1.         0.5        0.33333333]
 [1.5        0.95       0.5       ]
 [1.66666667 0.8        0.85      ]]

Column player:
[[1.         1.5        1.66666667]
 [0.5        0.95       0.8       ]
 [0.33333333 0.5        0.85      ]]
\end{pyout}

The function \mintinline{python}{get_equilibria} which will directly compute the
equilibria:

\begin{pyin}
def get_equilibria(profits, offset=0):
    """Return the equilibria for a given offset when 3 taxis are
    provided.

    Args:
        profits: a matrix with expected profits
        offset: a float

    Returns:
        A tuple of Nash equilibria
    """
    game = get_game(profits=profits, offset=offset)
    return tuple(game.support_enumeration())
\end{pyin}

This can be used to obtain the equilibria in the game.

\begin{pyin}
equilibria = get_equilibria(profits=profits)
\end{pyin}

The equilibria are:

\begin{pyin}
for eq in equilibria:
    print(eq)
\end{pyin}

giving:

\begin{pyout}
(array([0., 1., 0.]), array([0., 1., 0.]))
(array([0., 0., 1.]), array([0., 0., 1.]))
(array([0. , 0.7, 0.3]), array([0. , 0.7, 0.3]))
\end{pyout}

There are 3 Nash equilibria: 3 possible pairs of behaviour that the 2
companies could stabilise at:

\begin{itemize}
    \item the first equilibrium \(((0, 1, 0), (0, 1, 0))\) corresponds to both
          firms always using 2 taxis;
    \item the second equilibrium \(((0, 0, 1), (0, 0, 1))\) corresponds to both
          firms always using 3 taxis;
    \item the third equilibrium \(((0, 0.7, 0.3), (0, 0.7, 0.3))\) corresponds to
          both firms using 2 taxis 70\% of the time and 3 taxis otherwise.
\end{itemize}

A good thing to note is that the two taxi companies will never only provide a
single taxi (which would be harmful to the customers).

The code below can be used to find the number of Nash equilibria for a given offset and
stop when there is a single equilibrium:

\begin{pyin}
offset = 0
while len(get_equilibria(profits=profits, offset=offset)) > 1:
    offset += 0.01
\end{pyin}

This gives a final offset value of:

\begin{pyin}
print(round(offset, 2))
\end{pyin}

\begin{pyout}
0.15
\end{pyout}

and now confirm that the Nash equilibrium is where both taxi firms provide
three vehicles:

\begin{pyin}
print(get_equilibria(profits=profits, offset=offset))
\end{pyin}

giving:

\begin{pyout}
((array([0., 0., 1.]), array([0., 0., 1.])),)
\end{pyout}

Therefore, in order to ensure that the maximum amount of taxis are used, the
council should offer a \(\pounds 0.15\) per hour incentive to both taxi
companies for putting on 3 taxis.

\section{Solving with R}\label{sec:game_theory_solving-with-R}

R does not have a uniquely appropriate library for the game considered here, we
will choose to use Recon\index{Recon}~\cite{oliveira2019recon} which has
functionality for finding the Nash equilibria for two player games when only
considering pure strategies (where the players only choose to use a single
action at a time).

\begin{Rin}
library(Recon)

#' Returns the equilibria in pure strategies
#' for a given offset
#'
#' @param profits: a matrix with expected profits
#' @param offset: a float
#'
#' @return a list of equilibria
get_equilibria <- function(profits, offset = 0){
  new_profits <- rbind(
    profits[c(1, 2), ],
    profits[3, ] + offset
  )
  sim_nasheq(new_profits, t(new_profits))
}
\end{Rin}

This gives the pure Nash equilibria:

\begin{Rin}

profits <- rbind(
  c(1, 1 / 2, 1 / 3),
  c(3 / 2, 19 / 20, 1 / 2),
  c(5 / 3, 4 / 5, 17 / 20)
)
eqs <- get_equilibria(profits = profits)
print(eqs)
\end{Rin}

which gives:

\begin{Rout}
$`Equilibrium 1`
[1] "2" "2"

$`Equilibrium 2`
[1] "3" "3"

\end{Rout}

There are 2 pure Nash equilibria: 2 possible pairs of behaviour that the two
companies might converge to.

\begin{itemize}
    \item the first equilibrium \(((0, 1, 0), (0, 1, 0))\) corresponds to both
          firms always using 2 taxis;
    \item the second equilibrium \(((0, 0, 1), (0, 0, 1))\) corresponds to both
          firms always using 3 taxis.
\end{itemize}

There is in fact a third Nash equilibrium where both taxi firms use 2 taxis 70\%
of the time and 3 taxis the rest of the time but Recon is unable
to find Nash equilibria with mixed behaviour for games with more than two
strategies.

As discussed, the council would like to offset the cost of 3
taxis so as to encourage the taxi company to provide a better service.

The code below gives the number of equilibria for a given offset and stops when
there is a single equilibrium:

\begin{Rin}
offset <- 0
while (length(
  get_equilibria(profits = profits, offset = offset)
  ) > 1){
  offset <- offset + 0.01
}
\end{Rin}

This gives a final offset value of:

\begin{Rin}
print(round(offset, 2))
\end{Rin}

\begin{Rout}
[1] 0.15
\end{Rout}

Now confirm that the Nash equilibrium is where both taxi firms provide
three vehicles:

\begin{Rin}
eqs <- get_equilibria(profits = profits, offset = offset)
print(eqs)
\end{Rin}

giving:

\begin{Rout}
$`Equilibrium 1`
[1] "3" "3"
\end{Rout}

Therefore, in order to ensure that the maximum amount of taxis are used, the
council should offer a \(\pounds 0.15\) per hour incentive to both taxi
companies for putting on 3 taxis.

\section{Wider context}\label{sec:game_theory_wider_context}

The definition of a normal form game here as well as the solution concept of
Nash equilibrium are common starting points for the use of game theory as a
study of emergent behaviour. Other topics include different forms of games, for
example extensive form games\index{extensive form games} which are represented
by trees and more explicitly model asynchronous decision making. Other solution
concepts involve the specific study of the emergence mechanisms through models
based on natural evolutionary process: Moran processes\index{Moran process} or
replicator dynamics\index{replicator dynamics}. A good text book to read on
these topics is~\cite{maschler2013game}.

John Nash\index{John Nash} whose life was portrayed in the 2001 movie ``A
Beautiful Mind'' (which is an adaptation of~\cite{nasar2011beautiful}) won the
Nobel Prize for~\cite{nash1950equilibrium} in which he proved that a Nash
equilibrium always exists. In~\cite{nash1951non} John Nash gives an
application of game theory to a specific version of poker.

Another application of the concept of Nash equilibrium
is~\cite{deo2011centralized} where the authors identify worst case
scenarios for ambulance diversion: a practice where an emergency room will
declare itself too full to accept new patients. When there are multiple emergency
units serving a same population strategic behaviour becomes relevant. The
authors of this paper identify the effect of this decentralised decision making
and also propose an approach that is socially optimal: similarly to the taxi
problem considered in this chapter.

A specific area of a lot of research in game theory is the study of cooperative
behaviour. Axelrod\index{Axelrod}~\cite{axelrod1990evolution} started this work
with computer tournaments where he invited code submissions of strategies to
play a game called the Iterated Prisoner's
Dilemma\index{Iterated Prisoner's Dilemma}. The outcome of this was an
explanation for how cooperation can emerge in a complex system with a set of 5
rules which included the need to be `nice'. The conclusions of Axelrod's work
and these set of rules continue to be examined and often refuted. For example,
more recent work involving so called Zero-Determinant\index{Zero-Determinant}
strategies which considered extortion as a mathematical
concept~\cite{press2012iterated} and also a possible outcome as opposed to
cooperation.
A review and systemic analysis of some of the research on behaviour, of which
game theory is a subset,
is given in~\cite{press2012iterated}.