|
30 | 30 | )
|
31 | 31 | from pymc.model import Model
|
32 | 32 | from pymc.sampling import sample, sample_posterior_predictive
|
| 33 | +from pymc.tests import test_distributions as td |
33 | 34 | from pymc.tests.helpers import select_by_precision
|
34 | 35 | from pymc.tests.test_distributions_random import BaseTestDistributionRandom
|
35 | 36 |
|
36 | 37 |
|
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 |
| - |
109 | 38 | class TestGaussianRandomWalk(BaseTestDistributionRandom):
|
110 | 39 | # Override default size for test class
|
111 | 40 | size = None
|
@@ -158,6 +87,17 @@ def test_grw_inference(self):
|
158 | 87 | np.testing.assert_allclose([mu, sigma], [recovered_mu, recovered_sigma], atol=0.2)
|
159 | 88 |
|
160 | 89 |
|
| 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 | + |
161 | 101 | @pytest.mark.xfail(reason="Timeseries not refactored")
|
162 | 102 | def test_AR():
|
163 | 103 | # AR1
|
|
0 commit comments