vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
596 stars 78 forks source link

quality() vs modularity? #72

Closed silverbullet1472 closed 3 years ago

silverbullet1472 commented 3 years ago

Hello Traag,
Thanks your work on this library, it really helps our research a lot!!

However, I notice that:
If we use modularity for partition,
fp = la.find_partition(G, la.ModularityVertexPartition, n_iterations=100, weights='weight')

And when retrieve the 'q', 'modularity' , the value is much smaller than what quality() function gives:

mp = la.ModularityVertexPartition(G,weights='weight', initial_membership=fp.membership) mp.quality() # 0.753 mp.modularity # 0.281 mp.q

So my question is:

Sorry to bother you.

vtraag commented 3 years ago
  • How is q or modularity calculated (are they the same thing?)

mp.quality() is calculed using the weights from the weight attribute that you indicate. mp.modularity does not take into account the weights. It also does not correctly take the directionality of the graph into account. In short, you should use mp.quality(), but not mp.modularity, unless you have an unweighted, undirected graph, in which case the two should be identical.

  • I guess this comment should be deleted?

Yes, quite right, it can be removed from the source code. It was previously unscaled, but then people got confused, but I did not remove that comment.

silverbullet1472 commented 3 years ago

Thanks for explanation! Btw, is there a way to scale the quality output for CPM? Because I saw in gephi's leiden plugin, the quality is a number from 0 to 1. Thanks!

vtraag commented 3 years ago

Thanks for explanation!

You're welcome!

is there a way to scale the quality output for CPM?

Yes, you can also simply scale it with (twice) the number of edges (or total edge weight), that is also what is done in the Gephi plugin.

silverbullet1472 commented 3 years ago

I see, thanks for your work on this project, and also very professional help !

ivan-marroquin commented 2 years ago

Hi @vtraag

Many thanks for making available such great package. In one of your comments, you mentioned the following:

mp.quality() is calculed using the weights from the weight attribute that you indicate. mp.modularity does not take into account the weights. It also does not correctly take the directionality of the graph into account. In short, you should use mp.quality(), but not mp.modularity, unless you have an unweighted, undirected graph, in which case the two should be identical.

Check the C++ files to compute the different modularity approaches, I noticed that quality is computed as follows: double q = (2.0 - this->graph->is_directed())*mod

If my observation is correct. Then, modularity takes into account the weights, and thus, quality as well. The difference between quality and modularity is: 1) for undirected graphs quality = 2* mod, but 2) for directed graphs quality= mod.

Am I correct? If so, it should not make difference whether to use modularity or quality to assess the goodness of the community detection.

Kind regards, Ivan

wongs19 commented 1 year ago

Hi @vtraag

Many thanks for making available such great package. In one of your comments, you mentioned the following:

mp.quality() is calculed using the weights from the weight attribute that you indicate. mp.modularity does not take into account the weights. It also does not correctly take the directionality of the graph into account. In short, you should use mp.quality(), but not mp.modularity, unless you have an unweighted, undirected graph, in which case the two should be identical.

Check the C++ files to compute the different modularity approaches, I noticed that quality is computed as follows: double q = (2.0 - this->graph->is_directed())*mod

If my observation is correct. Then, modularity takes into account the weights, and thus, quality as well. The difference between quality and modularity is: 1) for undirected graphs quality = 2* mod, but 2) for directed graphs quality= mod.

Am I correct? If so, it should not make difference whether to use modularity or quality to assess the goodness of the community detection.

Kind regards, Ivan

I have the same problems. The result which ModularityVertexPartition funtion return is confused. Hope @vtraag can reply. Thanks!