Skip to content

Commit c2037db

Browse files
committed
add seed option and rename everywhere
1 parent 5b2673a commit c2037db

10 files changed

+35
-22
lines changed

benchmarks/utilita.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
set_config(print_changed_only=False)
2323

2424

25-
def network_generation(p=int, N=int, M=10, style='powerlaw', gamma=2.8, prob=0.1, scale=False, nxseed=None):
25+
def network_generation(p=int, N=int, M=10, style='powerlaw', gamma=2.8, prob=0.1, scale=False, seed=None):
2626
"""
2727
Generates a law-power network with number of connected components bigger than 1.
2828
:param p: int
@@ -36,8 +36,8 @@ def network_generation(p=int, N=int, M=10, style='powerlaw', gamma=2.8, prob=0.1
3636
X - dual variable.
3737
Theta - true precision matrix.
3838
"""
39-
Sigma, Theta = generate_precision_matrix(p=p, M=M, style=style, gamma=gamma, prob=prob, scale=scale, nxseed=nxseed)
40-
S, samples = sample_covariance_matrix(Sigma, N)
39+
Sigma, Theta = generate_precision_matrix(p=p, M=M, style=style, gamma=gamma, prob=prob, scale=scale, seed=seed)
40+
S, samples = sample_covariance_matrix(Sigma, N, seed = seed)
4141
X = samples.T
4242

4343
return S, X, Theta

examples/dev/joint_estimation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
for j in range(J):
3636

37-
Sigma, Theta = group_power_network(p, K = allK[j], M = 5, scale = False, nxseed = 1235)
37+
Sigma, Theta = group_power_network(p, K = allK[j], M = 5, scale = False, seed = 1235)
3838
S, sample = sample_covariance_matrix(Sigma, N)
3939

4040
print("Shape of empirical covariance matrix: ", S.shape)

examples/fgl_powerlaw/exp_powerlaw_fgl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
reg = 'FGL'
3131

32-
Sigma, Theta = time_varying_power_network(p, K, M, nxseed = 2340)
32+
Sigma, Theta = time_varying_power_network(p, K, M, seed = 2340)
3333

3434
#single_heatmap_animation(Theta)
3535

examples/ggl_powerlaw/exp_powerlaw_ggl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# whether to save the plots as pdf-files
3131
save = False
3232

33-
Sigma, Theta = group_power_network(p, K, M, nxseed = 2340)
33+
Sigma, Theta = group_power_network(p, K, M, seed = 2340)
3434

3535
draw_group_heatmap(Theta, save = save)
3636

examples/plot_basic_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
p = 20
2424
N = 1000
2525

26-
Sigma, Theta = generate_precision_matrix(p=p, M=1, style='erdos', prob=0.1, nxseed=1234)
26+
Sigma, Theta = generate_precision_matrix(p=p, M=1, style='erdos', prob=0.1, seed=1234)
2727

2828
S, sample = sample_covariance_matrix(Sigma, N)
2929

examples/plot_nonconforming_ggl.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
p = 50
2424
M = 10
2525
B = int(p/M)
26-
N = 100
26+
N = 200
2727

2828
#%%
2929
# Generating the data
@@ -36,13 +36,13 @@
3636
p_arr = (p-B)*np.ones(K, dtype = int)
3737
num_samples = N*np.ones(K)
3838

39-
Sigma, Theta = generate_precision_matrix(p=p, M=M, style = 'powerlaw', gamma = 2.8, prob = 0.1, nxseed = 3456)
39+
Sigma, Theta = generate_precision_matrix(p=p, M=M, style = 'powerlaw', gamma = 2.8, prob = 0.1, seed = 3456)
4040

4141
all_obs = dict()
4242
S = dict()
4343
for k in np.arange(K):
4444

45-
_, obs = sample_covariance_matrix(Sigma, N)
45+
_, obs = sample_covariance_matrix(Sigma, N, seed = 456)
4646

4747
# drop the k-th block starting from the end
4848
all_obs[k] = pd.DataFrame(obs).drop(np.arange(p-(k+1)*B, p-k*B), axis = 0)

examples/plot_powerlaw_fgl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
reg = 'FGL'
3333

34-
Sigma, Theta = time_varying_power_network(p, K, M, scale = False, nxseed = 2340)
34+
Sigma, Theta = time_varying_power_network(p, K, M, scale = False, seed = 2340)
3535

3636
S, sample = sample_covariance_matrix(Sigma, N)
3737

examples/plot_powerlaw_ggl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
reg = 'GGL'
2828

29-
Sigma, Theta = group_power_network(p, K, M, scale = False, nxseed = 2340)
29+
Sigma, Theta = group_power_network(p, K, M, scale = False, seed = 2340)
3030

3131
S, sample = sample_covariance_matrix(Sigma, N)
3232

gglasso/helper/data_generation.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .basic_linalg import trp
1313

1414

15-
def generate_precision_matrix(p=100, M=10, style = 'powerlaw', gamma = 2.8, prob = 0.1, scale = False, nxseed = None):
15+
def generate_precision_matrix(p=100, M=10, style = 'powerlaw', gamma = 2.8, prob = 0.1, scale = False, seed = None):
1616
"""
1717
Generates a sparse precision matrix with associated covariance matrix from a random network.
1818
@@ -36,8 +36,8 @@ def generate_precision_matrix(p=100, M=10, style = 'powerlaw', gamma = 2.8, prob
3636
scale : boolean, optional
3737
whether Sigma (cov. matrix) is scaled by diagonal entries (as described by Danaher et al.). If set to True, then the generated precision matrix is not
3838
the inverse of Sigma anymore.
39-
nxseed : int, optional
40-
Seed for network creation. The default is None.
39+
seed : int, optional
40+
Seed for network creation and matrix entries. The default is None.
4141
4242
Returns
4343
-------
@@ -55,6 +55,11 @@ def generate_precision_matrix(p=100, M=10, style = 'powerlaw', gamma = 2.8, prob
5555
A = np.zeros((p,p))
5656
Sigma = np.zeros((p,p))
5757

58+
if seed is not None:
59+
nxseed = seed
60+
else:
61+
nxseed = None
62+
5863
for m in np.arange(M):
5964

6065
if nxseed is not None:
@@ -69,7 +74,9 @@ def generate_precision_matrix(p=100, M=10, style = 'powerlaw', gamma = 2.8, prob
6974
A_m = nx.to_numpy_array(G_m)
7075

7176
# generate random numbers for the nonzero entries
72-
np.random.seed(1234)
77+
if seed is not None:
78+
np.random.seed(seed)
79+
7380
B1 = np.random.uniform(low = .1, high = .4, size = (L,L))
7481
B2 = np.random.choice(a = [-1,1], p=[.5, .5], size = (L,L))
7582

@@ -121,7 +128,7 @@ def generate_precision_matrix(p=100, M=10, style = 'powerlaw', gamma = 2.8, prob
121128

122129
return Sigma, Theta
123130

124-
def time_varying_power_network(p=100, K=10, M=10, scale = False, nxseed = None):
131+
def time_varying_power_network(p=100, K=10, M=10, scale = False, seed = None):
125132
"""
126133
generates a power law network. The first block disappears at half-time, while the second block appears
127134
third block decays exponentially
@@ -135,7 +142,7 @@ def time_varying_power_network(p=100, K=10, M=10, scale = False, nxseed = None):
135142
assert M*L == p
136143
assert M >=3
137144

138-
Sigma_0,_ = generate_precision_matrix(p = p, M = M, style = 'powerlaw', scale = scale, nxseed = nxseed)
145+
Sigma_0,_ = generate_precision_matrix(p = p, M = M, style = 'powerlaw', scale = scale, seed = seed)
139146

140147
for k in np.arange(K):
141148
Sigma_k = Sigma_0.copy()
@@ -160,7 +167,7 @@ def time_varying_power_network(p=100, K=10, M=10, scale = False, nxseed = None):
160167

161168
return Sigma, Theta
162169

163-
def group_power_network(p=100, K=10, M=10, scale = False, nxseed = None):
170+
def group_power_network(p=100, K=10, M=10, scale = False, seed = None):
164171
"""
165172
generates a power law network. In each single network one block disappears (randomly)
166173
p: dimension
@@ -172,8 +179,11 @@ def group_power_network(p=100, K=10, M=10, scale = False, nxseed = None):
172179
L = int(p/M)
173180
assert M*L == p
174181

175-
Sigma_0,_ = generate_precision_matrix(p = p, M = M, style = 'powerlaw', scale = scale, nxseed = nxseed)
182+
Sigma_0,_ = generate_precision_matrix(p = p, M = M, style = 'powerlaw', scale = scale, seed = seed)
176183
# contains the number of the block disappearing for each k=1,..,K
184+
if seed is not None:
185+
np.random.seed(seed)
186+
177187
block = np.random.randint(M, size = K)
178188

179189
for k in np.arange(K):
@@ -200,11 +210,14 @@ def ensure_sparsity(Sigma, Theta):
200210
return Sigma, Theta
201211

202212

203-
def sample_covariance_matrix(Sigma, N):
213+
def sample_covariance_matrix(Sigma, N, seed = None):
204214
"""
205215
samples data for a given covariance matrix Sigma (with K layers)
206216
return: sample covariance matrix S
207217
"""
218+
if seed is not None:
219+
np.random.seed(seed)
220+
208221
if len(Sigma.shape) == 2:
209222
assert abs(Sigma - Sigma.T).max() <= 1e-10
210223
(p,p) = Sigma.shape

gglasso/tests/test_solvers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_SGL_scikit():
195195
N = 100
196196

197197

198-
Sigma, Theta = generate_precision_matrix(p=p, M=2, style = 'erdos', gamma = 2.8, prob = 0.1, scale = False, nxseed = None)
198+
Sigma, Theta = generate_precision_matrix(p=p, M=2, style = 'erdos', gamma = 2.8, prob = 0.1, scale = False, seed = None)
199199
S, samples = sample_covariance_matrix(Sigma, N) # sample from multivar_norm(Sigma)
200200

201201
lambda1 = 0.01

0 commit comments

Comments
 (0)