# Liquid democracy and spectral theory

In this post we will show some practical examples of how liquid democracy can be understood in terms of, and make use of results from spectral graph theory. For more background please see [Vigna2009]. Wikipedia says:

In mathematics, spectral graph theory is the study of properties of a graph in relationship to the characteristic polynomial, eigenvalues, and eigenvectors of matrices associated to the graph, such as its adjacency matrix or Laplacian matrix.

What does this have to do with liquid democracy? To answer this, let’s remember what defines liquid democracy is: system of *transitive proxy voting*. **Proxy** in that I can delegate my vote to some entity. **Transitive** because that entity can itself delegate the vote, and so on. Imagine a simple case with three voters, Alice, Bob, and Charlie. Alice delegates to Bob, and Bob delegates to Charlie. It is very natural to represent this as a graph, this is what we call the **delegation graph**

Assuming each voter starts with one vote, this would give us the following weights for each voter:

Alice = 1, Bob = 2, Charlie = 3

Because Alice and Bob have delegated their votes, Charlie will end up casting three votes, one for himself, one for Bob and one for Alice. What determines these weights is the structure of the graph; who is voting for who is encoded in the connections between vertices. In graph theory, the object that encodes this information is the adjacency matrix. Here’s the adjacency matrix for our example:

Where each row shows who each voter delegated to. Alice (A) delegated to Bob (B), hence the *1* in the second position of the first row. Similarly, Bob (B) delegated to Charlie, © as can be seen in the second row. Because Charlie did not delegate, the third row is all zeroes.

We can express the liquid tally above with these equations (1’s represent voter’s initial weights)

0*A + 0*B + 0*C + 1 = A

1*A + 0*B + 0*C + 1 = B

0*A + 1*B + 0*C + 1 = C

Note how the 0’s and 1’s above correspond to the columns of the adjacency matrix. The above can be represented[1] in matrix form:

(A B C) * AdjacencyMatrix = (A B C)

This is an *eigenvalue equation*, whose eigenvector (the (A B C) row vector) corresponds to the result of the liquid tally. Note how the equation is recursive, which fits the recursive nature of transitive delegation. Vote weights are calculated in terms of vote weights themselves, the same way each delegate transmits votes that result from previous delegations.

When the adjacency matrix is used to evaluate the importance or influence of nodes in a graph in this way we are speaking of eigenvector centrality. Our example shows that calculating centrality is basically equivalent to tallying in liquid democracy. This is what makes the connection between spectral theory and liquid democracy.

## EIGENVECTOR CENTRALITY, KATZ AND PAGERANK

Yes, that’s PageRank as in google, in case you thought this whole talk of centrality was a bit abstract, it’s what made google what it is today. Eigenvector centrality, Katz centrality, and PageRank are related methods to measure the importance of a node in a network. We won’t go into the differences between each measure, besides noting that both Katz and PageRank include an attenuation factor that decreases contributions from distant nodes, whereas eigenvector centrality does not.

In order to run some examples we will use the networkX library, which includes several functions for calculating centrality as well as many other useful features. If you want to play along with this code you will need to install the library as well as its dependencies, including matplotlib and numpy. If you’re on windows I suggest downloading the comprehensive WinPython package which includes everything you need here.

Let’s first create the graph corresponding to our example with Alice, Bob and Charlie. Here’s the code that does that

`# construct graph`

G = nx.DiGraph()

G.add_edges_from([("Alice", "Bob"), ("Bob", "Charlie")])

# draw it

nx.draw_spring(G)

plt.savefig("graph.png")

This generated the image shown earlier. Now to calculate the tally, we will run both Katz and PageRank.

`# katz`

centrality = nx.katz_centrality(G.reverse(), alpha=1.0, beta=1.0, normalized=False)

print("katz %s" % centrality)

# pagerank

centrality = nx.pagerank(G, alpha=1.0)

print("pagerank %s" % dict(zip(centrality.keys(), denormalize(centrality.values()))))

which gives us

`katz {‘Charlie’: 3.0, ‘Bob’: 2.0, ‘Alice’: 1.0}`

pagerank {'Charlie': 3.0, 'Bob': 2.0, 'Alice': 1.0}

Both results match the tally we showed before. A couple of minor points above. First, the PageRank result was rescaled to make it match Katz. Second, the adjacency matrix for Katz was reversed as the networkx 1.8.1 Katz implementation is using a right eigenvector (this has been changed to left eigenvector in master).

More importantly, the alpha parameter is a damping factor. In the language of liquid democracy it modulates just how transitive delegation is by reducing contributions the further away the originate. For example, let’s change the above to alpha = 0.5:

`katz {‘Charlie’: 1.75, ‘Bob’: 1.5, ‘Alice’: 1.0}`

pagerank {'Charlie': 1.75, 'Bob': 1.5, 'Alice': 1.0}

Now Charlie receives 25% of Alice’s vote and 50% of Bob’s vote. So alpha quantifies the fraction of the vote that is effectively delegated. We can interpret then that a liquid democracy tally is a *special case* of Katz centrality and PageRank. In fact, liquid democracy is the limiting case of Katz and PageRank when alpha = 1.0, ie no damping (which is why you get viscous democracy in [Boldi2011]).

## WHAT ABOUT CYCLES?

One of the first things you have to deal with if you’ve implemented a liquid tallying algorithm is the possibility of cycles in the delegation graph, otherwise the procedure will blow up. Having detected a cycle at tally time the standard treatment is to consider votes that enter it as lost. In order to prevent that undesirable situation you can do cycle detection at vote time to warn the user that his/her vote may produce such a cycle.

What happens if we add a cycle to our example? Let’s try it

`G = nx.DiGraph()`

G.add_edges_from([("Alice", "Bob"), ("Bob", "Charlie"), ("Charlie", "Donna"), ("Donna", "Bob")])

and we get

`File “katz.py”, line 70, in`

centrality = nx.pagerank(G, alpha=1.0, max_iter=1000)

File "C:\tools\WinPython-64bit-2.7.6.4\python-2.7.6.amd64\lib\site-packages\networkx\algorithms\link_analysis\pagerank_alg.py", line 143, in pagerank

'in %d iterations.'%(i-1))

networkx.exception.NetworkXError: pagerank: power iteration failed to converge in 1000 iterations.

The reason this happens has to do with the details of the algorithm that calculates eigenvectors; in particular the relationship between its convergence and the attenuation factor alpha[2]. The short story is this: using an attenuation factor of 1.0 on a graph with cycles may cause problems.

Just as liquid tally algorithms have to deal with cycles, so do we in order to make centrality work correctly. Fortunately there are fast algorithms to detect cycles in graphs. NetworkX offers an implementaton of an improved version of Tarjan’s strongly connected components algorithm, we will use it to define a function that removes cycles in a graph

`def de_cycle(G):`

def r(source, target):

print("remove %s -> %s" % (source, target))

G.remove_edge(source, target)

return target

cycles = list(nx.simple_cycles(G))

if(len(cycles) > 0):

for cycle in cycles:

print ("cycle %s" % cycle)

# quick and dirty side-effecting use of reduce, see proper 2-batch iteration: http://stackoverflow.com/questions/5764782/iterate-through-pairs-of-items-in-python-list

reduce(r, cycle)

return G

Using this function we can obtain liquid tallies for any delegation graph correctly, using either Katz or PageRank. See the bottom of this post for the full python script demonstrating this.

## LIQUID DEMOCRACY AND DEGREE (OR BRANCHING FACTOR)

Before we said that liquid democracy is the limiting case of Katz centrality and PageRank when alpha = 1.0. In the last section we saw another requirement besides that of alpha = 1.0: that the delegation graph must be acyclic, in other words a DAG. There is one more property that we can consider, **degree**.

A node’s degree is the number of (in our case, outward) connections with other nodes. In terms of delegation, it is the number of delegates that a voter chooses. Standard liquid democracy uses degree = 1, but such a requirement could in theory be relaxed. How does this fit in with Katz and PageRank? Lets construct a graph where voters may choose one or two delegates.

`G = nx.DiGraph()`

G.add_edges_from([("Alice", "Bob"), ("Bob", "Charlie")])

G.add_edges_from([("Bob", "Florence"), ("Charlie", "Donna"), ("Florence", "Edward")])

which gives

resulting values

