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.


1 Answer 1


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),
                   'Y':Y  # add your data here
B = patsy.dmatrix('bs(X, knots=np.arange(-5,5,1), degree=3)', df, 

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: visual evidence of success

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

  • 1
    $\begingroup$ 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? $\endgroup$
    – jbuddy_13
    Nov 12, 2020 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.