|
| 1 | +import aesara |
| 2 | +import aesara.tensor as at |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +import scipy as sp |
| 6 | +import scipy.stats as st |
| 7 | + |
| 8 | +from aeppl import joint_logprob |
| 9 | +from aeppl.transforms import LogTransform, TransformValuesOpt |
| 10 | +from tests.utils import assert_no_rvs |
| 11 | + |
| 12 | + |
| 13 | +def test_continuous_rv_censoring(): |
| 14 | + x_rv = at.random.normal(0.5, 1, name="x_rv") |
| 15 | + cens_x_rv = at.clip(x_rv, -2, 2) |
| 16 | + |
| 17 | + cens_x = cens_x_rv.type() |
| 18 | + |
| 19 | + logp = joint_logprob({cens_x_rv: cens_x}) |
| 20 | + assert_no_rvs(logp) |
| 21 | + |
| 22 | + logp_fn = aesara.function([cens_x], logp) |
| 23 | + ref_scipy = st.norm(0.5, 1) |
| 24 | + |
| 25 | + assert logp_fn(-3) == -np.inf |
| 26 | + assert logp_fn(3) == -np.inf |
| 27 | + |
| 28 | + assert np.isclose(logp_fn(-2), ref_scipy.logcdf(-2)) |
| 29 | + assert np.isclose(logp_fn(2), ref_scipy.logsf(2)) |
| 30 | + assert np.isclose(logp_fn(0), ref_scipy.logpdf(0)) |
| 31 | + |
| 32 | + |
| 33 | +def test_discrete_rv_censoring(): |
| 34 | + x_rv = at.random.poisson(2) |
| 35 | + cens_x_rv = at.clip(x_rv, 1, 4) |
| 36 | + cens_x_rv.name = "cens_x_rv" |
| 37 | + |
| 38 | + cens_x = cens_x_rv.type() |
| 39 | + |
| 40 | + logp = joint_logprob({cens_x_rv: cens_x}) |
| 41 | + assert_no_rvs(logp) |
| 42 | + |
| 43 | + logp_fn = aesara.function([cens_x], logp) |
| 44 | + ref_scipy = st.poisson(2) |
| 45 | + |
| 46 | + assert logp_fn(0) == -np.inf |
| 47 | + assert logp_fn(5) == -np.inf |
| 48 | + |
| 49 | + assert np.isclose(logp_fn(1), ref_scipy.logcdf(1)) |
| 50 | + assert np.isclose(logp_fn(4), np.logaddexp(ref_scipy.logsf(4), ref_scipy.logpmf(4))) |
| 51 | + assert np.isclose(logp_fn(2), ref_scipy.logpmf(2)) |
| 52 | + |
| 53 | + |
| 54 | +def test_one_sided_censoring(): |
| 55 | + x_rv = at.random.normal(0, 1) |
| 56 | + lb_cens_x_rv = at.clip(x_rv, -1, x_rv) |
| 57 | + ub_cens_x_rv = at.clip(x_rv, x_rv, 1) |
| 58 | + |
| 59 | + lb_cens_x = lb_cens_x_rv.type() |
| 60 | + ub_cens_x = ub_cens_x_rv.type() |
| 61 | + |
| 62 | + lb_logp = joint_logprob({lb_cens_x_rv: lb_cens_x}) |
| 63 | + ub_logp = joint_logprob({ub_cens_x_rv: ub_cens_x}) |
| 64 | + assert_no_rvs(lb_logp) |
| 65 | + assert_no_rvs(ub_logp) |
| 66 | + |
| 67 | + logp_fn = aesara.function([lb_cens_x, ub_cens_x], [lb_logp, ub_logp]) |
| 68 | + ref_scipy = st.norm(0, 1) |
| 69 | + |
| 70 | + assert np.all(np.array(logp_fn(-2, 2)) == -np.inf) |
| 71 | + assert np.all(np.array(logp_fn(2, -2)) != -np.inf) |
| 72 | + np.testing.assert_almost_equal(logp_fn(-1, 1), ref_scipy.logcdf(-1)) |
| 73 | + np.testing.assert_almost_equal(logp_fn(1, -1), ref_scipy.logpdf(-1)) |
| 74 | + |
| 75 | + |
| 76 | +def test_useless_censoring(): |
| 77 | + x_rv = at.random.normal(0.5, 1, size=3) |
| 78 | + cens_x_rv = at.clip(x_rv, x_rv, x_rv) |
| 79 | + |
| 80 | + cens_x = cens_x_rv.type() |
| 81 | + |
| 82 | + logp = joint_logprob({cens_x_rv: cens_x}, sum=False) |
| 83 | + assert_no_rvs(logp) |
| 84 | + |
| 85 | + logp_fn = aesara.function([cens_x], logp) |
| 86 | + ref_scipy = st.norm(0.5, 1) |
| 87 | + |
| 88 | + np.testing.assert_allclose(logp_fn([-2, 0, 2]), ref_scipy.logpdf([-2, 0, 2])) |
| 89 | + |
| 90 | + |
| 91 | +def test_random_censoring(): |
| 92 | + lb_rv = at.random.normal(0, 1, size=2) |
| 93 | + x_rv = at.random.normal(0, 2) |
| 94 | + cens_x_rv = at.clip(x_rv, lb_rv, [1, 1]) |
| 95 | + |
| 96 | + lb = lb_rv.type() |
| 97 | + cens_x = cens_x_rv.type() |
| 98 | + logp = joint_logprob({cens_x_rv: cens_x, lb_rv: lb}, sum=False) |
| 99 | + assert_no_rvs(logp) |
| 100 | + |
| 101 | + logp_fn = aesara.function([lb, cens_x], logp) |
| 102 | + res = logp_fn([0, -1], [-1, -1]) |
| 103 | + assert res[0] == -np.inf |
| 104 | + assert res[1] != -np.inf |
| 105 | + |
| 106 | + |
| 107 | +def test_broadcasted_censoring_constant(): |
| 108 | + lb_rv = at.random.uniform(0, 1, name="lb_rv") |
| 109 | + x_rv = at.random.normal(0, 2, name="x_rv") |
| 110 | + cens_x_rv = at.clip(x_rv, lb_rv, [1, 1]) |
| 111 | + |
| 112 | + lb = lb_rv.type() |
| 113 | + lb.name = "lb" |
| 114 | + cens_x = cens_x_rv.type() |
| 115 | + cens_x.name = "cens_x" |
| 116 | + |
| 117 | + logp = joint_logprob({cens_x_rv: cens_x, lb_rv: lb}) |
| 118 | + assert_no_rvs(logp) |
| 119 | + |
| 120 | + |
| 121 | +def test_broadcasted_censoring_random(): |
| 122 | + lb_rv = at.random.normal(0, 1, name="lb_rv") |
| 123 | + x_rv = at.random.normal(0, 2, size=2, name="x_rv") |
| 124 | + cens_x_rv = at.clip(x_rv, lb_rv, 1) |
| 125 | + |
| 126 | + lb = lb_rv.type() |
| 127 | + lb.name = "lb" |
| 128 | + cens_x = cens_x_rv.type() |
| 129 | + cens_x.name = "cens_x" |
| 130 | + |
| 131 | + logp = joint_logprob({cens_x_rv: cens_x, lb_rv: lb}) |
| 132 | + assert_no_rvs(logp) |
| 133 | + |
| 134 | + |
| 135 | +def test_fail_base_and_censored_have_values(): |
| 136 | + """Test failure when both base_rv and clipped_rv are given value vars""" |
| 137 | + x_rv = at.random.normal(0, 1) |
| 138 | + cens_x_rv = at.clip(x_rv, x_rv, 1) |
| 139 | + |
| 140 | + x = x_rv.type() |
| 141 | + cens_x = cens_x_rv.type() |
| 142 | + with pytest.raises(RuntimeError): |
| 143 | + joint_logprob({cens_x_rv: cens_x, x_rv: x}) |
| 144 | + |
| 145 | + # Test failure when base is not a RandomVariable |
| 146 | + x = at.vector("x") |
| 147 | + clipped_x_rv = at.clip(x, x, 1) |
| 148 | + |
| 149 | + clipped_x = clipped_x_rv.type() |
| 150 | + with pytest.raises(RuntimeError): |
| 151 | + joint_logprob({clipped_x_rv: clipped_x}) |
| 152 | + |
| 153 | + |
| 154 | +def test_fail_multiple_censored_single_base(): |
| 155 | + """Test failure when multiple clipped_rvs share a single base_rv""" |
| 156 | + base_rv = at.random.normal(0, 1) |
| 157 | + cens_rv1 = at.clip(base_rv, -1, 1) |
| 158 | + cens_rv1.name = "cens1" |
| 159 | + cens_rv2 = at.clip(base_rv, -1, 1) |
| 160 | + cens_rv2.name = "cens2" |
| 161 | + |
| 162 | + cens_vv1 = cens_rv1.clone() |
| 163 | + cens_vv2 = cens_rv2.clone() |
| 164 | + with pytest.raises(RuntimeError): |
| 165 | + joint_logprob({cens_rv1: cens_vv1, cens_rv2: cens_vv2}) |
| 166 | + |
| 167 | + |
| 168 | +def test_deterministic_clipping(): |
| 169 | + x_rv = at.random.normal(0, 1) |
| 170 | + clip = at.clip(x_rv, 0, 0) |
| 171 | + y_rv = at.random.normal(clip, 1) |
| 172 | + |
| 173 | + x = x_rv.type() |
| 174 | + y = y_rv.type() |
| 175 | + logp = joint_logprob({x_rv: x, y_rv: y}) |
| 176 | + assert_no_rvs(logp) |
| 177 | + |
| 178 | + logp_fn = aesara.function([x, y], logp) |
| 179 | + assert np.isclose( |
| 180 | + logp_fn(-1, 1), |
| 181 | + st.norm(0, 1).logpdf(-1) + st.norm(0, 1).logpdf(1), |
| 182 | + ) |
| 183 | + |
| 184 | + |
| 185 | +@aesara.config.change_flags(compute_test_value="raise") |
| 186 | +def test_censored_test_value(): |
| 187 | + x_rv = at.random.normal(0, 1) |
| 188 | + cens_x_rv = at.clip(x_rv, -1, 1) |
| 189 | + cens_x = cens_x_rv.type() |
| 190 | + cens_x.tag.test_value = 0 |
| 191 | + joint_logprob({cens_x_rv: cens_x}) |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.xfail(reason="Transform does not work with Elemwise ops, see #60") |
| 195 | +def test_censored_transform(): |
| 196 | + x_rv = at.random.normal(0.5, 1, name="x_rv") |
| 197 | + cens_x_rv = at.clip(x_rv, 0, x_rv) |
| 198 | + |
| 199 | + cens_x = cens_x_rv.type() |
| 200 | + |
| 201 | + transform = TransformValuesOpt({cens_x: LogTransform()}) |
| 202 | + logp = joint_logprob({cens_x_rv: cens_x}, extra_rewrites=transform) |
| 203 | + |
| 204 | + cens_x_val = -1 |
| 205 | + obs_logp = logp.eval({cens_x: cens_x_val}) |
| 206 | + exp_logp = sp.stats.norm(0.5, 1).logpdf(np.exp(cens_x_val)) + cens_x_val |
| 207 | + |
| 208 | + assert np.isclose(obs_logp, exp_logp) |
0 commit comments