-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.py
More file actions
80 lines (68 loc) · 2.68 KB
/
Problem2.py
File metadata and controls
80 lines (68 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import time
from compnet.graphs import ConfigModelDegreeGraph
import numpy as np
import matplotlib.pyplot as plt
from joblib import Parallel, delayed
def get_gcs_sample(N, pi):
degree_dict = {1: 1-pi, 4: pi}
G = ConfigModelDegreeGraph(N=N, degree_dict=degree_dict)
G.generate_graph()
G.find_neighbourhoods()
gcs = G.get_giantcomponentsize() / N
return gcs
def gamma_func(pi):
"""
This is the gamma function returning the probability that a vertex belongs
to the giant component in the specific case of p1=1-pi and p4=pi for a
configuration model random graph instance.
:param pi: input parameter pi
:returns gamma: the probability that a vertex belongs to the GC
"""
def mu_func(pi):
"""
This is the mu function returning the probability that an end vertex
does not belong to the GC in the specific case of p1=1-pi and p4=pi for
a configuration model random graph instance.
:param pi: input parameter pi
:returns gamma: the probability that an end vertex does not belong to
the GC
"""
return (1-np.sqrt(pi)) / (2*np.sqrt(pi))
# we put a threshold where we know that the GC will appear
if pi > 1. / 9.:
return 1 - (1 - pi) * mu_func(pi) - pi * (mu_func(pi))**4
else:
return 0.
if __name__ == "__main__":
n_samples = 20
pi_values = np.linspace(0.01, 0.99, 25)
N_values = [100, 500, 1000]
gcs_meas = {N: {'means': [], 'stds': []} for N in N_values}
for N in N_values:
gcs_values_means = []
gcs_values_stds = []
for pi in pi_values:
print(f'Sampling for PI {pi:.2f}')
gcs_samples = []
for _ in range(n_samples):
gcs_sample = get_gcs_sample(N, pi)
print(gcs_sample)
gcs_samples.append(gcs_sample)
gcs_values_means.append(np.mean(gcs_samples))
gcs_values_stds.append(np.std(gcs_samples) / np.sqrt(n_samples))
#print('std', np.std(gcs_samples) / np.sqrt(n_samples))
gcs_meas[N]['means'] = gcs_values_means
gcs_meas[N]['stds'] = gcs_values_stds
analytic_pi = np.linspace(0, 1, 200)
analytic_gamma = np.vectorize(gamma_func)(analytic_pi)
fig = plt.figure()
for N in N_values:
plt.errorbar(pi_values, gcs_meas[N]['means'], yerr=gcs_meas[N]['stds'], fmt='.', label=f'GCS measures for $N={N}$')
plt.plot(analytic_pi, analytic_gamma, label=r'$\gamma$ function')
plt.xlabel('$\pi$')
plt.ylabel('Size of GCS')
plt.title('Behaviour of GCS in the configuration model')
plt.legend()
plt.grid()
plt.close()
fig.savefig('assets/gcs.pdf', bbox_inches='tight')