@@ -26,6 +26,40 @@ def rng():
26
26
return np .random .default_rng (seed )
27
27
28
28
29
+ @pytest .mark .filterwarnings (r"ignore:INLA is currently experimental" )
30
+ def test_AR1 (rng ):
31
+ T = 10
32
+ x = np .zeros ((T ,))
33
+
34
+ # true stationarity:
35
+ true_theta = 0.95
36
+ # true standard deviation of the innovation:
37
+ true_sigma = 2.0
38
+ # true process mean:
39
+ true_center = 0.0
40
+
41
+ for t in range (1 , T ):
42
+ x [t ] = true_theta * x [t - 1 ] + rng .normal (loc = true_center , scale = true_sigma )
43
+
44
+ y_obs = rng .poisson (np .exp (x ))
45
+
46
+ with pm .Model () as ar1_inla :
47
+ theta = pm .Normal ("theta" , 0 , 1.0 )
48
+ tau = pm .Exponential ("tau" , 0.5 )
49
+
50
+ x = pm .AR (
51
+ "x" , rho = theta , tau = tau , steps = T - 1 , init_dist = pm .Normal .dist (0 , 100 , shape = (T ,))
52
+ )
53
+
54
+ y = pm .Poisson ("y" , mu = pm .math .exp (x ), observed = y_obs )
55
+
56
+ # Use INLA
57
+ idata = pmx .fit (method = "INLA" , x = x , Q = tau , return_latent_posteriors = False )
58
+
59
+ theta_inla = idata .posterior .theta .mean (axis = (0 , 1 ))
60
+ np .testing .assert_allclose (np .array ([true_theta ]), theta_inla , atol = 0.2 )
61
+
62
+
29
63
@pytest .mark .filterwarnings (r"ignore:INLA is currently experimental" )
30
64
def test_3_layer_normal (rng ):
31
65
"""
@@ -61,36 +95,3 @@ def test_3_layer_normal(rng):
61
95
posterior_mean_true = (mu_mu + mu_true ) / 2
62
96
posterior_mean_inla = idata .posterior .mu .mean (axis = (0 , 1 ))
63
97
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