Autoregressive Models¶
TODO: description…
AR(K) Model¶
TODO: math
TODO: diagram
import probflow as pf
class AutoregressiveModel(pf.Model):
def __init__(self, k):
self.beta = pf.Parameter([k, 1])
self.mu = pf.Parameter()
self.sigma = pf.ScaleParameter()
def __call__(self, x):
preds = x @ self.beta() + self.mu()
return pf.Normal(preds, self.sigma())
import probflow as pf
import torch
class AutoregressiveModel(pf.Model):
def __init__(self, k):
self.beta = pf.Parameter([k, 1])
self.mu = pf.Parameter()
self.sigma = pf.ScaleParameter()
def __call__(self, x):
x = torch.tensor(x)
preds = x @ self.beta() + self.mu()
return pf.Normal(preds, self.sigma())
If we have a timeseries x
,
N = 1000
x = np.linspace(0, 10, N) + np.random.randn(N)
Then we can pull out k
-length windows into a feature matrix X
, such that each row of X
corresponds to a single time point for which we are trying to predict the next sample’s value (in y
).
k = 50
X = np.empty([N-k-1, k])
y = x[k:]
for i in range(N-k-1):
X[i, :] = x[i:i+k]
Then, we can create and fit the model:
model = AutoregressiveModel(k)
model.fit(X, y)
Note that this is exactly equivalent to just doing a linear regression on the lagged feature matrix:
model = pf.LinearRegression(k)
model.fit(X, y)