Using Pinball Loss for Quantile Regression in scikit-learn
Align alpha with business risk, train with HistGradientBoostingRegressor loss='quantile', and evaluate using mean_pinball_loss with matching alpha in GridSearchCV.
On this page
Key idea
Quantile regression predicts specific percentiles of a target distribution rather than the mean. To use pinball loss effectively, select the alpha parameter to match your operational risk tolerance. For example, if you need to cap network interruptions 99% of the time, set alpha to 0.99. Train your model using HistGradientBoostingRegressor with loss='quantile' and the chosen quantile value. Evaluate performance using mean_pinball_loss with the identical alpha during cross-validation or grid search. Wrap the metric in make_scorer with greater_is_better=False since it is a loss function. This ensures the optimization objective matches the evaluation metric, preventing misalignment between training goals and scoring.
Selecting the quantile based on decision goals
Choose the alpha parameter to align with your specific risk tolerance or service level agreement. In network reliability, if you promise customers no connection discontinuities larger than one minute on 99% of days, your target functional is the 99th quantile. The pinball loss is the strictly consistent scoring function for this functional. Defaulting to 0.5 predicts the median, which may not satisfy high-reliability requirements.
Training models with consistent loss functions
Use the pinball loss directly in model constructors like GradientBoostingRegressor or HistGradientBoostingRegressor. Set the loss parameter to 'quantile' and provide the quantile argument matching your alpha. This ensures the optimization objective matches the evaluation metric. Consistent scoring functions act as truth serum, guaranteeing that truthful prediction is optimal in expectation.
from sklearn.ensemble import HistGradientBoostingRegressor
model = HistGradientBoostingRegressor(loss='quantile', quantile=0.99)Evaluating performance with mean_pinball_loss
Calculate prediction error using the same alpha value during cross-validation or grid search. The mean_pinball_loss function requires the alpha argument to compute the correct loss. Wrap it in make_scorer and set greater_is_better=False because lower loss is better. This maintains consistency between training and scoring processes.
from sklearn.metrics import make_scorer, mean_pinball_loss
scorer = make_scorer(mean_pinball_loss, alpha=0.99, greater_is_better=False)Understanding D² score for skill assessment
Interpret model quality relative to a baseline intercept-only model that predicts the target quantile. The d2_pinball_score computes the fraction of deviance explained. It generalizes R² where squared error is replaced by pinball deviance. A score of 0.0 means the model performs as well as the null baseline, while negative values indicate worse performance.
from sklearn.metrics import make_scorer, d2_pinball_score
scorer = make_scorer(d2_pinball_score, alpha=0.99)Applicability
- Does the alpha parameter match the quantile specified in the model constructor?
- Is greater_is_better=False set when creating a scorer from a loss function?
- Is the target variable continuous numerical data suitable for regression?
- Does the D² score baseline correspond to the chosen quantile level?
Where this applies
Pinball loss is not strictly consistent for the mode functional. Evaluation metrics require matching alpha values between training and scoring. D² score can be negative if the model performs worse than the null baseline. Cross-validation scoring must use make_scorer to handle custom alpha parameters correctly.