示例#1
0
    np.random.seed(33)
    delhi = Model([
        ModelUnit("Delhi",
                  18_000_000,
                  I0=T_pred[-1],
                  RR0=RR_pred[-1],
                  mobility=0)
    ])
    delhi.run(14, np.zeros((1, 1)))

    t_pred = [
        dates[-1] + pd.Timedelta(days=i) for i in range(len(delhi[0].delta_T))
    ]

    plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
    PlotDevice().title("Delhi: Reproductive Number Estimate").xlabel(
        "Date").ylabel("Rt", rotation=0, labelpad=20)
    plt.show()

    delhi[0].lower_CI[0] = T_CI_lower[-1]
    delhi[0].upper_CI[0] = T_CI_upper[-1]
    print(delhi[0].delta_T)
    print(delhi[0].lower_CI)
    print(delhi[0].upper_CI)
    plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts,
                     anomaly_dates, anomalies, CI)
    plt.scatter(t_pred,
                delhi[0].delta_T,
                color="tomato",
                s=4,
示例#2
0
        "dates": dates,
        "RR_pred": RR_pred,
        "RR_CI_upper": RR_CI_upper,
        "RR_CI_lower": RR_CI_lower,
        "T_pred": T_pred,
        "T_CI_upper": T_CI_upper,
        "T_CI_lower": T_CI_lower,
        "total_cases": total_cases[2:],
        "new_cases_ts": new_cases_ts,
    })
    print("  + Rt today:", RR_pred[-1])
    
    plot_RR_est(dates, RR_pred, RR_CI_lower, RR_CI_upper, CI)\
        .ylabel("Estimated $R_t$")\
        .xlabel("Date")\
        .title(state)\
        .size(11, 8)\
        .save(figs/f"Rt_est_{state}.png", dpi=600, bbox_inches="tight")\
        .show()

    plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)\
        .ylabel("Predicted/Observed Cases")\
        .xlabel("Date")\
        .title(state)\
        .size(11, 8)\
        .save(figs/f"T_est_{state}.png", dpi=600, bbox_inches="tight")\
        .show()



    # estimates["anomaly"] = estimates["dates"].isin(set(anomaly_dates))
示例#3
0
state_cases = pd.read_csv(data/"Bihar_cases_data_Jul23.csv", parse_dates=["date_reported"], dayfirst=True)
state_ts = state_cases["date_reported"].value_counts().sort_index()
district_names, population_counts, _ = etl.district_migration_matrix(data/"Migration Matrix - District.csv")
populations = dict(zip(district_names, population_counts))

# first, look at state level predictions
(
    dates,
    RR_pred, RR_CI_upper, RR_CI_lower,
    T_pred, T_CI_upper, T_CI_lower,
    total_cases, new_cases_ts,
    anomalies, anomaly_dates
) = analytical_MPVS(state_ts, CI = CI, smoothing = convolution(window = smoothing)) 

plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI, ymin=0, ymax=4)\
    .title("Bihar: Reproductive Number Estimate Comparisons")\
    .xlabel("Date")\
    .ylabel("Rt", rotation=0, labelpad=20)
plt.ylim(0, 4)

# public data 
paths = { 
    "v3": [data_path(_) for _ in (1, 2)],
    "v4": [data_path(_) for _ in range(3, 13)]
}

for target in paths['v3'] + paths['v4']:
    download_data(data, target)

