def SIS_process(G, degree_prob, tmax, tau, gamma): N = G.order() plt.figure(5) plt.clf() plt.figure(6) plt.clf() for index, starting_node in enumerate([x * N / 10. for x in range(10)]): plt.figure(5) t, S, I = EoN.fast_SIS(G, tau, gamma, initial_infecteds=[starting_node], tmax=tmax) #print(I[-1]) subt = scipy.linspace(0, tmax, 501) subI = EoN.subsample(subt, t, I) plt.plot(subt, subI) if I[-1] > 100: plt.figure(6) shift = EoN.get_time_shift(t, I, 1000) plt.plot(subt - shift, subI) plt.figure(5) plt.savefig('sw_SIS_epi_N{}_p{}_k{}_tau{}.pdf'.format(N, p, k, tau)) plt.figure(6) plt.savefig('sw_SIS_epi_N{}_p{}_k{}_tau{}_shifted.pdf'.format( N, p, k, tau))
def process_degree_distribution(N, Pk, color, Psi, DPsi, symbol, label, count): report_times = scipy.linspace(0, 30, 3000) sums = 0 * report_times for cnt in range(count): G = generate_network(Pk, N) t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho) plt.plot(t, I * 1. / N, '-', color=color, alpha=0.1, linewidth=1) subsampled_I = EoN.subsample(report_times, t, I) sums += subsampled_I * 1. / N ave = sums / count plt.plot(report_times, ave, color='k') #Do EBCM N = G.order( ) #N is arbitrary, but included because our implementation of EBCM assumes N is given. t, S, I, R = EoN.EBCM_uniform_introduction(N, Psi, DPsi, tau, gamma, rho, tmin=0, tmax=10, tcount=41) plt.plot(t, I / N, symbol, color=color, markeredgecolor='k', label=label) for cnt in range(3): #do 3 highlighted simulations G = generate_network(Pk, N) t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho) plt.plot(t, I * 1. / N, '-', color='k', linewidth=0.1)
def simulate_process(graph_function, iterations, tmax, tcount, rho, kave, tau, gamma, symbol): Isum = scipy.zeros(tcount) report_times = scipy.linspace(0, tmax, tcount) for counter in range(iterations): G = graph_function() t, S, I = EoN.fast_SIS(G, tau, gamma, rho=rho, tmax=tmax) I = EoN.subsample(report_times, t, I) Isum += I plt.plot(report_times, Isum * 1. / (N * iterations), symbol)
def SIR_process(G, degree_prob, tau, gamma, tmax=10): N = G.order() plt.figure(2) plt.clf() plt.figure(3) plt.clf() plt.figure(5) plt.clf() for index, starting_node in enumerate([x * N / 100. for x in range(100)]): plt.figure(2) t, S, I, R = EoN.fast_SIR(G, tau, gamma, initial_infecteds=[starting_node]) subt = scipy.linspace(0, t[-1], 101) subI, subR = EoN.subsample(subt, t, I, R) plt.plot(subt, subI) if R[-1] > 500: plt.figure(3) shift = EoN.get_time_shift(t, R, threshold) plt.plot(subt - shift, subI) plt.figure(5) plt.plot(subt - shift, subR) #t, S, I, R = EoN.EBCM(degree_prob, tau, gamma, rho) rho = 1. / N def psi(x): return sum(degree_prob[k] * x**k for k in degree_prob) def psiPrime(x): return sum(k * degree_prob[k] * x**(k - 1) for k in degree_prob) t, S, I, R = EoN.EBCM_uniform_introduction(N, psi, psiPrime, tau, gamma, rho, tmax=tmax) shift = EoN.get_time_shift(t, R, threshold) plt.figure(2) #plt.savefig('sw_SIR_epi_N{}_p{}_k{}_tau{}.pdf'.format(N,p,k,tau)) plt.figure(3) plt.plot(t - shift, I, '--') plt.xlabel('$t$', fontsize=18) plt.ylabel('$I$', fontsize=18) #plt.set_xtick_labels(fontsize = 15) xmax = get_xmax(t - shift, I) plt.axis(xmax=xmax) plt.savefig('sw_SIR_epi_N{}_p{}_k{}_tau{}_shifted.pdf'.format( N, p, k, tau)) plt.figure(5) plt.plot(t - shift, R, '--')
def process_degree_distribution(Gbig, Gsmall, color, Psi, DPsi, symbol): t, S, I, R = EoN.fast_SIR(Gsmall, tau, gamma, rho=rho) plt.plot(t, I * 1. / Gsmall.order(), ':', color=color) t, S, I, R = EoN.fast_SIR(Gbig, tau, gamma, rho=rho) plt.plot(t, I * 1. / Gbig.order(), color=color) N = Gbig.order( ) #N is arbitrary, but included because our implementation of EBCM assumes N is given. t, S, I, R = EoN.EBCM(N, lambda x: (1 - rho) * Psi(x), lambda x: (1 - rho) * DPsi(x), tau, gamma, 1 - rho) I = EoN.subsample(report_times, t, I) plt.plot(report_times, I / N, symbol, color=color, markeredgecolor='k')
def sim_and_plot(G, tau, gamma, rho, tmax, tcount, ax): t, S, I = EoN.fast_SIS(G, tau, gamma, rho=rho, tmax = tmax) report_times = scipy.linspace(0, tmax, tcount) I = EoN.subsample(report_times, t, I) ax.plot(report_times, I/N, color='grey', linewidth=5, alpha=0.3) t, S, I, = EoN.SIS_heterogeneous_meanfield_from_graph(G, tau, gamma, rho=rho, tmax=tmax, tcount=tcount) ax.plot(t, I/N, '--') t, S, I = EoN.SIS_compact_pairwise_from_graph(G, tau, gamma, rho=rho, tmax=tmax, tcount=tcount) ax.plot(t, I/N) t, S, I = EoN.SIS_homogeneous_pairwise_from_graph(G, tau, gamma, rho=rho, tmax=tmax, tcount=tcount) ax.plot(t, I/N, '-.')
report_times = scipy.linspace(tmin, tmax, 21) #for simulations plt.figure(0) tau = 0.005 G = nx.complete_graph(N) t, S, I = complete_graph_lumped(N, tau, gamma, I0, tmin, tmax, tcount) plt.plot(t, I/N, label = 'Prediction') #now check with simulation obs_I = 0*report_times print("done with complete graph ODE. Now simulating") for counter in range(iterations): IC = random.sample(range(N),I0) t, S, I = EoN.fast_SIS(G, tau, gamma, initial_infecteds = IC, tmax = tmax) obs_I += EoN.subsample(report_times, t, I) plt.plot(report_times, obs_I*1./(iterations*N), 'o', label='Simulation') plt.axis(ymin=0, ymax=1) plt.xlabel('$t$') plt.ylabel('$[I]$') plt.legend() plt.savefig('fig2p11a.png') print("done with complete graph. Now star --- warning, this may be slow")
iterations = 200 tmax = 40 tau_c = gamma/kave rho = 0.05 tcount=1001 report_times = scipy.linspace(0,tmax,tcount) for tau, label in zip([0.9*tau_c, tau_c, 1.1*tau_c, 1.5*tau_c],['a', 'b', 'c', 'd']): plt.clf() Isum = scipy.zeros(len(report_times)) for counter in range(iterations): G = nx.configuration_model([kave]*N) t, S, I = EoN.fast_SIS(G, tau, gamma, tmax=tmax, rho=rho) I=I*1./N I = EoN.subsample(report_times, t, I) Isum += I plt.plot(report_times, Isum/iterations, color = 'grey', linewidth = 5, alpha=0.3) S0 = (1-rho)*N I0 = rho*N t, S, I = EoN.SIS_homogeneous_meanfield(S0, I0, kave, tau, gamma, tmin=0, tmax=tmax, tcount=tcount) plt.plot(t, I/N, '--') S0 = (1-rho)*N I0 = rho*N SI0 = (1-rho)*N*kave*rho SS0 = (1-rho)*N*kave*(1-rho) t, S, I = EoN.SIS_homogeneous_pairwise(S0, I0, SI0, SS0, kave, tau, gamma, tmin = 0,
tau = 1. gamma = 1. colors = [ '#5AB3E6', '#FF2000', '#009A80', '#E69A00', '#CD9AB3', '#0073B3', '#F0E442' ] kave = 5 G = nx.fast_gnp_random_graph(N, kave / (N - 1.)) initial_infecteds = random.sample(range(N), int(0.01 * N)) print('simulating') t, S, I, R = EoN.fast_SIR(G, tau, gamma, initial_infecteds=initial_infecteds) report_times = scipy.linspace(0, 10, 101) S, I, R = EoN.subsample(report_times, t, S, I, R) plt.plot(report_times, S, color=colors[1], label='simulation') plt.plot(report_times, I, color=colors[1]) plt.plot(report_times, R, color=colors[1]) print('doing ODE models') t, S, I, R = EoN.SIR_effective_degree_from_graph( G, tau, gamma, initial_infecteds=initial_infecteds, tmax=10, tcount=51) plt.plot(t, S, color=colors[2], dashes=[6, 6], label='effective degree') plt.plot(t, I, color=colors[2], dashes=[6, 6]) plt.plot(t, R, color=colors[2], dashes=[6, 6]) t, S, I, R = EoN.SIR_heterogeneous_pairwise_from_graph( G, tau, gamma, initial_infecteds=initial_infecteds, tmax=10, tcount=51) plt.plot(t,
def rec_time_fxn(u, K, gamma): duration = 0 for counter in range(K): duration += random.expovariate(K * gamma) return duration def trans_time_fxn(u, v, tau): return random.expovariate(tau) display_ts = scipy.linspace(0, 50, 26) for G, filename in ([regular, 'fig9p2a.png'], [ER, 'fig9p2b.png']): plt.clf() Isum = scipy.zeros(len(display_ts)) for K, symbol in ([1, 's'], [3, 'd']): for counter in range(iterations): t, S, I, R = EoN.fast_nonMarkov_SIR(G, trans_time_fxn=trans_time_fxn, trans_time_args=(tau, ), rec_time_fxn=rec_time_fxn, rec_time_args=(K, gamma), rho=rho) newI = EoN.subsample(display_ts, t, I) Isum += newI Isum /= iterations plt.plot(display_ts, Isum, symbol) plt.xlabel('$t$') plt.ylabel('Prevalence') plt.savefig(filename)