<jupyter_start><jupyter_text>Spectral Density PlotsWe're going to get histogram (density) plots of the spectral of various graphs we compare in Section 3.  Single Model For defense slides  Uncorrelated<jupyter_code>n = 100
m = 6
p = (n-m)*m / (n*(n-1)/2) # so that volume of ER and BA graphs match

evals_ER = []

for _ in range(1000):

    G1 = nx.erdos_renyi_graph(n,p)

    A1 = nx.adjacency_matrix(G1)

    evals1,evecs1 = eigs(A1)
    
    evals_ER += list(evals1)
2*np.sqrt(n*p*(1-p))
plt.figure(figsize=(6,4))
plt.hist(evals_ER,bins=100,alpha=0.5,normed=True,label='Uncorrelated');
# plt.xlim([0,50]);
plt.ylabel('Frequency (Normalized)',color='k');
plt.xlabel('Adjacency Spectrum',color='k')
plt.xticks(color='k');
plt.yticks(color='k');
# plt.legend();

fig = plt.gcf();
fig.savefig('/Users/peterwills/Desktop/ER_spectrum.pdf',dpi=300,bbox_inches='tight');
n = 100
m = 6
p = (n-m)*m / (n*(n-1)/2) # so that volume of ER and BA graphs match

evals_ER = []

for _ in range(1000):

    G1 = nx.barabasi_albert_graph(n,m)

    A1 = nx.adjacency_matrix(G1)

    evals1,evecs1 = eigs(A1)
    
    evals_ER += list(evals1)
plt.figure(figsize=(6,4))
plt.hist(evals_ER,bins=100,alpha=0.5,normed=True,label='Uncorrelated');
# plt.xlim([0,50]);
plt.ylabel('Frequency (Normalized)',color='k');
plt.xlabel('Adjacency Spectrum',color='k')
plt.xticks(color='k');
plt.yticks(color='k');
# plt.legend();

fig = plt.gcf();
fig.savefig('/Users/peterwills/Desktop/PA_spectrum.pdf',dpi=300,bbox_inches='tight');
k = 4
n = 100
p = 0.0404 # to match watts strogatz
beta = 0.1

evals_WS = []
evals_WS0 = []

for _ in range(1000):

    G1 = nx.connected_watts_strogatz_graph(n,k,0)
    G2 = nx.connected_watts_strogatz_graph(n,k,beta)

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]

    evals1,evecs1 = eigs(A1)
    evals2,evecs2 = eigs(A2)

    evals_WS0 += list(evals1)
    evals_WS += list(evals2)
plt.rc('text', usetex=True)

plt.figure(figsize=(6,4))
_,bins,_ = plt.hist(evals_WS,bins=100,alpha=0.5,normed=True,label=r'$\beta=0.1$');
plt.hist(evals_WS0,bins=bins,alpha=0.5,normed=True,label=r'$\beta=0$ ');
# plt.xlim([-5,6])
plt.xlabel('Adjacency Spectrum',color='k');
plt.ylabel('Frequency (Normalized)',color='k');
plt.xticks(color='k');
plt.yticks(color='k');
plt.legend();

fig = plt.gcf();
fig.savefig('/Users/peterwills/Desktop/ws_spectrum.pdf',dpi=300,bbox_inches='tight');<jupyter_output><empty_output><jupyter_text>Preferential Attachment<jupyter_code>n = 100
m = 6
p = (n-m)*m / (n*(n-1)/2) # so that volume of ER and BA graphs match

evals_PA = []
evals_ER = []

for _ in range(1000):

    G1 = nx.erdos_renyi_graph(n,p)
    G2 = nx.barabasi_albert_graph(n,m)

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]
    L1,L2 = [nc.laplacian_matrix(A) for A in [A1,A2]]

    evals1,evecs1 = eigs(L1)
    evals2,evecs2 = eigs(L2)
    
    evals_ER += list(evals1[1:])
    evals_PA += list(evals2[1:])
plt.figure(figsize=(6,4))
vals,bins,_ = plt.hist(evals_PA,bins=100,alpha=0.5,normed=True,label='Preferential Attachment');
plt.hist(evals_ER,bins=bins,alpha=0.5,normed=True,label='Uncorrelated');
plt.xlim([0,50]);
plt.xlabel('Laplacian Spectrum',color='k');
plt.ylabel('Frequency (Normalized)',color='k');
plt.xticks(color='k');
plt.yticks(color='k');
plt.legend();

