# Spline regression via PyMC3

I've looked through PyMC3 documentation and haven't seen any tutorials/resources on learning to use Splines w/ PyMC3. Could anyone recommend a resource? I see that Stan tutorials are available. I figure if one isn't available in the official PyMC3 documentation, someone here might have written one.

Here is a minimal example, which works for a DataFrame df with columns X and Y. It uses patsy, which is (still) my go-to package for splines in Python

df = pd.DataFrame({'X':np.arange(-5, 5, .1),
})
B = patsy.dmatrix('bs(X, knots=np.arange(-5,5,1), degree=3)', df,
return_type='dataframe')

with pm.Model() as model:
a_raw = pm.Normal('a_raw', mu=0, sd=1, shape=num_basis)
tau = pm.Cauchy('tau', alpha=1, beta=1)
sigma = pm.Cauchy('sigma', alpha=1, beta=1)
y_hat = pm.Deterministic('y_hat', tt.dot(B, a_raw*tau))
y = pm.Normal('y', mu=y_hat, sd=sigma, observed=df.Y)
trace = pm.sample(500, cores=2)


It seems to work:

Here is a notebook that puts the whole thing in context: https://gist.github.com/aflaxman/d34ceddec6663c15930abff7257d84f9

• Wow, thanks! Question- B = patsy.dmatrix('bs(X, knots=np.arange(-5,5,1), degree=3)', df, return_type='dataframe') I notice that changes to degree increase/decrease the number of columns produced. I believe that degree affects the number of knots influencing any given data point, but the total number of knots should be the same, regardless. So I'm confused...why is it variable? Nov 12, 2020 at 14:38