def test_sir_phg_expon(do_plot=False): N = 1000 reproductive_factor = 2.2 infectious_period_mean = 10 dist = ph_expon(lambd=1 / infectious_period_mean) assert dist.mean() == infectious_period_mean gam = 1 / dist.mean() beta = reproductive_factor * gam sir = sir_classical( name="SIR", population=N, reproductive_factor=reproductive_factor, infectious_period_mean=infectious_period_mean, num_days=160, ) sir_ph_exp = sir_phg(name="SIR-PH-Expo", population=N, beta=beta, infectious_time_distribution=dist, num_days=160) error = print_error(sir, sir_ph_exp) if do_plot: from matplotlib import pyplot as plt from epistoch.utils.plotting import plot_sir fig = plot_sir(sir, title="PH-expo-tests") plot_sir(sir_ph_exp, N, fig, linestyle="--") plt.show() assert 0.0 == pytest.approx(error, abs=1e-2)
def test_sir_phg_erlang(do_plot=False): N = 1000 reproductive_factor = 2.2 infectious_period_mean = 10 n = 3 dist = ph_erlang(n=n, lambd=n / infectious_period_mean) assert dist.mean() == infectious_period_mean gam = 1 / dist.mean() beta = reproductive_factor * gam num_days = 160 sir_g_model = sir_g( "SIR-G", population=N, reproductive_factor=reproductive_factor, infectious_time_distribution=dist, num_days=num_days, num_periods=2000, ) sir_ph_erlang = sir_phg("SIR-PHG", population=N, beta=beta, infectious_time_distribution=dist) error = print_error(sir_g_model, sir_ph_erlang) if do_plot: from matplotlib import pyplot as plt from epistoch.utils.plotting import plot_sir fig = plot_sir(sir_g_model, title="PH-erlang-tests") plot_sir(sir_ph_erlang, fig, linestyle="--") plt.show() assert 0.0 == pytest.approx(error, abs=1e-2)
def compare_models( name, dist, N, I0=1, num_days=100, R0=2.25, do_plots=True, use_latex=False, plot_path=".", plot_ext="pdf" ): name1 = "SIR" SIR_classic = sir_classical( name=name1, population=N, I0=I0, reproductive_factor=R0, infectious_period_mean=dist.mean(), num_days=num_days ) name2 = "SIR-G" SIR_general = sir_g( name=name2, population=N, I0=I0, num_days=num_days, num_periods=2000, reproductive_factor=R0, infectious_time_distribution=dist, method="loss", ) report_summary(SIR_classic) report_summary(SIR_general) print_error(SIR_classic, SIR_general) if do_plots: fig = plot_sir(SIR_classic, title=name + ": SIR and SIR-G models", use_latex=use_latex) plot_sir(SIR_general, fig, linestyle="--") plt.show() filename = os.path.join(plot_path, name + "-SIR-comp." + plot_ext) print(f"Saving picture in file {os.path.abspath(filename)}") fig.savefig(filename, bbox_inches="tight") fig2 = plot_IR(SIR_classic, title=name + ": I, R as function of S") plot_IR(SIR_general, fig2, linestyle="--") plt.show() filename = os.path.join(plot_path, name + "-IR-comp." + plot_ext) print(f"Saving picture in file {os.path.abspath(filename)}") fig2.savefig(filename, bbox_inches="tight") print("Done")
def variance_analysis( N=1000000, I0=1, num_days=100, R0=2.25, infectious_period_mean=4.4, cvs=[0.5, 1.0, 2.0], use_latex=False, plot_path=".", plot_ext="pdf", ): dists = {} models = {} print("Computing Classical") sir_classic = sir_classical( name="SIR", population=N, I0=I0, reproductive_factor=R0, infectious_period_mean=infectious_period_mean, num_days=num_days, ) fig = plot_sir(sir_classic, formats={"I": "r-"}, title="SIR-G with Different Distributions", linewidth=3) print("Computing Constant SIR-G") dist = utils.stats.constant(infectious_period_mean) sir_constant = sir_g( name="Const", population=N, I0=I0, num_days=num_days, num_periods=2000, reproductive_factor=R0, infectious_time_distribution=dist, method="loss", ) fig = plot_sir(sir_constant, fig, formats={"I": "b-."}, linewidth=3, use_latex=use_latex) dists["gamma"] = utils.stats.get_gamma dists["lognorm"] = utils.stats.get_lognorm for cv in cvs: for dist_name, dist_getter in dists.items(): infectious_period_sd = cv * infectious_period_mean dist = dist_getter(infectious_period_mean, infectious_period_sd) mod_name = f"{dist_name}-{cv:.2}" print(f"Running {mod_name}") num_days = round(100 * max(1, cv)) models[(dist, cv)] = sir_g( name=mod_name, population=N, I0=I0, num_days=num_days, num_periods=2000, reproductive_factor=R0, infectious_time_distribution=dist, method="loss", ) # models[(dist, cv)].name = mod_name plot_sir(models[(dist, cv)], fig, formats={"I": ""}, linestyle="--") plt.show() filename = os.path.join(plot_path, "Variance-Analysis." + plot_ext) print(f"Saving picture in file {os.path.abspath(filename)}") fig.savefig(filename, bbox_inches="tight") return models
def test_seird(do_plot=False): N = 1000 beta = 0.2 exposed_time_mean = 1 die_time_mean = 7 recover_time_mean = 12 exposed_time_exp = ph_expon(mean=exposed_time_mean) die_time_exp = ph_expon(mean=die_time_mean) recover_time_exp = ph_expon(mean=recover_time_mean) exposed_time = ph_erlang(n=10, mean=exposed_time_mean) die_time = ph_erlang(n=15, mean=die_time_mean) recover_time = ph_erlang(n=8, mean=recover_time_mean) fatality_rate = 0.2 I0 = 10 num_days = 200 model_expo = seird_ph( "SEIRD-Expo", population=N, beta=beta, exposed_time=exposed_time_exp, time_to_die=die_time_exp, time_to_recover=recover_time_exp, fatality_rate=fatality_rate, I0=I0, num_days=num_days, ) model_gen = seird_ph( "SEIRD-Gen", population=N, beta=beta, exposed_time=exposed_time, time_to_die=die_time, time_to_recover=recover_time, fatality_rate=fatality_rate, I0=I0, num_days=num_days, ) if do_plot: from matplotlib import pyplot as plt from epistoch.utils.plotting import plot_sir formats = { "S": "b-", "E": "c-", "I": "r-", "R": "g-", "Rc": "y-", "D": "m-" } legend_fmt = { "loc": "upper right", "shadow": True, "framealpha": 1.0, "bbox_to_anchor": (1, 1) } report_summary(model_expo) fig = plot_sir(model_expo, title="SEIRD Expo vs Erlang", formats=formats) report_summary(model_gen) fig = plot_sir(model_gen, formats=formats, fig=fig, linestyle=":", legend_fmt=legend_fmt) plt.show() filename = os.path.join("./paper/epistoch/figures/", "SEIRD.pdf") print(f"Saving picture in file {os.path.abspath(filename)}") fig.savefig(filename, bbox_inches="tight")