In this tutorial, we will demonstrate how to build a model for time series forecasting in NumPyro. Specifically, we will replicate the Seasonal, Global Trend (SGT) model from the Rlgt: Bayesian Exponential Smoothing Models with Trend Modifications package. The time series data that we will use for this tutorial is the lynx dataset, which contains annual numbers of lynx trappings from 1821 to 1934 in Canada.
import os
from IPython.display import set_matplotlib_formats
import matplotlib.pyplot as plt
import pandas as pd
import jax.numpy as np
from jax import lax, random, vmap
from jax.nn import softmax
import numpyro; numpyro.set_host_device_count(4)
import numpyro.distributions as dist
from numpyro.diagnostics import autocorrelation, hpdi
from numpyro import handlers
from numpyro.infer import MCMC, NUTS, Predictive
if "NUMPYRO_SPHINXBUILD" in os.environ:
set_matplotlib_formats('svg')
assert numpyro.__version__.startswith('0.2.4')
First, lets import and take a look at the dataset.
URL = "https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/lynx.csv"
lynx = pd.read_csv(URL, index_col=0)
data = lynx["value"].values
print("Length of time series:", data.shape[0])
plt.figure(figsize=(8, 4))
plt.plot(lynx["time"], data)
plt.show()
Length of time series: 114
The time series has a length of 114 (a data point for each year), and by looking at the plot, we can observe seasonality in this dataset, which is the recurrence of similar patterns at specific time periods. e.g. in this dataset, we observe a cyclical pattern every 10 years, but there is also a less obvious but clear spike in the number of trappings every 40 years. Let us see if we can model this effect in NumPyro.
In this tutorial, we will use the first 80 values for training and the last 34 values for testing.
y_train, y_test = np.array(data[:80], dtype=np.float32), data[80:]
The model we are going to use is called Seasonal, Global Trend, which when tested on 3003 time series of the M-3 competition, has been known to outperform other models originally participating in the competition:
$$ \begin{align} \text{exp_val}_{t} &= \text{level}_{t-1} + \text{coef_trend} \times \text{level}_{t-1}^{\text{pow_trend}} + \text{s}_t \times \text{level}_{t-1}^{\text{pow_season}}, \\ \sigma_{t} &= \sigma \times \text{exp_val}_{t}^{\text{powx}} + \text{offset}, \\ y_{t} &\sim \text{StudentT}(\nu, \text{exp_val}_{t}, \sigma_{t}) \end{align} $$, where level
and s
follows the following recursion rules:
A more detailed explanation for SGT model can be found in this vignette from the authors of the Rlgt package. Here we summarize the core ideas of this model:
exp_val
consists of a trending component and a seasonal component:level
, $a$ is coef_trend
, and $b$ is pow_trend
. Note that when $b \sim 0$, the trend is linear with $a$ is the slope, and when $b \sim 1$, the trend is exponential with $a$ is the rate. So that function can cover a large family of trend.level
and s
are updated to new values. Coefficients level_sm
and s_sm
are used to make the transition smoothly.powx
is near $0$, the error $\sigma_t$ will be nearly constant while when powx
is near $1$, the error will be propotional to the expected value.Note that level
and s
are updated recursively while we collect the expected value at each time step. NumPyro uses JAX in the backend to JIT compile many critical parts of the NUTS algorithm, including the verlet integrator and the tree building process. However, doing so using Python's for
loop in the model will result in a long compilation time for the model, so we use jax.lax.scan
instead. A detailed explanation for using this utility can be found in lax.scan documentation. Here we use it to collect expected values while the pair (level, s)
plays the role of carrying state.
def scan_exp_val(
y, init_s, level_sm, s_sm, coef_trend, pow_trend, pow_season, future=0):
N = y.shape[0]
duration = N + future
seasonality = init_s.shape[0]
def scan_fn(carry, t):
level, s, moving_sum = carry
season = s[0] * level ** pow_season
exp_val = level + coef_trend * level ** pow_trend + season
exp_val = np.clip(exp_val, a_min=0)
# use exoected vale when forecasting
y_t = np.where(t >= N, exp_val, y[t])
moving_sum = moving_sum + y[t] - \
np.where(t >= seasonality, y[t - seasonality], 0.)
level_p = np.where(t >= seasonality, moving_sum / seasonality, y_t - season)
level = level_sm * level_p + (1 - level_sm) * level
level = np.clip(level, a_min=0)
new_s = (s_sm * (y_t - level) / season + (1 - s_sm)) * s[0]
# repeat s when forecasting
new_s = np.where(t >= N, s[0], new_s)
s = np.concatenate([s[1:], new_s[None]], axis=0)
return (level, s, moving_sum), exp_val
level_init = y[0]
s_init = np.concatenate([init_s[1:], init_s[:1]], axis=0)
moving_sum = level_init
(last_level, last_s, moving_sum), exp_vals = lax.scan(
scan_fn, (level_init, s_init, moving_sum), np.arange(1, duration))
return exp_vals, last_level, last_s
With our utility function defined above, we are ready to specify the model using NumPyro primitives. In NumPyro, we use the primitive sample(name, prior)
to declare a latent random variable with a corresponding prior
. These primitives can have custom interpretations depending on the effect handlers that are used by NumPyro inference algorithms in the backend. e.g. we can condition on specific values using the substitute
handler, or record values at these sample sites in the execution trace using the trace
handler. Note that these details are not important for specifying the model, or running inference, but curious readers are encouraged to read the tutorial on effect handlers in Pyro.
def sgt(y, seasonality, future=0):
# heuristically, standard derivation of Cauchy prior depends on
# the max value of data
cauchy_sd = np.max(y) / 150
nu = numpyro.sample("nu", dist.Uniform(2, 20))
powx = numpyro.sample("powx", dist.Uniform(0, 1))
sigma = numpyro.sample("sigma", dist.HalfCauchy(cauchy_sd))
offset_sigma = numpyro.sample("offset_sigma", dist.TruncatedCauchy(
low=1e-10, loc=1e-10, scale=cauchy_sd))
coef_trend = numpyro.sample("coef_trend", dist.Cauchy(0, cauchy_sd))
pow_trend_beta = numpyro.sample("pow_trend_beta", dist.Beta(1, 1))
# pow_trend takes values from -0.5 to 1
pow_trend = 1.5 * pow_trend_beta - 0.5
pow_season = numpyro.sample("pow_season", dist.Beta(1, 1))
level_sm = numpyro.sample("level_sm", dist.Beta(1, 2))
s_sm = numpyro.sample("s_sm", dist.Uniform(0, 1))
init_s = numpyro.sample("init_s", dist.Cauchy(0, y[:seasonality] * 0.3))
exp_val, last_level, last_s = scan_exp_val(
y, init_s, level_sm, s_sm, coef_trend, pow_trend, pow_season, future=future)
if future == 0: # training
omega = sigma * exp_val ** powx + offset_sigma
numpyro.sample("y", dist.StudentT(nu, exp_val, omega), obs=y[1:])
# we return last `level` and last `s` for custom forecasting
return last_level, last_s
else: # forecasting
exp_val = exp_val[y.shape[0] - 1:]
assert exp_val.shape[0] == future
omega = sigma * exp_val ** powx + offset_sigma
numpyro.sample("y", dist.StudentT(nu, exp_val, omega))
Note that all prior parameters are retrieved from this file in the original source.
First, we want to choose a good value for seasonality
. Following the demo in Rlgt, we will set seasonality=38
. Indeed, this value can be guessed by looking at the plot of the training data, where the second order seasonality effect has a periodicity around $40$ years. Note that $38$ is also one of the highest-autocorrelation lags.
print("Lag values sorted according to their autocorrelation values:\n")
print(np.argsort(autocorrelation(y_train))[::-1])
Lag values sorted according to their autocorrelation values: [ 0 67 57 38 68 1 29 58 37 56 28 10 19 39 66 78 47 77 9 79 48 76 30 18 20 11 46 59 69 27 55 36 2 8 40 49 17 21 75 12 65 45 31 26 7 54 35 41 50 3 22 60 70 16 44 13 6 25 74 53 42 32 23 43 51 4 15 14 34 24 5 52 73 64 33 71 72 61 63 62]
Now, let us run $4$ MCMC chains (using the No-U-Turn Sampler algorithm) with $5000$ warmup steps and $5000$ sampling steps per each chain. The returned value will be a collection of $20000$ samples.
%%time
kernel = NUTS(sgt)
mcmc = MCMC(kernel, num_warmup=5000, num_samples=5000, num_chains=4)
mcmc.run(random.PRNGKey(2), y_train, seasonality=38)
mcmc.print_summary()
samples = mcmc.get_samples()
mean std median 5.0% 95.0% n_eff r_hat coef_trend 35.90 141.51 13.49 -89.69 160.01 1253.52 1.00 init_s[0] 93.88 115.72 68.03 -48.20 267.54 563.41 1.01 init_s[1] -20.74 68.27 -22.57 -132.33 86.30 5638.05 1.00 init_s[2] 31.34 91.71 21.30 -109.71 172.00 5751.88 1.00 init_s[3] 126.35 123.06 109.89 -67.25 304.43 5308.82 1.00 init_s[4] 449.81 249.57 407.17 72.60 799.35 3962.26 1.00 init_s[5] 1192.26 459.23 1124.14 481.37 1836.72 2008.70 1.00 init_s[6] 2013.44 656.04 1932.91 967.32 2981.27 2277.62 1.00 init_s[7] 3725.03 1110.80 3613.86 1992.54 5409.12 1860.04 1.00 init_s[8] 2606.46 840.53 2476.63 1328.71 3882.42 1682.46 1.01 init_s[9] 956.78 426.92 899.53 306.73 1601.91 3984.62 1.00 init_s[10] 50.27 105.87 36.33 -106.86 205.88 5185.16 1.00 init_s[11] -0.19 56.79 -2.60 -77.66 75.06 1584.26 1.00 init_s[12] -7.15 67.90 -10.02 -106.90 98.28 708.60 1.00 init_s[13] 66.97 100.33 47.12 -74.98 216.03 5203.29 1.00 init_s[14] 335.98 259.67 276.09 -12.78 709.36 3362.87 1.00 init_s[15] 967.16 400.29 904.31 385.98 1558.11 849.66 1.01 init_s[16] 1271.75 465.74 1209.79 557.99 1961.61 2849.68 1.00 init_s[17] 1386.30 547.93 1284.83 571.16 2217.53 2725.54 1.00 init_s[18] 613.97 307.38 550.73 175.59 1070.71 3319.73 1.00 init_s[19] 16.35 92.00 3.67 -119.55 147.91 1989.23 1.00 init_s[20] -30.11 66.05 -23.57 -144.37 63.33 4514.26 1.00 init_s[21] -16.96 47.14 -6.20 -93.59 42.93 1696.88 1.00 init_s[22] -0.60 43.07 -0.91 -65.73 61.91 3235.73 1.00 init_s[23] 39.57 83.49 24.38 -82.75 165.80 5226.04 1.00 init_s[24] 529.39 330.23 475.21 10.02 986.28 4837.91 1.00 init_s[25] 942.35 457.37 864.48 277.80 1594.75 3319.35 1.00 init_s[26] 1855.40 912.22 1731.51 765.43 2798.29 632.94 1.01 init_s[27] 1308.19 492.55 1231.14 542.53 1982.89 715.91 1.00 init_s[28] 217.56 172.60 181.49 -27.86 469.58 4992.26 1.00 init_s[29] -11.99 82.70 -19.44 -142.68 108.59 2410.68 1.00 init_s[30] -1.89 87.65 -8.74 -136.86 121.59 5177.89 1.00 init_s[31] -39.47 75.38 -37.94 -170.10 70.53 283.89 1.01 init_s[32] -9.67 86.13 -19.10 -141.70 119.14 5254.49 1.00 init_s[33] 120.82 134.18 100.77 -82.96 317.41 4123.89 1.00 init_s[34] 504.63 302.64 451.40 90.37 929.83 1448.21 1.00 init_s[35] 1101.46 463.20 1026.01 443.47 1797.68 725.92 1.01 init_s[36] 1891.59 666.78 1793.22 884.27 2871.91 1886.95 1.00 init_s[37] 1433.02 573.39 1352.83 525.74 2222.67 463.54 1.01 level_sm 0.00 0.00 0.00 0.00 0.00 3763.91 1.00 nu 12.25 4.73 12.72 5.40 20.00 6521.37 1.00 offset_sigma 33.50 30.53 25.27 0.00 72.23 5231.03 1.00 pow_season 0.08 0.04 0.08 0.01 0.14 1244.31 1.01 pow_trend_beta 0.26 0.18 0.24 0.00 0.51 135.08 1.02 powx 0.63 0.14 0.62 0.42 0.86 358.39 1.01 s_sm 0.08 0.09 0.06 0.00 0.20 637.77 1.01 sigma 9.24 9.60 6.29 0.33 20.04 2796.07 1.00 Number of divergences: 4402 CPU times: user 1min 15s, sys: 219 ms, total: 1min 16s Wall time: 48.6 s
Given samples
from mcmc
, we want to do forecasting for the testing dataset y_test
. First, we will make some utilities to do forecasting given a sample. Note that to retrieve the last level
and last s
value, we run the model forward by constraining the latent sites to a sample from the posterior using the substitute
handler:
... level, s = substitute(sgt, sample)(y, seasonality)
# Ref: https://github.com/cbergmeir/Rlgt/blob/master/Rlgt/R/forecast.rlgtfit.R
def sgt_forecast(future, sample, y, level, s):
seasonality = s.shape[0]
moving_sum = np.sum(y[-seasonality:])
pow_trend = 1.5 * sample["pow_trend_beta"] - 0.5
yfs = [0] * (seasonality + future)
for t in range(future):
season = s[0] * level ** sample["pow_season"]
exp_val = level + sample["coef_trend"] * level ** pow_trend + season
exp_val = np.clip(exp_val, a_min=0)
omega = sample["sigma"] * exp_val ** sample["powx"] + sample["offset_sigma"]
yf = numpyro.sample("yf[{}]".format(t), dist.StudentT(
sample["nu"], exp_val, omega))
yf = np.clip(yf, a_min=1e-30)
yfs[t] = yf
moving_sum = moving_sum + yf - \
np.where(t >= seasonality, yfs[t - seasonality], y[-seasonality + t])
level_p = moving_sum / seasonality
level_tmp = sample["level_sm"] * level_p + (1 - sample["level_sm"]) * level
level = np.where(level_tmp > 1e-30, level_tmp, level)
# s is repeated instead of being updated
s = np.concatenate([s[1:], s[:1]], axis=0)
def forecast(future, rng_key, sample, y, seasonality):
level, s = handlers.substitute(sgt, sample)(y, seasonality)
forecast_model = handlers.seed(sgt_forecast, rng_key)
forecast_trace = handlers.trace(forecast_model).get_trace(
future, sample, y, level, s)
results = [np.clip(forecast_trace["yf[{}]".format(t)]["value"], a_min=1e-30)
for t in range(future)]
return np.stack(results, axis=0)
Then, we can use jax.vmap to get prediction given a collection of samples. This allows us to vectorize the computation across the test dataset which can be dramatically faster as compared to using for-loop to collect predictions per test data point.
rng_keys = random.split(random.PRNGKey(3), samples["nu"].shape[0])
forecast_marginal = vmap(lambda rng_key, sample: forecast(
len(y_test), rng_key, sample, y_train, seasonality=38))(rng_keys, samples)
Finally, let's get sMAPE, root mean square error of the prediction, and visualize the result with the mean prediction and the 90% highest posterior density interval (HPDI).
y_pred = np.mean(forecast_marginal, axis=0)
sMAPE = np.mean(np.abs(y_pred - y_test) / (y_pred + y_test)) * 200
msqrt = np.sqrt(np.mean((y_pred - y_test) ** 2))
print("sMAPE: {:.2f}, rmse: {:.2f}".format(sMAPE, msqrt))
sMAPE: 62.87, rmse: 1248.86
plt.figure(figsize=(8, 4))
plt.plot(lynx["time"], data)
t_future = lynx["time"][80:]
hpd_low, hpd_high = hpdi(forecast_marginal)
plt.plot(t_future, y_pred, lw=2)
plt.fill_between(t_future, hpd_low, hpd_high, alpha=0.3)
plt.title("Forecasting lynx dataset with SGT model (90% HPDI)")
plt.show()
As we can observe, the model has been able to learn both the first and second order seasonality effects, i.e. a cyclical pattern with a periodicity of around 10, as well as spikes that can be seen once every 40 or so years. Moreover, we not only have point estimates for the forecast but can also use the uncertainty estimates from the model to bound our forecasts.
NumPyro provides a convenient utility Predictive to get predictive distribution. Let's see how to use it to get forecasting values.
Notice that in the sgt
model defined above, there is a keyword future
which controls the execution of the model - depending on whether future > 0
or future == 0
. The following code predicts the last 34 values from the original time-series.
predictive = Predictive(sgt, samples, return_sites=["y"])
forecast2 = predictive(random.PRNGKey(4), y_train, seasonality=38, future=34)["y"]
Let's plot the result to verify that we get the expected one.
plt.figure(figsize=(8, 4))
plt.plot(lynx["time"], data)
t_future = lynx["time"][80:]
hpd_low, hpd_high = hpdi(forecast2)
plt.plot(t_future, np.mean(forecast2, axis=0), lw=2)
plt.fill_between(t_future, hpd_low, hpd_high, alpha=0.3)
plt.title("Forecasting lynx dataset with SGT model (90% HPDI)")
plt.show()
We would like to thank Slawek Smyl for many helpful resources and suggestions. Fast inference would not have been possible without the support of JAX and the XLA teams, so we would like to thank them for providing such a great open-source platform for us to build on, and for their responsiveness in dealing with our feature requests and bug reports.
[1] Rlgt: Bayesian Exponential Smoothing Models with Trend Modifications
,
Slawek Smyl, Christoph Bergmeir, Erwin Wibowo, To Wang Ng, Trustees of Columbia University