Comparing Linear Bayesian Regressors
LinearRegression
()
.
fit
(
X
,
y
)
brr
=
(
compute_score
=
True
,
max_iter
=
30
)
.
fit
(
X
,
y
)
ard
=
(
compute_score
=
True
,
max_iter
=
30
)
.
fit
(
X
,
y
)
df
=
(
{
"Weights of true generative process"
:
true_weights
,
"ARDRegression"
:
ard
.
coef_
,
"BayesianRidge"
:
brr
.
coef_
,
"LinearRegression"
:
olr
.
coef_
,
}
)
10
,
6
))
ax
=
(
df
.
T
,
norm
=
(
linthresh
=
10e-4
,
vmin
=-
80
,
vmax
=
80
),
cbar_kws
=
{
"label"
:
"coefficients' values"
},
cmap
=
"seismic_r"
,
)
(
"linear model"
)
(
"coefficients"
)
(
rect
=
(
0
,
0
,
1
,
0.95
))
_
=
(
"Models' coefficients"
)
Due to the added noise, none of the models recover the true weights. Indeed,
all models always have more than 10 non-zero coefficients. Compared to the OLS
estimator, the coefficients using a Bayesian Ridge regression are slightly
shifted toward zero, which stabilises them. The ARD regression provides a
sparser solution: some of the non-informative coefficients are set exactly to
zero, while shifting others closer to zero. Some non-informative coefficients
are still present and retain large values.
(
ard_scores
,
color
=
"navy"
,
label
=
"ARD"
)
(
brr_scores
,
color
=
"red"
,
label
=
"BayesianRidge"
)
(
"Log-likelihood"
)
(
"Iterations"
)
(
1
,
30
)
()
_
=
(
"Models log-likelihood"
)
Indeed, both models minimize the log-likelihood up to an arbitrary cutoff
defined by the max_iter parameter.
=
(
(
degree
=
10
,
include_bias
=
False
),
(),
(),
)
.
fit
(
X
,
y
)
brr_poly
=
(
(
degree
=
10
,
include_bias
=
False
),
(),
(),
)
.
fit
(
X
,
y
)
y_ard
,
y_ard_std
=
ard_poly
.
predict
(
X_plot
,
return_std
=
True
)
y_brr
,
y_brr_std
=
brr_poly
.
predict
(
X_plot
,
return_std
=
True
)
X_plot
,
y_plot
,
color
=
"black"
,
label
=
"Ground Truth"
)
ax
.
plot
(
X_plot
,
y_brr
,
color
=
"red"
,
label
=
"BayesianRidge with polynomial features"
)
ax
.
plot
(
X_plot
,
y_ard
,
color
=
"navy"
,
label
=
"ARD with polynomial features"
)
ax
.
fill_between
(
X_plot
.
ravel
(),
y_ard
-
y_ard_std
,
y_ard
+
y_ard_std
,
color
=
"navy"
,
alpha
=
0.3
,
)
ax
.
fill_between
(
X_plot
.
ravel
(),
y_brr
-
y_brr_std
,
y_brr
+
y_brr_std
,
color
=
"red"
,
alpha
=
0.3
,
)
ax
.
legend
()
_
=
ax
.
set_title
(
"Polynomial fit of a non-linear feature"
)
The error bars represent one standard deviation of the predicted gaussian
distribution of the query points. Notice that the ARD regression captures the
ground truth the best when using the default parameters in both models, but
further reducing the lambda_init hyperparameter of the Bayesian Ridge can
reduce its bias (see example
Curve Fitting with Bayesian Ridge Regression).
Finally, due to the intrinsic limitations of a polynomial regression, both
models fail when extrapolating.
Total running time of the script: (0 minutes 0.713 seconds)
Related examples