fig = plt.gcf();
fig.savefig(os.path.join(figures_dir,'pa_spectrum.pdf'),dpi=300,bbox_inches='tight');<jupyter_output><empty_output><jupyter_text>Watts-Strogatz This one is weird, because we need to get the bins from the histogram we want to appear second.<jupyter_code>k = 4
n = 100
p = 0.0404 # to match watts strogatz
beta = 0.1

evals_WS = []
evals_ER = []

for _ in range(1000):

    G1 = nx.erdos_renyi_graph(n,p)
    G2 = nx.connected_watts_strogatz_graph(n,k,beta)

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]

    evals1,evecs1 = eigs(A1)
    evals2,evecs2 = eigs(A2)
    
    evals_ER += list(evals1)
    evals_WS += list(evals2)
plt.figure()
_,bins,_ = plt.hist(evals_ER,bins=100,alpha=0.5,normed=True,label='Uncorrelated',color=colors[1]);

plt.figure(figsize=(6,4))
plt.hist(evals_WS,bins=bins,alpha=0.5,normed=True,label='Watts-Strogatz');
plt.hist(evals_ER,bins=bins,alpha=0.5,normed=True,label='Uncorrelated');
plt.xlim([-5,6])
plt.xlabel('Adjacency Spectrum',color='k');
plt.ylabel('Frequency (Normalized)',color='k');
plt.xticks(color='k');
plt.yticks(color='k');
plt.legend();

fig = plt.gcf();
fig.savefig(os.path.join(figures_dir,'ws_spectrum.pdf'),dpi=300,bbox_inches='tight');<jupyter_output><empty_output><jupyter_text>Lattice<jupyter_code>n = 100
N = 10
M = 10

G2 = nx.grid_2d_graph(N,M)
deg_seq = [item[1] for item in G2.degree_iter()]

evals_L = []
evals_ER = []

for _ in range(1000):

    G1 = nx.random_degree_sequence_graph(deg_seq)
    # G2 is deterministic

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]

    evals1,evecs1 = eigs(A1)
    evals2,evecs2 = eigs(A2) # lazy
    
    evals_ER += list(evals1)
    evals_L += list(evals2)
plt.figure(figsize=(6,4))
_,bins,_ = plt.hist(evals_L,bins=100,alpha=0.5,normed=True,label='Lattice');
plt.hist(evals_ER,bins=bins,alpha=0.5,normed=True,label='Rand. Deg. Seq.');
plt.xlabel('Adjacency Spectrum',color='k');
plt.ylabel('Frequency (Normalized)',color='k');
plt.xticks(color='k');
plt.yticks(color='k');
plt.legend();

fig = plt.gcf();
fig.savefig(os.path.join(figures_dir,'lattice_spectrum.pdf'),dpi=300,bbox_inches='tight');<jupyter_output><empty_output><jupyter_text>SBM<jupyter_code>n = 100
p = 0.12

# SBM parameters, must average to p so that we have same volume as ER graph
# (note there are 2 partitions of equal size).
pp = 1.9*p
qq = 0.1*p

evals_SBM = []
evals_ER = []

for _ in range(1000):

    G1 = nx.erdos_renyi_graph(n,p)
    G2 = nx.planted_partition_graph(2,n//2,pp,qq)

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]

    evals1,evecs1 = eigs(A1)
    evals2,evecs2 = eigs(A2)
    
    evals_ER += list(evals1)
    evals_SBM += list(evals2)
plt.figure(figsize=(6,4))
_,bins,_ = plt.hist(evals_SBM,bins=100,alpha=0.5,normed=True,label='SBM');
plt.hist(evals_ER,bins=bins,alpha=0.5,normed=True,label='Uncorrelated');
plt.xlim([-7,15])
plt.xlabel('Adjacency Spectrum',color='k');
plt.ylabel('Frequency (Normalized)',color='k');
plt.xticks(color='k');
plt.yticks(color='k');
plt.legend();

fig = plt.gcf();
fig.savefig(os.path.join(figures_dir,'sbm_spectrum.pdf'),dpi=300,bbox_inches='tight');
fig.savefig('/Users/peterwills/Desktop/sbm_spectrum.pdf',dpi=300,bbox_inches='tight');<jupyter_output><empty_output><jupyter_text>Random stuff<jupyter_code>diff = np.array(evals_SBM).reshape(-1,100) - np.array(evals_ER).reshape(-1,100)
diff = diff[:,::-1]
k = 5
diff = diff[:,:k]
plt.plot(diff.mean(axis=0) + diff.std(axis=0))
plt.plot(diff.mean(axis=0) - diff.std(axis=0))
plt.plot(diff.mean(axis=0))<jupyter_output><empty_output><jupyter_text>Others, for fun  SBM Laplacian & Normalized Laplacian<jupyter_code>n = 100
p = 0.12

