CoCalc Public FilesTESTS / eon-test.ipynbOpen with one click!
Authors: Hal Snyder, ℏal Snyder
Views : 85
Description: Example program from Epidemics on Networks (EoN) package
Compute Environment: Ubuntu 18.04 (Deprecated)
In [1]:
import networkx as nx import matplotlib.pyplot as plt import EoN N=10**5 G=nx.barabasi_albert_graph(N, 5) #create a barabasi-albert graph tmax = 20 iterations = 5 #run 5 simulations tau = 0.1 #transmission rate gamma = 1.0 #recovery rate rho = 0.005 #random fraction initially infected for counter in range(iterations): #run simulations t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho, tmax = tmax) if counter == 0: plt.plot(t, I, color = 'k', alpha=0.3, label='Simulation') plt.plot(t, I, color = 'k', alpha=0.3) #Now compare with ODE predictions. Read in the degree distribution of G #and use rho to initialize the various model equations. #There are versions of these functions that allow you to specify the #initial conditions rather than starting from a graph. #we expect a homogeneous model to perform poorly because the degree #distribution is very heterogeneous t, S, I, R = EoN.SIR_homogeneous_pairwise_from_graph(G, tau, gamma, rho=rho, tmax = tmax) plt.plot(t, I, '-.', label = 'Homogeneous pairwise', linewidth = 5) #meanfield models will generally overestimate SIR growth because they #treat partnerships as constantly changing. t, S, I, R = EoN.SIR_heterogeneous_meanfield_from_graph(G, tau, gamma, rho=rho, tmax=tmax) plt.plot(t, I, ':', label = 'Heterogeneous meanfield', linewidth = 5) #The EBCM model does not account for degree correlations or clustering t, S, I, R = EoN.EBCM_from_graph(G, tau, gamma, rho=rho, tmax = tmax) plt.plot(t, I, '--', label = 'EBCM approximation', linewidth = 5) #the preferential mixing model captures degree correlations. t, S, I, R = EoN.EBCM_pref_mix_from_graph(G, tau, gamma, rho=rho, tmax=tmax) plt.plot(t, I, label = 'Pref mix EBCM', linewidth=5, dashes=[4, 2, 1, 2, 1, 2]) plt.xlabel('$t$') plt.ylabel('Number infected') plt.legend() plt.savefig('SIR_BA_model_vs_sim.png')
In [ ]: