Skip to content

Commit 49a3ad6

Browse files
author
Ian Schweer
committed
Add the arma11 model
1 parent 41b523f commit 49a3ad6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pymc as pm
2+
import pytensor
3+
import pytensor.tensor as pt
4+
def model(data):
5+
y_obs = data["y"] # Observed series
6+
up_to = data["T"] # Timesteps
7+
with pm.Model() as pymc_model:
8+
mu = pm.Normal(
9+
"mu", mu=0, sigma=10
10+
) # mean coefficient
11+
phi = pm.Normal(
12+
"phi", mu=0, sigma=2
13+
) # autoregressive coef
14+
theta = pm.Normal(
15+
"theta", mu=0, sigma=2
16+
) # moving average
17+
sigma = pm.Cauchy(
18+
"sigma", alpha=0, beta=2.5
19+
) # noise scale
20+
21+
# scan variables
22+
i = pt.arange(up_to)
23+
outputs_info = [pt.as_tensor_variable(np.asarray(0.0)), pt.as_tensor_variable(np.asarray(0.0))]
24+
25+
def step(prev_obs, current_obs, seq, _, prev_error, mu, phi, theta):
26+
y_hat = pt.switch(pt.gt(seq, 0), mu + phi * prev_obs + theta * prev_error, mu + phi * mu)
27+
return [y_hat, current_obs - y_hat]
28+
29+
[predictions, _], _ = pytensor.scan(fn=step,
30+
outputs_info=outputs_info,
31+
sequences=[{"input": pt.as_tensor_variable(y_obs), "taps": [-1, 0]}, i],
32+
non_sequences=[mu, phi, theta])
33+
34+
final_predictions = predictions[-1]
35+
pm.Normal("output", mu=final_predictions, sigma=sigma, observed=y_obs)
36+
37+
return pymc_model

0 commit comments

Comments
 (0)