<jupyter_start><jupyter_text>Clustering coefficients<jupyter_code>## star example
g = ig.Graph.Erdos_Renyi(n=22, m=0)
g.vs['color'] = 'grey'
g.vs[0]['color'] = g.vs[1]['color'] = 'red'
g.add_edge(0,1)
for i in range(20):
    g.add_edge(0,i+2)
    g.add_edge(1,i+2)
g.vs['size']=10
ly = g.layout("kk")  ## kamada-kawai; usually nice
ig.plot(g, layout=ly, bbox=(0,0,350,250))
## overall transitivity T
g.transitivity_undirected()
## Local transitivity for red and grey nodes respectively
print( g.transitivity_local_undirected(1) , g.transitivity_local_undirected(2) )
## clustering coef. 
g.transitivity_avglocal_undirected()
## Now for ER graph (both have expected value p)
L = []
size = 500 ## try 50, 100, 500 ...
for p in np.arange(.1,.65,.1):
    g = ig.Graph.Erdos_Renyi(n=size, p=p)
    L.append([p,g.transitivity_undirected(),g.transitivity_avglocal_undirected()])
pd.DataFrame(L, columns=['p','T','CC'])
## histogram - local CC's
lcc = g.transitivity_local_undirected()
plt.boxplot(lcc);<jupyter_output><empty_output><jupyter_text>Power law distribution<jupyter_code># gamma = -2
d = powerLawSample(n=50,xmin=2,xmax=20)
g = ig.Graph.Degree_Sequence(d, method="no_multiple")
g.vs['size']=10
ly = g.layout("kk")  ## kamada-kawai; usually nice
ig.plot(g, layout=ly, bbox=(0,0,350,250))

deg = np.sort(d).tolist()
freq = [len(list(group))/len(deg) for key, group in groupby(deg)]
deg = np.unique(deg)
plt.bar(deg,freq)
plt.xlabel('Degree', fontsize=14);

plt.loglog(deg,freq,'o')
plt.xlabel('Degree',fontsize=14)
p = powerLawDist(xmin=2,xmax=20)
plt.loglog(np.arange(2,21,1),p,'--');
# gamma = -3
p = powerLawDist(xmin=2,xmax=20,gamma=-3.0)
d = powerLawSample(n=50,xmin=2,xmax=20,gamma=-3.0)
g = ig.Graph.Degree_Sequence(d, method="no_multiple")
g.vs['size']=10
ly = g.layout("kk")  ## kamada-kawai; usually nice
ig.plot(g, layout=ly, bbox=(0,0,350,250))

deg = np.sort(d)
freq = [len(list(group))/len(deg) for key, group in groupby(deg)]
deg = np.unique(deg)
plt.bar(deg,freq)
plt.xlabel('Degree', fontsize=14);

plt.loglog(deg,freq,'o')
plt.xlabel('Degree', fontsize=14)
plt.loglog(np.arange(2,21,1),p,'--');
## look at larger sample
p = powerLawDist(xmin=2,xmax=20, gamma=-2.5)
d = powerLawSample(n=1000,xmin=2,xmax=20, gamma=-2.5)
deg = np.sort(d)
freq = [len(list(group))/len(deg) for key, group in groupby(deg)]
deg = np.unique(deg)
plt.loglog(deg,freq,'o')
plt.loglog(np.arange(2,21,1),p,'--');

## igraph has a power law graph generator given n and m
g = ig.Graph.Static_Power_Law(n=50,m=150,exponent_out=2.5)
g.vs['size']=10
ly = g.layout("kk")  ## kamada-kawai; usually nice
ig.plot(g, layout=ly, bbox=(0,0,350,250))<jupyter_output><empty_output><jupyter_text>Chung-Lu Model<jupyter_code>## we can also use the CL model
def CL(d):
    m = np.sum(d)//2
    n = len(d)
    s = np.sum(d)
    p = [i/s for i in d]
    g = ig.Graph.Erdos_Renyi(n=n, m=0)
    while g.ecount() < m:
        e = np.random.choice(n, size=2, replace=False, p=p)
        g.add_edge(e[0],e[1])
        g.simplify()
    return g
# gamma = -2
p = powerLawDist(xmin=2,xmax=20)
d = powerLawSample(n=50,xmin=2,xmax=20)
g = CL(d)
g.vs['size']=10
ly = g.layout("kk")  ## kamada-kawai; usually nice
ig.plot(g, layout=ly, bbox=(0,0,350,250))<jupyter_output><empty_output><jupyter_text>Barabasi-Albert Graph<jupyter_code>g = ig.Graph.Barabasi(50,2)
g.vs['size']=10
ly = g.layout("kk")  ## kamada-kawai; usually nice
ig.plot(g, layout=ly, bbox=(0,0,350,250))
d = g.degree()
deg = np.sort(d)
freq = [len(list(group))/len(deg) for key, group in groupby(deg)]
deg = np.unique(deg)
plt.xlabel('degree', fontsize=14)
plt.bar(deg,freq);

## log-log binned plot for a larger graph
g = ig.Graph.Barabasi(5000,3)
d = g.degree()
deg = np.sort(d)
freq = [len(list(group))/len(deg) for key, group in groupby(deg)]
bins = np.logspace(np.log10(np.min(deg)), np.log10(np.max(deg)), num=15)
widths = (bins[1:] - bins[:-1])
hist = np.histogram(deg, bins=bins)
Hist = hist[0]/widths
Bins = bins[:-1]
# plot it!
X = [Bins[i] for i in range(len(Hist)) if Hist[i]!=0]
Y = [Hist[i] for i in range(len(Hist)) if Hist[i]!=0]
plt.loglog(X,Y,'o');

## add regression fit
x = np.array([np.log10(i) for i in X]).reshape(-1,1)
y = [np.log10(i) for i in Y]
# Create linear regression object
from sklearn.linear_model import LinearRegression
regr = LinearRegression()
rf = regr.fit(x,y)
pred = [10**i for i in rf.predict(x)]
print('fitted gamma:',rf.coef_[0])
plt.loglog(X, Y, 'o') ## as above
plt.loglog(X, pred) ## fitted
plt.xlabel('Degree', fontsize=14)
plt.ylabel('Frequency',fontsize=14);

## path length vs ER graph
g = ig.Graph.Barabasi(500,2)
m = g.ecount()
print('BA: ',g.average_path_length())
g = ig.Graph.Erdos_Renyi(n=500, m=m)
print('ER:',g.average_path_length())
## look at binned (log) degrees for ER graph
g = ig.Graph.Barabasi(5000,2)
m = g.ecount()
g = ig.Graph.Erdos_Renyi(n=5000,m=m)
## plot (ignore degree==0 nodes)
d = np.sort(g.degree())
deg = [i for i in d if i>0] ## ignore degree-0 nodes
freq = [len(list(group))/len(deg) for key, group in groupby(deg)]
bins=np.logspace(np.log10(np.min(deg)), np.log10(np.max(deg)), num=15)
widths = (bins[1:] - bins[:-1])
hist = np.histogram(deg, bins=bins)
hist_norm = hist[0]/widths
# plot it!
plt.loglog(bins[:-1], hist_norm, 'o')
plt.xlabel('Degree', fontsize=14)
plt.ylabel('Frequency',fontsize=14);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20393258426966293, 'fraction_numerical': 0.04400749063670412, 'mean_word_length': 5.466101694915254, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '115752', 'license': 'MIT', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2311, 'n_words': 448, 'path': 'Notebooks/03.SmallWorld.ipynb', 'repo_name': 'ftheberge/ComplexNetworks2019', 'size': '5340'}