all_xy = jura[["Xloc", "Yloc"]].to_numpy(float)
all_z = jura["logCd"].to_numpy(float)
vg_all = Variogram(all_xy, all_z, model="spherical", n_lags=15, estimator="matheron", use_gpu=False).fit()
nug_a, psill_a, range_a = (float(v) for v in vg_all.fitted_params[:3])
tau_a = nug_a / psill_a
oracle_s2 = ConjugateBayesianKriging("spherical", range_=range_a, nugget_ratio=tau_a).fit(all_xy, all_z)._comps[0]["s2"]
rng = np.random.default_rng(0)
rows = []
t0 = time.perf_counter()
for n in [8, 12, 20, 40, 80, 160]:
cov_t, cov_g, cov_o, w_t, w_g = [], [], [], [], []
for rep in range(60):
idx = rng.permutation(len(all_z))
trn, tst = idx[:n], idx[n:n + 100]
m = ConjugateBayesianKriging("spherical", range_=range_a, nugget_ratio=tau_a).fit(all_xy[trn], all_z[trn])
loc_, sc_, df_ = m._component_predictions(all_xy[tst], signal=False)
loc_, sc_, df_ = loc_[0], sc_[0], df_[0]
unit = sc_ / np.sqrt(m._comps[0]["s2"])
yt = all_z[tst]
qt, qg = stats.t.ppf(0.95, df_), stats.norm.ppf(0.95)
cov_t.append(np.mean(np.abs(yt - loc_) <= qt * sc_))
cov_g.append(np.mean(np.abs(yt - loc_) <= qg * sc_))
cov_o.append(np.mean(np.abs(yt - loc_) <= qg * unit * np.sqrt(oracle_s2)))
w_t.append(np.mean(2 * qt * sc_))
w_g.append(np.mean(2 * qg * sc_))
rows.append({"n": n, "dof": n - 1, "coverage: conjugate t": np.mean(cov_t), "coverage: plug-in Gaussian": np.mean(cov_g),
"coverage: oracle Gaussian": np.mean(cov_o), "width: t": np.mean(w_t), "width: Gaussian": np.mean(w_g),
"t / Gaussian width": np.mean(w_t) / np.mean(w_g)})
small = pd.DataFrame(rows).set_index("n")
print(f"{6 * 60} fits in {time.perf_counter() - t0:.1f} s")
display(small.round(3))
fig, ax = plt.subplots(figsize=(7.5, 4.5))
for col, c_ in [("coverage: conjugate t", "tab:blue"), ("coverage: plug-in Gaussian", "tab:red"), ("coverage: oracle Gaussian", "0.5")]:
ax.plot(small.index, small[col], "o-", c=c_, label=col.replace("coverage: ", ""))
ax.axhline(0.9, color="k", ls="--", lw=1)
ax.set_xscale("log")
ax.set_xticks(small.index, small.index)
ax.set_xlabel("number of training samples n")
ax.set_ylabel("coverage of nominal 90% intervals")
ax.legend()
plt.tight_layout()
plt.show()
record("Student-t closer to 90% than plug-in Gaussian at n = 8",
"PASS" if abs(small.loc[8, "coverage: conjugate t"] - 0.9) < abs(small.loc[8, "coverage: plug-in Gaussian"] - 0.9) else "FAIL",
f"t {small.loc[8, 'coverage: conjugate t']:.3f}, Gaussian {small.loc[8, 'coverage: plug-in Gaussian']:.3f}")