def write_expected_deg(G, n, d_mean, D, times): # for obs_degrees, # key = degree # value = freq print "iterating" for i in xrange(times): model.iterate(G) print "storing degree informations" obs_degrees = {} for x in G.nodes_iter(): if G.degree(x) in obs_degrees: obs_degrees[G.degree(x)] += 1 else: obs_degrees[G.degree(x)] = 1 outstring = "degree\tfrequency\n" for key in obs_degrees.iterkeys(): outstring += "%s\t%d\n" % (key, obs_degrees[key]) # save data print "saving data" outfile = open("observed_degree_{0}n_{1}k_{2}D.dat".format(n, d_mean, D), "w") outfile.write(outstring) outfile.close()
def write_expected_deg(G, n, d_mean, D, times): #for obs_degrees, #key = degree #value = freq print "iterating" for i in xrange(times): model.iterate(G) print "storing degree informations" obs_degrees = {} for x in G.nodes_iter(): if G.degree(x) in obs_degrees: obs_degrees[G.degree(x)] += 1 else: obs_degrees[G.degree(x)] = 1 outstring = "degree\tfrequency\n" for key in obs_degrees.iterkeys(): outstring += "%s\t%d\n" % (key, obs_degrees[key]) #save data print "saving data" outfile = open('observed_degree_{0}n_{1}k_{2}D.dat'.format(n, d_mean, D), 'w') outfile.write(outstring) outfile.close()
def write_edge_existence(G, iterations, times, n, d_mean, D, sample_rate, throw_away): #put this first so that the "Exiting..." occurs before #any time is spent on lots of computing if 'c' in G.__dict__: edge_expected_freq = expected_edge_freq(G) else: print "G.c not initalized. Exiting..." return #edge_obs_freq keeps track of how often a specific edge appears #key = edge #value = frequency edge_obs_freq = {} print "Collecting data on edge existence" for i in xrange(times): if i % (times / 10) == 0: print "%d percent" % (100 * i / times) #Method 1 remake_graph(G) #Method 2 #model.reconstruct(G, d_mean) number_of_samples = 0 for j in xrange(iterations + throw_away): if j >= throw_away and j % sample_rate == 0: number_of_samples += 1 for edge in G.edges_iter(): if edge in edge_obs_freq: edge_obs_freq[edge] += 1 else: edge_obs_freq[edge] = 1 model.iterate(G) #normalize observed frequencies for key in edge_obs_freq.iterkeys(): edge_obs_freq[key] = float( edge_obs_freq[key]) / (number_of_samples * times) #save the data outstring = "edge\tdistance\tobs_freq\texp_freq\n" for key in edge_obs_freq.iterkeys(): outstring += "%s\t%f\t%f\t%f\n" % (key, model.length( G, key), edge_obs_freq[key], edge_expected_freq[key]) outfile = open( 'edgeExistence_{0}n_{1}k_{2}d_{3}times_{4}iterations.dat'.format( n, d_mean, D, times, iterations), 'w') outfile.write(outstring) outfile.close()
def write_edge_existence(G, iterations, times, n, d_mean, D, sample_rate, throw_away): #put this first so that the "Exiting..." occurs before #any time is spent on lots of computing if 'c' in G.__dict__: edge_expected_freq = expected_edge_freq(G) else: print "G.c not initalized. Exiting..." return #edge_obs_freq keeps track of how often a specific edge appears #key = edge #value = frequency edge_obs_freq = {} print "Collecting data on edge existence" for i in xrange(times): if i % (times/10) == 0: print "%d percent" % (100 * i / times) #Method 1 remake_graph(G) #Method 2 #model.reconstruct(G, d_mean) number_of_samples = 0 for j in xrange(iterations + throw_away): if j >= throw_away and j % sample_rate == 0: number_of_samples += 1 for edge in G.edges_iter(): if edge in edge_obs_freq: edge_obs_freq[edge] += 1 else: edge_obs_freq[edge] = 1 model.iterate(G) #normalize observed frequencies for key in edge_obs_freq.iterkeys(): edge_obs_freq[key] = float(edge_obs_freq[key]) / (number_of_samples*times) #save the data outstring = "edge\tdistance\tobs_freq\texp_freq\n" for key in edge_obs_freq.iterkeys(): outstring += "%s\t%f\t%f\t%f\n" % (key, model.length(G, key), edge_obs_freq[key], edge_expected_freq[key]) outfile = open('edgeExistence_{0}n_{1}k_{2}d_{3}times_{4}iterations.dat'.format(n, d_mean, D, times, iterations), 'w') outfile.write(outstring) outfile.close()
times = 5*10**4 stats_size = 1 alpha = 2 outstring = 'N\tMean Pair-Wise Distance\n' print 'Working on ', print 'opPairwise_{0}k_{1}D_{2}alpha_{3}min_{4}max_{5}times_{6}size.dat'.format(k, D, alpha, n_min, n_max, times, stats_size) print 'N\tMean Pair-Wise Distance' for n in xrange(n_min, n_max + n_step, n_step): print "Starting n = %d" % n pairwise_total = 0 for j in xrange(stats_size): G = model.make_opinion_graph(n, .5 * n * k, D, alpha) for i in xrange(n*times): model.iterate(G) giant_component = nx.connected_component_subgraphs(G)[0] iter_pw_distance = 0 #diameter may not be diameter for vertex_one in giant_component.nodes(): for vertex_two in giant_component.nodes(): if vertex_one == vertex_two: break iter_pw_distance += nx.shortest_path_length( giant_component, vertex_one, vertex_two) buf = giant_component.number_of_nodes() pairs = buf * (buf - 1) / 2 pairwise_total += float(iter_pw_distance) / pairs
k = 4 D = 1 times = 1*10**3 stats_size = 50 alpha = 2 outstring = 'N\tDiameter\n' print 'N\tDiameter' for n in xrange(n_min, n_max + n_step, n_step): print "Starting n = %d" % n diam_total = 0 for j in xrange(stats_size): G = model.make_opinion_graph(n, .5 * n * k, D, alpha) for i in xrange(n*times): model.iterate(G) subgraphs = nx.connected_component_subgraphs(G) max_diameter = 0 #diameter may not be diameter for SG in subgraphs: #of largest connected subgraph if SG.number_of_nodes() < 2: break diameter = nx.diameter(SG) if diameter > max_diameter: max_diameter = diameter diam_total += max_diameter outstring += '{0}\t{1}\n'.format(n, diam_total / float(stats_size)) print '{0}\t{1}'.format(n, diam_total / float(stats_size))