Skip to content

Instantly share code, notes, and snippets.

@jg-you
Created August 25, 2016 21:02
Show Gist options
  • Save jg-you/104161fd3017cca58776b8f8ac83b50d to your computer and use it in GitHub Desktop.
Save jg-you/104161fd3017cca58776b8f8ac83b50d to your computer and use it in GitHub Desktop.
Draw graphon Stochastic Block Models
import matplotlibt.pyplot as plt
import numpy as np
plt.figure(figsize=(5,4))
X, Y = np.meshgrid(np.linspace(0,1), np.linspace(0,1))
plt.pcolormesh(X,Y,graphon_val(X,Y,p,n))
plt.colorbar()
import numpy as np
# Define vectorized function which returns the value of the graphon
# at point (x,y) given the stochastic block model of probability matrix
# p and size vector n
def graphon_val(x, y, p, n):
ns = np.sum(n)
ncs = np.cumsum(n)
nx = x * ns
ny = y * ns
ix = 0
iy = 0
while nx > ncs[ix]:
ix += 1
while ny > ncs[iy]:
iy += 1
return p[ix, iy]
graphon_val = np.vectorize(graphon_val, excluded=(2,3))
@jg-you
Copy link
Author

jg-you commented Aug 25, 2016

Example with

p = np.array([[0.12, 0.10, 0.10, 0.05, 0.05, 0.00],
              [0.10, 0.12, 0.10, 0.00, 0.05, 0.05],
              [0.10, 0.10, 0.12, 0.05, 0.00, 0.05],
              [0.05, 0.00, 0.05, 0.60, 0.00, 0.00],
              [0.05, 0.05, 0.00, 0.00, 0.60, 0.00],
              [0.00, 0.05, 0.05, 0.00, 0.00, 0.60]])
n = [50, 50, 50, 100, 200, 200]

download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment