Skip to content

Instantly share code, notes, and snippets.

@gweissman
Last active October 29, 2015 15:47
Show Gist options
  • Save gweissman/2402935 to your computer and use it in GitHub Desktop.
Save gweissman/2402935 to your computer and use it in GitHub Desktop.
Calculate Newman's assortativity coefficient for a mixing matrix
# calculate the assortativity coefficient for a mixing matrix of a graph
# ref: MEJ Newman, 'Mixing patterns in networks', Phys Rev E 67, 026126 (2003)
#
# define assortativity coefficient as
# trace (m) - sum (m^2)
# ac = -------------------------
# 1 - sum (m^2)
#
# where m is the mixing matrix of a graph
assortcoeff <- function(m) {
tr <- sum(diag(m))
sumsq <- sum (rowSums(m)*colSums(m))
(tr - sumsq) / (1 - sumsq)
}
@gweissman
Copy link
Author

This solution should generalize for both directed and undirected graphs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment