Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Modularity: Definition and Derivation

Graph-Based Clustering · Tutorial 243 of 700

Modularity: Definition and Derivation

Learn how modularity compares a graph partition’s within-community edges with a degree-preserving baseline, and how to calculate and interpret the resulting score.

Intermediate 12 min read

What You'll Learn

  • Define modularity for an undirected graph using its adjacency matrix, degrees, and a proposed partition.
  • Derive the community-level formula from observed and baseline within-community edge weight.
  • Calculate modularity for toy unweighted and weighted graphs.
  • Distinguish the standard configuration-model baseline from the exact finite stub-pairing expectation.
  • Interpret zero, positive, and negative scores without treating modularity as a significance test.
  • Recognize when modularity can mislead and how it differs from a raw edge-count objective.

Why Compare Within-Community Edges With a Baseline?

A partition can place many graph edges within its proposed communities simply because some vertices have many edges. To assess whether a partition groups vertices more strongly than their degrees alone would suggest, modularity compares observed within-community edge weight with a degree-based reference. In a cell-cell neighborhood graph, this provides a numerical summary of how well a proposed grouping aligns with the graph’s connectivity. It does not, by itself, establish that the groups are biologically distinct.

The key is the comparison, not the raw number of internal edges. A high-degree cell is expected to connect to many other cells even under a null model that does not favor any particular community assignment. Modularity discounts the within-community connectivity that such a degree-based baseline would predict.

Definition for an Undirected Graph

Consider an undirected, loop-free graph with \(n\) vertices. Let \(A_{ij}\) be 1 if vertices \(i\) and \(j\) are connected and 0 otherwise. The adjacency matrix is symmetric, \(A_{ij}=A_{ji}\), and its diagonal entries are zero. The degree of vertex \(i\) is \(k_i=\sum_j A_{ij}\), and the number of edges is \(m=\frac{1}{2}\sum_i k_i\). A partition assigns every vertex to exactly one community. Write \(c_i\) for the community of vertex \(i\), and let \(\delta(c_i,c_j)\) equal 1 when \(c_i=c_j\) and 0 otherwise.

Definition: The standard modularity of a partition of an undirected, loop-free graph is
$$ Q=\frac{1}{2m}\sum_{i=1}^{n}\sum_{j=1}^{n} \left(A_{ij}-\frac{k_i k_j}{2m}\right)\delta(c_i,c_j). $$
The term \(A_{ij}\) is the observed adjacency, while \(k_i k_j/(2m)\) is the standard degree-based expected-weight baseline used in classic modularity. The sum is over ordered pairs, so each undirected edge is counted twice.

The baseline is motivated by a degree-preserving configuration model: vertices are represented by degree-many stubs, and stubs are paired to form edges. The standard modularity baseline \(k_i k_j/(2m)\) is a factorized reference that preserves expected degrees. It is not the exact finite expected adjacency for distinct vertices under uniform pairing of a fixed number of stubs. For a finite stub-pairing model, that distinction matters; it is addressed below.

For a graph with nonnegative edge weights, replace \(A_{ij}\) by the weight \(w_{ij}\), degree \(k_i\) by strength \(s_i=\sum_j w_{ij}\), and edge count \(m\) by total edge weight \(W=\frac{1}{2}\sum_i s_i\). This gives the standard weighted modularity. In either case, the score is a comparison to a specified baseline, not a probability or a test statistic with an automatic significance threshold.

Deriving the Community-Level Formula

The defining sum can be reorganized by community. Let \(L_c\) be the number of edges whose two endpoints are in community \(c\), and let \(K_c=\sum_{i:c_i=c} k_i\) be the sum of the degrees of vertices in that community. Because the adjacency sum is over ordered pairs, each internal edge contributes twice to \(\sum_{i,j\in c} A_{ij}\). Therefore, its observed contribution after division by \(2m\) is \(2L_c/(2m)=L_c/m\).

For the baseline part, the sum over ordered pairs within \(c\) factorizes: \(\sum_{i,j\in c} k_i k_j/(2m)=K_c^2/(2m)\). Dividing that quantity by \(2m\) gives \(K_c^2/(2m)^2\). Adding the observed and baseline terms over all communities yields the equivalent formula:

$$ Q=\sum_c\left[\frac{L_c}{m}-\left(\frac{K_c}{2m}\right)^2\right]. $$

This form makes the interpretation direct. The first term is the fraction of all edges that are internal to community \(c\). The second is the standard baseline’s predicted fraction of edge weight internal to that community, based on its share \(K_c/(2m)\) of all degree. Modularity adds the differences across communities.

Justification: Since \(2m=\sum_i k_i\), the degree share of community \(c\) is \(K_c/(2m)\). Under the standard factorized baseline, the expected fraction of edge weight whose two ends fall in \(c\) is the product of that share with itself, \((K_c/(2m))^2\). Thus each community contributes its observed internal-edge fraction minus this baseline fraction. This derivation uses the standard modularity baseline, not an assertion that it is the exact finite stub-pairing expectation.

Worked Example: Two Triangles Connected by a Bridge

Suppose a hypothetical six-vertex graph has edges forming a triangle on vertices 1, 2, and 3, a triangle on vertices 4, 5, and 6, and one additional edge joining vertices 3 and 4. Consider the partition into those two triangles. Each triangle has three internal edges, and the graph has \(m=7\) edges total.

The degrees are \(k_1=k_2=k_5=k_6=2\), and \(k_3=k_4=3\). Thus each community has degree sum \(K_c=7\), while \(2m=14\). Substituting in the community-level formula gives

$$ Q=\left[\frac{3}{7}-\left(\frac{7}{14}\right)^2\right] +\left[\frac{3}{7}-\left(\frac{7}{14}\right)^2\right] =\frac{6}{7}-\frac{1}{2} =\frac{5}{14} \approx 0.3571. $$

The partition has positive modularity: its internal-edge fraction, \(6/7\), exceeds the standard degree-based baseline contribution, \(1/2\), by about 0.3571. This describes the partition relative to that baseline; it is not a p-value and does not show that these two groups are true biological cell types.

Finite Stub Pairing and the Standard Baseline

A careful distinction is needed between the usual modularity formula and the exact expectation under uniform finite stub pairing. In the latter model, there are \(2m\) stubs, and each possible pairing of stubs is equally likely. For two distinct vertices \(i\) and \(j\), each of the \(k_i\) stubs at \(i\) can pair with any one of the \(k_j\) stubs at \(j\). A particular pair of stubs is matched with probability \(1/(2m-1)\), because after fixing one stub there are \(2m-1\) possible partners. By summing these probabilities, the exact expected number of edges between the distinct vertices is \(k_i k_j/(2m-1)\).

This is different from \(k_i k_j/(2m)\). The latter remains the conventional baseline in classic modularity and is often a useful large-graph approximation to the finite stub-pairing expectation. It should not be described as the exact distinct-vertex expectation for uniform finite stub pairing. The finite pairing model also permits loops and multiple edges, unlike a simple graph.

Worked Example: Comparing the Two Baselines

Suppose a hypothetical stub-pairing configuration model has \(m=4\) edges in total and two distinct vertices each of degree 2. There are \(2m=8\) stubs. For each of the \(2\times2=4\) pairs consisting of one stub from each vertex, the probability of being paired together is \(1/(8-1)=1/7\). Hence the exact finite expected number of edges between them is

$$ 2\times2\times\frac{1}{7}=\frac{4}{7}\approx 0.5714. $$

The standard modularity baseline for that pair is instead

$$ \frac{k_i k_j}{2m}=\frac{2\times2}{8}=0.5000. $$

The values differ because the first calculation is the exact expectation under finite uniform stub pairing, whereas the second is the standard factorized modularity reference. They become closer as the graph grows, but they are not identical in this finite example.

If the null model instead samples simple graphs with the given degree sequence, loops and multiple edges are prohibited. The expected adjacency probabilities for that model are generally not given exactly by either expression alone; they depend on the degree sequence and the graph constraint. Therefore, a modularity score should be reported and interpreted with its baseline convention in mind.

Weighted Graphs and Alternative Partitions

For a weighted graph, let \(W=\sum_{i<j}w_{ij}\) be total edge weight, \(s_i=\sum_j w_{ij}\) the strength of vertex \(i\), \(L_c\) the total weight of edges internal to \(c\), and \(S_c=\sum_{i:c_i=c}s_i\). The standard weighted formula follows the same logic: compare the observed fraction \(L_c/W\) with the baseline fraction \((S_c/(2W))^2\). The weights must be on a meaningful, nonnegative scale for this interpretation to apply.

Worked Example: Weighted Path Partition

Consider a hypothetical four-vertex path with edge weights \(w_{12}=2\), \(w_{23}=1\), and \(w_{34}=2\), and no other edges. Partition the vertices into \(\{1,2\}\) and \(\{3,4\}\). The total weight is \(W=5\). The strengths are \(s_1=2\), \(s_2=3\), \(s_3=3\), and \(s_4=2\), so each community has strength sum \(S_c=5\). The internal weight is 2 in the first community and 2 in the second. Thus

$$ Q=\left[\frac{2}{5}-\left(\frac{5}{10}\right)^2\right] +\left[\frac{2}{5}-\left(\frac{5}{10}\right)^2\right] =0.3000. $$

The two communities contain \(4/5\) of the graph’s total edge weight internally, compared with a combined standard baseline contribution of \(1/2\). Their difference is 0.3000. The edge of weight 1 between vertices 2 and 3 is the only between-community edge in this partition.

Worked Example: One Community Versus Singletons

Use the same weighted path. If all four vertices form one community, then \(L=W=5\) and \(S=2W=10\), so

$$ Q=\frac{5}{5}-\left(\frac{10}{10}\right)^2=0. $$

Now put each vertex in its own community. There are no internal edges, so the observed terms are all zero. The four strengths are 2, 3, 3, and 2, and the modularity is

$$ Q=-\left[\left(\frac{2}{10}\right)^2+ \left(\frac{3}{10}\right)^2+ \left(\frac{3}{10}\right)^2+ \left(\frac{2}{10}\right)^2\right] =-\frac{26}{100}=-0.2600. $$

The all-in-one partition has score zero because all edge weight is internal and its baseline prediction is also one. The singleton partition scores below zero because it has no internal edges even though the baseline assigns positive expected internal weight to each singleton. Neither calculation is a test of whether a partition is correct.

Interpreting, Diagnosing, and Comparing Modularity

For a fixed graph and the standard baseline, a larger \(Q\) indicates that a larger excess of edge weight lies within communities relative to the degree-based reference. A score near zero indicates little net excess under this comparison; a negative score means the partition has less within-community weight than the baseline predicts in aggregate. These descriptions are relative, not absolute quality labels. Scores should not be compared uncritically across graphs with different construction choices, edge weights, or null-model conventions.

Modularity also differs from simply maximizing the number or fraction of internal edges. A raw within-edge count ignores how many edges are expected from the degree totals. A cut-based criterion instead focuses directly on edges crossing community boundaries, sometimes relative to community size or volume. Such criteria answer related but different questions; modularity’s defining feature is subtracting the degree-based expected contribution. Neither a high modularity score nor a small cut alone establishes that the partition corresponds to a biological mechanism.

Useful checks include verifying that the adjacency matrix is symmetric, that the stated \(m\) or \(W\) agrees with the edge list, and that each vertex belongs to exactly one community. For weighted graphs, check that total weight and strengths use the same edge weights. A frequent arithmetic error is to count each undirected edge once in \(L_c\) but then treat the adjacency sum as if it counted it once; the matrix formula counts it twice, which is why the community formula uses \(L_c/m\), not \(L_c/(2m)\).

A further pitfall is to say, “The configuration model’s exact expected adjacency is \(k_i k_j/(2m)\), so modularity is the exact excess over that model.” For distinct vertices under finite uniform stub pairing, this is not correct: the exact expectation is \(k_i k_j/(2m-1)\). The correction is to describe \(k_i k_j/(2m)\) as the standard modularity baseline (or a large-graph approximation in this context) and to keep it distinct from exact finite stub-pairing expectations. For weighted graphs, likewise, use the standard strength-based modularity baseline rather than claiming that an unweighted stub-pairing derivation applies unchanged.

Finally, modularity is a summary of a given partition, not a complete diagnostic of the graph or a measure of uncertainty. Different partitions can have similar scores, and a score alone does not identify which vertices are unstable or explain why a grouping arose. It is most informative when the graph construction, partition, and baseline are all reported clearly.

Check Your Understanding

Answer each question, then compare your reasoning with the brief solutions below.

  1. In the standard unweighted modularity formula, why does an internal edge contribute twice to the adjacency sum?
  2. A community has \(L_c=4\), \(K_c=8\), and the graph has \(m=10\) edges. What is its contribution to \(Q\)?
  3. For \(m=5\) and two distinct vertices of degree 2, calculate the exact expected number of edges between them under finite uniform stub pairing and the standard modularity baseline.
  4. What does a negative modularity score mean for a specified partition?
  5. Why is modularity not a p-value for the claim that a community is biologically real?

Solutions. (1) The graph is undirected, so an edge between \(i\) and \(j\) appears as both \(A_{ij}\) and \(A_{ji}\). (2) Its contribution is \(L_c/m-(K_c/(2m))^2=4/10-(8/20)^2=0.2400\). (3) The exact finite expectation is \(2\times2/(2m-1)=4/9\approx0.4444\), while the standard baseline is \(2\times2/(2m)=4/10=0.4000\). (4) The partition has less within-community edge weight than the standard degree-based baseline predicts in aggregate. (5) Modularity is a score for comparing observed connectivity with a baseline; it does not calculate a tail probability under a specified inferential test or establish biological causation.

Clinical Biostats