Calibrated prediction intervals
Fit three quantile models on training rows, freeze those fits, and estimate the calibration correction on a separate fold. The interval artifact stores the three fits, target miscoverage, calibration count, crossing policy, and correction in one JSON string. Scoring needs only the artifact and the input features.
Fit and calibrate
This example uses row IDs to create three disjoint sets. The generated features and noise depend on separate hashes of each ID.
LOAD duckboost;
CREATE TABLE observations AS
WITH sampled AS (
SELECT i AS id,
(md5_number_lower('x-' || i) % 1000000) / 1000000.0 AS x,
(md5_number_lower('noise-' || i) % 1000000) / 1000000.0 AS u
FROM range(1200) t(i)
)
SELECT id, x, sin(6 * x) + (0.2 + x) * (2 * u - 1) AS y
FROM sampled;
CREATE TABLE fits AS
SELECT
duckboost_train(y, [x], MAP {
'objective':'quantile', 'quantile_alpha':'0.05', 'n_estimators':'30',
'max_depth':'3', 'feature_names':'x'
}) AS lo,
duckboost_train(y, [x], MAP {
'objective':'quantile', 'quantile_alpha':'0.5', 'n_estimators':'30',
'max_depth':'3', 'feature_names':'x'
}) AS med,
duckboost_train(y, [x], MAP {
'objective':'quantile', 'quantile_alpha':'0.95', 'n_estimators':'30',
'max_depth':'3', 'feature_names':'x'
}) AS hi
FROM observations WHERE id < 700;
CREATE TABLE intervals AS
SELECT duckboost_calibrate_interval(lo, med, hi, 0.10, y, [x]) AS model
FROM fits, observations WHERE id BETWEEN 700 AND 899;
CREATE TABLE holdout AS SELECT * FROM observations WHERE id >= 900;
SELECT round((model::JSON->>'correction')::DOUBLE, 5) AS correction,
(model::JSON->>'n_calibration')::INTEGER AS calibration_rows
FROM intervals;┌────────────┬──────────────────┐
│ correction │ calibration_rows │
├────────────┼──────────────────┤
│ 0.0147 │ 200 │
└────────────┴──────────────────┘
If you tune hyperparameters or use early stopping, complete those decisions before opening the calibration fold. Each calibration group requires the same three fitted models and alpha on every row. Supply the lower quantile fit, the 0.5 fit, and the upper quantile fit in that order. Calibration rejects NULL targets and feature lists. A NULL feature value inside the list follows each fitted tree’s missing-value routing.
Interpret the correction
At calibration and scoring time, duckboost sorts each row’s three predictions by value. Call the sorted predictions (L, M, U). For each of the (n) calibration rows, compute (s_i = (L_i-y_i, y_i-U_i)). The correction (c) is the calibration score at one-based rank ((n+1)(1-)) among the ordered scores. For scores 1 through 10 and (), the correction is 10. Calibration raises an error if the rank exceeds the number of rows.
The returned fields are lower = (L-c), median = (M), and upper = (U+c). A negative correction shrinks the band and can move an endpoint past the reported median. If lower > upper, the prediction set is empty and its width is zero. Under exchangeability of calibration and future observations, split conformal prediction gives marginal coverage of at least (1-). Realized coverage on any particular test set varies. Romano, Patterson, and Candès, Conformalized Quantile Regression (2019)
Score untouched rows and export
WITH predictions AS (
SELECT y, duckboost_predict_interval(model, [x]) AS band
FROM holdout, intervals
)
SELECT count(*) AS test_rows,
count_if(y BETWEEN band.lower AND band.upper) AS covered,
round(avg((y BETWEEN band.lower AND band.upper)::INTEGER), 4) AS coverage,
round(avg(greatest(0, band.upper - band.lower)), 4) AS mean_width
FROM predictions;┌───────────┬─────────┬──────────┬────────────┐
│ test_rows │ covered │ coverage │ mean_width │
├───────────┼─────────┼──────────┼────────────┤
│ 300 │ 264 │ 0.88 │ 1.4968 │
└───────────┴─────────┴──────────┴────────────┘
Save intervals.model in a DuckDB table or store its text in a model registry. A NULL artifact or feature list yields a NULL prediction. For extension-free deployment, generate the scoring query once and execute it wherever the raw numeric columns are available. The optional fourth argument lists identity columns to return alongside the three estimates.
SET VARIABLE score_sql = (
SELECT duckboost_interval_to_sql(model, 'holdout', ['x'], ['id'])
FROM intervals
);
SELECT id, lower, median, upper
FROM query(getvariable('score_sql'))
ORDER BY id LIMIT 5;┌─────┬──────────────────────┬─────────────────────┬─────────────────────┐
│ id │ lower │ median │ upper │
├─────┼──────────────────────┼─────────────────────┼─────────────────────┤
│ 900 │ -0.16175037838839668 │ 0.8196173606227226 │ 1.0382770421716554 │
│ 901 │ -1.741114051246941 │ -0.8045823435730228 │ 0.2998676845284153 │
│ 902 │ -0.03409803117671717 │ 0.2988892184273629 │ 0.9038189448061316 │
│ 903 │ -1.854198187123509 │ -0.8045823435730228 │ 0.03934223500124105 │
│ 904 │ -1.854198187123509 │ -0.8045823435730228 │ 0.03934223500124105 │
└─────┴──────────────────────┴─────────────────────┴─────────────────────┘