Coverage for src/causalspyne/erdo_renyi_plp.py: 100%
10 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-05-15 16:30 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-05-15 16:30 +0000
1"""
2concrete class to generate simple DAGs
4"""
5import numpy as np
8class Erdos_Renyi_PLP:
9 """
10 uniformly (w.r.t. each edge) decide if the edge will exist w.r.t. a prob.
11 threshold
12 trick: PLP^T to ensure a DAG, L is lower triangular (topological order)
13 P is permutation, P permute the labels
14 (i,j) entry indicate j->i
15 row permutation (i,j), (k,j) becomes (k,j) (i,j)
16 column permutation
17 """
19 def __init__(self, rng):
20 self.rng = rng
22 def __call__(self, num_nodes, degree):
23 prob = float(degree) / (num_nodes - 1)
24 # lower triagular, k=-1 is the lower off diagonal
25 mat_lower_triangle_binary = np.tril(
26 (self.rng.random((num_nodes, num_nodes)) < prob
27 ).astype(float), k=-1
28 )
29 # permutes first axis only
30 mat_perm = self.rng.permutation(np.eye(num_nodes, num_nodes))
31 mat_b_permuted = mat_perm.T.dot(
32 mat_lower_triangle_binary).dot(mat_perm)
33 return mat_b_permuted