File size: 4,388 Bytes
508aecf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #!/usr/bin/env python
"""TASK 1 diagnostic: why does run_dcp give coverage 1.0 / eff 1305 / thr 0.0?
For ONE fb1 raw run (seed 0): fit the QRF, compute the QRF grid on the 500
calib rows, call _estim_dist per row exactly as run_dcp does, and print the
distribution of cdf(Y_cal).
"""
import sys
import numpy as np
sys.path.insert(0, "/work")
from run_realdata_v30 import load_dataset, fit_qrf, N_EXPLORE, N_CALIB, N_TEST, Q_GRID
# import run_full's _estim_dist without running the module
import importlib.util
spec = importlib.util.spec_from_file_location("runfull", "/work/run_full.py")
rf = importlib.util.module_from_spec(spec)
spec.loader.exec_module(rf)
_estim_dist = rf._estim_dist
QrfBbox = rf.QrfBbox
from scipy.stats.mstats import mquantiles
X, y_full = load_dataset("fb1")
print("data", X.shape, y_full.shape)
seed = 0
rng = np.random.default_rng(seed)
np.random.seed(seed)
perm = rng.permutation(len(y_full))
i_explore = perm[:N_EXPLORE]
i_calib = perm[N_EXPLORE:N_EXPLORE + N_CALIB]
i_test = perm[N_EXPLORE + N_CALIB:N_EXPLORE + N_CALIB + N_TEST]
i_train = perm[N_EXPLORE + N_CALIB + N_TEST:]
y = y_full
X_train, X_calib, X_test = X[i_train], X[i_calib], X[i_test]
y_train, y_calib, y_test = y[i_train], y[i_calib], y[i_test]
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_calib = scaler.transform(X_calib)
X_test = scaler.transform(X_test)
y_min = float(np.min(y_full))
y_max = float(np.max(y_full))
print("y_min/y_max", y_min, y_max, "span", y_max - y_min)
model = fit_qrf(X_train, y_train, seed=seed, leaf_mode="split")
print("QRF fit done")
bbox = QrfBbox(model)
percentiles = bbox.get_quantiles()
print("percentiles (get_quantiles):", percentiles[:5], "...", percentiles[-3:],
"len", len(percentiles))
n2 = len(X_calib)
quantiles = bbox.predict(X_calib)
print("quantiles matrix", quantiles.shape)
# sanity on the grid itself: is it monotone? do rows vary?
print("row 0 quantiles:", quantiles[0][:6])
print("row 0 monotone:", bool(np.all(np.diff(quantiles[0]) >= 0)))
print("row 1 quantiles:", quantiles[1][:6])
cdf_values = np.empty(n2)
for i in range(n2):
cdf, inv_cdf = _estim_dist(quantiles[i], percentiles, y_min=y_min,
y_max=y_max, smooth_tails=True, tau=0.01)
cdf_values[i] = cdf(np.float64(y_calib[i]))
qs = np.quantile(cdf_values, [0.05, 0.25, 0.5, 0.75, 0.95])
print("\ncdf(Y_cal) quantiles 0.05/0.25/0.5/0.75/0.95:", np.round(qs, 6))
print("frac <= 0:", float(np.mean(cdf_values <= 0)),
"frac >= 1:", float(np.mean(cdf_values >= 1)))
print("frac == 0.5 exactly:", float(np.mean(cdf_values == 0.5)))
print("n distinct values:", len(np.unique(cdf_values)))
# compare with the ORIGINAL utils._estim_dist on the same inputs
sys.path.insert(0, "/work/pcp_src")
from other_baselines.utils import _estim_dist as orig_estim
cdf_o = np.empty(n2)
for i in range(n2):
cdf, _ = orig_estim(quantiles[i], percentiles, y_min=y_min, y_max=y_max,
smooth_tails=True, tau=0.01)
cdf_o[i] = cdf(np.float64(y_calib[i]))
print("\nmax |port - original|:", float(np.max(np.abs(cdf_o - cdf_values))))
# What does the original dcp.py DO with these? reproduce its calibrate:
alpha = 0.10
scores = np.abs(np.clip(cdf_values, 0, 1) - 1 / 2)
level_adjusted = (1.0 - alpha) * (1.0 + 1.0 / float(n2))
alpha_calibrated = 0.5 - mquantiles(scores, prob=level_adjusted)[0]
print("scores quantiles:", np.round(np.quantile(scores, [0.05, 0.5, 0.95]), 6))
print("alpha_calibrated:", float(alpha_calibrated))
# original DCP.predict: inv_cdf at 0.5 - alpha_calibrated and 0.5 + alpha_calibrated
# (NOTE: original uses inv_cdf(self.alpha_calibrated) = inv_cdf(0.5 - t) where
# t = alpha_calibrated ... no: predict uses inv_cdf(alpha_calibrated) directly)
# With alpha_cal = 0.5 - q(scores, (1-alpha)(1+1/n)) this is the same t.
cdf_t, inv_t = _estim_dist(quantiles[0], percentiles, y_min=y_min, y_max=y_max,
smooth_tails=True, tau=0.01)
t_cal = 0.5 - float(alpha_calibrated)
print("inv_cdf(0.5-t), inv_cdf(0.5+t) row0:",
float(inv_t(0.5 - t_cal)), float(inv_t(0.5 + t_cal)))
# what if we pass alpha_calibrated directly (original predict semantics)?
print("inv_cdf(alpha_cal), inv_cdf(1-alpha_cal) row0:",
float(inv_t(float(alpha_calibrated))), float(inv_t(1 - float(alpha_calibrated))))
|