|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
| 14 | +from contextlib import ExitStack as does_not_raise |
| 15 | + |
14 | 16 | import aesara
|
15 | 17 | import aesara.tensor as at
|
16 | 18 | import numpy as np
|
|
31 | 33 | )
|
32 | 34 |
|
33 | 35 | from pymc3.aesaraf import floatX, walk_model
|
34 |
| -from pymc3.distributions.continuous import Normal, Uniform |
| 36 | +from pymc3.distributions.continuous import Exponential, Normal, Uniform |
35 | 37 | from pymc3.distributions.discrete import Bernoulli
|
36 | 38 | from pymc3.distributions.logp import logpt
|
37 | 39 | from pymc3.model import Model
|
@@ -69,6 +71,140 @@ def test_logpt_basic():
|
69 | 71 | assert a_value_var in res_ancestors
|
70 | 72 |
|
71 | 73 |
|
| 74 | +def test_logpt_add(): |
| 75 | + """ |
| 76 | + Mare sure we can compute a log-likelihood for ``loc + Y`` where ``Y`` is an unregistered |
| 77 | + random variable and ``loc`` is an tensor variable or a registered random variable |
| 78 | + """ |
| 79 | + with Model() as m: |
| 80 | + loc = Uniform("loc", 0, 1) |
| 81 | + x = Normal.dist(0, 1) + loc |
| 82 | + m.register_rv(x, "x") |
| 83 | + |
| 84 | + loc_value_var = m.rvs_to_values[loc] |
| 85 | + x_value_var = m.rvs_to_values[x] |
| 86 | + |
| 87 | + x_logp = logpt(x, m.rvs_to_values[x]) |
| 88 | + |
| 89 | + res_ancestors = list(walk_model((x_logp,), walk_past_rvs=True)) |
| 90 | + res_rv_ancestors = [ |
| 91 | + v for v in res_ancestors if v.owner and isinstance(v.owner.op, RandomVariable) |
| 92 | + ] |
| 93 | + |
| 94 | + # There shouldn't be any `RandomVariable`s in the resulting graph |
| 95 | + assert len(res_rv_ancestors) == 0 |
| 96 | + assert loc_value_var in res_ancestors |
| 97 | + assert x_value_var in res_ancestors |
| 98 | + |
| 99 | + # Test logp is correct |
| 100 | + f_logp = aesara.function([x_value_var, loc_value_var], x_logp) |
| 101 | + np.testing.assert_almost_equal(f_logp(50, 50), sp.norm(50, 1).logpdf(50)) |
| 102 | + np.testing.assert_almost_equal(f_logp(50, 0), sp.norm(0, 1).logpdf(50), decimal=5) |
| 103 | + |
| 104 | + |
| 105 | +def test_logpt_mul(): |
| 106 | + """ |
| 107 | + Mare sure we can compute a log-likelihood for ``scale * Y`` where ``Y`` is an unregistered |
| 108 | + random variable and ``scale`` is an tensor variable or a registered random variable |
| 109 | + """ |
| 110 | + with Model() as m: |
| 111 | + scale = Uniform("scale", 0, 1) |
| 112 | + x = Exponential.dist(1) * scale |
| 113 | + m.register_rv(x, "x") |
| 114 | + |
| 115 | + scale_value_var = m.rvs_to_values[scale] |
| 116 | + x_value_var = m.rvs_to_values[x] |
| 117 | + |
| 118 | + x_logp = logpt(x, m.rvs_to_values[x]) |
| 119 | + |
| 120 | + res_ancestors = list(walk_model((x_logp,), walk_past_rvs=True)) |
| 121 | + res_rv_ancestors = [ |
| 122 | + v for v in res_ancestors if v.owner and isinstance(v.owner.op, RandomVariable) |
| 123 | + ] |
| 124 | + |
| 125 | + # There shouldn't be any `RandomVariable`s in the resulting graph |
| 126 | + assert len(res_rv_ancestors) == 0 |
| 127 | + assert scale_value_var in res_ancestors |
| 128 | + assert x_value_var in res_ancestors |
| 129 | + |
| 130 | + # Test logp is correct |
| 131 | + f_logp = aesara.function([x_value_var, scale_value_var], x_logp) |
| 132 | + np.testing.assert_almost_equal(f_logp(0, 5), sp.expon(scale=5).logpdf(0)) |
| 133 | + np.testing.assert_almost_equal(f_logp(-2, -2), sp.expon(scale=2).logpdf(2)) |
| 134 | + assert f_logp(2, -2) == -np.inf |
| 135 | + |
| 136 | + |
| 137 | +def test_logpt_mul_add(): |
| 138 | + """ |
| 139 | + Mare sure we can compute a log-likelihood for ``loc + scale * Y`` where ``Y`` is an unregistered |
| 140 | + random variable and ``loc`` and ``scale`` are tensor variables or registered random variables |
| 141 | + """ |
| 142 | + with Model() as m: |
| 143 | + loc = Uniform("loc", 0, 1) |
| 144 | + scale = Uniform("scale", 0, 1) |
| 145 | + x = loc + scale * Normal.dist(0, 1) |
| 146 | + m.register_rv(x, "x") |
| 147 | + |
| 148 | + loc_value_var = m.rvs_to_values[loc] |
| 149 | + scale_value_var = m.rvs_to_values[scale] |
| 150 | + x_value_var = m.rvs_to_values[x] |
| 151 | + |
| 152 | + x_logp = logpt(x, m.rvs_to_values[x]) |
| 153 | + |
| 154 | + res_ancestors = list(walk_model((x_logp,), walk_past_rvs=True)) |
| 155 | + res_rv_ancestors = [ |
| 156 | + v for v in res_ancestors if v.owner and isinstance(v.owner.op, RandomVariable) |
| 157 | + ] |
| 158 | + |
| 159 | + # There shouldn't be any `RandomVariable`s in the resulting graph |
| 160 | + assert len(res_rv_ancestors) == 0 |
| 161 | + assert loc_value_var in res_ancestors |
| 162 | + assert scale_value_var in res_ancestors |
| 163 | + assert x_value_var in res_ancestors |
| 164 | + |
| 165 | + # Test logp is correct |
| 166 | + f_logp = aesara.function([x_value_var, loc_value_var, scale_value_var], x_logp) |
| 167 | + np.testing.assert_almost_equal(f_logp(-1, 0, 2), sp.norm(0, 2).logpdf(-1)) |
| 168 | + np.testing.assert_almost_equal(f_logp(95, 100, 15), sp.norm(100, 15).logpdf(95), decimal=6) |
| 169 | + |
| 170 | + |
| 171 | +def test_logpt_not_implemented(): |
| 172 | + """Test that logpt for add and mul fail if inputs are 0 or 2 unregistered rvs""" |
| 173 | + |
| 174 | + with Model() as m: |
| 175 | + variable1 = at.as_tensor_variable(1, "variable1") |
| 176 | + variable2 = at.scalar("variable2") |
| 177 | + unregistered1 = Normal.dist(0, 1) |
| 178 | + unregistered2 = Normal.dist(0, 1) |
| 179 | + registered1 = Normal("registered1", 0, 1) |
| 180 | + registered2 = Normal("registered2", 0, 1) |
| 181 | + |
| 182 | + x_fail1 = variable1 + variable2 |
| 183 | + x_fail2 = unregistered1 + unregistered2 |
| 184 | + x_fail3 = registered1 + variable1 |
| 185 | + x_fail4 = registered1 + registered2 |
| 186 | + |
| 187 | + x_pass1 = variable1 + unregistered2 |
| 188 | + x_pass2 = unregistered1 + variable2 |
| 189 | + x_pass3 = registered1 + unregistered1 |
| 190 | + |
| 191 | + m.register_rv(x_fail1, "x_fail1") |
| 192 | + m.register_rv(x_fail2, "x_fail2") |
| 193 | + m.register_rv(x_fail3, "x_fail3") |
| 194 | + m.register_rv(x_fail4, "x_fail4") |
| 195 | + m.register_rv(x_pass1, "x_pass1") |
| 196 | + m.register_rv(x_pass2, "x_pass2") |
| 197 | + m.register_rv(x_pass3, "x_pass3") |
| 198 | + |
| 199 | + for rv, value_var in m.rvs_to_values.items(): |
| 200 | + if "fail" in rv.name: |
| 201 | + with pytest.raises(NotImplementedError): |
| 202 | + logpt(rv, value_var) |
| 203 | + else: |
| 204 | + with does_not_raise(): |
| 205 | + logpt(rv, value_var) |
| 206 | + |
| 207 | + |
72 | 208 | @pytest.mark.parametrize(
|
73 | 209 | "indices, size",
|
74 | 210 | [
|
|
0 commit comments