2010-09-08 19:46:36 +08:00
|
|
|
"""
|
|
|
|
|
==================================================
|
2010-09-29 22:26:24 +08:00
|
|
|
Automatic Relevance Determination Regression (ARD)
|
2010-09-08 19:46:36 +08:00
|
|
|
==================================================
|
2010-09-29 22:26:24 +08:00
|
|
|
|
2013-07-22 21:48:44 +08:00
|
|
|
Fit regression model with Bayesian Ridge Regression.
|
|
|
|
|
|
|
|
|
|
See :ref:`bayesian_ridge_regression` for more information on the regressor.
|
2011-07-23 20:14:24 +08:00
|
|
|
|
|
|
|
|
Compared to the OLS (ordinary least squares) estimator, the coefficient
|
2013-04-12 02:51:28 +08:00
|
|
|
weights are slightly shifted toward zeros, which stabilises them.
|
2011-07-23 20:14:24 +08:00
|
|
|
|
|
|
|
|
The histogram of the estimated weights is very peaked, as a sparsity-inducing
|
|
|
|
|
prior is implied on the weights.
|
|
|
|
|
|
|
|
|
|
The estimation of the model is done by iteratively maximizing the
|
|
|
|
|
marginal log-likelihood of the observations.
|
2016-12-01 23:52:18 +08:00
|
|
|
|
|
|
|
|
We also plot predictions and uncertainties for ARD
|
|
|
|
|
for one dimensional regression using polynomial feature expansion.
|
|
|
|
|
Note the uncertainty starts going up on the right side of the plot.
|
|
|
|
|
This is because these test samples are outside of the range of the training
|
|
|
|
|
samples.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2010-09-08 19:46:36 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2010-09-29 22:26:24 +08:00
|
|
|
from scipy import stats
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.linear_model import ARDRegression, LinearRegression
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2022-02-14 18:27:54 +08:00
|
|
|
# %%
|
2014-06-02 04:53:23 +08:00
|
|
|
# Generating simulated data with Gaussian weights
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2011-07-23 20:14:24 +08:00
|
|
|
# Parameters of the example
|
2010-09-29 22:26:24 +08:00
|
|
|
np.random.seed(0)
|
2011-07-23 20:14:24 +08:00
|
|
|
n_samples, n_features = 100, 100
|
2014-06-02 04:53:23 +08:00
|
|
|
# Create Gaussian data
|
2010-09-29 22:26:24 +08:00
|
|
|
X = np.random.randn(n_samples, n_features)
|
2015-12-08 02:13:40 +08:00
|
|
|
# Create weights with a precision lambda_ of 4.
|
2010-09-08 19:46:36 +08:00
|
|
|
lambda_ = 4.0
|
|
|
|
|
w = np.zeros(n_features)
|
2011-07-23 20:14:24 +08:00
|
|
|
# Only keep 10 weights of interest
|
2010-09-29 22:26:24 +08:00
|
|
|
relevant_features = np.random.randint(0, n_features, 10)
|
2010-09-08 19:46:36 +08:00
|
|
|
for i in relevant_features:
|
2010-09-29 22:26:24 +08:00
|
|
|
w[i] = stats.norm.rvs(loc=0, scale=1.0 / np.sqrt(lambda_))
|
2016-06-28 00:13:49 +08:00
|
|
|
# Create noise with a precision alpha of 50.
|
2010-09-08 19:46:36 +08:00
|
|
|
alpha_ = 50.0
|
2011-12-20 22:34:17 +08:00
|
|
|
noise = stats.norm.rvs(loc=0, scale=1.0 / np.sqrt(alpha_), size=n_samples)
|
2011-07-23 20:14:24 +08:00
|
|
|
# Create the target
|
2010-09-29 22:26:24 +08:00
|
|
|
y = np.dot(X, w) + noise
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2022-02-14 18:27:54 +08:00
|
|
|
# %%
|
2011-07-23 20:14:24 +08:00
|
|
|
# Fit the ARD Regression
|
2011-12-20 22:34:17 +08:00
|
|
|
clf = ARDRegression(compute_score=True)
|
2010-09-29 22:26:24 +08:00
|
|
|
clf.fit(X, y)
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2011-07-23 20:14:24 +08:00
|
|
|
ols = LinearRegression()
|
|
|
|
|
ols.fit(X, y)
|
|
|
|
|
|
2022-02-14 18:27:54 +08:00
|
|
|
# %%
|
2016-12-01 23:52:18 +08:00
|
|
|
# Plot the true weights, the estimated weights, the histogram of the
|
|
|
|
|
# weights, and predictions with standard deviations
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(figsize=(6, 5))
|
|
|
|
|
plt.title("Weights of the model")
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.plot(clf.coef_, color="darkblue", linestyle="-", linewidth=2, label="ARD estimate")
|
|
|
|
|
plt.plot(
|
|
|
|
|
ols.coef_, color="yellowgreen", linestyle=":", linewidth=2, label="OLS estimate"
|
|
|
|
|
)
|
|
|
|
|
plt.plot(w, color="orange", linestyle="-", linewidth=2, label="Ground truth")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel("Features")
|
|
|
|
|
plt.ylabel("Values of the weights")
|
|
|
|
|
plt.legend(loc=1)
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(figsize=(6, 5))
|
|
|
|
|
plt.title("Histogram of the weights")
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.hist(clf.coef_, bins=n_features, color="navy", log=True)
|
2018-07-23 15:49:01 +08:00
|
|
|
plt.scatter(
|
|
|
|
|
clf.coef_[relevant_features],
|
|
|
|
|
np.full(len(relevant_features), 5.0),
|
2015-10-22 20:12:06 +08:00
|
|
|
color="gold",
|
|
|
|
|
marker="o",
|
|
|
|
|
label="Relevant features",
|
|
|
|
|
)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.ylabel("Features")
|
|
|
|
|
plt.xlabel("Values of the weights")
|
|
|
|
|
plt.legend(loc=1)
|
2010-09-08 19:46:36 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(figsize=(6, 5))
|
|
|
|
|
plt.title("Marginal log-likelihood")
|
2015-10-22 20:12:06 +08:00
|
|
|
plt.plot(clf.scores_, color="navy", linewidth=2)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.ylabel("Score")
|
|
|
|
|
plt.xlabel("Iterations")
|
2016-12-01 23:52:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Plotting some predictions for polynomial regression
|
|
|
|
|
def f(x, noise_amount):
|
|
|
|
|
y = np.sqrt(x) * np.sin(x)
|
|
|
|
|
noise = np.random.normal(0, 1, len(x))
|
|
|
|
|
return y + noise_amount * noise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
degree = 10
|
|
|
|
|
X = np.linspace(0, 10, 100)
|
|
|
|
|
y = f(X, noise_amount=1)
|
|
|
|
|
clf_poly = ARDRegression(threshold_lambda=1e5)
|
|
|
|
|
clf_poly.fit(np.vander(X, degree), y)
|
|
|
|
|
|
|
|
|
|
X_plot = np.linspace(0, 11, 25)
|
|
|
|
|
y_plot = f(X_plot, noise_amount=0)
|
|
|
|
|
y_mean, y_std = clf_poly.predict(np.vander(X_plot, degree), return_std=True)
|
|
|
|
|
plt.figure(figsize=(6, 5))
|
|
|
|
|
plt.errorbar(X_plot, y_mean, y_std, color="navy", label="Polynomial ARD", linewidth=2)
|
|
|
|
|
plt.plot(X_plot, y_plot, color="gold", linewidth=2, label="Ground Truth")
|
|
|
|
|
plt.ylabel("Output y")
|
|
|
|
|
plt.xlabel("Feature X")
|
|
|
|
|
plt.legend(loc="lower left")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|