This example illustrates how quantile regression can predict non-trivial
conditional quantiles.
The left figure shows the case when the error distribution is normal,
but has non-constant variance, i.e. with heteroscedasticity.
The right figure shows an example of an asymmetric error distribution,
namely the Pareto distribution.
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clause
Dataset generation
To illustrate the behaviour of quantile regression, we will generate two
synthetic datasets. The true generative random processes for both datasets
will be composed by the same expected value with a linear relationship with a
single feature x.
Let’s first visualize the datasets as well as the distribution of the
residuals y-mean(y).
importmatplotlib.pyplotasplt_,axs=plt.subplots(nrows=2,ncols=2,figsize=(15,11),sharex="row",sharey="row")axs[0,0].plot(x,y_true_mean,label="True mean")axs[0,0].scatter(x,y_normal,color="black",alpha=0.5,label="Observations")axs[1,0].hist(y_true_mean-y_normal,edgecolor="black")axs[0,1].plot(x,y_true_mean,label="True mean")axs[0,1].scatter(x,y_pareto,color="black",alpha=0.5,label="Observations")axs[1,1].hist(y_true_mean-y_pareto,edgecolor="black")axs[0,0].set_title("Dataset with heteroscedastic Normal distributed targets")axs[0,1].set_title("Dataset with asymmetric Pareto distributed target")axs[1,0].set_title("Residuals distribution for heteroscedastic Normal distributed targets")axs[1,1].set_title("Residuals distribution for asymmetric Pareto distributed target")axs[0,0].legend()axs[0,1].legend()axs[0,0].set_ylabel("y")axs[1,0].set_ylabel("Counts")axs[0,1].set_xlabel("x")axs[0,0].set_xlabel("x")axs[1,0].set_xlabel("Residuals")_=axs[1,1].set_xlabel("Residuals")
With the heteroscedastic Normal distributed target, we observe that the
variance of the noise is increasing when the value of the feature x is
increasing.
With the asymmetric Pareto distributed target, we observe that the positive
residuals are bounded.
These types of noisy targets make the estimation via
LinearRegression less efficient, i.e. we need
more data to get stable results and, in addition, large outliers can have a
huge impact on the fitted coefficients. (Stated otherwise: in a setting with
constant variance, ordinary least squares estimators converge much faster to
the true coefficients with increasing sample size.)
In this asymmetric setting, the median or different quantiles give additional
insights. On top of that, median estimation is much more robust to outliers
and heavy tailed distributions. But note that extreme quantiles are estimated
by very few data points. 95% quantile are more or less estimated by the 5%
largest values and thus also a bit sensitive outliers.
In the remainder of this tutorial, we will show how
QuantileRegressor can be used in practice and
give the intuition into the properties of the fitted models. Finally,
we will compare the both QuantileRegressor
and LinearRegression.
Fitting a QuantileRegressor
In this section, we want to estimate the conditional median as well as
a low and high quantile fixed at 5% and 95%, respectively. Thus, we will get
three linear models, one for each quantile.
We will use the quantiles at 5% and 95% to find the outliers in the training
sample beyond the central 90% interval.
Now, we can plot the three linear models and the distinguished samples that
are within the central 90% interval from samples that are outside this
interval.
Since the noise is still Normally distributed, in particular is symmetric,
the true conditional mean and the true conditional median coincide. Indeed,
we see that the estimated median almost hits the true mean. We observe the
effect of having an increasing noise variance on the 5% and 95% quantiles:
the slopes of those quantiles are very different and the interval between
them becomes wider with increasing x.
To get an additional intuition regarding the meaning of the 5% and 95%
quantiles estimators, one can count the number of samples above and below the
predicted quantiles (represented by a cross on the above plot), considering
that we have a total of 100 samples.
We can repeat the same experiment using the asymmetric Pareto distributed
target.
Due to the asymmetry of the distribution of the noise, we observe that the
true mean and estimated conditional median are different. We also observe
that each quantile model has different parameters to better fit the desired
quantile. Note that ideally, all quantiles would be parallel in this case,
which would become more visible with more data points or less extreme
quantiles, e.g. 10% and 90%.
Comparing QuantileRegressor and LinearRegression
In this section, we will linger on the difference regarding the loss functions that
QuantileRegressor and
LinearRegression are minimizing.
Indeed, LinearRegression is a least squares
approach minimizing the mean squared error (MSE) between the training and
predicted targets. In contrast,
QuantileRegressor with quantile=0.5
minimizes the mean absolute error (MAE) instead.
Why does it matter? The loss functions specify what exactly the model is aiming
to predict, see
user guide on the choice of scoring function.
In short, a model minimizing MSE predicts the mean (expectation) and a model
minimizing MAE predicts the median.
Let’s compute the training errors of such models in terms of mean
squared error and mean absolute error. We will use the asymmetric Pareto
distributed target to make it more interesting as mean and median are not
equal.