# Machine-learning-assisted correction of correlated qubit errors in a
topological code

P. Baireuther  Instituut-Lorentz, Universiteit Leiden, P.O. Box 9506, 2300 RA
Leiden, The Netherlands  T. E. O’Brien  Instituut-Lorentz, Universiteit
Leiden, P.O. Box 9506, 2300 RA Leiden, The Netherlands  B. Tarasinski  QuTech,
Delft University of Technology, P.O. Box 5046, 2600 GA Delft, The Netherlands
C. W. J. Beenakker  Instituut-Lorentz, Universiteit Leiden, P.O. Box 9506,
2300 RA Leiden, The Netherlands

(December 2017)

###### Abstract

A fault-tolerant quantum computation requires an efficient means to detect and
correct errors that accumulate in encoded quantum information. In the context
of machine learning, neural networks are a promising new approach to quantum
error correction. Here we show that a recurrent neural network can be trained,
using only experimentally accessible data, to detect errors in a widely used
topological code, the surface code, with a performance above that of the
established minimum-weight perfect matching (or “blossom”) decoder. The
performance gain is achieved because the neural network decoder can detect
correlations between bit-flip (X) and phase-flip (Z) errors. The machine
learning algorithm adapts to the physical system, hence no noise model is
needed. The long short-term memory layers of the recurrent neural network
maintain their performance over a large number of quantum error correction
cycles, making it a practical decoder for forthcoming experimental
realizations of the surface code.

##  1 Introduction

Figure 1: Schematic of the surface code. Left: $N$ physical data qubits are
arranged on a $d\times d$ square lattice (where $d=\sqrt{N}$ is known as the
distance of the code). For each square one makes the four-fold $\sigma_{x}$ or
$\sigma_{z}$ correlated measurement of Eq. (2). A further set of two-fold
$\sigma_{x}$ and $\sigma_{z}$ measurements are performed on the boundary,
bringing the total number of measurements to $N-1$. Right: Since direct four-
fold parity measurements are impractical, the measurements are instead
performed by entanglement with an ancilla qubit, followed by a measurement of
the ancilla in the computational basis. Both data qubits and ancilla qubits
accumulate errors during idle periods (labeled I) and during gate operations
(Hadamard H and cnot), which must be accounted for by a decoder. The data
qubits are also entangled with the rest of the surface code by the grayed out
gates.

A quantum computer needs the help of a powerful classical computer to overcome
the inherent fragility of entangled qubits. By encoding the quantum
information in a nonlocal way, local errors can be detected and corrected
without destroying the entanglement [1, 2]. Since the efficiency of the
quantum error correction protocol can make the difference between failure and
success of a quantum computation, there is a major push towards more and more
efficient decoders [3]. Topological codes such as the surface code, which
store a logical qubit in the topology of an array of physical qubits, are
particularly attractive because they combine a favorable performance on small
circuits with scalability to larger circuits [4, 5, 6, 7, 8, 9].

In a pioneering work [10], Torlai and Melko have shown that the data
processing power of machine learning (artificial neural networks [11, 12, 13])
can be harnessed to produce a flexible, adaptive decoding algorithm. A test on
a topological code (Kitaev’s toric code [14]) revealed a performance for
phase-flip errors that was comparable to decoders based on the minimum-weight
perfect matching (MWPM or “blossom”) algorithm of Edmonds [15, 16, 17]. The
machine learning paradigm promises a flexibility that the classic algorithms
lack, both with respect to different types of topological codes and with
respect to different types of errors.

Several groups are exploring the capabilities of a neural network decoder [18,
19, 20], but existing designs cannot yet be efficiently deployed as a decoder
in a surface code architecture [21, 22, 23]. Two key features which are
essential for this purpose are 1: The neural network must have a “memory”, in
order to be able to process repeated cycles of stabilizer measurement whilst
detecting correlations between cycles; and 2: The network must be able to
learn from measured data, it should not be dependent on the uncertainties of
theoretical modeling.

In this work we design a recurrent neural network decoder that has both these
features, and demonstrate a performance improvement over a blossom decoder in
a realistic simulation of a forthcoming error correction experiment. Our
decoder achieves this improvement through its ability to detect bit-flip (X)
and phase-flip (Z) errors separately as well as correlations (Y). The blossom
decoder treats a Y-error as a pair of uncorrelated X and Z errors, which
explains the improved performance of the neural network. We study the
performance of the decoder in a simplified model where the Y-error rate can be
adjusted independently of the X- and Z-error rates, and measure the decoder
efficiency in a realistic model (density matrix simulation) of a state-of-the-
art 17-qubit surface code experiment (Surface-17).

The outline of this paper is as follows. In the next section 2 we summarize
the results from the literature we need on quantum error correction with the
surface code. The design principles of the recurrent neural network that we
will use are presented in Sec. 3, with particular attention for the need of an
internal memory in an efficient decoder. This is one key aspect that
differentiates our recurrent network from the feedforward networks proposed
independently [18, 19, 20] (see Sec. 4). A detailed description of the
architecture and training protocol is given in Sec. 5. In Sec. 6 we compare
the performance of the neural network decoder to the blossom decoder for a
particular circuit model with varying error rates. We conclude in Sec. 7 with
a demonstration of the potential of machine learning for real-world quantum
error correction, by decoding data from a realistic quantum simulation of the
Surface-17 experiment.

##  2 Overview of the surface code

To make this paper self-contained we first describe the operation of the
surface code and formulate the decoding problem. The expert reader may skip
directly to the next section.

In a quantum error correcting (QEC) code, single logical qubits (containing
the quantum information to be protected) are spread across a larger array of
$N$ noisy physical data qubits [24, 25]. The encoding is achieved by $N-1$
binary parity check measurements on the data qubits [26]. Before these
measurements, the state of the physical system is described by a complex
vector $|\psi\rangle$ within a $2^{N}$-dimensional Hilbert space
$\mathcal{H}$. Each parity check measurement $M_{i}$ projects $|\psi\rangle$
onto one of two $2^{N-1}$-dimensional subspaces, dependent on the outcome
$s_{i}$ of the measurement. As all parity check measurements commute, the
result of a single cycle of $N-1$ measurements is to project $|\psi\rangle$
into the intersection of all subspaces $\mathcal{H}_{\vec{s}}$ decided by the
measurements $\vec{s}={s_{1},\ldots,s_{N-1}}$ ($s_{i}\in\\{0,1\\}$). This is a
Hilbert space of dimension $2^{N}/2^{N-1}=2$, giving the required logical
qubit $|\psi\rangle_{L}$.

Repeated parity check measurements $\vec{s}(t)$ do not affect the qubit within
this space, nor entanglement between the logical qubit states and other
systems. However, errors in the system will cause the qubit to drift out of
the logical subspace. This continuous drift is discretized by the projective
measurement, becoming a series of discrete jumps between subspaces
$\mathcal{H}_{\vec{s}(t)}$ as time $t$ progresses. Since $\vec{s}(t)$ is
directly measured, the qubit may be corrected, i.e. brought back to the
initial logical subspace $\mathcal{H}_{\vec{s}(0)}$. When performing this
correction, a decision must be made on whether to map the logical state
$|0\rangle_{L}^{\vec{s}(t)}\in\mathcal{H}_{\vec{s}(t)}$ to
$|0\rangle_{L}^{\vec{s}(0)}$ or
$|1\rangle_{L}^{\vec{s}(0)}\in\mathcal{H}_{\vec{s}(0)}$, as no a priori
relationship exists between the labels in these two spaces. If this is done
incorrectly, the net action of the time evolution and correction is a logical
bit-flip error. A similar choice must be made for the
$\\{|+\rangle_{L},|-\rangle_{L}\\}$ logical states, which if incorrect results
in a logical phase-flip error.

Information about the best choice of correction (to most-likely prevent
logical bit-flip or phase-flip errors) is stored within the measurement
vectors $\vec{s}$, which detail the path the system took in state-space from
$\mathcal{H}_{\vec{s}(0)}$ to $\mathcal{H}_{\vec{s}(t)}$. The non-trivial task
of decoding, or extracting this correction, is performed by a classical
decoder. Optimal (maximum-likelihood) decoding is an NP-hard problem [27],
except in the presence of specific error models [28]. However, a fault-
tolerant decoder need not be optimal, and polynomial time decoders exist with
sufficient performance to demonstrate error mitigation on current quantum
hardware [5]. This sub-optimality is quantified by the decoder efficiency [29]

$\eta_{d}=\epsilon_{L}^{\mathrm{(opt)}}/\epsilon_{L}^{\mathcal{D}},$ (1)

where $\epsilon_{L}^{\mathcal{D}}$ is the probability of a logical error per
cycle using the decoder $\mathcal{D}$, and $\epsilon_{L}^{\mathrm{(opt)}}$ is
the probability of a logical error per cycle using the optimal decoder [31].

The QEC code currently holding the record for the best performance under a
scalable decoder is the surface code [4, 16, 5, 3]. As illustrated in Fig. 1,
the surface code is defined on a $d\times d$ lattice of data qubits, where
$d=\sqrt{N}$ is the distance of the code. The measurement operators are
defined by coloring lattice squares as on a checkerboard. Each square
corresponds to a correlated measurement of the stabilizer operator

${\cal
S}_{\alpha}=\sigma^{\text{a}}_{\alpha}\otimes\sigma^{\text{b}}_{\alpha}\otimes\sigma^{\text{c}}_{\alpha}\otimes\sigma^{\text{d}}_{\alpha},$
(2)

with $\alpha=z$ on the green squares and $\alpha=x$ on the blue squares. The
operator $\sigma^{\text{D}}_{\alpha}$ is the Pauli matrix acting on the qubit
in the D-corner of the square (labeled a,b,c,d in Fig 1). The checkerboard is
extended slightly beyond the boundary of the lattice [32], giving an
additional set of two-qubit
$\sigma_{\alpha}^{\text{D}}\sigma_{\alpha}^{\text{D}^{\prime}}$ measurements,
and bringing the total number of measurements to $(d-1)^{2}+2(d-1)=N-1$, as it
should be.

All measurements commute because green and blue squares either share two
corners or none. A bit-flip or phase-flip on any data qubit in the bulk of the
code causes two measurements to change sign, producing unit syndrome
increments

$\delta s_{i}(t)\equiv s_{i}(t)-s_{i}(t-1)\mod 2.$ (3)

This theme is continued even when the measurement of $s_{i}$ itself is allowed
to be faulty; such measurement errors cause two correlated error signals
$\delta s_{i}(t)=1$ separated in time, rather than in space.

As all observable errors can be built from combinations of bit-flip and phase-
flip errors, these measurements allow the mapping of surface-code decoding to
the minimum-weight perfect matching (MWPM) problem [16, 5]. Every instance of
non-zero $\delta s_{i}(t)$ is mapped to a vertex in a graph, with an edge
between two vertices representing the probability of some combination of
errors causing these signals. A ‘boundary’ vertex is included to account for
qubits on the edge of the lattice, whose errors may only cause a single error
signal. Then, the most probable matching of vertices, weighted by the product
of probabilities on individual edges, gives the required error correction.
This matching can be found in polynomial time with Edmonds’ blossom algorithm
[15].

Under current experimental parameters, with the smallest non-trivial $N$
($N=9$, or distance $d=\sqrt{N}=3$), this blossom decoder already crosses the
quantum memory threshold — whereby quantum information on a logical qubit can
be stored for a longer time than on any physical component. However, the
decoder itself performs only with efficiency $\eta_{d}=0.64$, leaving much
room for improvement [29].

##  3 Neural network detection of correlated errors

The sub-optimality of the blossom decoder comes primarily from its inability
to optimally detect Pauli-Y ($\sigma_{y}$) errors [31, 29, 33]. These errors
correspond to a combination of a bit-flip (X) and a phase-flip (Z) on the same
qubit, and are thus treated by a MWPM decoder as two independent errors. Since
these correlations exist as patterns on the graph, one may expect that the
pattern matching capabilities of a neural network could be exploited to
identify the correlations, producing an improvement over existing decoders.
This is the primary motivation of the research we report in what follows.

A key issue in the design of any practical decoder is to ensure that the
decoder is able to operate for an unspecified number of cycles $T$. A
feedforward neural network is trained on a dataset with a specific fixed $T$.
The central advance of this work is to use a recurrent neural network to
efficiently decode an arbitrary, unspecified number of cycles. In order to
learn time correlations the network possesses an internal memory that it
utilizes to store information about previous cycles. This is important because
errors on the ancilla qubits or during ancilla qubit readout lead to error
signals that are correlated over several cycles.

We adopt the recurrent neural network architecture known as a “long short-term
memory” (LSTM) layer [34, 35]. (See App. A for details of our network.) These
layers have two internal states: a short-term memory $\vec{h}_{t}$, and a
long-term memory $\vec{c}_{t}$ that is updated each cycle and retains
information over several cycles. During training, the parameters that
characterize the LSTM layers are updated using back propagation, in order to
efficiently update and utilize the long-term memory to detect logical errors,
even if the corresponding syndrome patterns are non-local in time. The
parameters of the LSTM layers themselves are the same for each cycle; only
their memory changes. This allows for a very efficient algorithm, whose
computational cost per cycle is independent of how many cycles the network has
to decode.

We now formulate the QEC problem that a decoder needs to solve. To be useful
for upcoming QEC experiments and future fault-tolerant quantum algorithms, it
is critical that any decoder uses data that could be generated by such
experiments. This implies that the data available to the neural network, both
for input and labels, must be data generated by qubit measurements (as opposed
to a listing of occurred errors, which is not available in an actual
experiment).

The data available to the decoder after $T$ cycles are the $T$ syndromes
$\vec{s}(t)$, and a final syndrome $\vec{f}$ calculated from readout of the
final data qubits. From this, a decoder must output a single bit of data, the
so-called “final parity correction” that decides on the correction of the
final logical state. The decoder may be trained and tested using the scheme
described in Ref. Obr17. The system is prepared in a known logical state,
chosen from $|0\rangle_{L}$ and $|1\rangle_{L}$ or from $|+\rangle_{L}$ and
$|-\rangle_{L}$, which is held for $T$ cycles and then readout. The final
logical state can be determined by the parity of all data qubit measurements,
to which the final parity correction may be directly added. This gives a
standard binary classification problem for the neural network. Since it is a
priori unknown in which basis the logical qubit will be measured, we need to
train two separate decoders — one for the $x$-basis and one for the $z$-basis.

##  4 Related Work

###  4.1 Approaches going beyond blossom decoding

The neural network decoder improves on the blossom decoder by including
correlations between Pauli-X and Pauli-Z errors. It is possible to account for
these correlations without using machine learning, by adapting the minimum-
weight perfect matching (blossom) algorithm.

Fowler [33] and Delfosse and Tillich [36] achieved this by performing repeated
rounds of X-error and Z-error decoding in series. After each round of X-error
decoding, the weights on the Z-graph are updated based on the likelihood of
underlying Z-errors assuming the X-matching is correct. The overhead from
repeated serial repetitions of the blossom algorithm is limited by restriction
to a small window of decoding for each repetition, resulting in a constant-
time algorithm.

We can compare the results obtained in Ref. Fow13 to our results by extracting
the improvement of correlated over basic fault-tolerant corrections for a
distance-3 code. For a depolarization probability comparable to the one we use
in Fig. 3 the improvement is approximately $24\%$. This is similar to the
improvement we obtained with the neural network decoder.

Both the neural network decoder and the improved blossom decoder perform below
the optimal maximum-likelihood decoder. Several approaches exist to reach the
optimal limit, we mention the incorporation of X–Z correlations via a belief
propagation algorithm [37], and approaches based on renormalization group
methods or Monte Carlo methods [28, 38, 39].

Bravyi, Suchara, and Vargo [28] reported a density-matrix renormalization
group (DMRG) method for exact single-round maximum-likelihood decoding in
polynomial time, assuming bit-flip and dephasing noise. Their performance
continues to be better than the blossom decoder for multi-round decoding. The
method is somewhat limited in the choice of error model; in particular it
cannot account for Y-errors.