`pagerank {‘Charlie’: 2.0, ‘Alice’: 1.0, ‘Edward’: 3.0, ‘Donna’: 3.0, ‘Bob’: 2.0, ‘Florence’: 2.0}`

katz {'Charlie': 3.0, 'Bob': 2.0, 'Edward': 4.0, 'Florence': 3.0, 'Alice': 1.0, 'Donna': 4.0}

We see how Katz centrality does not yield a correct tally as it is not dividing outgoing weights for voters who split their delegation among two delegates, instead we get inflated weights. But the PageRank result does work, Bob’s two votes are split correctly, and the delegation proceeds normally from then on.

## IN SUMMARY

- Liquid democracy is a special case of Katz centrality given
- a damping factor alpha = 1.0
- a directed acyclic graph of degree d = 1
- Liquid democracy is a special case of PageRank given
- a damping factor alpha = 1.0
- a directed acyclic graph of degree d >= 1

That’s it for our quick tour of the relationship between liquid democracy and spectral theory. We have also seen how liquid democracy could be extended to include damping (as in [Boldi2011]), or to allow “multi delegation”.

Notes/References

[Vigna2009] Sebastiano Vigna — Spectral Ranking http://arxiv.org/abs/0912.0238

[Page1998] The PageRank Citation Ranking: Bringing Order to the Web http://ilpubs.stanford.edu:8090/422/1/1999-66.pdf

[Boldi2011] Viscous Democracy for Social Networks http://chato.cl/papers/boldi_bonchi_castillo_vigna_2011_viscous_democracy_social_networks.pdf

[1] For simplicity, I have ignored the initial weights associated with each voter in the matrix equation. These initial weights are what makes liquid tallying equivalent to undamped Katz centrality rather than eigenvector centrality.

[2] For details as to alpha and PageRank See http://vigna.di.unimi.it/ftp/papers/PageRankFunctional.pdf section 5 Limit behaviour.

In the case of the networkX implementation of Katz centrality an alpha of 1.0 is guaranteed to converge as all eigenvalues of an acyclic graph are 0 (see http://www.emis.de/journals/JIS/VOL7/Sloane/sloane15.pdf and http://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.centrality.katz_centrality.html#networkx.algorithms.centrality.katz_centrality)

## PYTHON SCRIPT: RANDOM LIQUID TALLYING WITH KATZ/PAGERANK

`import networkx as nx`

import matplotlib.pyplot as plt

from random import randrange

def simple_graph():

G = nx.DiGraph()

G.add_edges_from([("Alice", "Bob"), ("Bob", "Charlie")])

# with cycle

G.add_edges_from([("Alice", "Bob"), ("Bob", "Charlie"), ("Charlie", "Donna"), ("Donna", "Bob")])

return G

def random_graph(n):

G = nx.DiGraph()

G.add_nodes_from(range(0,n - 1))

for node in G.nodes():

target = randrange(n - 2)

if(target == node):

target = n - 1

G.add_edge(node, target)

return G

# denormalizes page rank scores

def denormalize(array):

m = min(array)

factor = 1 / m

return map(lambda x: round(x*factor, 5), array)

def de_cycle(G):

def r(source, target):

print("remove %s -> %s" % (source, target))

G.remove_edge(source, target)

return target

cycles = list(nx.simple_cycles(G))

if(len(cycles) > 0):

for cycle in cycles:

print ("cycle %s" % cycle)

# proper 2-batch iteration: http://stackoverflow.com/questions/5764782/iterate-through-pairs-of-items-in-python-list

reduce(r, cycle)

return G

import sys

if(len(sys.argv) != 2):

nodes = 10

else:

nodes = int(sys.argv[1])

# G = simple_graph()

G = random_graph(nodes)

nx.draw_spring(G)

plt.savefig("graph.png")

de_cycle(G)

centrality = nx.pagerank(G, alpha=1.0, max_iter=1000)

print("pagerank %s" % dict(zip(centrality.keys(), denormalize(centrality.values()))))

cycles = list(nx.simple_cycles(G))

print("cycles %s" % cycles)

centrality = nx.katz_centrality(G.reverse(), alpha=1.0, beta=1.0, normalized=False)

print("katz %s" % centrality)

# draw graph without cycles

plt.clf()

nx.draw_spring(G)

plt.savefig("graph_decycled.png")