Splines#

Introduction#

Often, the model we want to fit is not a perfect line between some \(x\) and \(y\). Instead, the parameters of the model are expected to vary over \(x\). There are multiple ways to handle this situation, one of which is to fit a spline. The spline is effectively multiple individual lines, each fit to a different section of \(x\), that are tied together at their boundaries, often called knots.

Below is a full working example of how to fit a spline using PyMC. The data and model are taken from Statistical Rethinking 2e by Richard McElreath’s [McElreath, 2018].

For more information on this method of non-linear modeling, I suggesting beginning with chapter 5 of Bayesian Modeling and Computation in Python [Martin et al., 2021].

from pathlib import Path

import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pymc as pm

from patsy import dmatrix
%matplotlib inline
%config InlineBackend.figure_format = "retina"

RANDOM_SEED = 8927
az.style.use("arviz-darkgrid")

Cherry blossom data#

The data for this example is the number of days (doy for “days of year”) that the cherry trees were in bloom in each year (year). For convenience, years missing a doy were dropped (which is a bad idea to deal with missing data in general!).

try:
    blossom_data = pd.read_csv(Path("..", "data", "cherry_blossoms.csv"), sep=";")
except FileNotFoundError:
    blossom_data = pd.read_csv(pm.get_data("cherry_blossoms.csv"), sep=";")


blossom_data.dropna().describe()
year doy temp temp_upper temp_lower
count 787.000000 787.00000 787.000000 787.000000 787.000000
mean 1533.395172 104.92122 6.100356 6.937560 5.263545
std 291.122597 6.25773 0.683410 0.811986 0.762194
min 851.000000 86.00000 4.690000 5.450000 2.610000
25% 1318.000000 101.00000 5.625000 6.380000 4.770000
50% 1563.000000 105.00000 6.060000 6.800000 5.250000
75% 1778.500000 109.00000 6.460000 7.375000 5.650000
max 1980.000000 124.00000 8.300000 12.100000 7.740000
blossom_data = blossom_data.dropna(subset=["doy"]).reset_index(drop=True)
blossom_data.head(n=10)
year doy temp temp_upper temp_lower
0 812 92.0 NaN NaN NaN
1 815 105.0 NaN NaN NaN
2 831 96.0 NaN NaN NaN
3 851 108.0 7.38 12.10 2.66
4 853 104.0 NaN NaN NaN
5 864 100.0 6.42 8.69 4.14
6 866 106.0 6.44 8.11 4.77
7 869 95.0 NaN NaN NaN
8 889 104.0 6.83 8.48 5.19
9 891 109.0 6.98 8.96 5.00

After dropping rows with missing data, there are 827 years with the numbers of days in which the trees were in bloom.

blossom_data.shape
(827, 5)

If we visualize the data, it is clear that there a lot of annual variation, but some evidence for a non-linear trend in bloom days over time.

blossom_data.plot.scatter(
    "year", "doy", color="cornflowerblue", s=10, title="Cherry Blossom Data", ylabel="Days in bloom"
);
../_images/1945097dee689f38b03c82f9ce3063cbf25a11941d53138fd2ed4746b465bc12.png

The model#

We will fit the following model.

\(D \sim \mathcal{N}(\mu, \sigma)\)
\(\quad \mu = a + Bw\)
\(\qquad a \sim \mathcal{N}(100, 10)\)
\(\qquad w \sim \mathcal{N}(0, 10)\)
\(\quad \sigma \sim \text{Exp}(1)\)

The number of days of bloom \(D\) will be modeled as a normal distribution with mean \(\mu\) and standard deviation \(\sigma\). In turn, the mean will be a linear model composed of a y-intercept \(a\) and spline defined by the basis \(B\) multiplied by the model parameter \(w\) with a variable for each region of the basis. Both have relatively weak normal priors.

Prepare the spline#

The spline will have 15 knots, splitting the year into 16 sections (including the regions covering the years before and after those in which we have data). The knots are the boundaries of the spline, the name owing to how the individual lines will be tied together at these boundaries to make a continuous and smooth curve. The knots will be unevenly spaced over the years such that each region will have the same proportion of data.

num_knots = 15
knot_list = np.quantile(blossom_data.year, np.linspace(0, 1, num_knots))
knot_list
array([ 812., 1036., 1174., 1269., 1377., 1454., 1518., 1583., 1650.,
       1714., 1774., 1833., 1893., 1956., 2015.])

Below is a plot of the locations of the knots over the data.

blossom_data.plot.scatter(
    "year", "doy", color="cornflowerblue", s=10, title="Cherry Blossom Data", ylabel="Day of Year"
)
for knot in knot_list:
    plt.gca().axvline(knot, color="grey", alpha=0.4);
../_images/59bdc733c35c1aaf7872e3262df04f794b7f5e2e55183d890662e4545b219ecc.png

We can use patsy to create the matrix \(B\) that will be the b-spline basis for the regression. The degree is set to 3 to create a cubic b-spline.

B = dmatrix(
    "bs(year, knots=knots, degree=3, include_intercept=True) - 1",
    {"year": blossom_data.year.values, "knots": knot_list[1:-1]},
)
B
DesignMatrix with shape (827, 17)
  Columns:
    ['bs(year, knots=knots, degree=3, include_intercept=True)[0]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[1]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[2]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[3]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[4]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[5]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[6]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[7]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[8]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[9]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[10]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[11]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[12]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[13]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[14]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[15]',
     'bs(year, knots=knots, degree=3, include_intercept=True)[16]']
  Terms:
    'bs(year, knots=knots, degree=3, include_intercept=True)' (columns 0:17)
  (to view full data, use np.asarray(this_obj))

The b-spline basis is plotted below, showing the domain of each piece of the spline. The height of each curve indicates how influential the corresponding model covariate (one per spline region) will be on model’s inference of that region. The overlapping regions represent the knots, showing how the smooth transition from one region to the next is formed.

spline_df = (
    pd.DataFrame(B)
    .assign(year=blossom_data.year.values)
    .melt("year", var_name="spline_i", value_name="value")
)

color = plt.cm.magma(np.linspace(0, 0.80, len(spline_df.spline_i.unique())))

fig = plt.figure()
for i, c in enumerate(color):
    subset = spline_df.query(f"spline_i == {i}")
    subset.plot("year", "value", c=c, ax=plt.gca(), label=i)
plt.legend(title="Spline Index", loc="upper center", fontsize=8, ncol=6);
../_images/69c27e84e3f422f6887481f616c2e358df34cf919f8d2cffb5a4ad4bb70a2ba1.png

Fit the model#

Finally, the model can be built using PyMC. A graphical diagram shows the organization of the model parameters (note that this requires the installation of python-graphviz, which I recommend doing in a conda virtual environment).

COORDS = {"splines": np.arange(B.shape[1])}
with pm.Model(coords=COORDS, rng_seeder=RANDOM_SEED) as spline_model:
    a = pm.Normal("a", 100, 5)
    w = pm.Normal("w", mu=0, sigma=3, size=B.shape[1], dims="splines")
    mu = pm.Deterministic("mu", a + pm.math.dot(np.asarray(B, order="F"), w.T))
    sigma = pm.Exponential("sigma", 1)
    D = pm.Normal("D", mu=mu, sigma=sigma, observed=blossom_data.doy, dims="obs")
pm.model_to_graphviz(spline_model)
../_images/17cd70137a277e67d62658427dffefe09a19ffdd5c576bf752546f44947c01fa.svg
with spline_model:
    idata = pm.sample_prior_predictive()
    idata.extend(pm.sample(draws=1000, tune=1000, random_seed=RANDOM_SEED, chains=4))
    pm.sample_posterior_predictive(idata, extend_inferencedata=True)
/Users/benjamv/opt/miniconda3/envs/pymc_env/lib/python3.10/site-packages/pymc/aesaraf.py:1005: UserWarning: The parameter 'updates' of aesara.function() expects an OrderedDict, got <class 'dict'>. Using a standard dictionary here results in non-deterministic behavior. You should use an OrderedDict if you are using Python 2.7 (collections.OrderedDict for older python), or use a list of (shared, update) pairs. Do not just convert your dictionary to this type before the call as the conversion will still be non-deterministic.
  aesara_function = aesara.function(
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
/Users/benjamv/opt/miniconda3/envs/pymc_env/lib/python3.10/site-packages/pymc/aesaraf.py:1005: UserWarning: The parameter 'updates' of aesara.function() expects an OrderedDict, got <class 'dict'>. Using a standard dictionary here results in non-deterministic behavior. You should use an OrderedDict if you are using Python 2.7 (collections.OrderedDict for older python), or use a list of (shared, update) pairs. Do not just convert your dictionary to this type before the call as the conversion will still be non-deterministic.
  aesara_function = aesara.function(
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [a, w, sigma]
100.00% [8000/8000 00:07<00:00 Sampling 4 chains, 0 divergences]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 29 seconds.
100.00% [4000/4000 00:00<00:00]

Analysis#

Now we can analyze the draws from the posterior of the model.

Parameter Estimates#

Below is a table summarizing the posterior distributions of the model parameters. The posteriors of \(a\) and \(\sigma\) are quite narrow while those for \(w\) are wider. This is likely because all of the data points are used to estimate \(a\) and \(\sigma\) whereas only a subset are used for each value of \(w\). (It could be interesting to model these hierarchically allowing for the sharing of information and adding regularization across the spline.) The effective sample size and \(\widehat{R}\) values all look good, indicating that the model has converged and sampled well from the posterior distribution.

az.summary(idata, var_names=["a", "w", "sigma"])
mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
a 103.673 0.781 102.169 105.071 0.021 0.015 1437.0 2008.0 1.0
w[0] -1.821 2.216 -5.899 2.384 0.034 0.029 4240.0 3196.0 1.0
w[1] -1.662 2.061 -5.407 2.257 0.038 0.028 2863.0 3048.0 1.0
w[2] -0.303 1.929 -4.052 3.243 0.035 0.028 3066.0 2875.0 1.0
w[3] 3.329 1.503 0.628 6.191 0.031 0.022 2310.0 2662.0 1.0
w[4] 0.157 1.555 -2.746 3.031 0.029 0.021 2786.0 3043.0 1.0
w[5] 2.087 1.594 -0.853 5.098 0.032 0.022 2530.0 2985.0 1.0
w[6] -3.604 1.469 -6.320 -0.875 0.029 0.020 2625.0 2843.0 1.0
w[7] 5.515 1.487 2.688 8.265 0.029 0.021 2571.0 2755.0 1.0
w[8] -0.110 1.563 -2.954 2.857 0.029 0.022 2814.0 2896.0 1.0
w[9] 2.206 1.596 -0.804 5.268 0.030 0.021 2801.0 2804.0 1.0
w[10] 3.737 1.552 0.885 6.680 0.031 0.022 2509.0 2798.0 1.0
w[11] 0.329 1.534 -2.408 3.343 0.029 0.020 2810.0 3199.0 1.0
w[12] 4.100 1.512 1.143 6.857 0.028 0.020 2981.0 3127.0 1.0
w[13] 0.993 1.612 -2.324 3.767 0.031 0.022 2686.0 3007.0 1.0
w[14] -1.808 1.790 -5.167 1.553 0.031 0.023 3265.0 3301.0 1.0
w[15] -6.020 1.920 -9.741 -2.588 0.036 0.025 2920.0 2952.0 1.0
w[16] -6.205 1.896 -9.707 -2.470 0.031 0.022 3725.0 3109.0 1.0
sigma 5.953 0.145 5.679 6.224 0.002 0.001 4755.0 3020.0 1.0

The trace plots of the model parameters look good (homogeneous and no sign of trend), further indicating that the chains converged and mixed.

az.plot_trace(idata, var_names=["a", "w", "sigma"]);
../_images/d9e08e5ca5cbd94161bc78294cab193f0bc7737d2b80345a92e69292a66d6d6b.png
az.plot_forest(idata, var_names=["w"], combined=False, r_hat=True);
../_images/ceafe5ada95f54894f7416003a140716e54e869eca9f082114dd8e8fd21267d7.png

Another visualization of the fit spline values is to plot them multiplied against the basis matrix. The knot boundaries are shown as vertical lines again, but now the spline basis is multiplied against the values of \(w\) (represented as the rainbow-colored curves). The dot product of \(B\) and \(w\) – the actual computation in the linear model – is shown in black.

wp = idata.posterior["w"].mean(("chain", "draw")).values

spline_df = (
    pd.DataFrame(B * wp.T)
    .assign(year=blossom_data.year.values)
    .melt("year", var_name="spline_i", value_name="value")
)

spline_df_merged = (
    pd.DataFrame(np.dot(B, wp.T))
    .assign(year=blossom_data.year.values)
    .melt("year", var_name="spline_i", value_name="value")
)


color = plt.cm.rainbow(np.linspace(0, 1, len(spline_df.spline_i.unique())))
fig = plt.figure()
for i, c in enumerate(color):
    subset = spline_df.query(f"spline_i == {i}")
    subset.plot("year", "value", c=c, ax=plt.gca(), label=i)
spline_df_merged.plot("year", "value", c="black", lw=2, ax=plt.gca())
plt.legend(title="Spline Index", loc="lower center", fontsize=8, ncol=6)

for knot in knot_list:
    plt.gca().axvline(knot, color="grey", alpha=0.4);
../_images/1655ccbb62d84e59770d4ee3852fe94e427a9e9e16efa5b5e721f3116e7dd3c9.png

Model predictions#

Lastly, we can visualize the predictions of the model using the posterior predictive check.

post_pred = az.summary(idata, var_names=["mu"]).reset_index(drop=True)
blossom_data_post = blossom_data.copy().reset_index(drop=True)
blossom_data_post["pred_mean"] = post_pred["mean"]
blossom_data_post["pred_hdi_lower"] = post_pred["hdi_3%"]
blossom_data_post["pred_hdi_upper"] = post_pred["hdi_97%"]
blossom_data.plot.scatter(
    "year",
    "doy",
    color="cornflowerblue",
    s=10,
    title="Cherry blossom data with posterior predictions",
    ylabel="Days in bloom",
)
for knot in knot_list:
    plt.gca().axvline(knot, color="grey", alpha=0.4)

blossom_data_post.plot("year", "pred_mean", ax=plt.gca(), lw=3, color="firebrick")
plt.fill_between(
    blossom_data_post.year,
    blossom_data_post.pred_hdi_lower,
    blossom_data_post.pred_hdi_upper,
    color="firebrick",
    alpha=0.4,
);
../_images/96370216b8ffdc458f10a7a41d7c33961ba69dbf2ddab96bec4aaa2a8f4aaf06.png

References#

1

Richard McElreath. Statistical rethinking: A Bayesian course with examples in R and Stan. Chapman and Hall/CRC, 2018.

2

Osvaldo A Martin, Ravin Kumar, and Junpeng Lao. Bayesian Modeling and Computation in Python. Chapman and Hall/CRC, 2021. doi:10.1201/9781003019169.

Authors#

  • Created by Joshua Cook

  • Updated by Tyler James Burch

  • Updated by Chris Fonnesbeck

Watermark#

%load_ext watermark
%watermark -n -u -v -iv -w -p aesara,xarray,patsy
Last updated: Sun Jun 05 2022

Python implementation: CPython
Python version       : 3.10.4
IPython version      : 8.3.0

aesara: 2.5.1
xarray: 2022.3.0
patsy : 0.5.2

sys       : 3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:45:10) [Clang 12.0.1 ]
pymc      : 4.0.0b6
matplotlib: 3.5.1
numpy     : 1.21.6
arviz     : 0.12.1
pandas    : 1.4.2

Watermark: 2.3.1

License notice#

All the notebooks in this example gallery are provided under the MIT License which allows modification, and redistribution for any use provided the copyright and license notices are preserved.

Citing PyMC examples#

To cite this notebook, use the DOI provided by Zenodo for the pymc-examples repository.

Important

Many notebooks are adapted from other sources: blogs, books… In such cases you should cite the original source as well.

Also remember to cite the relevant libraries used by your code.

Here is an citation template in bibtex:

@incollection{citekey,
  author    = "<notebook authors, see above>"
  title     = "<notebook title>",
  editor    = "PyMC Team",
  booktitle = "PyMC examples",
  doi       = "10.5281/zenodo.5654871"
}

which once rendered could look like: