|
| 1 | +# Copyright 2020 The PyMC Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import warnings |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import pytest |
| 19 | +import scipy.stats as st |
| 20 | + |
| 21 | +from aesara.tensor.random.op import RandomVariable |
| 22 | + |
| 23 | +import pymc as pm |
| 24 | + |
| 25 | +from pymc.distributions import joint_logp |
| 26 | + |
| 27 | + |
| 28 | +class TestBound: |
| 29 | + """Tests for pm.Bound distribution""" |
| 30 | + |
| 31 | + def test_continuous(self): |
| 32 | + with pm.Model() as model: |
| 33 | + dist = pm.Normal.dist(mu=0, sigma=1) |
| 34 | + with warnings.catch_warnings(): |
| 35 | + warnings.filterwarnings( |
| 36 | + "ignore", "invalid value encountered in add", RuntimeWarning |
| 37 | + ) |
| 38 | + UnboundedNormal = pm.Bound("unbound", dist, transform=None) |
| 39 | + InfBoundedNormal = pm.Bound( |
| 40 | + "infbound", dist, lower=-np.inf, upper=np.inf, transform=None |
| 41 | + ) |
| 42 | + LowerNormal = pm.Bound("lower", dist, lower=0, transform=None) |
| 43 | + UpperNormal = pm.Bound("upper", dist, upper=0, transform=None) |
| 44 | + BoundedNormal = pm.Bound("bounded", dist, lower=1, upper=10, transform=None) |
| 45 | + LowerNormalTransform = pm.Bound("lowertrans", dist, lower=1) |
| 46 | + UpperNormalTransform = pm.Bound("uppertrans", dist, upper=10) |
| 47 | + BoundedNormalTransform = pm.Bound("boundedtrans", dist, lower=1, upper=10) |
| 48 | + |
| 49 | + assert joint_logp(LowerNormal, -1).eval() == -np.inf |
| 50 | + assert joint_logp(UpperNormal, 1).eval() == -np.inf |
| 51 | + assert joint_logp(BoundedNormal, 0).eval() == -np.inf |
| 52 | + assert joint_logp(BoundedNormal, 11).eval() == -np.inf |
| 53 | + |
| 54 | + assert joint_logp(UnboundedNormal, 0).eval() != -np.inf |
| 55 | + assert joint_logp(UnboundedNormal, 11).eval() != -np.inf |
| 56 | + assert joint_logp(InfBoundedNormal, 0).eval() != -np.inf |
| 57 | + assert joint_logp(InfBoundedNormal, 11).eval() != -np.inf |
| 58 | + |
| 59 | + value = model.rvs_to_values[LowerNormalTransform] |
| 60 | + assert joint_logp(LowerNormalTransform, value).eval({value: -1}) != -np.inf |
| 61 | + value = model.rvs_to_values[UpperNormalTransform] |
| 62 | + assert joint_logp(UpperNormalTransform, value).eval({value: 1}) != -np.inf |
| 63 | + value = model.rvs_to_values[BoundedNormalTransform] |
| 64 | + assert joint_logp(BoundedNormalTransform, value).eval({value: 0}) != -np.inf |
| 65 | + assert joint_logp(BoundedNormalTransform, value).eval({value: 11}) != -np.inf |
| 66 | + |
| 67 | + ref_dist = pm.Normal.dist(mu=0, sigma=1) |
| 68 | + assert np.allclose(joint_logp(UnboundedNormal, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 69 | + assert np.allclose(joint_logp(LowerNormal, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 70 | + assert np.allclose(joint_logp(UpperNormal, -5).eval(), joint_logp(ref_dist, 5).eval()) |
| 71 | + assert np.allclose(joint_logp(BoundedNormal, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 72 | + |
| 73 | + def test_discrete(self): |
| 74 | + with pm.Model() as model: |
| 75 | + dist = pm.Poisson.dist(mu=4) |
| 76 | + with warnings.catch_warnings(): |
| 77 | + warnings.filterwarnings( |
| 78 | + "ignore", "invalid value encountered in add", RuntimeWarning |
| 79 | + ) |
| 80 | + UnboundedPoisson = pm.Bound("unbound", dist) |
| 81 | + LowerPoisson = pm.Bound("lower", dist, lower=1) |
| 82 | + UpperPoisson = pm.Bound("upper", dist, upper=10) |
| 83 | + BoundedPoisson = pm.Bound("bounded", dist, lower=1, upper=10) |
| 84 | + |
| 85 | + assert joint_logp(LowerPoisson, 0).eval() == -np.inf |
| 86 | + assert joint_logp(UpperPoisson, 11).eval() == -np.inf |
| 87 | + assert joint_logp(BoundedPoisson, 0).eval() == -np.inf |
| 88 | + assert joint_logp(BoundedPoisson, 11).eval() == -np.inf |
| 89 | + |
| 90 | + assert joint_logp(UnboundedPoisson, 0).eval() != -np.inf |
| 91 | + assert joint_logp(UnboundedPoisson, 11).eval() != -np.inf |
| 92 | + |
| 93 | + ref_dist = pm.Poisson.dist(mu=4) |
| 94 | + assert np.allclose(joint_logp(UnboundedPoisson, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 95 | + assert np.allclose(joint_logp(LowerPoisson, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 96 | + assert np.allclose(joint_logp(UpperPoisson, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 97 | + assert np.allclose(joint_logp(BoundedPoisson, 5).eval(), joint_logp(ref_dist, 5).eval()) |
| 98 | + |
| 99 | + def create_invalid_distribution(self): |
| 100 | + class MyNormal(RandomVariable): |
| 101 | + name = "my_normal" |
| 102 | + ndim_supp = 0 |
| 103 | + ndims_params = [0, 0] |
| 104 | + dtype = "floatX" |
| 105 | + |
| 106 | + my_normal = MyNormal() |
| 107 | + |
| 108 | + class InvalidDistribution(pm.Distribution): |
| 109 | + rv_op = my_normal |
| 110 | + |
| 111 | + @classmethod |
| 112 | + def dist(cls, mu=0, sigma=1, **kwargs): |
| 113 | + return super().dist([mu, sigma], **kwargs) |
| 114 | + |
| 115 | + return InvalidDistribution |
| 116 | + |
| 117 | + def test_arguments_checks(self): |
| 118 | + msg = "Observed Bound distributions are not supported" |
| 119 | + with pm.Model() as m: |
| 120 | + x = pm.Normal("x", 0, 1) |
| 121 | + with pytest.raises(ValueError, match=msg): |
| 122 | + pm.Bound("bound", x, observed=5) |
| 123 | + |
| 124 | + msg = "Cannot transform discrete variable." |
| 125 | + with pm.Model() as m: |
| 126 | + x = pm.Poisson.dist(0.5) |
| 127 | + with warnings.catch_warnings(): |
| 128 | + warnings.filterwarnings( |
| 129 | + "ignore", "invalid value encountered in add", RuntimeWarning |
| 130 | + ) |
| 131 | + with pytest.raises(ValueError, match=msg): |
| 132 | + pm.Bound("bound", x, transform=pm.distributions.transforms.log) |
| 133 | + |
| 134 | + msg = "Given dims do not exist in model coordinates." |
| 135 | + with pm.Model() as m: |
| 136 | + x = pm.Poisson.dist(0.5) |
| 137 | + with pytest.raises(ValueError, match=msg): |
| 138 | + pm.Bound("bound", x, dims="random_dims") |
| 139 | + |
| 140 | + msg = "The dist x was already registered in the current model" |
| 141 | + with pm.Model() as m: |
| 142 | + x = pm.Normal("x", 0, 1) |
| 143 | + with pytest.raises(ValueError, match=msg): |
| 144 | + pm.Bound("bound", x) |
| 145 | + |
| 146 | + msg = "Passing a distribution class to `Bound` is no longer supported" |
| 147 | + with pm.Model() as m: |
| 148 | + with pytest.raises(ValueError, match=msg): |
| 149 | + pm.Bound("bound", pm.Normal) |
| 150 | + |
| 151 | + msg = "Bounding of MultiVariate RVs is not yet supported" |
| 152 | + with pm.Model() as m: |
| 153 | + x = pm.MvNormal.dist(np.zeros(3), np.eye(3)) |
| 154 | + with pytest.raises(NotImplementedError, match=msg): |
| 155 | + pm.Bound("bound", x) |
| 156 | + |
| 157 | + msg = "must be a Discrete or Continuous distribution subclass" |
| 158 | + with pm.Model() as m: |
| 159 | + x = self.create_invalid_distribution().dist() |
| 160 | + with pytest.raises(ValueError, match=msg): |
| 161 | + pm.Bound("bound", x) |
| 162 | + |
| 163 | + def test_invalid_sampling(self): |
| 164 | + msg = "Cannot sample from a bounded variable" |
| 165 | + with pm.Model() as m: |
| 166 | + dist = pm.Normal.dist(mu=0, sigma=1) |
| 167 | + BoundedNormal = pm.Bound("bounded", dist, lower=1, upper=10) |
| 168 | + with pytest.raises(NotImplementedError, match=msg): |
| 169 | + pm.sample_prior_predictive() |
| 170 | + |
| 171 | + def test_bound_shapes(self): |
| 172 | + with pm.Model(coords={"sample": np.ones((2, 5))}) as m: |
| 173 | + dist = pm.Normal.dist(mu=0, sigma=1) |
| 174 | + bound_sized = pm.Bound("boundedsized", dist, lower=1, upper=10, size=(4, 5)) |
| 175 | + bound_shaped = pm.Bound("boundedshaped", dist, lower=1, upper=10, shape=(3, 5)) |
| 176 | + bound_dims = pm.Bound("boundeddims", dist, lower=1, upper=10, dims="sample") |
| 177 | + |
| 178 | + initial_point = m.initial_point() |
| 179 | + dist_size = initial_point["boundedsized_interval__"].shape |
| 180 | + dist_shape = initial_point["boundedshaped_interval__"].shape |
| 181 | + dist_dims = initial_point["boundeddims_interval__"].shape |
| 182 | + |
| 183 | + assert dist_size == (4, 5) |
| 184 | + assert dist_shape == (3, 5) |
| 185 | + assert dist_dims == (2, 5) |
| 186 | + |
| 187 | + def test_bound_dist(self): |
| 188 | + # Continuous |
| 189 | + bound = pm.Bound.dist(pm.Normal.dist(0, 1), lower=0) |
| 190 | + assert pm.logp(bound, -1).eval() == -np.inf |
| 191 | + assert np.isclose(pm.logp(bound, 1).eval(), st.norm(0, 1).logpdf(1)) |
| 192 | + |
| 193 | + # Discrete |
| 194 | + bound = pm.Bound.dist(pm.Poisson.dist(1), lower=2) |
| 195 | + assert pm.logp(bound, 1).eval() == -np.inf |
| 196 | + assert np.isclose(pm.logp(bound, 2).eval(), st.poisson(1).logpmf(2)) |
| 197 | + |
| 198 | + def test_array_bound(self): |
| 199 | + with pm.Model() as model: |
| 200 | + dist = pm.Normal.dist() |
| 201 | + with warnings.catch_warnings(): |
| 202 | + warnings.filterwarnings( |
| 203 | + "ignore", "invalid value encountered in add", RuntimeWarning |
| 204 | + ) |
| 205 | + LowerPoisson = pm.Bound("lower", dist, lower=[1, None], transform=None) |
| 206 | + UpperPoisson = pm.Bound("upper", dist, upper=[np.inf, 10], transform=None) |
| 207 | + BoundedPoisson = pm.Bound("bounded", dist, lower=[1, 2], upper=[9, 10], transform=None) |
| 208 | + |
| 209 | + first, second = joint_logp(LowerPoisson, [0, 0], sum=False)[0].eval() |
| 210 | + assert first == -np.inf |
| 211 | + assert second != -np.inf |
| 212 | + |
| 213 | + first, second = joint_logp(UpperPoisson, [11, 11], sum=False)[0].eval() |
| 214 | + assert first != -np.inf |
| 215 | + assert second == -np.inf |
| 216 | + |
| 217 | + first, second = joint_logp(BoundedPoisson, [1, 1], sum=False)[0].eval() |
| 218 | + assert first != -np.inf |
| 219 | + assert second == -np.inf |
| 220 | + |
| 221 | + first, second = joint_logp(BoundedPoisson, [10, 10], sum=False)[0].eval() |
| 222 | + assert first == -np.inf |
| 223 | + assert second != -np.inf |
0 commit comments