<jupyter_start><jupyter_text>Confidence Intervals This is a simple demonstration of the algorithms and how well they do with confidence intervals. Each algorithm proposed below are Bayesian in nature and have confidence intervals attached to them. Obviously we won't be able to plot multidimensional confidence intervals so easily but this will at least give us an idea.**Note**: This is a heads up but I didn't do any special training procedures to train the algorithms. So the performance may not be optimum.Below are the **Algorithms** that have confidence intervals.* Bayesian Ridge Regression* ARD Regression* Gaussian Process Regression<jupyter_code>import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, BayesianRidge, ARDRegression
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import WhiteKernel, ConstantKernel, RBF
from sklearn.multioutput import MultiOutputRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import time as time

import matplotlib.pyplot as plt
%matplotlib inline
# Reproducibility
rng = np.random.RandomState(0)

# Generate sample data
X = 15 * rng.rand(100, 1)
f = lambda x: np.sin(x)
y = f(np.sin(X).ravel())[:, None]
# print(y.shape)
y += 3 * (0.5 - rng.rand(X.shape[0]))[:, None]  # add noise

# Generate plot data
X_plot = np.linspace(0, 20, 10000)[:, None]<jupyter_output><empty_output><jupyter_text>Bayesian Ridge Regression<jupyter_code># Define GP Model
bayes_model = BayesianRidge(
    n_iter=1000,
    normalize=True,         # good standard practice
    verbose=1,              # Gives updates
    compute_score=True
)

# train GP Model
t0 = time.time()
bayes_model.fit(X, y)
t1 = time.time() - t0

# Predictions
ypred, ystd = bayes_model.predict(X_plot, return_std=True)
fig, ax = plt.subplots(figsize=(10, 5))

plt.scatter(X, y, c='k', label='data')
plt.plot(X_plot, f(X_plot), color='navy', lw=2, label='True')

# Plot standard deviations
plt.fill_between(X_plot[:, 0], 
                 ypred.squeeze() - ystd, 
                 ypred.squeeze() + ystd, color='darkorange',
                 alpha=0.2)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>The mean looks ok but the variance estimates don't look nearly as good as the GPs below.  ARD Regression<jupyter_code># Define GP Model
ard_model = ARDRegression(
    n_iter=1000,
    verbose=1,              # Gives updates
)

# train GP Model
t0 = time.time()
ard_model.fit(X, y)
t1 = time.time() - t0

# Predictions
ypred, ystd = ard_model.predict(X_plot, return_std=True)
fig, ax = plt.subplots(figsize=(10, 5))

plt.scatter(X, y, c='k', label='data')
plt.plot(X_plot, f(X_plot), color='navy', lw=2, label='True')

# Plot standard deviations
plt.fill_between(X_plot[:, 0], 
                 ypred.squeeze() - ystd, 
                 ypred.squeeze() + ystd, color='darkorange',
                 alpha=0.2)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Gaussian Process Regression<jupyter_code># define kernel function
kernel = ConstantKernel() * RBF() + WhiteKernel()

# define GP model
gp_model = GaussianProcessRegressor(
    kernel=kernel,            # kernel function (very important)
    normalize_y=True,         # good standard practice
    random_state=123,         # reproducibility
    n_restarts_optimizer=10,  # good practice (avoids local minima)
)

# train GP Model
t0 = time.time()
gp_model.fit(X, y)
t1 = time.time() - t0

# Predictions
ypred, ystd = gp_model.predict(X_plot, return_std=True)
fig, ax = plt.subplots(figsize=(10, 5))

plt.scatter(X, y, c='k', label='data')
plt.plot(X_plot, f(X_plot), color='navy', lw=2, label='True')

# Plot standard deviations
plt.fill_between(X_plot[:, 0], 
                 ypred.squeeze() - ystd, 
                 ypred.squeeze() + ystd, color='darkorange',
                 alpha=0.2)
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11350681536555142, 'fraction_numerical': 0.0161090458488228, 'mean_word_length': 4.154533844189016, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '419737', 'license': 'MIT', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1324, 'n_words': 370, 'path': 'notebooks/na_data/demos/3_confidence_intervals.ipynb', 'repo_name': 'IPL-UV/ml4ocean', 'size': '4035'}