Skip to content

Commit 7ca496b

Browse files
added AR1 testcase
1 parent 8ed64fd commit 7ca496b

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tests/inference/INLA/test_inla.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
@pytest.fixture(scope="session")
2424
def rng():
25-
seed = 12345
25+
seed = 123
2626
return np.random.default_rng(seed)
2727

2828

@@ -61,3 +61,36 @@ def test_3_layer_normal(rng):
6161
posterior_mean_true = (mu_mu + mu_true) / 2
6262
posterior_mean_inla = idata.posterior.mu.mean(axis=(0, 1))
6363
np.testing.assert_allclose(posterior_mean_true, posterior_mean_inla, atol=0.1)
64+
65+
66+
@pytest.mark.filterwarnings(r"ignore:INLA is currently experimental")
67+
def test_AR1(rng):
68+
T = 10
69+
x = np.zeros((T,))
70+
71+
# true stationarity:
72+
true_theta = 0.95
73+
# true standard deviation of the innovation:
74+
true_sigma = 2.0
75+
# true process mean:
76+
true_center = 0.0
77+
78+
for t in range(1, T):
79+
x[t] = true_theta * x[t - 1] + rng.normal(loc=true_center, scale=true_sigma)
80+
81+
y_obs = rng.poisson(np.exp(x))
82+
83+
with pm.Model() as ar1:
84+
theta = pm.Normal("theta", 0.0, 1.0)
85+
tau = pm.Exponential("tau", 0.5)
86+
87+
x = pm.AR(
88+
"x", rho=theta, tau=tau, steps=T - 1, init_dist=pm.Normal.dist(0, 100, shape=(T,))
89+
)
90+
91+
y = pm.Poisson("y", mu=pm.math.exp(x), observed=y_obs)
92+
93+
idata = pm.sample()
94+
95+
theta_inla = idata.posterior.theta.mean(axis=(0, 1))
96+
np.testing.assert_allclose(np.array([true_theta]), theta_inla, atol=0.1)

0 commit comments

Comments
 (0)