The Markov-chain Monte Carlo method of Hutter, Wootton, and Loss [39] samples
over the set of corrections to approximate the maximum-likelihood decoding via
the Metropolis algorithm. This again outperforms the blossom decoder, but it
suffers from an increased run-time cost, with an additional $O(N^{2})$
computational cost.

###  4.2 Approaches based on machine learning

The existence of algorithms [33, 36, 37, 28, 38, 39] that improve on the
blossom decoder does not diminish the appeal of machine learning decoders,
since these offer a flexibility to different types of topological codes that a
dedicated decoder lacks.

Torlai and Melko [10] implemented a machine learning decoder based on a
restricted Boltzmann machine, while Varsamopoulos, Criger, and Bertels [18]
and Krastanov and Jiang [19] used feedforward neural networks. The key
distinction with our work is that we use a recurrent neural network, and
thereby allow the decoder to detect correlations between arbitrary cycles of
stabilizer measurements.

Refs. Tor16 and Kra17 were limited to the study of models without circuit-
level noise (i.e. without measurement error between repeated cycles), and so
no direct quantitative comparison with the performance of our decoder is
possible.

One feedforward neural network in Ref. Var17 was constructed to take the
syndrome from 3 cycles as input. While it cannot decode an arbitrary number of
cycles, it can account for circuit noise at the 3-cycle level. Over that time
frame their performance lies within error bars from that of our recurrent
neural network. (The equivalence of the Pauli-frame-update error rate of Ref.
Var17 and our parity-bit error rate is discussed in App. B.)

##  5 Design of the neural network decoder

Figure 2: Architecture of the recurrent neural network decoder, consisting of
two neural networks. The upper half is network 1 and the lower half is network
2. Ovals denote the long short-term memory (LSTM) layers and fully connected
evaluation layers, while boxes denote input and output data. Solid arrows
denote data flow in the system, and dashed arrows denote the internal memory
flow of the LSTM layers.

The neural network consists of two LSTM layers with internal state sizes
$N_{L}^{(1)}$ and $N_{L}^{(2)}$, and a fully connected evaluation layer with
$N_{L}^{(E)}$ neurons. We implement the decoder using the TensorFlow library
[40], taking $N_{L}^{(1)}=N_{L}^{(2)}=N_{L}^{(E)}=64$. The LSTM layers receive
as input sets of syndrome increments $\delta\vec{s}(t)$ from both the
$x$-stabilizer and the $z$-stabilizer measurements.

When a final parity prediction is required from the network at time $T$,
information from the recurrent network is passed to an evaluation layer, along
with the syndrome increment

$\delta\vec{f}(T)=\vec{f}-\vec{s}(T)\mod 2$ (4)

between final syndrome $\vec{f}$ calculated from the data qubit measurements
and the last syndrome readout $\vec{s}(T)$ from the ancilla qubits. Note that,
while $\vec{s}(t)$ is passed to each decoder in both the $x$-basis and the
$z$-basis, the final syndrome $\vec{f}$ is only available to a decoder in its
own basis.

The memory of the recurrent network solves the issue of how to concatenate
multiple decoding cycles, but one remaining issue occurs at the end of the
computation: the final syndrome breaks time-translational invariance. Within
any cycle, the decoder must account for the possibility that an error signal
($\delta s_{i}(t)=1$) should be propagated forward in time to future cycles.
This is not the case for the final syndrome, as this is calculated directly
from the data qubit measurements, and any errors in the data qubits do not
propagate forward in time.

To achieve time-translational invariance of the decoder we split the problem
into two separate tasks, as shown in Fig. 2. Task 1 is to estimate the
probability $p_{1}$ that the parity of bit-flip errors during T cycles is odd,
based solely on the syndrome increments $\delta\vec{s}(t)$ up to that point
(i.e. those extracted from ancilla measurements). Task 2 is to estimate the
probability $p_{2}$ that the final data qubit measurements make any adjustment
to the final parity measurement, based solely on new information from the
final syndrome increment $\delta\vec{f}(T)$. The final parity probability is
then given by the probabilistic sum

$p=p_{1}(1-p_{2})+p_{2}(1-p_{1}).$ (5)

We use two separate networks for the two tasks. The first network gets $T$
rounds of syndrome increments $\delta\vec{s}(t)$ but not the final syndrome
increment (upper half of Fig. 2). The second network gets only the last
$T_{0}$ syndrome increments $\delta\vec{s}(t)$, but its evaluation layer gets
the last output of the second LSTM layer concatenated with the final syndrome
increment (lower half of Fig. 2). For Surface-$17$, we observe optimal
performance when we allow the task-2 network a window of $T_{0}=3$ cycles,
giving a decoder that works for experiments of three or more cycles. In
general, the number of cycles fed to the second network should be on the order
of the length of the longest time-correlations between syndromes. As task 2
only requires decoding of a fixed number of cycles, it could potentially be
performed by a simpler feedforward network, but we found it convenient to keep
the same architecture as task 1 because of the similarity between the two
tasks.

We discuss the details of the network architecture and training procedure in
App. A. The source code is available [41].

##  6 Neural network performance

Figure 3: Comparison of logical qubit decay between blossom and neural network
decoders for a Pauli error channel model, with $p_{x}=p_{y}=p_{z}=0.048\%$ and
$p_{m}=0.14\%$. We plot the probability that the decoder corrects the logical
qubit after $t$ cycles of stabilizer measurement and error accumulation. All
data is averaged over $5\cdot 10^{4}$ datasets, with error bars obtained by
boot-strapping (using $3\sigma$ for the error). Lines are two-parameter fits
of the data to Eq. (8).

We determine the neural network performance on the 17-qubit distance-$3$
surface code, referred to as “Surface-$17$”, which is under experimental
development [23].

We take at first a simplified Pauli error channel model [42], similar to Refs.
Fow12b, Tom14 but without correlated two-qubit errors. In this model the
performance of the blossom decoder is understood and individual error types
can be focused upon. Stabilizer measurements are made by entangling two or
four data qubits with an ancilla qubit, which is readout in the computational
basis (right panel in Fig. 1).

The process is broken into seven steps: four coherent steps over which cnot
gates are performed, two steps in which Hadamard gates are performed, and one
measurement step. During idle, Hadamard, and cnot steps, both data and ancilla
qubits have independent chances of a $\sigma_{x}$ error (with probability
$p_{x}$), a $\sigma_{y}$ error (with probability $p_{y}$), and a $\sigma_{z}$
error (with probability $p_{z}$). This implies that the total probability
during any step for a qubit to accumulate a $y$-error (as opposed to an
$x$-error, a $z$-error, or no error) is

$\mbox{$y$-error prob.}=p_{y}(1-p_{x})(1-p_{z})+p_{x}p_{z}(1-p_{y}).$ (6)

With this definition $p_{y}=0$ implies that $x$-errors and $z$-errors are
uncorrelated (it does not imply that there are no $y$-errors).

Data qubits behave similarly during measurement steps, but ancilla qubits are
projected into the computational basis and so cannot incur phase errors.
Instead, a measurement has a $p_{m}$ chance of returning the wrong result,
without the qubit state being affected. Qubits are reused after measurement
without reset, and so the syndromes $s_{i}(t)$ are obtained by changes in the
readout $m_{i}(t)$ of an ancilla qubit between rounds,

$s_{i}(t)=m_{i}(t)-m_{i}(t-1)\mod 2.$ (7)

The performance of the logical qubit is measured using the protocol outlined
in Ref. Obr17 (Methods section). The logical qubit is prepared in the
$|0\rangle$ state, held for $T$ cycles, and finally measured and decoded. The
decoder seeks to determine whether or not the qubit underwent a logical bit-
flip during this time. The probability that the decoder obtains the correct
answer gives the logical qubit fidelity, which can be plotted as a function of
the number of cycles. Fig. 3 shows the decay in fidelity over 300 cycles for
$p_{x}=p_{y}=p_{z}=0.048\%$ and $p_{m}=0.14\%$, which corresponds to a
physical error rate of approximately $1\%$ per cycle.

