def run_simulation(school: SIR, nonschool: SIR): for _ in range(30): school.run(1) nonschool.forward_epi_step(dB = 2*school.dT[-1]//3) # doesn't preserve population dT_school = np.array(school.dT) dT_nonschool = np.array(nonschool.dT) dT = dT_school + dT_nonschool var_up_school = np.array(school.upper_CI) - dT_school var_dn_school = np.array(school.lower_CI) - dT_school var_up_nonschool = np.array(nonschool.upper_CI) - dT_nonschool var_dn_nonschool = np.array(nonschool.lower_CI) - dT_nonschool dT_CI_l = dT - np.sqrt(var_dn_school**2 + var_dn_nonschool**2) dT_CI_u = dT + np.sqrt(var_up_school**2 + var_up_nonschool**2) return (dT[1:], dT_CI_l[1:], dT_CI_u[1:])
import pymc3 as pm sir_model = SIR("test", population=500000, I0=100, dT0=20, Rt0=1.01, random_seed=0) total_t = 0 schedule = [(1.01, 75), (1.4, 75), (0.9, 75)] R0_timeseries = [] for (R0, t) in schedule: R0_timeseries += [R0] * t sir_model.Rt0 = R0 sir_model.run(t) total_t += t plt.plot(sir_model.dT) plt.show() plt.plot(R0_timeseries, "-", color="black", label="$R_0$") plt.plot(sir_model.Rt, "-", color="dodgerblue", label="$R_t$") plt.legend(framealpha=1, handlelength=1, loc="best") plt.PlotDevice().xlabel("time").ylabel("reproductive rate").adjust(left=0.10, bottom=0.15, right=0.99, top=0.99) plt.ylim(0.5, 1.5) plt.show() # 1: parametric scheme:
totals=False) plt.Rt(dates, Rt_pred[1:], Rt_CI_upper[1:], Rt_CI_lower[1:], CI, ymin=0, ymax=3)\ .title("\nBihar: Reproductive Number Estimate (Covid19India Data)")\ .annotate(f"public data from {str(dates[0]).split()[0]} to {str(dates[-1]).split()[0]}")\ .xlabel("\ndate")\ .ylabel("$R_t$", rotation=0, labelpad=20)\ .show() Bihar = SIR(name="Bihar", population=99_000_000, dT0=T_pred[-1], Rt0=Rt_pred[-1], mobility=0, random_seed=0) Bihar.run(14) plt.daily_cases(dates, T_pred[1:], T_CI_upper[1:], T_CI_lower[1:], new_cases_ts[1:], anomaly_dates, anomalies, CI, prediction_ts = [(Bihar.dT[:-1], Bihar.lower_CI[1:], Bihar.upper_CI[1:], None, "predicted cases")])\ .title("\nBihar: Daily Cases")\ .xlabel("\ndate")\ .ylabel("cases\n")\ .annotate("\nBayesian training process on empirical data, with anomalies identified")\ .show() # now, do district-level estimation smoothing = 10 district_time_series = get_time_series(dfn[dfn.detected_state == "Bihar"], "detected_district").Hospitalized migration = np.zeros((len(district_names), len(district_names))) estimates = []