# SBM parameters, must average to p so that we have same volume as ER graph
# (note there are 2 partitions of equal size).
pp = 1.9*p
qq = 0.1*p

evals_SBM = []
evals_ER = []

for _ in range(1000):

    G1 = nx.erdos_renyi_graph(n,p)
    G2 = nx.planted_partition_graph(2,n//2,pp,qq)

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]
    L1,L2 = [nc.laplacian_matrix(A) for A in [A1,A2]]

    evals1,evecs1 = eigs(L1)
    evals2,evecs2 = eigs(L2)
    
    evals_ER += list(evals1[1:])
    evals_SBM += list(evals2[1:])
plt.figure(figsize=(6,4))
_,bins,_ = plt.hist(evals_SBM,bins=100,alpha=0.5,normed=True,label='SBM');
plt.hist(evals_ER,bins=bins,alpha=0.5,normed=True,label='Uncorrelated');
# plt.xlim([-7,15])
plt.xlabel('Laplacian Spectrum');
plt.ylabel('Frequency (Normalized)');
plt.legend();

# fig = plt.gcf();
# fig.savefig(os.path.join(figures_dir,'sbm_spectrum.pdf'),dpi=300,bbox_inches='tight');
diff = np.array(evals_SBM).reshape(-1,99) - np.array(evals_ER).reshape(-1,99)
plt.plot(diff.mean(axis=0) + diff.std(axis=0))
plt.plot(diff.mean(axis=0) - diff.std(axis=0))
plt.plot(diff.mean(axis=0))
k = 5
diff = diff[:,:k]
plt.plot(diff.mean(axis=0) + diff.std(axis=0))
plt.plot(diff.mean(axis=0) - diff.std(axis=0))
plt.plot(diff.mean(axis=0))<jupyter_output><empty_output><jupyter_text>Normalized Laplacian<jupyter_code>n = 100
p = 0.12

# SBM parameters, must average to p so that we have same volume as ER graph
# (note there are 2 partitions of equal size).
pp = 1.9*p
qq = 0.1*p

evals_SBM = []
evals_ER = []

for _ in range(1000):

    G1 = nx.erdos_renyi_graph(n,p)
    G2 = nx.planted_partition_graph(2,n//2,pp,qq)

    A1,A2 = [nx.adjacency_matrix(G) for G in [G1,G2]]
    L1,L2 = [nc.laplacian_matrix(A,normalized=True) for A in [A1,A2]]

    evals1,evecs1 = eigs(L1)
    evals2,evecs2 = eigs(L2)
    
    evals_ER += list(evals1[1:])
    evals_SBM += list(evals2[1:])
plt.figure(figsize=(6,4))
_,bins,_ = plt.hist(evals_SBM,bins=100,alpha=0.5,normed=True,label='SBM');
plt.hist(evals_ER,bins=bins,alpha=0.5,normed=True,label='Uncorrelated');
# plt.xlim([-7,15])
plt.xlabel('Normalized Laplacian Spectrum');
plt.ylabel('Frequency (Normalized)');
plt.legend();
diff = np.array(evals_SBM).reshape(-1,99) - np.array(evals_ER).reshape(-1,99)
plt.plot(diff.mean(axis=0) + diff.std(axis=0));
plt.plot(diff.mean(axis=0) - diff.std(axis=0));
plt.plot(diff.mean(axis=0));
k = 5
diff = diff[:,:k]
plt.plot(diff.mean(axis=0) + diff.std(axis=0));
plt.plot(diff.mean(axis=0) - diff.std(axis=0));
plt.plot(diff.mean(axis=0));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19148038802193168, 'fraction_numerical': 0.04681568958245466, 'mean_word_length': 5.559474412171507, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '198118', 'license': 'MIT', 'n_tokens_mistral': 4557, 'n_tokens_neox': 4197, 'n_words': 611, 'path': 'spectral_density.ipynb', 'repo_name': 'peterewills/graph_metrics_paper_code', 'size': '9484'}