Lesson 6 of 9
Advanced Regression Diagnostics: Prediction Intervals & Quantile Losses
In enterprise forecasting, point predictions are rarely sufficient. Business operations require prediction intervals: 'We predict electricity demand will be 4,500 MW, with a 90% confidence bound between 4,200 MW and 4,900 MW'. Standard regression predicts only the conditional mean E[y|X]. Quantile Regression (`GradientBoostingRegressor(loss='quantile')`) optimizes the Pinball loss function to predict arbitrary conditional percentiles directly.
Quantile Regression & Empirical Prediction Intervals
import numpy as np
import pandas as pd
from sklearn.ensemble import GradientBoostingRegressor
# Generate non-linear heteroscedastic energy demand data
np.random.seed(42)
n = 800
X = np.sort(np.random.uniform(0, 10, n)).reshape(-1, 1)
# True mean demand
mean_demand = 50.0 + 10.0 * np.sin(X.ravel())
# Heteroscedastic noise: uncertainty expands significantly at higher X values
noise_scale = 1.0 + 0.8 * X.ravel()
y = mean_demand + np.random.normal(0, noise_scale, n)
# Train 3 separate Quantile Gradient Boosters: 10th, 50th (median), and 90th percentiles
quantiles = [0.10, 0.50, 0.90]
models = {}
for q in quantiles:
gbr = GradientBoostingRegressor(
loss='quantile',
alpha=q, # Target percentile
n_estimators=100,
max_depth=3,
learning_rate=0.1,
random_state=42
)
gbr.fit(X, y)
models[q] = gbr
# Inference on evaluation grid
X_eval = np.array([[2.0], [5.0], [8.0]])
p10 = models[0.10].predict(X_eval)
p50 = models[0.50].predict(X_eval)
p90 = models[0.90].predict(X_eval)
print("=== QUANTILE REGRESSION UNCERTAINTY FORECASTS ===")
for x_val, low, med, high in zip(X_eval.ravel(), p10, p50, p90):
print(f"Input X={x_val:.1f} -> Median Demand: {med:.2f} MW | 80% Bound: [{low:.2f} to {high:.2f}] (Bandwidth: {high-low:.2f})")
print("\nNotice: Prediction interval width automatically expands where variance is higher!")Pro Tip: Conformal Prediction for Distribution-Free Guarantees
Quantile regression models can suffer from 'quantile crossing' (where the predicted 10th percentile accidentally exceeds the 50th percentile). In production, adopt Split Conformal Prediction, which provides mathematically proven, distribution-free coverage guarantees `1 - alpha` on any black-box model without retraining.
MlRegressionEvaluation Visual Representation
Hands-On Challenge: Empirical Coverage Verification
Evaluate the fitted quantile models on a held-out test set of 200 samples. Calculate the empirical fraction of points that fall between the predicted 10th and 90th percentiles, verifying that true coverage is within ยฑ2% of the theoretical 80% target.
Quick Quiz & Key Concept Checks
Common Pitfall: Confusing Prediction Intervals with Confidence Intervals
A Confidence Interval quantifies uncertainty in the estimated mean parameter E[y|X]. A Prediction Interval quantifies uncertainty in predicting an individual future observation y, incorporating both parameter error and irreducible observation noise ฯยฒ. Prediction intervals are always wider than confidence intervals.