dfn = load_all_data(
    v3_paths = [data/filepath for filepath in paths['v3']], 
    v4_paths = [data/filepath for filepath in paths['v4']]
示例#4
0
alpha = 3.8
beta = 2.25
vs = 0.999

true_Rt = pd.read_table("./true_Rt.txt", dtype="float", squeeze=True)
obs_cases = pd.read_table("./obs_cases.txt", dtype="float", squeeze=True)

(dates, RR_pred, RR_CI_upper, RR_CI_lower, T_pred, T_CI_upper, T_CI_lower,
 total_cases, new_cases_ts, anomalies, anomaly_dates) = analytical_MPVS(
     obs_cases,
     CI=CI,
     alpha=alpha,
     beta=beta,
     variance_shift=vs,
     smoothing=lambda ts: box_filter(ts, smoothing, smoothing // 2))

print("Rt today:", RR_pred[-1])
print("a, b, vs, MSE:", alpha, beta, vs,
      ((true_Rt.loc[len(true_Rt) - len(RR_pred):] - RR_pred)**2).sum())

plot_RR_est(dates, RR_pred, RR_CI_lower, RR_CI_upper, CI)\
    .ylabel("Estimated $R_t$")\
    .title("Synthetic Data Estimation")\
    .size(11, 8)
plt.plot(true_Rt.index, true_Rt.values, 'k--', label="True $R_t$")
plt.xlim(0, 150)
plt.ylim(0, 2.5)
plt.legend()
# plt.savefig("synthetic_Rt_est.png", dpi=600, bbox_inches="tight")
plt.show()
示例#5
0
def state_checks():
    root = cwd()
    data = root/"data"

    # model details
    gamma      = 0.2
    prevalence = 1

    dfn = get_dataframe()

    states = ["Maharashtra", "Andhra Pradesh", "Tamil Nadu", "Madhya Pradesh", "Punjab", "Gujarat", "Kerala", "Bihar"]

    # first, check reimplementation against known figures
    CI = 0.99
    for state in states: 
        ts = get_time_series(dfn[
            (dfn.date_announced <= "2020-05-08") & 
            (dfn.detected_state == state)
        ])
        (
            dates,
            RR_pred, RR_CI_upper, RR_CI_lower,
            T_pred, T_CI_upper, T_CI_lower,
            total_cases, new_cases_ts,
            anomalies, anomaly_date
        ) = analytical_MPVS(ts.Hospitalized, CI = CI)
        plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
        plt.title(f"{state}: Estimated $R_t$", loc="left", fontsize=20)
        plt.figure()
        plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)
        plt.title(f"{state}: Predicted Cases", loc="left", fontsize=20)

        plt.show()

    # generate smoothing checks for national time series
    ts = get_time_series(dfn)

    CI = 0.95
    # 1: box filter
    RRfig, RRax = plt.subplots(3, 1, sharex=True)
    T_fig, T_ax = plt.subplots(3, 1, sharex=True)
    for (i, n) in enumerate((5, 10, 15)): 
        (
            dates, 
            RR_pred, RR_CI_upper, RR_CI_lower, 
            T_pred, T_CI_upper, T_CI_lower, 
            total_cases, new_cases_ts, 
            anomalies, anomaly_dates
        ) = analytical_MPVS(ts.Hospitalized, CI = CI, smoothing = lambda ts: box_filter(ts, n, None))
        
        plt.sca(RRax[i])
        plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
        plt.title(f"smoothing window: {n}, no local smoothing", loc = "left", fontsize = 16)
        
        plt.sca(T_ax[i])
        plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)
        plt.title(f"smoothing window: {n}, no local smoothing", loc = "left", fontsize = 16)
    
    plt.figure(RRfig.number)
    RRfig.tight_layout()
    plt.suptitle("Estimated Rt (box filter convolution)", fontsize = 20)
    plt.figure(T_fig.number)
    T_fig.tight_layout()
    plt.suptitle("Estimated Daily Cases (box filter convolution)", fontsize = 20)
    plt.show()


    # 2: box filter, local smooothing
    RRfig, RRax = plt.subplots(3, 1, sharex=True)
    T_fig, T_ax = plt.subplots(3, 1, sharex=True)
    for (i, n) in enumerate((5, 10, 15)): 
        s = n//2 + 1
        (
            dates, 
            RR_pred, RR_CI_upper, RR_CI_lower, 
            T_pred, T_CI_upper, T_CI_lower, 
            total_cases, new_cases_ts, 
            anomalies, anomaly_dates
        ) = analytical_MPVS(ts.Hospitalized, CI = CI, smoothing = lambda ts: box_filter(ts, n, s))
        
        plt.sca(RRax[i])
        plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI)
        plt.title(f"smoothing window: {n}, local smoothing: last {s} points", loc = "left", fontsize = 16)
        
        plt.sca(T_ax[i])
        plot_T_anomalies(dates, T_pred, T_CI_upper, T_CI_lower, new_cases_ts, anomaly_dates, anomalies, CI)
        plt.title(f"smoothing window: {n}, local smoothing: last {s} points", loc = "left", fontsize = 16)
    
    plt.figure(RRfig.number)
    RRfig.tight_layout()
    plt.suptitle("Estimated Rt (box filter convolution, local smoothing)", fontsize = 20)
    plt.subplots_adjust(right = 0.9)

    plt.figure(T_fig.number)
    T_fig.tight_layout()
    plt.suptitle("Estimated Daily Cases (box filter convolution, local smoothing)", fontsize = 20)
    plt.subplots_adjust(right = 0.9)
    
    plt.show()