Skip to content

Commit

Permalink
Fix #361
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Sep 16, 2024
1 parent eb70415 commit 3bbbb5b
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions xgi/generators/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .classic import empty_hypergraph
from .lattice import ring_lattice
from .uniform import uniform_erdos_renyi_hypergraph

__all__ = [
"random_hypergraph",
Expand All @@ -20,15 +21,15 @@
]


def random_hypergraph(N, ps, order=None, seed=None):
def random_hypergraph(n, ps, order=None, seed=None):
"""Generates a random hypergraph
Generate N nodes, and connect any d+1 nodes
by a hyperedge with probability ps[d-1].
Parameters
----------
N : int
n : int
Number of nodes
ps : list of float
List of probabilities (between 0 and 1) to create a
Expand Down Expand Up @@ -56,37 +57,32 @@ def random_hypergraph(N, ps, order=None, seed=None):
>>> H = xgi.random_hypergraph(50, [0.1, 0.01])
"""
if seed is not None:
np.random.seed(seed)
ps = np.array(ps)

if order is not None:
if len(ps) != 1:
raise ValueError("ps must contain a single element if order is an int")

if (np.any(np.array(ps) < 0)) or (np.any(np.array(ps) > 1)):
if (ps < 0).any() or (ps > 1).any():
raise ValueError("All elements of ps must be between 0 and 1 included.")

nodes = range(N)
nodes = range(n)
hyperedges = []

H = empty_hypergraph()
H.add_nodes_from(nodes)

for i, p in enumerate(ps):

if order is not None:
d = order
else:
d = i + 1 # order, ps[0] is prob of edges (d=1)

potential_edges = combinations(nodes, d + 1)
n_comb = comb(N, d + 1, exact=True)
mask = np.random.random(size=n_comb) <= p # True if edge to keep

edges_to_add = [e for e, val in zip(potential_edges, mask) if val]

hyperedges += edges_to_add

H = empty_hypergraph()
H.add_nodes_from(nodes)
H.add_edges_from(hyperedges)
size = d + 1
if p > 0:
h_uniform = uniform_erdos_renyi_hypergraph(n, size, p, seed=seed)
H << h_uniform

return H

Expand Down

0 comments on commit 3bbbb5b

Please sign in to comment.