<jupyter_start><jupyter_text>Simple Regression<jupyter_code>%load_ext autoreload
%autoreload 2
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 
from matplotlib import cm

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

from macest.regression import models as reg_mod
from macest.regression import plots as reg_plot

sns.set_style('darkgrid')
sns.set_context('notebook')<jupyter_output><empty_output><jupyter_text>We simulate some data and split it into 4 sections The noise is not homoskedastic across the parameter space, half way through the data the noise decreases by a factor of 10<jupyter_code>X = np.linspace(0,1,10**3)
y = np.zeros(10**3)
y[0:500] = X[0:500]*abs(np.sin(2 *X[0:500])) + np.random.normal(0 , 1 , len(X[0:500]))
y[500:] = X[500:]*abs(np.sin(2 *X[500:])) + np.random.normal(0 , 0.1 , len(X[500:]))
X_pp_train, X_conf_train, y_pp_train, y_conf_train  = train_test_split(X,
                                                                       y,
                                                                       test_size=0.66,
                                                                       random_state=0)

X_conf_train, X_cal, y_conf_train, y_cal = train_test_split(X_conf_train, y_conf_train,
                                                            test_size=0.5, random_state=1)
X_cal, X_test, y_cal,  y_test, =  train_test_split(X_cal, y_cal, test_size=0.5, random_state=1)
plt.figure(figsize = (12,6))
# plt.plot(X,y, label = 'true')
plt.scatter(X_pp_train,y_pp_train, marker = '*', label = 'training data')
plt.legend()
point_pred_model = LinearRegression()
point_pred_model.fit(X_pp_train[:,None], y_pp_train)
point_pred_model.score(X_test[:,None], y_test)<jupyter_output><empty_output><jupyter_text>let's plot the learnt model<jupyter_code>plt.figure(figsize = (12,6))
plt.scatter(X_pp_train,y_pp_train, marker = '*', label = 'training data')
plt.plot(X, point_pred_model.predict(X[:,None]), color ='r', label = 'learnt model')
plt.legend()<jupyter_output><empty_output><jupyter_text>Let's initialise the confidence model, with random alpha, beta and k<jupyter_code>preds = point_pred_model.predict(X_conf_train[:,None])
train_error = y_conf_train - preds
test_error = abs(preds - y_conf_train)
y_conf_train_var = np.var(train_error)
macest_model = reg_mod.ModelWithPredictionInterval(point_pred_model,
                                                 X_conf_train[:,None],
                                                 test_error,)
macest_model.fit(X_cal[:,None],
               y_cal)<jupyter_output><empty_output><jupyter_text>Test how well calibrated our data is using unseen test data<jupyter_code>reg_plot.plot_calibration(macest_model, X_test[:,None], y_test)<jupyter_output><empty_output><jupyter_text>Let's plot the regression plots sorted by the prediction and then by the true value<jupyter_code>reg_plot.plot_predicted_vs_true(point_pred_model, macest_model,X[:,None], y)<jupyter_output><empty_output><jupyter_text>let's predict with an interval across the whole range of x, note that the confidence adapts locally to the variance found within the data<jupyter_code>X = np.linspace(0,1,10**3)
test_dists = macest_model.sample_prediction(X[:,None], nsamples=10**4,)
plt.figure(figsize = (16,8))

plt.fill_between(X, np.percentile(test_dists,95, axis=1), 
                 np.percentile(test_dists, 5, axis=1),color = 'b', alpha = 0.1,
                 label = '90% confidence'  )

plt.fill_between(X, np.percentile(test_dists,15, axis=1), 
                 np.percentile(test_dists, 85, axis=1),color = 'b', alpha = 0.2,
                 label = '70% confidence'  )

plt.fill_between(X, np.percentile(test_dists,25, axis=1), 
                 np.percentile(test_dists, 75, axis=1),color = 'b', alpha = 0.4,
                 label = '50% confidence'  )

plt.plot(X, X*abs(np.sin(2* X)), color = 'y', label = 'true' )
plt.plot(X,point_pred_model.predict(X[:,None]), color = 'r' , label = 'point pred')
plt.scatter(X_conf_train, y_conf_train, marker = '*', color = 'y', label = 'training data' )

plt.xlabel('x', fontsize = 20)
plt.ylabel('y', fontsize = 20)
plt.legend(loc = 'lower right')<jupyter_output><empty_output><jupyter_text>Let's try extrapolate, here we see that the epistemic uncertainty quickly dominates and the confidence interval becomes very large<jupyter_code>X = np.linspace(0,2, 3 * 10**2)
y = X*abs(np.sin(2*X))
test_dists = macest_model.sample_prediction(X[:,None], nsamples=10**4,)
plt.figure(figsize = (16,8))

plt.fill_between(X, np.percentile(test_dists,95, axis=1), 
                 np.percentile(test_dists, 5, axis=1),color = 'b', alpha = 0.1,
                 label = '90% confidence'  )

plt.fill_between(X, np.percentile(test_dists,15, axis=1), 
                 np.percentile(test_dists, 85, axis=1),color = 'b', alpha = 0.2,
                 label = '70% confidence'  )

plt.fill_between(X, np.percentile(test_dists,25, axis=1), 
                 np.percentile(test_dists, 75, axis=1),color = 'b', alpha = 0.4,
                 label = '50% confidence'  )

plt.plot(X, y, color = 'y', label = 'true' )
plt.plot(X,point_pred_model.predict(X[:,None]), color = 'r' , label = 'point pred')
plt.scatter(X_conf_train, y_conf_train, marker = '*', color = 'y', label = 'training data' )


plt.xlabel('x', fontsize = 20)
plt.ylabel('y', fontsize = 20)
plt.legend(loc = 'upper center')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.133917168399927, 'fraction_numerical': 0.0280970625798212, 'mean_word_length': 3.6144781144781146, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '289840', 'license': 'UPL-1.0', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1917, 'n_words': 389, 'path': 'notebooks/regression/basic_regression_example.ipynb', 'repo_name': 'LaudateCorpus1/macest', 'size': '5481'}