9

I'm relatively new to PYMC3 and I'm trying to implement a Bayesian Structure Time Series (BSTS) without regressors, for instance the model fit here in R. The model is as follows:

model

I can implement the local linear trend using a GaussianRandomWalk as follows:

delta = pymc3.GaussianRandomWalk('delta',mu=0,sd=1,shape=99)
mu = pymc3.GaussianRandomWalk('mu',mu=delta,sd=1,shape=100)

However, I'm at a loss for how to encode the seasonal variable (tau) in PYMC3. Do I need to roll a custom random walk class or is there some other trick?

Paul
  • 503
  • 4
  • 15

1 Answers1

0

You can use

w = pm.Normal('w', sd=sigma_tau, shape=S)
tau = w - tt.concatenate([[0.], w.cumsum()[:-1]])

Depending on the data it might also be faster to use cumsum for the other random walks, that often avoids correlations in the posterior, which makes life easier for the sampler.

aseyboldt
  • 1,090
  • 8
  • 14
  • Its not really clear to me why this would give the correct form of tau, can you elaborate? At any rate, this produces a dimension mismatch. If I take S = 12, then tau is a 12-dimensional vector whereas mu is a 100-dimensional. This prevents me from forming y=pm.Normal('y',mu=mu+tau,sd=sigma_y,observed=y_train). Also, I guess when you say cumsum trick for the walks you mean u=pm.Normal('u',sd=sigma_delta) and delta=u+tt.concatenate([[0.],u.cumsum()[:-1]])? – Paul Aug 22 '17 at 17:27
  • 1
    I would like to accept your response so that you can get your reputation, but I cannot without clairification regarding tensor shapes and forming y (see previous comment). – Paul Aug 28 '17 at 19:20