• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pymc-devs / pymc3 / 9392

pending completion
9392

Pull #3638

travis-ci

web-flow
Drop newly unused expit import.
Pull Request #3638: Simple stick breaking (Formerly #3620)

23 of 23 new or added lines in 1 file covered. (100.0%)

52180 of 100265 relevant lines covered (52.04%)

2.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pymc3/tests/test_math.py
1
import numpy as np
×
2
import numpy.testing as npt
×
3
import theano
×
4
import theano.tensor as tt
×
5
from theano.tests import unittest_tools as utt
×
6
from pymc3.math import (
×
7
    LogDet, logdet, probit, invprobit, expand_packed_triangular,
8
    log1pexp, log1mexp, kronecker, cartesian, kron_dot, kron_solve_lower)
9
from .helpers import SeededTest
×
10
import pytest
×
11
from pymc3.theanof import floatX
×
12

13

14
def test_kronecker():
×
15
    np.random.seed(1)
×
16
    # Create random matrices
17
    [a, b, c] = [np.random.rand(3, 3+i) for i in range(3)]
×
18

19
    custom = kronecker(a, b, c)       # Custom version
×
20
    nested = tt.slinalg.kron(a, tt.slinalg.kron(b, c))
×
21
    np.testing.assert_array_almost_equal(
×
22
        custom.eval(),
23
        nested.eval()   # Standard nested version
24
        )
25

26

27
def test_cartesian():
×
28
    np.random.seed(1)
×
29
    a = [1, 2, 3]
×
30
    b = [0, 2]
×
31
    c = [5, 6]
×
32
    manual_cartesian = np.array(
×
33
        [[1, 0, 5],
34
         [1, 0, 6],
35
         [1, 2, 5],
36
         [1, 2, 6],
37
         [2, 0, 5],
38
         [2, 0, 6],
39
         [2, 2, 5],
40
         [2, 2, 6],
41
         [3, 0, 5],
42
         [3, 0, 6],
43
         [3, 2, 5],
44
         [3, 2, 6],
45
         ]
46
        )
47
    auto_cart = cartesian(a, b, c)
×
48
    np.testing.assert_array_almost_equal(manual_cartesian, auto_cart)
×
49

50

51
def test_kron_dot():
×
52
    np.random.seed(1)
×
53
    # Create random matrices
54
    Ks = [np.random.rand(3, 3) for i in range(3)]
×
55
    # Create random vector with correct shape
56
    tot_size = np.prod([k.shape[1] for k in Ks])
×
57
    x = np.random.rand(tot_size).reshape((tot_size, 1))
×
58
    # Construct entire kronecker product then multiply
59
    big = kronecker(*Ks)
×
60
    slow_ans = tt.dot(big, x)
×
61
    # Use tricks to avoid construction of entire kronecker product
62
    fast_ans = kron_dot(Ks, x)
×
63
    np.testing.assert_array_almost_equal(slow_ans.eval(), fast_ans.eval())
×
64

65

66
def test_kron_solve_lower():
×
67
    np.random.seed(1)
×
68
    # Create random matrices
69
    Ls = [np.tril(np.random.rand(3, 3)) for i in range(3)]
×
70
    # Create random vector with correct shape
71
    tot_size = np.prod([L.shape[1] for L in Ls])
×
72
    x = np.random.rand(tot_size).reshape((tot_size, 1))
×
73
    # Construct entire kronecker product then solve
74
    big = kronecker(*Ls)
×
75
    slow_ans = tt.slinalg.solve_lower_triangular(big, x)
×
76
    # Use tricks to avoid construction of entire kronecker product
77
    fast_ans = kron_solve_lower(Ls, x)
×
78
    np.testing.assert_array_almost_equal(slow_ans.eval(), fast_ans.eval())
×
79

80

81
def test_probit():
×
82
    p = np.array([0.01, 0.25, 0.5, 0.75, 0.99])
×
83
    np.testing.assert_allclose(invprobit(probit(p)).eval(), p, atol=1e-5)
×
84

85

86
def test_log1pexp():
×
87
    vals = np.array([-1e20, -100, -10, -1e-4, 0, 1e-4, 10, 100, 1e20])
×
88
    # import mpmath
89
    # mpmath.mp.dps = 1000
90
    # [float(mpmath.log(1 + mpmath.exp(x))) for x in vals]
91
    expected = np.array([
×
92
        0.0,
93
        3.720075976020836e-44,
94
        4.539889921686465e-05,
95
        0.6930971818099453,
96
        0.6931471805599453,
97
        0.6931971818099453,
98
        10.000045398899218,
99
        100.0,
100
        1e+20])
101
    actual = log1pexp(vals).eval()
×
102
    npt.assert_allclose(actual, expected)
×
103

104

105
def test_log1mexp():
×
106
    vals = np.array([-1, 0, 1e-20, 1e-4, 10, 100, 1e20])
×
107
    # import mpmath
108
    # mpmath.mp.dps = 1000
109
    # [float(mpmath.log(1 - mpmath.exp(-x))) for x in vals]
110
    expected = np.array([
×
111
        np.nan,
112
        -np.inf,
113
        -46.051701859880914,
114
        -9.210390371559516,
115
        -4.540096037048921e-05,
116
        -3.720075976020836e-44,
117
        0.0])
118
    actual = log1mexp(vals).eval()
×
119
    npt.assert_allclose(actual, expected)
×
120

121

122
class TestLogDet(SeededTest):
×
123
    def setup_method(self):
×
124
        super().setup_method()
×
125
        utt.seed_rng()
×
126
        self.op_class = LogDet
×
127
        self.op = logdet
×
128

129
    @theano.configparser.change_flags(compute_test_value="ignore")
×
130
    def validate(self, input_mat):
131
        x = theano.tensor.matrix()
×
132
        f = theano.function([x], self.op(x))
×
133
        out = f(input_mat)
×
134
        svd_diag = np.linalg.svd(input_mat, compute_uv=False)
×
135
        numpy_out = np.sum(np.log(np.abs(svd_diag)))
×
136

137
        # Compare the result computed to the expected value.
138
        utt.assert_allclose(numpy_out, out)
×
139

140
        # Test gradient:
141
        utt.verify_grad(self.op, [input_mat])
×
142

143
    @pytest.mark.skipif(theano.config.device in ["cuda", "gpu"],
×
144
                        reason="No logDet implementation on GPU.")
145
    def test_basic(self):
146
        # Calls validate with different params
147
        test_case_1 = np.random.randn(3, 3) / np.sqrt(3)
×
148
        test_case_2 = np.random.randn(10, 10) / np.sqrt(10)
×
149
        self.validate(test_case_1.astype(theano.config.floatX))
×
150
        self.validate(test_case_2.astype(theano.config.floatX))
×
151

152

153
def test_expand_packed_triangular():
×
154
    with pytest.raises(ValueError):
×
155
        x = tt.matrix('x')
×
156
        x.tag.test_value = np.array([[1.]])
×
157
        expand_packed_triangular(5, x)
×
158
    N = 5
×
159
    packed = tt.vector('packed')
×
160
    packed.tag.test_value = floatX(np.zeros(N * (N + 1) // 2))
×
161
    with pytest.raises(TypeError):
×
162
        expand_packed_triangular(packed.shape[0], packed)
×
163
    np.random.seed(42)
×
164
    vals = np.random.randn(N, N)
×
165
    lower = floatX(np.tril(vals))
×
166
    lower_packed = floatX(vals[lower != 0])
×
167
    upper = floatX(np.triu(vals))
×
168
    upper_packed = floatX(vals[upper != 0])
×
169
    expand_lower = expand_packed_triangular(N, packed, lower=True)
×
170
    expand_upper = expand_packed_triangular(N, packed, lower=False)
×
171
    expand_diag_lower = expand_packed_triangular(N, packed, lower=True, diagonal_only=True)
×
172
    expand_diag_upper = expand_packed_triangular(N, packed, lower=False, diagonal_only=True)
×
173
    assert np.all(expand_lower.eval({packed: lower_packed}) == lower)
×
174
    assert np.all(expand_upper.eval({packed: upper_packed}) == upper)
×
175
    assert np.all(expand_diag_lower.eval({packed: lower_packed}) == floatX(np.diag(vals)))
×
176
    assert np.all(expand_diag_upper.eval({packed: upper_packed}) == floatX(np.diag(vals)))
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc