Last active
August 29, 2015 14:08
-
-
Save sangheestyle/111ddf2d3a1b6d7158db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import scipy as sc | |
import networkx as nx | |
from matplotlib.colors import LogNorm | |
from pylab import * | |
def sample(n): | |
m = sc.misc.comb(n, 2) | |
r_vals = np.random.random(m) | |
w_vals = [-log(1-r) for r in r_vals] | |
return w_vals | |
def calc_s(n, t): | |
a = np.zeros((n,n)) | |
il = np.tril_indices(n, -1) | |
a[il] = [1 if w>=t else 0 for w in sample(n)] | |
g = nx.from_numpy_matrix(a) | |
s = len(nx.connected_components(g).next()) | |
return s | |
n = 500 | |
t_min = 0 | |
t_max = 13 | |
n_slices = 50 | |
n_iter = 30 | |
x = [] | |
y = [] | |
for t in np.linspace(t_min, t_max, n_slices): | |
print "thresholder:", t | |
for i in range(n_iter): | |
s = calc_s(n, t) | |
y.append(float(s)/float(n)) | |
x.append(t) | |
hist2d(x, y, bins=40, norm=LogNorm()) | |
colorbar() | |
xlabel('Threshold') | |
ylabel('s/n') | |
savefig('ps5-1b.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment