Skip to content

Commit fca57d6

Browse files
committed
add test
1 parent 7a4d47c commit fca57d6

File tree

2 files changed

+79
-49
lines changed

2 files changed

+79
-49
lines changed

pymc3/step_methods/hmc/nuts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .base_hmc import BaseHMC, HMCStepData, DivergenceInfo
2222
from .integration import IntegrationError
2323
from pymc3.backends.report import SamplerWarning, WarningType
24-
from pymc3.math import logbern, log1mexp_numpy, logdiffexp_numpy
24+
from pymc3.math import logbern, logdiffexp_numpy
2525
from pymc3.theanof import floatX
2626
from pymc3.vartypes import continuous_types
2727

pymc3/tests/test_math.py

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@
1818
import theano.tensor as tt
1919
from theano.tests import unittest_tools as utt
2020
from pymc3.math import (
21-
LogDet, logdet, probit, invprobit, expand_packed_triangular,
22-
log1pexp, log1mexp, kronecker, cartesian, kron_dot, kron_solve_lower)
21+
LogDet,
22+
logdet,
23+
probit,
24+
invprobit,
25+
expand_packed_triangular,
26+
log1pexp,
27+
log1mexp,
28+
log1mexp_numpy,
29+
kronecker,
30+
cartesian,
31+
kron_dot,
32+
kron_solve_lower,
33+
)
2334
from .helpers import SeededTest
2435
import pytest
2536
from pymc3.theanof import floatX
@@ -28,14 +39,13 @@
2839
def test_kronecker():
2940
np.random.seed(1)
3041
# Create random matrices
31-
[a, b, c] = [np.random.rand(3, 3+i) for i in range(3)]
42+
[a, b, c] = [np.random.rand(3, 3 + i) for i in range(3)]
3243

33-
custom = kronecker(a, b, c) # Custom version
44+
custom = kronecker(a, b, c) # Custom version
3445
nested = tt.slinalg.kron(a, tt.slinalg.kron(b, c))
3546
np.testing.assert_array_almost_equal(
36-
custom.eval(),
37-
nested.eval() # Standard nested version
38-
)
47+
custom.eval(), nested.eval() # Standard nested version
48+
)
3949

4050

4151
def test_cartesian():
@@ -44,20 +54,21 @@ def test_cartesian():
4454
b = [0, 2]
4555
c = [5, 6]
4656
manual_cartesian = np.array(
47-
[[1, 0, 5],
48-
[1, 0, 6],
49-
[1, 2, 5],
50-
[1, 2, 6],
51-
[2, 0, 5],
52-
[2, 0, 6],
53-
[2, 2, 5],
54-
[2, 2, 6],
55-
[3, 0, 5],
56-
[3, 0, 6],
57-
[3, 2, 5],
58-
[3, 2, 6],
59-
]
60-
)
57+
[
58+
[1, 0, 5],
59+
[1, 0, 6],
60+
[1, 2, 5],
61+
[1, 2, 6],
62+
[2, 0, 5],
63+
[2, 0, 6],
64+
[2, 2, 5],
65+
[2, 2, 6],
66+
[3, 0, 5],
67+
[3, 0, 6],
68+
[3, 2, 5],
69+
[3, 2, 6],
70+
]
71+
)
6172
auto_cart = cartesian(a, b, c)
6273
np.testing.assert_array_almost_equal(manual_cartesian, auto_cart)
6374

@@ -102,16 +113,19 @@ def test_log1pexp():
102113
# import mpmath
103114
# mpmath.mp.dps = 1000
104115
# [float(mpmath.log(1 + mpmath.exp(x))) for x in vals]
105-
expected = np.array([
106-
0.0,
107-
3.720075976020836e-44,
108-
4.539889921686465e-05,
109-
0.6930971818099453,
110-
0.6931471805599453,
111-
0.6931971818099453,
112-
10.000045398899218,
113-
100.0,
114-
1e+20])
116+
expected = np.array(
117+
[
118+
0.0,
119+
3.720075976020836e-44,
120+
4.539889921686465e-05,
121+
0.6930971818099453,
122+
0.6931471805599453,
123+
0.6931971818099453,
124+
10.000045398899218,
125+
100.0,
126+
1e20,
127+
]
128+
)
115129
actual = log1pexp(vals).eval()
116130
npt.assert_allclose(actual, expected)
117131