A logical error rate per cycle $\epsilon$ can be obtained from these figures
by a two-parameter fit to the logical fidelity

$\mathcal{F}(t)=\tfrac{1}{2}+\tfrac{1}{2}(1-2\epsilon)^{t-t_{0}},$ (8)

where $t_{0}$ is a constant offset to account for the ‘majority vote’ behavior
of the error correcting circuit at low cycle number [29], and any additional
sample preparation and measurement error. We find $\epsilon=0.209\%$ for the
neural network decoder, a substantial improvement over the value
$\epsilon=0.274\%$ for the blossom decoder [30].

Figure 4: Comparison of the error rates $\epsilon$ of a logical qubit decoded
by a neural network and a blossom decoder, for different values of the
correlated error rate $p_{y}$. As $p_{y}$ increases, at fixed
$p_{x}=p_{z}=0.048\%$ and $p_{m}=0.14\%$, the blossom decoder (blue) produces
a larger error rate than the neural network decoder (red). Data points are
obtained by fitting decay curves, as in Fig. 3.

To demonstrate that the performance improvement is due to the capability of
the neural network to detect error correlations, we show in Fig. 4 how the
performance varies with varying $p_{y}$ (at fixed $p_{x}=p_{z}=0.048\%$ and
$p_{m}=0.14\%$). When $p_{y}=0$, the $\sigma_{x}$ and $\sigma_{z}$ errors are
independent and the blossom decoder performs near-optimally [29, 31]. The
neural network decoder then gives no improvement, but once $p_{y}\sim p_{x}$
the performance gain is evident.

##  7 Conclusion and outlook

Figure 5:  Same as Fig. 3, but now for a density matrix simulation of an
implementation of Surface-17 using superconducting transmon qubits [29].

In conclusion, we have designed and tested a recurrent neural network decoder
that outperforms the standard minimum-weight perfect matching (MWPM, or
“blossom”) decoder in the presence of correlated bit-flip and phase-flip
errors. The building block of the network, a long short-term memory layer,
allows the decoder to operate over the full duration of a quantum algorithm
with multiple cycles. A key feature of our design, which sets it apart from
independent proposals [18, 19, 20], is that the network can be trained solely
on experimental data, without requiring a priori assumptions from theoretical
modeling.

We believe that our neural network decoder provides a realistic option for
utilization in forthcoming experimental QEC implementations [23]. In support
of this, we have tested the performance in a real-world setting by using a
density matrix simulator to model Surface-$17$ with state-of-the-art
experimental parameters for superconducting transmon qubits [29]. In Fig. 5 we
show the decay of the fidelity over 100 cycles for the neural network and
blossom decoders, as well as an upper bound on the optimal fidelity. (The
latter is extracted directly from the simulation data.) The decoder efficiency
(1) of the neural network is $\eta_{d}=0.81$, a $26\%$ improvement over the
blossom decoder. This improvement was achieved after training on $4\cdot
10^{6}$ datasets, which require roughly $60\,{\rm s}$ to generate on
experimental hardware [23], making this approach immediately experimentally
viable.

We mention three directions for future research. The first is the extension to
other topological codes than the surface code, such as the color code. The
neural network itself is agnostic to the type of topological code used, so
this extension should be feasible without modifications of the design.
Secondly, for low error rates it will be challenging to train a neural network
decoder, because then the training dataset is unlikely to contain a sufficient
representation of two-qubit errors. This can potentially be overcome by
training on data with a higher error rate, but it remains to be seen whether a
decoder trained this way will outperform MWPM decoding. Finally, the decoder
needs to be scaled-up to surface codes that are deformed by lattice surgery
[43] or braiding [4] for the execution of logical gates. For this extension
the design of the decoder should be modified so that it is not tied to a
single code distance.

We have benefited from discussions with B. Criger, L. DiCarlo, A. G. Fowler,
V. Ostroukh, and B. Terhal. This research is supported by the Netherlands
Organization for Scientific Research (NWO/OCW), an ERC Synergy Grant, and by
the Office of the Director of National Intelligence (ODNI), Intelligence
Advanced Research Projects Activity (IARPA), via the U.S. Army Research Office
grant W911NF-16-1-0071. The views and conclusions contained herein are those
of the authors and should not be interpreted as necessarily representing the
official policies or endorsements, either expressed or implied, of the ODNI,
IARPA, or the U.S. Government. The U.S. Government is authorized to reproduce
and distribute reprints for Governmental purposes notwithstanding any
copyright annotation thereon.

