%matplotlib inline
from hypergraph import utils
n = 10
k = 3
f = 0.8
HG = utils.create_graph(n, k, f)
utils.plot_different_representations(HG.nodes(), HG.hyper_edges())
from hypergraph import diffusion_convergence_analytic
from matplotlib import pyplot as plt
def diffusion_on_hypergraph(hypergraph, markov_matrix,
t_max, plot_results=False):
"""Simulate numerically diffusion on a hypergraph,
Diffusion is simulated using markov chains.
"""
nodes = hypergraph.nodes()
hyper_edges = hypergraph.hyper_edges()
most_common, most_common_nodes, states = simulate_diffusion(
nodes, hyper_edges, markov_matrix, t_max)
if plot_results:
return plot_diffusion_results(most_common, most_common_nodes,
hyper_edges, "hypergraph")
else:
return utils.get_names_and_occurrences(most_common_nodes)[1]
def plot_diffusion_results(most_common, most_common_nodes, edges, name):
plt.figure(figsize=(8, 4))
utils.plot_hyperedges_frequencies(most_common, edges,
('Ocurrences of hyperedges in'
' a {}').format(name),
normed=True)
plt.figure(figsize=(8, 4))
return utils.plot_nodes_frequencies(most_common_nodes,
'Nodes in a {}'.format(name),
normed=True)
def simulate_diffusion(nodes, edges, markov_matrix, t_max):
engine = DiffusionEngine(markov_matrix)
most_common, states = engine.simulate(t_max)
most_common_nodes = count_nodes(nodes, edges, most_common)
return most_common, most_common_nodes, states
from hypergraph.diffusion_engine import DiffusionEngine
from hypergraph.markov_diffusion import (create_markov_matrix,
count_nodes)
t_max = 100
markov_matrix = create_markov_matrix(HG.hyper_edges())
nodes = HG.nodes()
edges = HG.hyper_edges()
engine = DiffusionEngine(markov_matrix, t_per_walker=20)
most_common, states = engine.simulate(t_max)
most_common_nodes = count_nodes(nodes, edges, most_common)
states
most_common
most_common_nodes
plot_diffusion_results(most_common, most_common_nodes, nodes, 'Hyper 100')
def plot_diffusion_results(most_common, most_common_nodes, edges, name):
plt.figure(figsize=(8, 4))
utils.plot_hyperedges_frequencies(most_common, edges,
('Ocurrences of hyperedges in'
' a {}').format(name),
normed=True)
plt.figure(figsize=(8, 4))
return utils.plot_nodes_frequencies(most_common_nodes,
'Nodes in a {}'.format(name),
normed=True)
from pylab import get_cmap, cm
import numpy as np
def plot_comparison_bars(x, ys, labels=None):
plt.figure(figsize=(12, 8))
colors = ["crimson", "burlywood", "#65df25", "#dcab11", "magenta"] + ["blue"] * 50
N = len(ys)
width = 1.0 / N - 0.04
colors = cm.get_cmap('jet', N)
if labels is None:
labels = [''] * N
print(width)
for i, y in enumerate(ys):
plt.bar(np.array(x) + i * width, y,
width=width, color=colors(i),
label=labels[i], alpha=0.7)
plt.legend(loc=0)
plot_comparison_bars(range(4), [range(4), [1, 2, 5, 2]])
t_max = 100
markov_matrix = create_markov_matrix(HG.hyper_edges())
nodes = HG.nodes()
edges = HG.hyper_edges()
engine = DiffusionEngine(markov_matrix, t_per_walker=30)
most_common, states = engine.simulate(t_max)
most_common_nodes = count_nodes(nodes, edges, most_common)
most_common_to_t_max = {}
for t_max in [100, 300, 600, 800, 900, 1200, 1300]:
most_common, states = engine.simulate(t_max)
most_common_nodes = count_nodes(nodes, edges, most_common)
most_common = list(most_common)
most_common.sort(key=lambda x: x[0])
print(most_common)
_, occurrences = zip(*most_common)
most_common_to_t_max[t_max] = utils.get_names_and_occurrences(most_common_nodes)[1]
most_common_to_t_max.values()
from hypergraph import analytical
from pylab import cm
to_compare = list(most_common_to_t_max.values())
nodes, predictions = analytical.analytical_hypergraph_diffusion(HG)
to_compare.append(predictions)
plot_comparison_bars(range(len(nodes)), to_compare)
It look like it does converge. However, what is the impact of number of walkers?
markov_matrix