Repository navigation
ENH: Add Shen's overlapping modularity quality function - #8647
Conversation
Add `nx.community.overlapping_modularity` implementing Shen et al.'s
extended modularity EQ (2009) as a quality function for overlapping
community covers. The implementation uses the community-sum form
EQ = sum_i [ L_tilde_C / m - gamma * (k_tilde_C / 2m)^2 ]
where L_tilde_C is the overlap-discounted intra-community edge count
(each edge weighted by 1/(O_v * O_w) with O_v the number of
communities containing v) and k_tilde_C is the overlap-discounted
degree sum. This mirrors the community-sum form used by
`nx.community.modularity` with overlap-discounted quantities replacing
the raw counterparts. When the cover is just a partition
(O_v = 1 for all v), EQ reduces to Q exactly.
Also add `nx.community.is_cover`, the cover-equivalent of
`is_partition`: a cover requires every node in G to belong to at
least one community, but allows overlap. Used internally to validate
input to overlapping_modularity, exposed publicly for users (e.g. to
sanity-check the output of `k_clique_communities` before scoring).
The resolution parameter gamma is not in Shen's original definition;
it is added for consistency with `modularity(resolution=...)`. With
gamma=1 the formula is exactly Shen's EQ.
Tests cover equivalence with standard modularity on partition inputs,
hand-calculated EQ values for a triangle under several covers,
weighted hand-calculation, k_clique_communities integration, resolution
parameter monotonicity, and the usual edge cases (directed not
implemented, empty graph, weight=None, invalid cover).
|
This is the third PR in the bipartite/overlapping community detection roadmap from #8610:
AlgebraShen et al. (2009) write EQ in the all-pairs form (Eq. 2): This is O(|C|^2) per community, that is, slow for real data, and dominated by the (much sparser) actual edge structure. This implementation is refactored to the community-sum form, matching what where The derivation is in two steps:
Result: O(|edges in C| + |C|) per community, same approach as Things worth looking closely
Out of scope (deferred to follow-up PRs)
References
|
There was a problem hiding this comment.
Very nice! Good topic -- fills a hole we've been neglecting in our community measures. Overlapping communities makes the questions more complicated. But its often a better description of the community structure.
Well written and understandable relative to existing stuff in the networkx community suite. Thanks @jtorrents
rossbar
left a comment
There was a problem hiding this comment.
LGTM, thanks @jtorrents !
The only thing to get in before merging is the keyword-only arguments comment; everything else is just a nit or high-level question!
|
|
||
| @not_implemented_for("directed") | ||
| @nx._dispatchable(edge_attrs="weight") | ||
| def overlapping_modularity(G, communities, weight="weight", resolution=1): |
There was a problem hiding this comment.
Let's make these keyword-only!
| def overlapping_modularity(G, communities, weight="weight", resolution=1): | |
| def overlapping_modularity(G, communities, *, weight="weight", resolution=1): |
| set, but sets may overlap. Accepts the output of | ||
| :func:`~networkx.algorithms.community.kclique.k_clique_communities` | ||
| directly. |
There was a problem hiding this comment.
Not a blocker or anything, just satisfying my own curiosity... why is k_clique_communities called out specifically here?
There was a problem hiding this comment.
I called it out originally because k_clique_communities is currently the only overlapping community-detection algorithm in NetworkX, so it's the natural producer of input for overlapping_modularity. Future-proofing seemed wise though, so I've generalized the wording to "any NetworkX overlapping community-detection algorithm such as ...".
A bit of context on why future-proofing matters here: I have follow-up PRs planned to add more overlapping community detection algorithms. The most direct one is EAGLE (Shen et al. 2009), the companion algorithm in the same paper that introduced EQ. EAGLE is an agglomerative hierarchical clustering on the set of maximal cliques, and it uses EQ as the cut criterion for its dendrogram. SLPA (Xie et al. 2011) is also on the roadmap as a label-propagation-class overlapping community detection algorithm, complementing EAGLE's agglomerative-hierarchical approach. Both are described in the broader discussion at #8610.
| resolution : float, optional (default=1) | ||
| Resolution parameter $\gamma$. Values smaller than 1 favor larger | ||
| communities; values greater than 1 favor smaller communities. | ||
|
|
There was a problem hiding this comment.
It might be worth mentioning (if possible) what impact this has on the number of nodes in multiple communities. The meaning of resolution in modularity is described as a parameter controlling the tradeoff between inter/intra community edges. Is that the same here? Does the degree of overlap between communities vary with this parameter?
Not a blocker, just curious!
There was a problem hiding this comment.
Good question. I've expanded the docstring in the last commit to address this:
-
On community size: resolution behaves exactly like in standard modularity. It scales only the null-model term
gamma * (k_tilde_C / 2m)^2, so smaller gamma shrinks the expected-edges penalty and favors larger communities; larger gamma does the opposite. -
On degree of overlap: independent of resolution in this function: the cover is supplied by the caller, not optimized internally. The overlap-discount factor
1/(O_v O_w)sits outside gamma in the formula, so it isn't rescaled either.
Actually, thinking about this made me realize one interesting thing about the resolution parameter, though it is not relevant to the current API: if someone were optimizing EQ (no such optimizer exists in NetworkX yet), resolution would change which cover scores best. Because resolution rescales k_v k_w / 2m but the 1/(O_v O_w) discount is not scaled, higher resolution penalizes the null-model contribution of high-degree (hub) nodes the most, making it less attractive to place them in multiple communities. Conversely, lower resolution would let hubs spread across communities more cheaply. This is a property of resolution's interaction with the EQ formula, not a property of the current scoring function, so I've left it out of the docstring.
There was a problem hiding this comment.
Correction to my previous comment ;) I had the sign of the hub effect reversed. Higher resolution makes hub-spreading more attractive under optimization, not less.
Sketch: a node v's contribution to sum_C (k_tilde_C / 2m)^2, summed over the O_v communities containing it, is
sum_{C ∋ v} γ * (k_v / O_v)^2 / (2m)^2 = γ * k_v^2 / (O_v * (2m)^2)
which decreases in O_v. So increasing O_v saves null-model penalty, and resolution scales the size of those savings.
Quick check on a star (hub h, 4 leaves, m = 4):
- One community
{h, l1, l2, l3, l4}:EQ = 1 - γ - Two communities
{h, l1, l2}, {h, l3, l4},O_h = 2:EQ = 1/2 - γ/2
Difference (1 - γ)/2: at resolution < 1 the non-overlapping cover wins, at resolution > 1 the spread cover wins.
Intuition: higher resolution -> bigger absolute null-model penalty -> bigger savings from anything that shrinks k_tilde_C, and spreading high-degree nodes does exactly that. None of this affects the current API or docstring (the cover is input, not optimized). Just flagging the corrected direction for the record.
There was a problem hiding this comment.
I initially struggled understanding resolution's relationship to community size (in modularity itself) and could only convince myself after trying it out with examples. The fact that modularity goes negative relatively quickly with increasing resolution also surprised me :). There's clearly lots to learn about for me on the community detection front!
There was a problem hiding this comment.
The "goes negative quickly" thing tripped me up too the first time. The intuition that finally stuck for me:
For any community C, the edge-term contribution L_C/m is bounded above by 1 (when C is the whole graph), but the null-term γ * (k_C/2m)^2 is also bounded above by γ (same case). So the trivial single-community cover gives Q = 1 - γ, that is, already negative for any γ > 1, regardless of how clustered the graph actually is. And for non-trivial covers the edge term is smaller while the null term is still quadratic in the per-community degree sum, so the crossover into negative territory happens even faster.
The deeper reason this matters is Fortunato & Barthélemy's (2007) resolution limit (https://doi.org/10.1073/pnas.0605965104): at γ=1, modularity can't resolve communities smaller than ~sqrt(2m) edges, no matter how well-separated they are. Tuning resolution up shrinks that floor (lets you detect smaller communities) at the cost of fragmenting larger ones; tuning it down does the opposite. Reichardt & Bornholdt's (2006) statistical-mechanics derivation (https://arxiv.org/abs/cond-mat/0603718) frames it as picking the scale at which you're looking for structure, which I find a better way to think about it than "tradeoff parameter."
| membership = {} | ||
| for community in communities: | ||
| for node in community: | ||
| if node in G: | ||
| membership[node] = membership.get(node, 0) + 1 |
There was a problem hiding this comment.
Using a defaultdict here makes the updating a little clearer:
| membership = {} | |
| for community in communities: | |
| for node in community: | |
| if node in G: | |
| membership[node] = membership.get(node, 0) + 1 | |
| membership = defaultdict(int) | |
| for community in communities: | |
| for node in community: | |
| if node in G: | |
| membership[node] += 1 |
Also not a blocker :)
Make weight and resolution keyword-only, switch to defaultdict for membership counting, generalize the k_clique_communities reference, and clarify the effect of the resolution parameter.
rossbar
left a comment
There was a problem hiding this comment.
Thanks for taking the time to address my questions - interesting stuff! LGTM!
Add
nx.community.overlapping_modularityimplementing Shen et al.'s extended modularity EQ (2009) as a quality function for overlapping community covers. The implementation uses the community-sum formwhere L_tilde_C is the overlap-discounted intra-community edge count (each edge weighted by 1/(O_v * O_w) with O_v the number of communities containing v) and k_tilde_C is the overlap-discounted degree sum. This mirrors the community-sum form used by
nx.community.modularitywith overlap-discounted quantities replacing the raw counterparts. When the cover is just a partition (O_v = 1 for all v), EQ reduces to Q exactly.Also add
nx.community.is_cover, the cover-equivalent ofis_partition: a cover requires every node in G to belong to at least one community, but allows overlap. Used internally to validate input to overlapping_modularity, exposed publicly for users (e.g. to sanity-check the output ofk_clique_communitiesbefore scoring).The resolution parameter gamma is not in Shen's original definition; it is added for consistency with
modularity(resolution=...). With gamma=1 the formula is exactly Shen's EQ.Tests cover equivalence with standard modularity on partition inputs, hand-calculated EQ values for a triangle under several covers, weighted hand-calculation, k_clique_communities integration, resolution parameter monotonicity, and the usual edge cases (directed not implemented, empty graph, weight=None, invalid cover).