Stata Graph Library For Network Analysis
Stata Graph Library For Network Analysis
Hirotaka Miura
Federal Reserve Bank of San Francisco
The views presented here are my own and do not necessarily represent the views of the Federal Reserve System or any person aliated with it.
Outline
Modeling relational data Matrix representations Centrality measures Clustering coecient Stata implementation Conclusion
3 / 41
Graph model
A graph model representing a network G = (V , E ) consists of a set of vertices V and a set of edges E .
|V | equals the number of vertices. |E | equals the number of edges.
An edge is dened as a link between two vertices i and j , not necessarily distinct, that has vertex i on one end and vertex j on the other.
An edge may be directed or undirected and may also be weighted with diering edge values or have all equal edge values of one in which case the network is said to be unweighted.
4 / 41
Special cases
Special types of vertices and edges exist for which standard graph algorithms are not designed to handle or there simply does not exist routines to accommodate such types. Thus the following types of vertices and edges are currently excluded from analysis:
Isolated vertex - a vertex that is not attached to any edges. Parallel edges - two or more edges that connect the same pair of vertices. Self-loop - an edge connecting vertex i to itself. Zero or negative weighted edge.
5 / 41
Edge list
Most suited for storing relational data in Stata, as it allows the use of options such as if exp and in range.
Plus others such as the Compressed Sparse Row format for ecient storage and access.
6 / 41
Adjacency matrix 0 1 0 0 1 0 1 1 0 1 0 0 0 1 0 0
Adjacency list
Vertex 1 2 3 4 Neighbor(s) 2 1 3 4 2 2
Edge list 1 2 2 3 4 2
7 / 41
3 3 12 12 4 4 11 11 2 2
13 13
14 14
Matrix representations
10 / 41
Adjacency matrix
Adjacency matrix A for unweighted networks is dened as a |V | |V | matrix with Aij entries being equal to one if an edge connects vertices i and j and zero otherwise. Aii entries are set to zero. Matrix A is symmetric if the network is undirected. For directed networks, rows of matrix A represent outgoing edges and columns represent incoming edges.
The convention of denoting Xij entries as an edge from i to j is adopted for all matrices.
For weighted networks, Aij entries are equal to the weight of the edge connecting vertices i and j .
11 / 41
Distance matrix
Distance matrix D is dened as a |V | |V | matrix with Dij entries being equal to the length of the shortest path between vertices i and j.
A path is dened as a way of reaching vertex j starting from vertex i using a combination of edges that do not go through a particular vertex more than once.
12 / 41
Path matrix
Path matrix P is dened as a |V | |V | matrix with Pij entries being equal to the number of shortest paths between vertices i and j . If no paths exist between vertices i and j , Pij is set to zero. Pii is set to one. P matrix is symmetric for undirected networks.
13 / 41
Example matrices
4 2 1
Adjacency matrix 0 1 0 0 1 0 1 1 0 1 0 0 0 1 0 0
Distance matrix 0 1 2 2 1 0 1 1 2 1 0 2 2 1 2 0
Path matrix 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
14 / 41
Centrality measures
15 / 41
Degree centrality measures the importance of a vertex by the number of connection the vertex has if the network is unweighted, and by the aggregate of the weights of edges connected to the vertex if the network is weighted (Freeman, 1978). For an undirected network, degree centrality for vertex i is dened as 1 |V | 1 Aij
j (=i )
(1)
where the leading divisor is adjusted for the exclusion of the j = i term.
16 / 41
Directed networks may entail vertices having dierent number of incoming and outgoing edges, and thus we have out-degree and in-degree centrality. Out-degree centrality for vertex i is dened similarly to equation (1). For in-degree, we simply transpose the adjacency matrix: 1 |V | 1 Aij .
j (=i )
(2)
17 / 41
Example
Undirected unweighted network
G (0.33) A (0.33)
Centrality comparison
Centrality A B F G Degree Closeness Betweenness Eigenvector Katz-Bonacich 0.33 Vertex
E (0.50)
D (0.33)
C (0.50)
C E 0.50
D 0.33
F (0.33)
B (0.33)
Degree centrality in parentheses. Figure from Jackson (2008). Drawn using NETPLOT (Corten, 2011).
18 / 41
Closeness centrality
Closeness centrality provides higher centrality scores to vertices that are situated closer to members of their component, or the set of reachable vertices, by taking the inverse of the average shortest paths as a measure of proximity (Freeman, 1978). That is, closeness centrality for vertex i is dened as (|V | 1) , j (=i ) Dij (3)
which reects how vertices with smaller average shortest path lengths receive higher centrality scores than those that are situated farther away from members of their component.
19 / 41
Example
Undirected unweighted network
G (0.40) A (0.40)
Centrality comparison
Centrality A B F G Degree Closeness Betweenness Eigenvector Katz-Bonacich 0.33 0.40 Vertex
E (0.55)
D (0.60)
C (0.55)
C E 0.50 0.55
D 0.33 0.60
F (0.40)
B (0.40)
Closeness centrality in parentheses. Figure from Jackson (2008). Drawn using NETPLOT (Corten, 2011). 20 / 41
Betweenness centrality
Betweenness centrality bestows larger centrality scores on vertices that lie on a higher proportion of shortest paths linking vertices other than itself. Let Pij denote the number of shortest paths from vertex i to j . Let Pij (k ) denote the number of shortest paths from vertex i to j that vertex k lies on. Then following Anthonisse (1971) and Freeman (1977), betweenness centrality measure for vertex k is dened as Pij (k ) . Pij (4)
ij :i =j ,k ij
To normalize (4), divide by (|V | 1)(|V | 2), the maximum number of paths a given vertex could lie on between pairs of other vertices.
21 / 41
Example
Undirected unweighted network
G (0.00) A (0.00)
Centrality comparison
Centrality A B F G Degree Closeness Betweenness Eigenvector Katz-Bonacich 0.33 0.40 0.00 Vertex
E (0.53)
D (0.60)
C (0.53)
F (0.00)
B (0.00)
Normalized betweenness centrality in parentheses. Figure from Jackson (2008). Drawn using NETPLOT (Corten, 2011). 22 / 41
Aij xj
(5)
which can be rewritten as x = Ax. The convention is to use the eigenvector corresponding to the dominant eigenvalue of A.
23 / 41
(6)
The Katz-Bonacich centrality, a variation of the eigenvector centrality, seeks to address these issues.
24 / 41
Example
Undirected unweighted network
G (0.33) A (0.33)
Centrality comparison
Centrality A B F G Degree Closeness Betweenness Eigenvector Katz-Bonacich 0.33 0.40 0.00 0.33 Vertex
E (0.45)
D (0.38)
C (0.45)
F (0.33)
B (0.33)
Eigenvector centrality in parentheses. Figure from Jackson (2008). Drawn using NETPLOT (Corten, 2011). 25 / 41
Katz-Bonacich centrality
The additional inclusion of a free parameter (also referred to as a decay factor) and a vector of exogenous factors into equation (7):
Avoids the exclusion of vertices with zero incoming edges. Allows connection values to decay over distance.
Attributed to Katz (1953), Bonacich (1987), and Bonacich and Lloyd (2001). Centrality measure is dened as a solution to the equation x = A x + (8) where is the free parameter and is the vector of exogenous factors which can vary or be constant across vertices. For the centrality measure to converge properly, absolute value of must be less than the absolute value of the inverse of the dominant eigenvalue of A.
A positive allows vertices with important neighbors to have higher status while a negative value reduces the status.
26 / 41
Example
Undirected unweighted network
G (3.99) A (3.99)
Centrality comparison
Centrality A B F G Vertex
E (5.06)
D (4.34)
C (5.06)
F (3.99)
* Maximum = 0.43 (0.33 used). Exogenous factors set to one for all vertices. Katz-Bonacich centrality in parentheses. Figure from Jackson (2008). Drawn using NETPLOT (Corten, 2011).
27 / 41
Clustering coecient
28 / 41
Clustering coecient
Clustering coecient is one way of gauging how tightly connected a network is. The general idea is to consider transitive relations:
If vertex j is connected to vertex i , and i is connected to k , then j is also connected to k .
Global clustering coecients provide indication on the degree of concentration of the entire network and consists of overall and average clustering coecients.
Overall clustering coecient is equal to all observed transitive relations divided by all possible transitive relations in the network. Average clustering coecient involves applying the denition of overall clustering coecient at the vertex level, then averaging across all the vertices.
29 / 41
Aji Aik
i ;j =i ;k =j ;k =i
(9)
where the numerator represents the sum over i of all closed triplets in which transitivity holds, and the denominator represents the sum over i of all possible triplets.
30 / 41
Aji Aik
j =i ;k =j ;k =i
(10)
(11)
j =i ;k =j ;k =i
(12)
where equals (Wji + Wik )/2 for arithmetic mean, Wji Wik for geometric mean, max(Wji , Wik ) for maximum, and min(Wji , Wik ) for minimum. For unweighted networks, W = A and the four types of clustering coecients are all equal.
32 / 41
Example
Undirected unweighted network
G (1.00) A (1.00)
E (0.33)
D (0.00)
C (0.33)
F (1.00)
B (1.00)
Local clustering coecients in parentheses. Figure from Jackson (2008). Drawn using NETPLOT (Corten, 2011).
33 / 41
Stata implementation
34 / 41
We demonstrate the use of network and netsummarize commands on a dataset of 15th-century Florentine marriages from Padgett and Ansell (1993) to compute betweenness and eigenvector centrality measures.
network generates vectors of betweenness and eigenvector centralities. netsummarize merges vectors to Stata dataset.
(15th century Florentine marriages 15th-century Florentine > (Padgett and Ansell 1993)) marriages Edge list stored as Stata dataset . list v1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. Peruzzi Peruzzi Peruzzi Castellan Castellan Strozzi Strozzi Bischeri Barbadori Ridolfi Ridolfi Medici Medici Medici Medici Tornabuon Guadagni Guadagni Albizzi Salviati v2 Castellan Strozzi Bischeri Strozzi Barbadori Ridolfi Bischeri Guadagni Medici Medici Tornabuon Tornabuon Albizzi Salviati Acciaiuol Guadagni Lambertes Albizzi Ginori Pazzi
35 / 41
variable label
v1 str9 v2 str9 betweenness_source float betweenness_target float eigenvector_source float eigenvector_target float Sorted by: Note:
38 / 41
Network visualization
Betweenness centrality
Ginori (0.00)
Eigenvector centrality
Ginori (0.07)
Tornabuon (0.09) Medici (0.52) Bischeri (0.10) Ridolfi (0.11) Salviati (0.14) Pazzi (0.00) Bischeri (0.28)
Tornabuon (0.33) Medici (0.43) Ridolfi (0.34) Salviati (0.15) Pazzi (0.04)
Conclusion
Dierent types of matrices, centrality measures, and clustering coecients can be generated from information retrieved from relational data. The command network provides access to SGL functions that generate network measures based on edge list stored in Stata. The postcomputation command netsummarize allows the user to generate standard and customized network measures which are merged into Stata dataset. Future developments include:
More ecient algorithms. Designing functions for additional network measures. Optimizing SGL in Mata.
40 / 41
References I
Anthonisse, J. M. (1971): The rush in a directed graph, Discussion paper, Stichting Mathematisch Centrum, Amsterdam, Technical Report BN 9/71. Barrat, A., M. Barth elemy, R. Pastor-Satorras, A. Vespignani (2004): The architecture of complex weighted networks, Proceedings of the National Academy of Sciences, 101(11), 37473752. Bonacich, P. (1972): Factoring and weighting approaches to status scores and clique identication, Journal of Mathematical Sociology 2, pp. 113120. (1987): Power and Centrality: A Family of Measures, The American Journal of Sociology, 92(5), 11701182. Bonacich, P., P. Lloyd (2001): Eigenvector-like measures of centrality for asymmetric relations, Social Networks, 23(3), 191201. Corten, R. (2011): Visualization of social networks in Stata using multidimensional scaling, Stata Journal, 11(1), 5263. Freeman, L. C. (1977): A set of measures of centrality based on betweenness, Sociometry, 40(1), 3541. (1978): Centrality in social networks: Conceptual clarication, Social Networks, (1), 215239. Jackson, M. O. (2008): Social and Economic Networks. Princeton University Press. Katz, L. (1953): A New Status Index Derived from Sociometric Analysis, Psychometrika, 18, 3943. Opsahl, T., Padgett, J. F., C. K. Ansell (1993): Robust action and the rise of the Medici, 1400-1434, The American Journal of Sociology, 98(6), 12591319.
and
and
and P. Panzarasa (2009): Clustering in weighted networks, Social Networks, 31(2), 155163. and
41 / 41