@@ -121,16 +135,21 @@ def test_log1mexp():
121135
# import mpmath
122136
# mpmath.mp.dps = 1000
123137
# [float(mpmath.log(1 - mpmath.exp(-x))) for x in vals]
124-
expected = np.array([
125-
np.nan,
126-
-np.inf,
127-
-46.051701859880914,
128-
-9.210390371559516,
129-
-4.540096037048921e-05,
130-
-3.720075976020836e-44,
131-
0.0])
138+
expected = np.array(
139+
[
140+
np.nan,
141+
-np.inf,
142+
-46.051701859880914,
143+
-9.210390371559516,
144+
-4.540096037048921e-05,
145+
-3.720075976020836e-44,
146+
0.0,
147+
]
148+
)
132149
actual = log1mexp(vals).eval()
133150
npt.assert_allclose(actual, expected)
151+
actual_ = log1mexp_numpy(vals)
152+
npt.assert_allclose(actual_, expected)
134153

135154

136155
class TestLogDet(SeededTest):
@@ -154,8 +173,10 @@ def validate(self, input_mat):
154173
# Test gradient:
155174
utt.verify_grad(self.op, [input_mat])
156175

157-
@pytest.mark.skipif(theano.config.device in ["cuda", "gpu"],
158-
reason="No logDet implementation on GPU.")
176+
@pytest.mark.skipif(
177+
theano.config.device in ["cuda", "gpu"],
178+
reason="No logDet implementation on GPU.",
179+
)
159180
def test_basic(self):
160181
# Calls validate with different params
161182
test_case_1 = np.random.randn(3, 3) / np.sqrt(3)
@@ -166,11 +187,11 @@ def test_basic(self):
166187

167188
def test_expand_packed_triangular():
168189
with pytest.raises(ValueError):
169-
x = tt.matrix('x')
170-
x.tag.test_value = np.array([[1.]])
190+
x = tt.matrix("x")
191+
x.tag.test_value = np.array([[1.0]])
171192
expand_packed_triangular(5, x)
172193
N = 5
173-
packed = tt.vector('packed')
194+
packed = tt.vector("packed")
174195
packed.tag.test_value = floatX(np.zeros(N * (N + 1) // 2))
175196
with pytest.raises(TypeError):
176197
expand_packed_triangular(packed.shape[0], packed)
@@ -182,9 +203,18 @@ def test_expand_packed_triangular():
182203
upper_packed = floatX(vals[upper != 0])
183204
expand_lower = expand_packed_triangular(N, packed, lower=True)
184205
expand_upper = expand_packed_triangular(N, packed, lower=False)
185-
expand_diag_lower = expand_packed_triangular(N, packed, lower=True, diagonal_only=True)
186-
expand_diag_upper = expand_packed_triangular(N, packed, lower=False, diagonal_only=True)
206+
expand_diag_lower = expand_packed_triangular(
207+
N, packed, lower=True, diagonal_only=True
208+
)
209+
expand_diag_upper = expand_packed_triangular(
210+
N, packed, lower=False, diagonal_only=True
211+
)
187212
assert np.all(expand_lower.eval({packed: lower_packed}) == lower)
188213
assert np.all(expand_upper.eval({packed: upper_packed}) == upper)
189-
assert np.all(expand_diag_lower.eval({packed: lower_packed}) == floatX(np.diag(vals)))
190-
assert np.all(expand_diag_upper.eval({packed: upper_packed}) == floatX(np.diag(vals)))
214+
assert np.all(
215+
expand_diag_lower.eval({packed: lower_packed}) == floatX(np.diag(vals))
216+
)
217+
assert np.all(
218+
expand_diag_upper.eval({packed: upper_packed}) == floatX(np.diag(vals))
219+
)
220+

0 commit comments

Comments
 (0)