-
Notifications
You must be signed in to change notification settings - Fork 266
Added Stochastic Variability in Community Detection Algorithms #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+134
−0
Closed
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c5e1ad
Added stochastic_variability.py file
SKG24 a3ac9c1
Update stochastic_variability.py
SKG24 5247f7e
Merge branch 'main' into main
ntamas dc52eb3
Update sg_execution_times.rst
SKG24 97b7192
Update stochastic_variability.py
SKG24 b6b07e9
Delete doc/source/sg_execution_times.rst
SKG24 417813a
Merge branch 'main' into main
szhorvat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| """ | ||
| .. _tutorials-stochastic-variability: | ||
|
|
||
| ========================================================= | ||
| Stochastic Variability in Community Detection Algorithms | ||
| ========================================================= | ||
|
|
||
| This example demonstrates the variability of stochastic community detection methods by analyzing the consistency of multiple partitions using similarity measures (NMI, VI, RI) on both random and structured graphs. | ||
|
|
||
| """ | ||
| # %% | ||
| # Import Libraries | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
| import igraph as ig | ||
| import numpy as np | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
| import matplotlib.pyplot as plt | ||
| import itertools | ||
|
|
||
| # %% | ||
| # First, we generate a graph. | ||
| # Generates a random Erdos-Renyi graph (no clear community structure) | ||
| def generate_random_graph(n, p): | ||
| return ig.Graph.Erdos_Renyi(n=n, p=p) | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
|
|
||
| # %% | ||
| # Generates a clustered graph with clear communities using the Stochastic Block Model (SBM) | ||
| def generate_clustered_graph(n, clusters, intra_p, inter_p): | ||
| block_sizes = [n // clusters] * clusters | ||
| prob_matrix = [[intra_p if i == j else inter_p for j in range(clusters)] for i in range(clusters)] | ||
| return ig.Graph.SBM(sum(block_sizes), prob_matrix, block_sizes) | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
|
|
||
| # %% | ||
| # Computes pairwise similarity (NMI, VI, RI) between partitions | ||
| def compute_pairwise_similarity(partitions, method): | ||
| """Computes pairwise similarity measure between partitions.""" | ||
| scores = [] | ||
| for p1, p2 in itertools.combinations(partitions, 2): | ||
| scores.append(ig.compare_communities(p1, p2, method=method)) | ||
| return scores | ||
|
|
||
| # %% | ||
| # Stochastic Community Detection | ||
| # Runs Louvain's method iteratively to generate partitions | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
| # Computes similarity metrics: | ||
| def run_experiment(graph, iterations=50): | ||
| """Runs the stochastic method multiple times and collects community partitions.""" | ||
| partitions = [graph.community_multilevel().membership for _ in range(iterations)] | ||
| nmi_scores = compute_pairwise_similarity(partitions, method="nmi") | ||
| vi_scores = compute_pairwise_similarity(partitions, method="vi") | ||
| ri_scores = compute_pairwise_similarity(partitions, method="rand") | ||
| return nmi_scores, vi_scores, ri_scores | ||
|
|
||
| # %% | ||
| # Parameters | ||
| n_nodes = 100 | ||
| p_random = 0.05 | ||
| clusters = 4 | ||
| p_intra = 0.3 # High intra-cluster connection probability | ||
| p_inter = 0.01 # Low inter-cluster connection probability | ||
|
|
||
| # %% | ||
| # Generate graphs | ||
| random_graph = generate_random_graph(n_nodes, p_random) | ||
| clustered_graph = generate_clustered_graph(n_nodes, clusters, p_intra, p_inter) | ||
|
|
||
| # %% | ||
| # Run experiments | ||
| nmi_random, vi_random, ri_random = run_experiment(random_graph) | ||
| nmi_clustered, vi_clustered, ri_clustered = run_experiment(clustered_graph) | ||
|
|
||
| # %% | ||
| # Lets, plot the histograms | ||
| fig, axes = plt.subplots(3, 2, figsize=(12, 10)) | ||
| measures = [(nmi_random, nmi_clustered, "NMI"), (vi_random, vi_clustered, "VI"), (ri_random, ri_clustered, "RI")] | ||
| colors = ["red", "blue", "green"] | ||
|
|
||
| for i, (random_scores, clustered_scores, measure) in enumerate(measures): | ||
| axes[i][0].hist(random_scores, bins=20, alpha=0.7, color=colors[i], edgecolor="black") | ||
| axes[i][0].set_title(f"Histogram of {measure} - Random Graph") | ||
| axes[i][0].set_xlabel(f"{measure} Score") | ||
| axes[i][0].set_ylabel("Frequency") | ||
|
|
||
| axes[i][1].hist(clustered_scores, bins=20, alpha=0.7, color=colors[i], edgecolor="black") | ||
| axes[i][1].set_title(f"Histogram of {measure} - Clustered Graph") | ||
| axes[i][1].set_xlabel(f"{measure} Score") | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
|
|
||
| plt.tight_layout() | ||
| plt.show() | ||
|
|
||
| # %% | ||
| # The results are plotted as histograms for random vs. clustered graphs, highlighting differences in detected community structures. | ||
| #The key reason for the inconsistency in random graphs and higher consistency in structured graphs is due to community structure strength: | ||
| #Random Graphs: Lack clear communities, leading to unstable partitions. Stochastic algorithms detect different structures across runs, resulting in low NMI, high VI, and inconsistent RI. | ||
| #Structured Graphs: Have well-defined communities, so detected partitions are more stable across multiple runs, leading to high NMI, low VI, and stable RI. | ||
|
szhorvat marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.