def test_stochastic_well_mixed(self): S, E, I, R = sympy.symbols("S E I R") N = 75000 tmax = 100 model = SymbolicEpiModel([S, E, I, R], N) model.set_processes([ (S, I, 2, E, I), (I, 1, R), (E, 1, I), ]) model.set_initial_conditions({S: N - 100, I: 100}) tt = np.linspace(0, tmax, 10) result_int = model.integrate(tt) t, result_sim = model.simulate(tmax, sampling_dt=1, return_compartments=[S, R]) for c, res in result_sim.items(): #print(c, np.abs(1-res[-1]/result_int[c][-1])) #print(c, np.abs(1-res[-1]/result_sim[c][-1])) assert (np.abs(1 - res[-1] / result_int[c][-1]) < 0.05)
def symbolic_simulation_temporally_forced(): from epipack import SymbolicEpiModel import sympy as sy from epipack.plottools import plot import numpy as np S, I, R, eta, rho, omega, t, T = \ sy.symbols("S I R eta rho omega t T") N = 1000 SIRS = SymbolicEpiModel([S,I,R],N)\ .set_processes([ (S, I, 3+sy.cos(2*sy.pi*t/T), I, I), (I, rho, R), (R, omega, S), ]) SIRS.set_parameter_values({ rho: 1, omega: 1 / 14, T: 100, }) SIRS.set_initial_conditions({S: N - 100, I: 100}) _t = np.linspace(0, 150, 1000) result = SIRS.integrate(_t) t_sim, result_sim = SIRS.simulate(max(_t)) ax = plot(_t, result) plot(t_sim, result_sim, ax=ax) ax.get_figure().savefig('symbolic_model_time_varying_rate.png', dpi=300)
def test_time_dependent_rates(self): B, t = sympy.symbols("B t") epi = SymbolicEpiModel([B]) epi.add_fission_processes([ (B, t, B, B), ]) epi.set_initial_conditions({B: 1}) result = epi.integrate([0, 3], adopt_final_state=True) assert (np.isclose(epi.y0[0], np.exp(3**2 / 2))) epi.set_initial_conditions({B: 1}) result = epi.integrate(np.linspace(0, 3, 10000), integrator='euler', adopt_final_state=True) eul = epi.y0[0] real = np.exp(3**2 / 2) assert (np.abs(1 - eul / real) < 1e-2)