|
| 1 | +# Copyright 2023 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 | +# MIT License |
| 16 | +# |
| 17 | +# Copyright (c) 2021-2022 aesara-devs |
| 18 | +# |
| 19 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 20 | +# of this software and associated documentation files (the "Software"), to deal |
| 21 | +# in the Software without restriction, including without limitation the rights |
| 22 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 23 | +# copies of the Software, and to permit persons to whom the Software is |
| 24 | +# furnished to do so, subject to the following conditions: |
| 25 | +# |
| 26 | +# The above copyright notice and this permission notice shall be included in all |
| 27 | +# copies or substantial portions of the Software. |
| 28 | +# |
| 29 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | +# SOFTWARE. |
| 36 | + |
| 37 | +import re |
| 38 | + |
| 39 | +import numpy as np |
| 40 | +import pytensor.tensor as pt |
| 41 | +import pytest |
| 42 | + |
| 43 | +import pymc as pm |
| 44 | + |
| 45 | +from pymc import logp |
| 46 | +from pymc.logprob import conditional_logp |
| 47 | +from pymc.testing import assert_no_rvs |
| 48 | + |
| 49 | + |
| 50 | +def test_argmax(): |
| 51 | + """Test whether the logprob for ```pt.argmax``` is correctly rejected""" |
| 52 | + x = pt.random.normal(0, 1, size=(3,)) |
| 53 | + x.name = "x" |
| 54 | + x_max = pt.argmax(x, axis=-1) |
| 55 | + x_max_value = pt.vector("x_max_value") |
| 56 | + |
| 57 | + with pytest.raises(RuntimeError, match=re.escape("Logprob method not implemented for Argmax")): |
| 58 | + x_max_logprob = logp(x_max, x_max_value) |
| 59 | + |
| 60 | + |
| 61 | +def test_max_non_iid_fails(): |
| 62 | + """Test whether the logprob for ```pt.max``` for non i.i.d is correctly rejected""" |
| 63 | + x = pm.Normal.dist([0, 1, 2, 3, 4], 1, shape=(5,)) |
| 64 | + x.name = "x" |
| 65 | + x_max = pt.max(x, axis=-1) |
| 66 | + x_max_value = pt.vector("x_max_value") |
| 67 | + with pytest.raises(RuntimeError, match=re.escape("Logprob method not implemented")): |
| 68 | + x_max_logprob = logp(x_max, x_max_value) |
| 69 | + |
| 70 | + |
| 71 | +def test_max_non_rv_fails(): |
| 72 | + """Test whether the logprob for ```pt.max``` for non-RVs is correctly rejected""" |
| 73 | + x = pt.exp(pt.random.beta(0, 1, size=(3,))) |
| 74 | + x.name = "x" |
| 75 | + x_max = pt.max(x, axis=-1) |
| 76 | + x_max_value = pt.vector("x_max_value") |
| 77 | + with pytest.raises(RuntimeError, match=re.escape("Logprob method not implemented")): |
| 78 | + x_max_logprob = logp(x_max, x_max_value) |
| 79 | + |
| 80 | + |
| 81 | +def test_max_multivariate_rv_fails(): |
| 82 | + _alpha = pt.scalar() |
| 83 | + _k = pt.iscalar() |
| 84 | + x = pm.StickBreakingWeights.dist(_alpha, _k) |
| 85 | + x.name = "x" |
| 86 | + x_max = pt.max(x, axis=-1) |
| 87 | + x_max_value = pt.vector("x_max_value") |
| 88 | + with pytest.raises(RuntimeError, match=re.escape("Logprob method not implemented")): |
| 89 | + x_max_logprob = logp(x_max, x_max_value) |
| 90 | + |
| 91 | + |
| 92 | +def test_max_categorical(): |
| 93 | + """Test whether the logprob for ```pt.max``` for unsupported distributions is correctly rejected""" |
| 94 | + x = pm.Categorical.dist([1, 1, 1, 1], shape=(5,)) |
| 95 | + x.name = "x" |
| 96 | + x_max = pt.max(x, axis=-1) |
| 97 | + x_max_value = pt.vector("x_max_value") |
| 98 | + with pytest.raises(RuntimeError, match=re.escape("Logprob method not implemented")): |
| 99 | + x_max_logprob = logp(x_max, x_max_value) |
| 100 | + |
| 101 | + |
| 102 | +def test_non_supp_axis_max(): |
| 103 | + """Test whether the logprob for ```pt.max``` for unsupported axis is correctly rejected""" |
| 104 | + x = pt.random.normal(0, 1, size=(3, 3)) |
| 105 | + x.name = "x" |
| 106 | + x_max = pt.max(x, axis=-1) |
| 107 | + x_max_value = pt.vector("x_max_value") |
| 108 | + with pytest.raises(RuntimeError, match=re.escape("Logprob method not implemented")): |
| 109 | + x_max_logprob = logp(x_max, x_max_value) |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize( |
| 113 | + "shape, value, axis", |
| 114 | + [ |
| 115 | + (3, 0.85, -1), |
| 116 | + (3, 0.01, 0), |
| 117 | + (2, 0.2, None), |
| 118 | + (4, 0.5, 0), |
| 119 | + ((3, 4), 0.9, None), |
| 120 | + ((3, 4), 0.75, (1, 0)), |
| 121 | + ], |
| 122 | +) |
| 123 | +def test_max_logprob(shape, value, axis): |
| 124 | + """Test whether the logprob for ```pt.max``` produces the corrected |
| 125 | +
|
| 126 | + The fact that order statistics of i.i.d. uniform RVs ~ Beta is used here: |
| 127 | + U_1, \\dots, U_n \\stackrel{\text{i.i.d.}}{\\sim} \text{Uniform}(0, 1) \\Rightarrow U_{(k)} \\sim \text{Beta}(k, n + 1- k) |
| 128 | + for all 1<=k<=n |
| 129 | + """ |
| 130 | + x = pt.random.uniform(0, 1, size=shape) |
| 131 | + x.name = "x" |
| 132 | + x_max = pt.max(x, axis=axis) |
| 133 | + x_max_value = pt.scalar("x_max_value") |
| 134 | + x_max_logprob = logp(x_max, x_max_value) |
| 135 | + |
| 136 | + assert_no_rvs(x_max_logprob) |
| 137 | + |
| 138 | + test_value = value |
| 139 | + |
| 140 | + n = np.prod(shape) |
| 141 | + beta_rv = pt.random.beta(n, 1, name="beta") |
| 142 | + beta_vv = beta_rv.clone() |
| 143 | + beta_rv_logprob = logp(beta_rv, beta_vv) |
| 144 | + |
| 145 | + np.testing.assert_allclose( |
| 146 | + beta_rv_logprob.eval({beta_vv: test_value}), |
| 147 | + (x_max_logprob.eval({x_max_value: test_value})), |
| 148 | + rtol=1e-06, |
| 149 | + ) |
0 commit comments