Modularity maximization is a technique for uncovering assortative communities in a network. This type of community is internally dense and externally sparse — that is, nodes assigned to the same community tend to be strongly connected to each other, while nodes in different communities tend to be weakly connected.

We typically use the generalized Louvain (GenLouvain for short) function for detecting communities. You can download that code here. The most important part of the GenLouvain software package is the “genlouvain.m” function. You give this function a “modularity matrix”, which is literally your observed matrix of connections and their weights minus a matrix whose elements represent the *expected* weights of those same connections under a null model. The choice of null model is not trivial and needs to be handled with care — some null models are only appropriate for certain types of networks. For example, if your network is a correlation matrix of regional BOLD time series (otherwise known as functional connectivity), you need special kinds of null models. Here I’ll show you two cases:

Degree-preserving null model (appropriate for sparse networks where connections are, at least in principle, independent of one another)

Uniform or “Potts” model (appropriate for correlation matrices).

For a sparse, positive-only matrix, we can estimate its modules using the below code:

You can do something similar if your matrix is fully-weighted/signed, like functional connectivity. To estimate communities, use code that looks something like this:

Note that in both cases we never changed the value of gamma, the resolution parameter. In general, making gamma smaller will result in fewer (but larger) communities, whereas making gamma larger will result in many (but smaller) communities. All code can be downloaded here.