## References

  * [1] D. A. Lidar, T. A. Brun, editors, Quantum error correction (Cambridge University Press, 2013).
  * [2] B. M. Terhal, Quantum error correction for quantum memories, https://doi.org/10.1103/RevModPhys.87.307Rev. Mod. Phys. 87, 307 (2015).
  * [3] A. G. Fowler, A. C. Whiteside, and L. C. L. Hollenberg, Towards practical classical processing for the surface code, https://doi.org/10.1103/PhysRevLett.108.180501Phys. Rev. Lett. 108, 180501 (2012).
  * [4] S. B. Bravyi and A. Yu. Kitaev, Quantum codes on a lattice with boundary, https://arxiv.org/abs/quant-ph/9811052arXiv:quant-ph/9811052.
  * [5] D. S. Wang, A. G. Fowler, and L. C. L. Hollenberg, Surface code quantum computing with error rates over 1$\%$, https://doi.org/10.1103/PhysRevA.83.020302Phys. Rev. A 83, 020302 (2011).
  * [6] A. G. Fowler, M. Mariantoni, J. M. Martinis, and A. N. Cleland, Surface codes: Towards practical large-scale quantum computation, https://doi.org/10.1103/PhysRevA.86.032324Phys. Rev. A 86, 032324 (2012).
  * [7] Yu Tomita and K. M. Svore. Low-distance surface codes under realistic quantum noise, https://doi.org/10.1103/PhysRevA.90.062320Phys. Rev. A 90, 062320 (2014).
  * [8] J. R. Wootton, A. Peter, J. R. Winkler, and D. Loss, Proposal for a minimal surface code experiment, https://doi.org/10.1103/PhysRevA.96.032338Phys. Rev. A 96, 032338 (2017).
  * [9] N. H. Nickerson, Error correcting power of small topological codes, https://arxiv.org/abs/1609.01753arXiv:1609.01753.
  * [10] G. Torlai and R. G. Melko, Neural decoder for topological codes, https://doi.org/10.1103/PhysRevLett.119.030501Phys. Rev. Lett. 119, 030501 (2017).
  * [11] R. Rojas, Neural Networks, https://doi.org/10.1007/978-3-642-61068-4(Springer, Berlin, Heidelberg, 1996).
  * [12] Y. Bengio, Learning deep architectures for AI, http://doi.org/10.1561/2200000006Foundations and Trends in Machine Learning 2, 1 (2009).
  * [13] S. Shalev-Shwartz and S. Ben-David, Understanding machine learning: From theory to algorithms (Cambridge University Press, 2014).
  * [14] A. Yu. Kitaev, Fault-tolerant quantum computation by anyons, https://doi.org/10.1016/S0003-4916(02)00018-0Ann. Physics 303, 2 (2003).
  * [15] J. Edmonds, Paths, trees, and flowers, http://doi.org/10.4153/CJM-1965-045-4Canad. J. Math. 17, 449 (1965).
  * [16] E. Dennis, A. Kitaev, A. Landahl, and J. Preskill, Topological quantum memory, https://doi.org/10.1063/1.1499754J. Math. Phys. 43, 4452 (2002).
  * [17] A. G. Fowler, Minimum weight perfect matching of fault-tolerant topological quantum error correction in average $O(1)$ parallel time, Quantum Inf. Comput. 15, 0145 (2015).
  * [18] S. Varsamopoulos, B. Criger, and K. Bertels, Decoding small surface codes with feedforward neural networks, https://doi.org/10.1088/2058-9565/aa955aQuantum Sci. Technol. 3, 015004 (2018).
  * [19] S. Krastanov and L. Jiang, Deep neural network probabilistic decoder for stabilizer codes, https://doi.org/10.1038/s41598-017-11266-1Sci. Rep. 7, 11003 (2017).
  * [20] Both Refs. Var17 and Kra17 represent independent research that was reported on arXiv at about the same time as the work reported here.
  * [21] J. Kelly, R. Barends, A. G. Fowler, A. Megrant, E. Jeffrey, T. C. White, D. Sank, J. Y. Mutus, B. Campbell, Yu Chen, Z. Chen, B. Chiaro, A. Dunsworth, I.-C. Hoi, C. Neill, P. J. J. O’Malley, C. Quintana, P. Roushan, A. Vainsencher, J. Wenner, A. N. Cleland, and J. M. Martinis, State preservation by repetitive error detection in a superconducting quantum circuit, https://doi.org/10.1038/nature14270Nature 519, 66 (2015).
  * [22] M. Takita, A. D. Córcoles, E. Magesan, B. Abdo, M. Brink, A. Cross, J. M. Chow, and J. M. Gambetta, Demonstration of weight-four parity measurements in the surface code architecture, https://doi.org/10.1103/PhysRevLett.117.210505Phys. Rev. Lett. 117, 210505 (2016).
  * [23] R. Versluis, S. Poletto, N. Khammassi, B. Tarasinski, N. Haider, D. J. Michalak, A. Bruno, K. Bertels, and L. DiCarlo, Scalable quantum circuit and control for a superconducting surface code, https://doi.org/10.1103/PhysRevApplied.8.034021Phys. Rev. Applied 8, 034021 (2017).
  * [24] P. W. Shor, Scheme for reducing decoherence in quantum computer memory, https://doi.org/10.1103/PhysRevA.52.R2493Phys. Rev. A 52, R2493 (1995).
  * [25] A. Steane, Multiple-particle interference and quantum error correction, https://doi.org/10.1098/rspa.1996.0136Proc. Royal Soc. A 452, 2551 (1996).
  * [26] D. Gottesman, Stabilizer codes and quantum error correction (Doctoral dissertation, California Institute of Technology, 1997).
  * [27] M.-H. Hsieh and F. Le Gall, NP-hardness of decoding quantum error-correction codes, https://doi.org/10.1103/PhysRevA.83.052331Phys. Rev. A 83, 052331 (2011).
  * [28] S. Bravyi, M. Suchara, and A. Vargo, Efficient algorithms for maximum likelihood decoding in the surface code, https://doi.org/10.1103/PhysRevA.90.032326Phys. Rev. A 90, 032326 (2014).
  * [29] T. E. O’Brien, B. Tarasinski, and L. DiCarlo, Density-matrix simulation of small surface codes under current and projected experimental noise, https://doi.org/10.1038/s41534-017-0039-xnpj Quantum Information 3, 39 (2017). The source code of the quantum simulator can be found at https://github.com/brianzi/quantumsimhttps://github.com/brianzi/quantumsim. The source code of the Surface-17 simulation can be found at https://github.com/obriente/surf17_circuithttps://github.com/obriente/surf17_circuit.
  * [30] The source code of the blossom decoder can be found at https://github.com/obriente/qgardenhttps://github.com/obriente/qgarden.
  * [31] B. Heim, K. M. Svore, and M. B. Hastings, Optimal circuit-level decoding for surface codes, https://arxiv.org/abs/1609.06373arXiv:1609.06373.
  * [32] H. Bombin, and M. A. Martin-Delgado, Optimal resources for topological two-dimensional stabilizer codes: Comparative study, https://doi.org/10.1103/PhysRevA.76.012305Phys. Rev. A 76, 012305 (2007).
  * [33] A. G. Fowler, Optimal complexity correction of correlated errors in the surface code, https://arxiv.org/abs/1310.0863arXiv:1310.0863.
  * [34] S. Hochreiter and J. Schmidhuber, Long short-term memory, https://doi.org/10.1162/neco.1997.9.8.1735 Neural Computation 9, 1735 (1997).
  * [35] W. Zaremba, I. Sutskever, and O. Vinyals, Recurrent neural network regularization, https://arxiv.org/abs/1409.2329arXiv:1409.2329.
  * [36] N. Delfosse and J.-P. Tillich, A decoding algorithm for CSS codes using the X/Z correlations, https://doi.org/10.1109/ISIT.2014.68749972014 IEEE International Symposium on Information Theory, 1071 (2014).
  * [37] B. Criger and I. Ashraf, Multi-path summation for decoding 2D topological codes, https://arxiv.org/abs/1709.02154arXiv:1709.02154.
  * [38] G. Duclos-Cianci and D. Poulin, Fast decoders for topological quantum codes, https://doi.org/10.1103/PhysRevLett.104.050504Phys. Rev. Lett. 104, 050504 (2010).
  * [39] A. Hutter, J. R. Wootton, and D. Loss, Efficient Markov chain Monte Carlo algorithm for the surface code, https://doi.org/10.1103/PhysRevA.89.022326Phys. Rev. A 89, 022326 (2014).
  * [40] M. Abadi, A. Agarwal, P. Barham, E. Brevdo, Z. Chen, C. Citro, G. S. Corrado, A. Davis, J. Dean, M. Devin, S. Ghemawat, I. Goodfellow, A. Harp, G. Irving, M. Isard, Y. Jia, R. Jozefowicz, L. Kaiser, M. Kudlur, J. Levenberg, D. Mané, R. Monga, S. Moore, D. Murray, C. Olah, M. Schuster, J. Shlens, B. Steiner, I. Sutskever, K. Talwar, P. Tucker, V. Vanhoucke, V. Vasudevan, F. Viégas, O. Vinyals, P. Warden, M. Wattenberg, M. Wicke, Y. Yu, and X. Zheng, TensorFlow: Large-scale machine learning on heterogeneous distributed systems, https://arxiv.org/abs/1603.04467arXiv:1603.04467.
  * [41] The source code of the neural network decoder can be found at https://github.com/baireuther/neural_network_decoderhttps://github.com/baireuther/neural_network_decoder.
  * [42] The source code of the error model can be found at https://github.com/baireuther/circuit_modelhttps://github.com/baireuther/circuit_model.
  * [43] C. Horsman, A. G. Fowler, S. Devitt, and R. van Meter, Surface code quantum computing by lattice surgery, https://doi.org/10.1088/1367-2630/14/12/123011New J. Phys. 14, 123011 (2012).
  * [44] D. P. Kingma and J. Ba, Adam: A method for stochastic optimization, https://arxiv.org/abs/1412.6980arXiv:1412.6980.
  * [45] G. E. Hinton, N. Srivastava, A. Krizhevsky, I. Sutskever, and R. R. Salakhutdinov, Improving neural networks by preventing co-adaptation of feature detectors, https://arxiv.org/abs/1207.0580arXiv:1207.0580.

##  Appendix A Details of the neural network decoder

###  A.1 Architecture

The decoder is composed of two networks. The first network maps a list of
syndrome increments $\delta\vec{s}(t)$ with $t=1,2,...,T$ to a probability
$p_{1}\in[0,1]$. The second network maps a list with the last few syndrome
increments $t=T-T_{0}+1,T-T_{0}+2,...,T$, together with a single final
syndrome increment $\delta\vec{f}(T)$ to a probability $p_{2}\in[0,1]$. The
probabilistic sum $p=p_{1}(1-p_{2})+p_{2}(1-p_{1})$ of these two outputs is
the probability that the logical qubit has incurred a bit-flip error. The cost
function we try to minimize is the cross-entropy between this probability and
the true final parity of bit-flip errors (labels) plus a small weight
regularization term.

We note that $p$ is invariant under the transformation $p_{1}\rightarrow
1-p_{1}$ and $p_{2}\rightarrow 1-p_{2}$. This ambiguity in the individual
error probabilities is irrelevant for the joint operation of the networks.
Moreover, it may be easily removed by testing the trained networks separately
on a trivial example where all syndromes are zero and both probabilities
should be $<1/2$.

