<jupyter_start><jupyter_text>Graph varianceIn this notebook, we illustrate that the prior variance is generally non-uniform across the vertices (Figure 1 in the paper).<jupyter_code>import networkx as nx
import matplotlib.pyplot as plt
from graph_matern.kernels.graph_matern_kernel import GraphMaternKernel
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42  # for the camera-ready paper
import numpy as np
import scipy
import random
seed = 123
random.seed(seed)
np.random.seed(seed)
num_nodes = 10
cmap = plt.get_cmap('plasma')
def check_eigvals(laplacian, eigvals, eigvecs):
    # (N, N) * (N, M) = (N, M)
    lhs = laplacian.dot(eigvecs) 
    # (M,) * (N, M) = (N, M)
    rhs = eigvals * eigvecs
    
    assert np.allclose(lhs, rhs)
def draw_marginal_variance(G, *args, **kwargs):
    laplacian = nx.laplacian_matrix(G).astype(np.float64)
    eigvals, eigvecs = scipy.sparse.linalg.eigsh(laplacian, k=num_nodes-1)
    
    check_eigvals(laplacian, eigvals, eigvecs)

    kernel = GraphMaternKernel((eigvecs, eigvals), nu=1.5, kappa=1.)

    node_ids = np.arange(num_nodes)[:, None]
    variance = kernel.K_diag(node_ids).numpy()
    variance /= np.mean(variance)  # normalize the variance by the mean, this basically amounts to a specific scaling constant
    
    vmax = variance.max()
    vmin = variance.min()
    if np.isclose(vmin, vmax):
        vmin = 0.0

    nx.draw(G, *args, cmap=cmap, node_color=variance, vmin=vmin, vmax=vmax, **kwargs)
    
    sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin = vmin, vmax=vmax))
    cbar = plt.colorbar(sm)
    # cbar.ax.tick_params(labelsize=25)
G = nx.complete_graph(num_nodes)

draw_marginal_variance(G, pos=nx.circular_layout(G))
G = nx.star_graph(num_nodes-1)
draw_marginal_variance(G)
G = nx.random_regular_graph(3, num_nodes)
draw_marginal_variance(G)
G = nx.barabasi_albert_graph(num_nodes, 3)
draw_marginal_variance(G)
prob = 0.5
fig = plt.figure(figsize=(20, 10))
gnp_graphs = []
for i in range(10):
    plt.subplot(2, 5, i+1)
    G = nx.gnp_random_graph(num_nodes, prob)
    gnp_graphs.append(G)
    draw_marginal_variance(G, pos=nx.spring_layout(G, seed=666))
fig = plt.figure(figsize=(20, 10))
barabasi_graphs = []
for i in range(10):
    plt.subplot(2, 5, i+1)
    G = nx.barabasi_albert_graph(num_nodes, 5)
    barabasi_graphs.append(G)
    draw_marginal_variance(G, pos=nx.spring_layout(G, seed=666))<jupyter_output><empty_output><jupyter_text>The next two graphs are in the paper.<jupyter_code>draw_marginal_variance(gnp_graphs[0], pos=nx.spring_layout(gnp_graphs[0], seed=666))
sd = 222
random.seed(sd)
np.random.seed(sd)
G = nx.barabasi_albert_graph(num_nodes, 5)
draw_marginal_variance(G, pos=nx.spring_layout(G, seed=666))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12813978886057517, 'fraction_numerical': 0.02184200946487077, 'mean_word_length': 4.935205183585313, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '287420', 'license': 'MIT', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1114, 'n_words': 214, 'path': 'examples/graph_variance.ipynb', 'repo_name': 'thomaspinder/Graph-Gaussian-Processes', 'size': '2747'}