Skip to content

Commit 9efa6af

Browse files
committed
Update timeseries tests
1 parent 9ed9c78 commit 9efa6af

File tree

2 files changed

+13
-85
lines changed

2 files changed

+13
-85
lines changed

pymc/tests/test_distributions.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,16 +2619,6 @@ def test_interpolated_transform(self, transform):
26192619
assert not np.isfinite(m.compile_logp()({"x": -1.0}))
26202620
assert not np.isfinite(m.compile_logp()({"x": 11.0}))
26212621

2622-
@pytest.mark.skip("Unsure how to use this test fixture")
2623-
def test_grw(self):
2624-
self.check_logp(
2625-
pm.GaussianRandomWalk,
2626-
Vector(R, 10),
2627-
{"mu": R, "sigma": Rplus, "steps": Nat},
2628-
lambda value, mu, sigma: sp.norm.logpdf(value, mu, sigma).cumsum().sum(),
2629-
decimal=select_by_precision(float64=6, float32=1),
2630-
)
2631-
26322622

26332623
class TestBound:
26342624
"""Tests for pm.Bound distribution"""
@@ -3420,9 +3410,7 @@ def test_no_warning_logp(self):
34203410
"sd_dist",
34213411
[
34223412
pm.Exponential.dist(1),
3423-
pytest.mark.xfail(
3424-
pm.MvNormal.dist(np.ones(3), np.eye(3)),
3425-
),
3413+
pm.MvNormal.dist(np.ones(3), np.eye(3)),
34263414
],
34273415
)
34283416
def test_sd_dist_automatically_resized(self, sd_dist):

pymc/tests/test_distributions_timeseries.py

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -30,82 +30,11 @@
3030
)
3131
from pymc.model import Model
3232
from pymc.sampling import sample, sample_posterior_predictive
33+
from pymc.tests import test_distributions as td
3334
from pymc.tests.helpers import select_by_precision
3435
from pymc.tests.test_distributions_random import BaseTestDistributionRandom
3536

3637

37-
@pytest.mark.skip("These are old tests that I may need delete")
38-
class TestGaussianRandomWalk:
39-
@pytest.mark.parametrize(
40-
"kwargs,expected",
41-
[
42-
({"steps": 5}, (6,)),
43-
({"size": 1}, (5,)),
44-
({"size": 2}, (5,)),
45-
# implied dims are not working
46-
pytest.param({"mu": [0, 0]}, (2, 5), marks=pytest.mark.xfail),
47-
],
48-
)
49-
def test_grw_rv_op_shape(self, kwargs, expected):
50-
"""Basic test for GRW RV op"""
51-
default_kwargs = dict(init=1, mu=3, sd=0.0000001, steps=4, size=None)
52-
53-
combined_kwargs = {**default_kwargs, **kwargs}
54-
grw = gaussianrandomwalk(
55-
combined_kwargs["mu"],
56-
combined_kwargs["sd"],
57-
combined_kwargs["init"],
58-
combined_kwargs["steps"],
59-
).eval()
60-
61-
assert grw.shape == expected
62-
63-
def test_grw_logp(self):
64-
# `at.diff` is currently broken with constants
65-
test_vals = [0, 1, 2]
66-
vals = at.vector("vals")
67-
mu = 1
68-
sigma = 2
69-
init = pm.Normal.dist(0, sigma)
70-
71-
with pm.Model():
72-
grw = GaussianRandomWalk("grw", mu, sigma, init=init, steps=2)
73-
74-
logp = pm.logp(grw, vals)
75-
logp_eval = logp.eval({vals: test_vals})
76-
77-
logp_reference = (
78-
stats.norm(0, sigma).logpdf(test_vals[0])
79-
+ stats.norm(mu, sigma).logpdf(np.diff(test_vals)).sum()
80-
)
81-
82-
np.testing.assert_almost_equal(logp_eval, logp_reference, decimal=6)
83-
84-
@pytest.mark.parametrize(
85-
"steps,size,expected",
86-
(
87-
(1, None, (2,)),
88-
(2, 1, (1, 3)),
89-
(2, 5, (5, 3)),
90-
(10, 5, (5, 11)),
91-
),
92-
)
93-
def test_grw_shape(self, steps, size, expected):
94-
grw_dist = pm.GaussianRandomWalk.dist(mu=0, sigma=1, steps=steps, size=size)
95-
expected_symbolic = tuple(grw_dist.shape.eval())
96-
assert expected_symbolic == expected
97-
98-
@pytest.mark.parametrize("size", (None, (1, 2), (10, 2), (3, 100, 2)))
99-
def test_init_automatically_resized(self, size):
100-
x = GaussianRandomWalk.dist(mu=[0, 1], init=pm.Normal.dist(), size=size)
101-
init = x.owner.inputs[-2]
102-
assert init.eval().shape == size if size is not None else (2,)
103-
104-
x = GaussianRandomWalk.dist(mu=[0, 1], init=pm.Normal.dist(size=5), shape=size)
105-
init = x.owner.inputs[-2]
106-
assert init.eval().shape == size if size is not None else (2,)
107-
108-
10938
class TestGaussianRandomWalk(BaseTestDistributionRandom):
11039
# Override default size for test class
11140
size = None
@@ -158,6 +87,17 @@ def test_grw_inference(self):
15887
np.testing.assert_allclose([mu, sigma], [recovered_mu, recovered_sigma], atol=0.2)
15988

16089

90+
class TestGRWScipy(td.TestMatchesScipy):
91+
def test_grw(self):
92+
self.check_logp(
93+
pm.GaussianRandomWalk,
94+
td.Vector(td.R, 10),
95+
{"mu": td.R, "sigma": td.Rplus, "steps": td.Nat},
96+
lambda value, mu, sigma: stats.norm.logpdf(value, mu, sigma).cumsum().sum(),
97+
decimal=select_by_precision(float64=6, float32=1),
98+
)
99+
100+
161101
@pytest.mark.xfail(reason="Timeseries not refactored")
162102
def test_AR():
163103
# AR1

0 commit comments

Comments
 (0)