Both networks consist of two LSTM layers with internal states
$\vec{c}_{t}^{i},\vec{h}_{t}^{i}\in\mathbb{R}^{64}$ and a fully connected
evaluation layer with 64 rectified linear units. The inputs of the first layer
are the syndrome increments. The inputs of the second layer are the outputs of
the first layer $\vec{h}_{t}^{1}$. For the first network, the input of the
evaluation layer is the final output of the second LSTM layer, subject to a
rectified linear activation function $\textnormal{ReL}(\vec{h}_{T}^{2})$. For
the second network, the input of the evaluation layer is
$\textnormal{ReL}(\vec{h}_{T}^{2})$ concatenated with the final syndrome
increment $\delta\vec{f}(T)$.

The source code including all the network parameters is available [41].

###  A.2 Training and evaluation

The two networks are trained simultaneously on mini-batches of size 64 from a
training dataset containing $4\cdot 10^{6}$ sequences of lengths between
$T=11$ and $T=20$ cycles. At the end of each sequence, the training set
contains the final syndrome increment and the final parity of bit-flip errors.
One epoch consists of $10^{4}$ mini-batches. The optimization is done using
the Adam optimizer [44] with a learning rate of $10^{-3}$. For regularization
we apply dropout [45] with a keep probability of $0.8$ after each LSTM layer
and after the evaluation layer. In addition, we apply weight decay with a
prefactor of $10^{-5}$ to the evaluation layer. After each epoch, the decoder
is evaluated on a validation dataset, which consists of $10^{4}$ sequences of
lengths between $T=81$ and $T=100$ cycles. If the logical error rate on the
validation dataset reaches a new minimum, the network is stored. The training
continues until the logical error rate on the validation dataset has not
improved for $100$ epochs. We train three decoders and choose the instance
that has the lowest logical error rate on the validation dataset.

To evaluate the chosen decoder, we use yet another dataset. This test dataset
consists of $5\cdot 10^{4}$ sequences of length $T=300$ for the Pauli error
channel model and $T=100$ for the density matrix simulation. In contrast to
the training and validation datasets, the test dataset contains a final
syndrome increment and a final parity of bit-flip errors after each cycle.
This cannot be achieved in a real experiment, but is extracted from the
simulation to keep the calculation time manageable. We evaluate the decoder on
the test dataset for $t_{n}=2+\sum_{n^{\prime}=1}^{n}n^{\prime}\leq T$ cycles,
chosen such that the resolution is high at small cycle numbers and lower at
large cycle numbers. If the decoders output is $p<0.5$, the final parity of
bit-flip errors is predicted to be even and otherwise odd. We then compare
this to the true final parity and average over the test dataset to obtain the
logical fidelity. Using a two-parameter fit to Eq. (8) we obtain the logical
error rate per cycle.

##  Appendix B Parity-bit error versus Pauli-frame-update error

Ref. Var17 described the error rate of the decoder in terms of its ability to
apply the correct Pauli frame update. The error rate $\epsilon$ from Eq. (8)
describes the correctness of the parity bit produced by the decoder, without
explicitly referring to a Pauli frame update. Here we show that the two error
rates are in fact the same.

We recall that a Pauli frame is a list of Pauli X, Y, or Z errors that have
occurred to data qubits [2]. Two Pauli frames are equivalent if they are
separated by stabilizer measurements, since these act as the identity on the
error-free subspace.

We begin by choosing logical operators $X_{L}$ and $Z_{L}$ in terms of Pauli
operators on the physical qubits. The choice is not unique because of a gauge
freedom: $SX_{L}=X_{L}$ on the logical subspace for any stabilizer operator
$S$.

Consider a syndrome $\vec{s}(t)$ that contains only a single non-zero
stabilizer measurement $s_{i}(t)$, corresponding to a stabilizer operator
$S_{i}$. There exist multiple Pauli frames $P_{i}$ that correct $S_{i}$ and
which commute with our chosen logical operators. Ref. Var17 considers a
‘simple’ decoder, which arbitrarily chooses one $P_{i}$ for each $S_{i}$.
Then, given a syndrome $\vec{s}(t)$ at time $t$ with many non-zero $s_{i}$, it
generates a Pauli frame as $P_{\mathrm{simple}}=\prod_{i,s_{i}(t)=1}P_{i}$.

The simple decoder is coupled to a neural network decoder, which outputs a
parity bit $p$ that determines whether or not to multiply
$P_{\mathrm{simple}}$ by $X_{L}$ (if the neural network is calculating
Z-parity) or $Z_{L}$ (if the neural network is calculating X-parity). We
denote the resulting Pauli frame update by $P_{\rm calc}$. If it differs from
the true Pauli frame update $P_{\rm true}$ the decoder has made an error, and
the rate at which this happens is the Pauli frame update error rate
$\epsilon_{P}$.

To see that this $\epsilon_{P}$ is equivalent to the parity-bit error rate
$\epsilon$, we consider for the sake of definiteness a neural network that
calculates the Z-parity. The two Pauli frames $P_{\mathrm{calc}}$ and
$P_{\mathrm{true}}$ differ by $X_{L}$ when
$[P_{\mathrm{true}},Z_{L}]\neq[P_{\mathrm{calc}},Z_{L}]$. But
$[P_{\mathrm{true}},Z_{L}]$ is the parity readout of the data qubits, and
$[P_{\mathrm{calc}},Z_{L}]$ is precisely our prediction. Alternatively, note
that the simple decoder is constructed to fix $[P_{\mathrm{simple}},Z_{L}]=0$,
and the choice to multiply this by $X_{L}$ precisely fixes
$[P_{\mathrm{calc}},Z_{L}]=p$.

We finally note that in a physical experiment the Pauli frame
$P_{\mathrm{true}}$ is undetermined unless the data qubits themselves are
measured in the Z or X basis, and the gauge freedom is fixed at random by this
measurement. The parity bit $p$ is therefore not only more convenient for a
neural network to output than a Pauli frame update, but also more appropriate,
as this way the neural network does not spend time trying to predict the
outcome of quantum randomness.


# Classification based credit risk analysis: The case of Lending Club

Aadi Gupta  Department of Mathematics, Indian Institute of Technology
Guwahati, Guwahati-781039, India, e-mail<EMAIL_ADDRESS>Priya
Gulati  Department of Mathematics, Indian Institute of Technology Guwahati,
Guwahati-781039, India, e-mail<EMAIL_ADDRESS>Siddhartha P.
Chakrabarty  Department of Mathematics, Indian Institute of Technology
Guwahati, Guwahati-781039, India, e-mail<EMAIL_ADDRESS>
###### Abstract

In this paper, we performs a credit risk analysis, on the data of past loan
applicants of a company named Lending Club. The calculation required the use
of exploratory data analysis and machine learning classification algorithms,
namely, Logistic Regression and Random Forest Algorithm. We further used the
calculated probability of default to design a credit derivative based on the
idea of a Credit Default Swap, to hedge against an event of default. The
results on the test set are presented using various performance measures.

Keywords: Credit risk; Classification algorithm; Exploratory data analysis

##  1 Introduction

Lending Club, headquartered in San Francisco, was the first peer-to-peer
lending institution to offer its securities through the Securities and
Exchange Commission (SEC) and enter the secondary market [1]. This article is
essentially a case study, on how financial engineering problems can be
addressed using Machine Learning (ML) and Exploratory Data Analysis (EDA)
approaches. Lending Club specializes in extending different types of loans to
urban customers, which is decided on the basis of the applicant’s profile.
Accordingly, the data considered in this work, contains information about past
loan applications and whether they were “defaulted” or “not”. One of the main
objectives of this work is the calculation of the Probability of Default (PD)
and (in case of a default) the determination of Loss Given Default (LGD),
Exposure at Default (EAD), and finally, the Expected Loss (EL), making use of
the historical data. Also, using the “Recovery Rate” (estimated while
calculating “LGD” from “EAD”) and the PD, a simple credit derivative is
implemented, based on the concept of Credit Default Swaps (CDS) which can be
used to hedge against such defaults. It may be noted that the total value a
lender is exposed to when a loan defaults, is the EAD and the consequent
unrecoverable amount for the lender is the LGD [2]. Accordingly, EL is defined
as,

$\text{EL}=\text{EAD}\times\text{LGD}\times\text{PD}.$

The driver of this work is the notion of “Classification Algorithm”, which
weighs the input data (of the applicant, in this case) to classify the input
features into positive and negative classes [3] (default on the loan or not,
in this case). Accordingly, we consider two “Classification Algorithms”,
namely, the Logistic Regression and the Random Forest.

The main idea behind Logistic Regression is to determine the probability of a
particular data point belonging to the positive class (in the case of binary
classification) [4]. The model does so by establishing a “linear relationship”
between the independent and the dependent variables. The weights of the linear
relations is determined through the minimization of a Loss Function, which is
achieved by using the “Gradient Descent Optimization Algorithm”. Since
Logistic Regression first predicts the probability of belonging to the
positive class, therefore it creates a linear decision boundary (based on a
threshold, set by the user), separating the two classes from one another. This
decision boundary can now be represented as a conditional probability [5].
Implementation of the Random Forest algorithm involves the training stage
construction of several decision trees [6], and predictions emanating from
these trees are averaged to arrive at a final prediction. Since the algorithm
uses an average of results to make the final prediction, the Random Forest
algorithm is referred to as an ensemble technique. Decision Trees are designed
to optimally split the considered dataset into smaller and smaller subsets, in
order to predict the value being targeted [7, 8]. Some of the criterion used
to calculate the purity or impurity of a node, include Entropy and Gini
Impurity. In summary, a decision tree splits the nodes on all the attributes
present in the data, and then chooses the split with the most Information
Gain, with the Decision Tree model of Classification and Regression Trees
(CART), being used in this paper.

The firm based model uses the value of a firm to represent the event of
default, with the default event being represented by the boundary conditions
of the process and the dynamics of the firm value [9]. In particular, we refer
to two well-established models. Firstly, we mention the Merton model based on
the the seminal paper of Black and Scholes [10], which is used to calculate
the default probability of a reference entity. In the context, the joint
density function for the first hitting times is determined [11]. Secondly, we
have the Black-Cox model, which addresses some of the disadvantages of the
Merton model [10]. In order to hedge against credit risk, the usage of credit
derivatives is a customary approach [12, 13, 14], with Credit Default Swaps
(CDS) being the most common choice of credit derivatives. This type of
contracts entail the buyer of the CDS to transfer the credit risk of a
reference entity (“Loans” in this case constitute the reference entity) to the
seller of the protection, until the credit has been settled. In return, the
protection buyer pays premiums (predetermined payments) to the protection
seller, which continues until the maturity of the CDS or a default, whichever
is earlier [10]. The interested reader may refer to [10] for the formula of
CDS spread per annum, to be used later in this paper. Another widely used
credit derivative, albeit more sophisticated than the CDS are the
Collateralized Debt Obligation (CDO), which is a structured product, based on
tranches [14]. CDOs can further classified into cash, synthetic and hybrid.
The interested reader may refer to [14] for a detailed presentation on pricing
of synthetic CDOs.

##  2 Methodology

The goal of this exercise is the approximation of a classification model, on
the data considered (from the peer-to-peer lending company Lending Club), in
order to predict as to whether an applicant (whose details are contained in
the considered database) is likely to default on the loan or not. Accordingly,
to this end, it is necessary to identify and understand the essential
variables, and take into account the summary statistics, in conjunction with
data visualization. The dataset used for the estimation of PD, EAD, LGD and EL
was obtained from Kaggle [15]. The data contains details of all the applicants
who had applied for a loan at Lending Club. There were separate files obtained
for the accepted and the rejected loans. The file “accepted loans” was only
used, since the observations were made on the applicants who ultimately paid
the loan, and those who defaulted on the loan. The data involved the details
of the applicants for the loan, such as FICO score, loan amount, interest
rate, purpose of loan etc. Machine Learning (ML) algorithms were applied on
this data to predict the PD after data exploration, data pre-processing and
feature cleaning.

The feature selection was performed on the considered data, bearing in mind
the goals of predictive modelling. Given the large number of existent
features, it was desirable to achieve a reduction in their numbers, thereby
retaining only the relevant ones. This action abetted the reduction in
computational cost and improvement in the performance of the model.

The data pre-processing was carried out before the application of the
classification model, in order to obtain the probabilities of default. This
exercise involved the removal or filling of missing data, removing features
which are repetitive or deemed unnecessary and the conversion of categorical
features to dummy variables. Some of the dropped columns were “emp_title”,
“emp_length”, “grade”, “issue_d” and “title”. After pre-processing, the data
was divided into the customary training set and test set. The former was then
used to train the classification model, with the results being evaluated
subsequently, on the latter.

In order to accomplish the goal of comparison of different models, the problem
of predicting the PD (denoted by $p$) was treated as a classification problem,
where, if $p\geq 0.5$, it is treated as $1$ (i.e., the person will default)
and if $p<0.5$, it is treated as $0$ (i.e., the person will not default).
Accordingly, $p=0.5$ was used as the threshold for the classifier.

It was observed from the data, that the classes are imbalanced, and therefore,
accuracy is not a good metric, when it comes to comparison of different
models. Accordingly, we instead used the “Confusion Matrix” (to evaluate the
performance of our model), which is a table with four different combinations
of actual and predicted values i.e., True Positive (TP), True Negative (TN),
False Positive (FP) and False Negative (FN). It is beneficial for measuring
Recall $\displaystyle{\left(\frac{\text{TP}}{\text{TP+FN}}\right)}$, Precision
$\displaystyle{\left(\frac{\text{TP}}{\text{TP+FP}}\right)}$ and F1-score
(Harmonic Mean of Recall and Precision).

Receiver Operating Characteristic (ROC) curve is used to compare the
performance of a classification algorithm by varying the threshold value [16].
ROC is a probability curve and the area under the curve (AUC) represents the
likelihood (Random Positive has dominant ranking over Random Negative) i.e.,
the degree of separability. The AUC represents how successful is the model in
differentiating between the classes. The higher the AUC value, the more
amenable is the model at classifying $0$ classes as $0$, and $1$ classes as
$1$ (i.e., correct prediction). The ROC curve was plotted with TPR (True
Positive Rate) against the FPR (False Positive Rate) where FPR is on the
$x$-axis and TPR is on the $y$-axis. Note that TPR is the same as Sensitivity
and $\displaystyle{\text{1-FPR}=\frac{\text{TN}}{\text{FP+TN}}}$ is also
called Specificity.

##  3 Results

The statistics-based feature selection methods were used to assess the
correlation between every input variable and the target variable, and those
input variables which demonstrated the strongest relationship vis-a-vis the
target variable, were selected. Further, for selecting the relevant input
variables, a comparative analysis was performed and some of the observations,
as a result of this exercise, are noted below:

  1. (A)

It looks like $F$ and $G$ sub-grades do not get paid back that often.

  2. (B)

The loans with the highest interest rates are faced with a greater likelihood
of default

  3. (C)

Only $339$ borrowers have reported an annual income exceeding $1$ million.

  4. (D)

It seems that the more the “dti”, the more likely is it that the loan will not
be paid.

  5. (E)

Only $1211$ borrowers have more than $40$ open credit lines.

  6. (F)

Only $1299$ borrowers have more than $80$ credit lines in the borrower credit
file.

The graph in Figure 1 shows the correlation between various features, like
int_rate and loan_status (i.e., whether the debtor will default or not).
Positive correlation implies that more the value of particular feature, more
is the chance that the debtor will default on their loan.

Figure 1: Correlation Coefficient between Target Variable and Various
Attributes

###  3.1 Logistic Regression

The classification report displays precision, recall and f1-score for both the
classes $0$ and $1$ i.e., applicants who will not default and applicants who
will default, respectively. Apart from that, the report also shows Macro
Average ($0.5\times\text{score}_{0}+0.5\times\text{score}_{1}$) and the
weighted average ($w_{0}\times\text{score}_{0}+w_{1}\times\text{score}_{1}$,
where $w_{0}$ and $w_{1}$ are set according to the number of data points in
each class). Accordingly, the Accuracy Score obtained is $97.17\%$ and the
Classification Report for the “Train Result” is given in Table 1.

| 0.0 | 1.0 | Accuracy | Macro Average | Weighted Average
---|---|---|---|---|---
Precision | 0.97 | 0.99 | 0.97 | 0.98 | 0.97
Recall | 1.00 | 0.87 | 0.97 | 0.93 | 0.97
f1-score | 0.98 | 0.92 | 0.97 | 0.95 | 0.97
Table 1: Train Evaluation Metrics for Logistic Regression

Further, the Accuracy Score is $88.81\%$ and the Classification Report for the
“Test Result” is given in Table 2.

| 0.0 | 1.0 | Accuracy | Macro Average | Weighted Average
---|---|---|---|---|---
Precision | 0.98 | 0.65 | 0.89 | 0.82 | 0.92
Recall | 0.87 | 0.95 | 0.89 | 0.91 | 0.89
f1-score | 0.93 | 0.77 | 0.89 | 0.85 | 0.90
Table 2: Test Evaluation Metrics for Logistic Regression

The Confusion Matrix for Logistic Regression Model and the ROC Curve for
Logistic Regression Model are given in Figures 2 and 3, respectively.

Figure 2: Confusion Matrix for Logistic Regression Model Figure 3: ROC Curve
for Logistic Regression Model

###  3.2 Random Forest Classifier

For the Random Forest classifier, the Accuracy Score obtained is $100.00\%$
and the Classification Report for the “Train Result” is given in Table 3.

| 0.0 | 1.0 | Accuracy | Macro Average | Weighted Average
---|---|---|---|---|---
Precision | 1.00 | 1.00 | 1.00 | 1.00 | 1.00
Recall | 1.00 | 1.00 | 1.00 | 1.00 | 1.00
f1-score | 1.00 | 1.00 | 1.00 | 1.00 | 1.00
Table 3: Train Evaluation Metrics for Random Forest

Further, the Accuracy Score is $97.11\%$ and the Classification Report for the
“Train Result” is given in Table 4.

| 0.0 | 1.0 | Accuracy | Macro Average | Weighted Average
---|---|---|---|---|---
Precision | 0.97 | 1.00 | 0.97 | 0.98 | 0.97
Recall | 1.00 | 0.86 | 0.97 | 0.93 | 0.97
f1-score | 0.98 | 0.92 | 0.97 | 0.95 | 0.97
Table 4: Test Evaluation Metrics for Random Forest

The Confusion Matrix for Logistic Regression Model and the ROC Curve for
Logistic Regression Model are given in Figures 4 and 5, respectively

Figure 4: Confusion Matrix for Random Forest Model Figure 5: ROC Curve for
Random Forest Model

###  3.3 Comparison

Finally, the comparative analysis, based on the AUC are presented in Table 5
and Figure 6. It may be noted from Table 5, that in terms of the AUC score,
Logistic Regression performs better than the Random Forest.

| roc_auc_score
---|---
Random Forest | 0.928
Logistic Regression | 0.910
Table 5: AUC Score for Logistic Regression and Random Forest Figure 6: Graph
showing Train and Test Accuracy for both the models

###  3.4 Calculating EAD, LGD, EL and price of the Credit Derivative

We begin with the following assumptions in order to calculate EAD and LGD.
Recovery rate (which is used to calculate the LGD and the price of the credit
derivative) is calculated using the recoveries mentioned for the “Charged Off”
clients present in the data. Also, the recovery rate is calculated for each
department separately. Pricing of the credit derivative is based upon the
theory highlighted in Section 1. Further, the following assumptions are made
while pricing the derivative:

  1. (A)

Only $1$ payment occurs from protection buyer to protection seller (either at
maturity or at the time of default).

  2. (B)

The prevalent (risk-free) interest rates are taken into account while
calculating the discounting factor.

  3. (C)

For the calculation of accural amount,we assume that on an average defaults
occur right in between of a payment period.

  4. (D)

At the time of the contract writing, the remaining loan amount (including
interest rate) is taken as the notional amount and the loan is treated as a
bond.

Table 6 shows some (illustrative) debtors with their probability of default,
along with EAD, LGD, EL and the price of the credit derivative.

Probability of Default | EAD | LGD | EL | Credit Derivative Price (in bps)
---|---|---|---|---
0.09 | 18330 | 16727.9 | 1505.52 | 0.0469114
0.41 | 11222.7 | 10241.8 | 4199.14 | 0.156411
0.82 | 20403.9 | 18620.6 | 15268.9 | 0.447656
0.97 | 37936.2 | 34620.6 | 33581.9 | 0.38918
Table 6: Table showing EAD, LGD, EL and derivative price for some
(illustrative) customers

##  4 Conclusion

The main objective of the paper was the calculation of probability of default
for the debtors of a loan lending organization and the consequent design of a
credit derivative (i.e., to calculate the premium) thereby hedging against
such defaults. Some conclusions based on the results obtained are as follows:

  1. (A)

Based on the performance metrics, the Decision Tree model performs better than
the Logistic Regression model.

  2. (B)

Further, when the dependent and independent variables have a high non-
linearity and complex relationship, linear models like Logistic Regression
will not perform well, since the entire algorithm is based on finding a linear
relationship (line of best fit in Logistic Regression) between the variables.
In such cases, a tree model (non-linear models) will outperform a classical
regression method. Hence, there exists a non-linear relationship between the
various attributes and the calculated probability.

  3. (C)

As expected, the calculated derivative premium is higher for loans linked with
obligors with high probability of default and short term loans.

## References

  * [1] Wikipedia, Lending Club.
URL https://en.wikipedia.org/wiki/LendingClub

  * [2] C. Bluhm, L. Overbeck, C. Wagner, Introduction to Credit Risk Modeling, Chapman and Hall/CRC, 2016.
  * [3] H. Kim, H. Cho, D. Ryu, Corporate default predictions using machine learning: Literature review, Sustainability 12 (16) (2020) 6325.
  * [4] C.-Y. J. Peng, K. L. Lee, G. M. Ingersoll, An introduction to logistic regression analysis and reporting, The Journal of Educational Research 96 (1) (2002) 3–14.
  * [5] S. Sperandei, Understanding logistic regression analysis, Biochemia medica 24 (1) (2014) 12–18.
  * [6] J. Ali, R. Khan, N. Ahmad, I. Maqsood, Random forests and decision trees, International Journal of Computer Science Issues 9 (5) (2012) 272.
  * [7] H. H. Patel, P. Prajapati, Study and analysis of decision tree based classification algorithms, International Journal of Computer Sciences and Engineering 6 (10) (2018) 74–78.
  * [8] H. Sharma, S. Kumar, A survey on decision tree algorithms of classification in data mining, International Journal of Science and Research 5 (4) (2016) 2094–2097.
  * [9] D. Duffie, K. J. Singleton, Modeling term structures of defaultable bonds, The Review of Financial Studies 12 (4) (1999) 687–720.
  * [10] W. Schoutens, J. Cariboni, Levy Processes in Credit Risk, John Wiley & Sons, 2010\.
  * [11] S. E. Shreve, Stochastic Calculus for Finance II: Continuous-time Models, Springer, 2004.
  * [12] J. Garcia, S. Goossens, W. Schoutens, Let’s jump together-pricing of credit derivatives: From index swaptions to cppis, Available at SSRN 1358704 (2007).
  * [13] T. R. Bielecki, M. Rutkowski, Credit Risk: Modeling, Valuation and Hedging, Springer Science & Business Media, 2013.
  * [14] P. J. Schonbucher, Credit Derivatives Pricing Models: Models, Pricing and Implementation, John Wiley & Sons, 2003.
  * [15] Kaggle, All Lending Club Loan Data.
URL https://www.kaggle.com/datasets/wordsforthewise/lending-club

  * [16] P. Sonego, A. Kocsor, S. Pongor, ROC analysis: Applications to the classification of biological sequences and 3D structures, Briefings in Bioinformatics 9 (3) (2008) 198–209.