# Vanilla autoregressive model?

I’m curious if someone has implemented a vanilla auto regressive model for time series. I know PyMC3 offers some classes tailored explicitly for time series. But I’m quite new to it and would like to start with something that has not abstracted any details away.

In other words, I’m looking for the simplest model possible. I believe the below is something I came across in a textbook.

Y_i ~B1 *X + B2 * Y_i-1

You have a toy example below. You are lacking the error terms in your equation.

Data generation process:

``````N = 52*6
y = np.zeros((N,))

true_theta = 0.8
true_tau = 1.0
true_center = 0.0

true_beta = 0.5

x = np.sin(np.arange(N))

for t in range(1, N):
y[t] = true_theta * y[t - 1] + np.random.normal(loc=true_center, scale=true_tau)

y = np.random.normal(y + true_beta * x, 1)
plt.plot(y);
``````

Model and results:

``````with pm.Model() as model:
theta = pm.Normal('theta', 0,0.5)
beta = pm.Normal('beta', 0,0.5)

tau = pm.HalfNormal('tau', 0.5)

ar = pm.AR('ar', theta, tau, shape=N)
sig = pm.HalfNormal('sig', 0.5)

y_ = pm.Normal('y_', beta * x + ar, sig, observed=y)
trace = pm.sample(2000)
az.summary(trace, var_names=['theta', 'beta', 'tau', 'sig'])
``````

Hope it helps!

3 Likes

Yes- you’re a wizard, thank you!

1 Like