# Refit the semivariograms and the LMC using only the counties still observed
def fit_from(idx_by_var):
dv, cv, shared = empirical_variograms(idx_by_var)
P = len(idx_by_var)
pairs = [(a, b) for a in range(P) for b in range(a + 1, P)]
lmc = mpk.fit_lmc(dv, cv, pairs, P, 300, model="Exp", fit_range=True, nugget=True, range_grid=RANGE_GRID)
return lmc, shared
# Predict the hidden counties. Obesity and inactivity can still be observed there.
def predict_hidden(hidden, idx_by_var, lmc, shared, P, k=K):
obs_idx = [idx_by_var[0]] + list(idx_by_var[1:P])
ds = datasets(P, target_idx=obs_idx[0], aux_idx=obs_idx[1:])
r = mpk.poisson_cokrige(ds, 0, sub_lmc(lmc, P), pop_rate=PR, shared_mean=shared,
coords_pred=XY[hidden], number_of_neighbors=k, mean_rate="global", use_gpu=USE_GPU)
return r["pred"], r["var"]
# Random masks, the paper's 35/70/100 design, and four whole-state holdouts
rng = np.random.default_rng(SEED)
pred_rows, keep_preds = [], {}
t0 = time.time()
designs = [(f"random {int(100 * f)}% hidden", f) for f in (0.30, 0.50, 0.65)] + [("paper: 35 / 70 / 100%", None)]
for name, frac in designs:
for rep in range(N_REPS):
# Hide a random share of diabetes counts. Obesity and inactivity stay observed everywhere.
if frac is not None:
hidden = np.sort(rng.choice(len(gdf), int(round(frac * len(gdf))), replace=False))
obs_t = np.setdiff1d(ALL, hidden)
idx_by_var = [obs_t, ALL, ALL]
else:
# Paper design: diabetes in 35% of counties, obesity in 70%, inactivity in every county
perm = rng.permutation(len(gdf))
obs_t = np.sort(perm[: int(0.35 * len(gdf))])
obs_o = np.sort(perm[: int(0.70 * len(gdf))])
hidden = np.setdiff1d(ALL, obs_t)
idx_by_var = [obs_t, obs_o, ALL]
lmc_r, shared_r = fit_from(idx_by_var)
for P, est in [(1, "PK"), (2, "PCK-2"), (3, "PCK-3")]:
pr_, va_ = predict_hidden(hidden, idx_by_var, lmc_r, shared_r, P)
pred_rows.append({"design": name, "rep": rep, "estimator": est, "range": lmc_r.range,
**scores(pr_, va_, RATE["diab"][hidden])})
# Hide diabetes for one whole state and predict it from the rest of the country
for st in HOLDOUT_STATES:
hidden = np.flatnonzero(gdf["state"].to_numpy() == st)
obs_t = np.setdiff1d(ALL, hidden)
idx_by_var = [obs_t, ALL, ALL]
lmc_r, shared_r = fit_from(idx_by_var)
for P, est in [(1, "PK"), (2, "PCK-2"), (3, "PCK-3")]:
pr_, va_ = predict_hidden(hidden, idx_by_var, lmc_r, shared_r, P)
keep_preds[(st, est)] = (hidden, pr_, va_)
pred_rows.append({"design": f"state: {st}", "rep": 0, "estimator": est, "range": lmc_r.range,
**scores(pr_, va_, RATE["diab"][hidden])})
print(f"Prediction experiments: {time.time() - t0:.0f} s")
PRED = pd.DataFrame(pred_rows)