vtraag / leidenalg

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

quality() and modularity values are not identical. #111

Closed soodimilanlouei closed 1 year ago

soodimilanlouei commented 1 year ago

Hi,

Many thanks for your great work on this package. I have an unweighted undirected network and I used the ModularityVertexPartition quality function to find communities for different values of max_comm_size. While it's been said in 72 that for such a network quality() and modularity should be identical, they are not in my case. In the plot below, you can see the quality and modularity for different values of max_comm_size (annotated on the plot). I was wondering why they are not identical. Also, can you provide the mathematical formulation for these two quantities? image

The code that I'm using:

partition = la.find_partition(g, la.ModularityVertexPartition, seed = 2022, max_comm_size = i) quality = la.SignificanceVertexPartition.FromPartition(partition).quality() modularity = la.SignificanceVertexPartition.FromPartition(partition).modularity

vtraag commented 1 year ago

Thanks!

Note that

quality = la.SignificanceVertexPartition.FromPartition(partition).quality()

calculates the significance of the partition partition, not the modularity. On the other hand,

modularity = la.SignificanceVertexPartition.FromPartition(partition).modularity

does calculate the modularity of partition. In fact, partition.modularity should simply be equal to modularity in this case, since the partitions in partition and la.SignificanceVertexPartition.FromPartition(partition) are identical, they are just using different quality functions.

Hopefully this explains your question!

soodimilanlouei commented 1 year ago

Thanks for the response. I also thought quality and modularity should be identical in this case, but as you can see in the plot that I attached above, they are not, and even the relationship between them is not linear and I'm trying to understand why.

vtraag commented 1 year ago

Thanks for the response.

You're welcome!

I also thought quality and modularity should be identical in this case

Not as you have defined them above.

If you calculate

quality = partition.quality()

then yes, quality == partition.modularity. For unweighted undirected graphs, partition.modularity == partition.quality() if partition is a ModularityVertexPartition. However, you are now calculating quality as the quality of a SignificanceVertexPartition, so that quality represents significance, not modularity.

Hopefully it is clear now?

If not, please post your exact script to reproduce the figure, I can then take a closer look and we can figure out what is happening.