%matplotlib inline
import pymc3 as pm
import matplotlib.pyplot as plt
import numpy as np
import theano
This tutorial first appeard as a post in small series on Bayesian GLMs on my blog:
- The Inference Button: Bayesian GLMs made easy with PyMC3
- This world is far from Normal(ly distributed): Robust Regression in PyMC3
- The Best Of Both Worlds: Hierarchical Linear Regression in PyMC3
In this blog post I will write about:
- How a few outliers can largely affect the fit of linear regression models.
- How replacing the normal likelihood with Student T distribution produces robust regression.
- How this can easily be done with
PyMC3
and its newglm
module by passing afamily
object.
This is the second part of a series on Bayesian GLMs (click here for part I about linear regression). In this prior post I described how minimizing the squared distance of the regression line is the same as maximizing the likelihood of a Normal distribution with the mean coming from the regression line. This latter probabilistic expression allows us to easily formulate a Bayesian linear regression model.
This worked splendidly on simulated data. The problem with simulated data though is that it’s, well, simulated. In the real world things tend to get more messy and assumptions like normality are easily violated by a few outliers.
Lets see what happens if we add some outliers to our simulated data from the last post.
Again, import our modules.
Create some toy data but also add some outliers.
= 100
size = 1
true_intercept = 2
true_slope
= np.linspace(0, 1, size)
x # y = a + b*x
= true_intercept + true_slope * x
true_regression_line # add noise
= true_regression_line + np.random.normal(scale=.5, size=size)
y
# Add outliers
= np.append(x, [.1, .15, .2])
x_out = np.append(y, [8, 6, 9])
y_out
= dict(x=x_out, y=y_out) data
Plot the data together with the true regression line (the three points in the upper left corner are the outliers we added).
= plt.figure(figsize=(12, 12))
fig = fig.add_subplot(111, xlabel='x', ylabel='y', title='Generated data and underlying model')
ax 'x', label='sampled data')
ax.plot(x_out, y_out, ='true regression line', lw=2.)
ax.plot(x, true_regression_line, label=0); plt.legend(loc
Robust Regression
Lets see what happens if we estimate our Bayesian linear regression model using the glm()
function as before. This function takes a Patsy
string to describe the linear model and adds a Normal likelihood by default.
with pm.Model() as model:
'y ~ x', data)
pm.GLM.from_formula(= pm.sample(progressbar=False, tune=1000) trace
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
To evaluate the fit, I am plotting the posterior predictive regression lines by taking regression parameters from the posterior distribution and plotting a regression line for each (this is all done inside of plot_posterior_predictive()
).
111, xlabel='x', ylabel='y',
plt.subplot(='Posterior predictive regression lines')
title'x', label='data')
plt.plot(x_out, y_out, =100,
pm.plots.plot_posterior_predictive_glm(trace, samples='posterior predictive regression lines')
label
plt.plot(x, true_regression_line, ='true regression line', lw=3., c='y')
label
=0); plt.legend(loc
As you can see, the fit is quite skewed and we have a fair amount of uncertainty in our estimate as indicated by the wide range of different posterior predictive regression lines. Why is this? The reason is that the normal distribution does not have a lot of mass in the tails and consequently, an outlier will affect the fit strongly.
A Frequentist would estimate a Robust Regression and use a non-quadratic distance measure to evaluate the fit.
But what’s a Bayesian to do? Since the problem is the light tails of the Normal distribution we can instead assume that our data is not normally distributed but instead distributed according to the Student T distribution which has heavier tails as shown next (I read about this trick in “The Kruschke”, aka the puppy-book; but I think Gelman was the first to formulate this).
Lets look at those two distributions to get a feel for them.
= pm.Normal.dist(mu=0, sd=1)
normal_dist = pm.StudentT.dist(mu=0, lam=1, nu=1)
t_dist = np.linspace(-8, 8, 300)
x_eval eval(), label='Normal', lw=2.)
plt.plot(x_eval, theano.tensor.exp(normal_dist.logp(x_eval)).eval(), label='Student T', lw=2.)
plt.plot(x_eval, theano.tensor.exp(t_dist.logp(x_eval)).'x')
plt.xlabel('Probability density')
plt.ylabel(; plt.legend()
As you can see, the probability of values far away from the mean (0 in this case) are much more likely under the T
distribution than under the Normal distribution.
To define the usage of a T distribution in PyMC3
we can pass a family object – StudentT
– that specifies that our data is Student T-distributed (see glm.families
for more choices). Note that this is the same syntax as R
and statsmodels
use.
with pm.Model() as model_robust:
= pm.glm.families.StudentT()
family 'y ~ x', data, family=family)
pm.GLM.from_formula(= pm.sample(progressbar=False, tune=1000)
trace_robust
=(12, 12))
plt.figure(figsize'x')
plt.plot(x_out, y_out,
pm.plots.plot_posterior_predictive_glm(trace_robust,='posterior predictive regression lines')
label
plt.plot(x, true_regression_line, ='true regression line', lw=3., c='y')
label; plt.legend()
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
There, much better! The outliers are barely influencing our estimation at all because our likelihood function assumes that outliers are much more probable than under the Normal distribution.
Summary
PyMC3
’sglm()
function allows you to pass in afamily
object that contains information about the likelihood.- By changing the likelihood from a Normal distribution to a Student T distribution – which has more mass in the tails – we can perform Robust Regression.
The next post will be about logistic regression in PyMC3 and what the posterior and oatmeal have in common.
Extensions:
- The Student-T distribution has, besides the mean and variance, a third parameter called degrees of freedom that describes how much mass should be put into the tails. Here it is set to 1 which gives maximum mass to the tails (setting this to infinity results in a Normal distribution!). One could easily place a prior on this rather than fixing it which I leave as an exercise for the reader ;).
- T distributions can be used as priors as well. I will show this in a future post on hierarchical GLMs.
- How do we test if our data is normal or violates that assumption in an important way? Check out this great blog post by Allen Downey.