In [3]:
%matplotlib inline

In [4]:
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())

Drawing different representations of hypergraph
Bipartite graph
Graph of hypereges as nodes
Clique graph

In [5]:
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

In [6]:
from hypergraph.diffusion_engine import DiffusionEngine
from hypergraph.markov_diffusion import (create_markov_matrix,
count_nodes)

In [7]:
t_max = 1000
markov_matrix = create_markov_matrix(HG.hyper_edges())
nodes = HG.nodes()
edges = HG.hyper_edges()
engine = DiffusionEngine(markov_matrix, t_per_walker=20)

In [8]:
most_common, states = engine.simulate(t_max)

In [9]:
most_common_nodes = count_nodes(nodes, edges, most_common)


$S = - k_{\mathrm{B}}\sum_i p_i \ln p_i \$

In [23]:
import numpy as np
from matplotlib import pyplot as plt

from collections import Counter
from itertools import chain
from hypergraph import utils
from hypergraph.markov_diffusion import (create_markov_matrix,
count_nodes)

def entropy(pis):
return -np.dot(np.log(pis), pis)

def compare_entropy(HG, t_max=10000, t_per_walker=100):
markov_matrix = create_markov_matrix(HG.hyper_edges())
nodes = HG.nodes()
edges = HG.hyper_edges()
engine = DiffusionEngine(markov_matrix, t_per_walker=t_per_walker)

most_common, states = engine.simulate(t_max)
most_common_nodes = count_nodes(nodes, edges, most_common)
plt.show()

states_per_time = list(zip(*states))

xs = list(range(len(states_per_time))[4:])
ys = []
for i in xs:
try:
cum_states = chain(*states_per_time[i-400:i])
most_common = Counter(cum_states).most_common()
most_common_nodes = count_nodes(nodes, edges, most_common)
frequencies = (utils.get_names_and_occurrences(most_common_nodes)[1])
entropy_value = entropy(frequencies)
if entropy_value is not float("nan"):
ys.append(entropy_value)
else:
ys.append(-1)

except:
ys.append(0)

fig = plt.plot(xs, ys)
plt.show()
return fig

In [24]:
k = 3
f = 1.6

for n in range(10, 20, 5):
HG = utils.create_graph(n, k, f)
fig = compare_entropy(HG)
plt.savefig('entropy_h_%s_%s_%s.png' % (n, k, f))

<matplotlib.figure.Figure at 0x7fc48a450310>
<matplotlib.figure.Figure at 0x7fc48a3d3a90>
In [9]: