|
| 1 | +# Copyright 2022 The TensorFlow Probability Authors. |
| 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 | +"""Tests for sample_parameters.""" |
| 16 | + |
| 17 | +import tensorflow as tf |
| 18 | +from tensorflow_probability.python.bijectors import invert |
| 19 | +from tensorflow_probability.python.bijectors import square |
| 20 | +from tensorflow_probability.python.distributions import inverse_gamma |
| 21 | +from tensorflow_probability.python.distributions import transformed_distribution |
| 22 | +from tensorflow_probability.python.experimental.sts_gibbs import sample_parameters |
| 23 | +from tensorflow_probability.python.internal import samplers |
| 24 | +from tensorflow_probability.python.internal import test_util |
| 25 | + |
| 26 | + |
| 27 | +@test_util.test_all_tf_execution_regimes |
| 28 | +class NormalScalePosteriorInverseGammaConjugate(test_util.TestCase): |
| 29 | + |
| 30 | + def testNoObservations(self): |
| 31 | + distribution = inverse_gamma.InverseGamma(16., 4.) |
| 32 | + posterior_distribution = sample_parameters.normal_scale_posterior_inverse_gamma_conjugate( |
| 33 | + distribution, observations=tf.constant([], dtype=tf.float32)) |
| 34 | + self.assertIsInstance(posterior_distribution, |
| 35 | + transformed_distribution.TransformedDistribution) |
| 36 | + self.assertIsInstance(posterior_distribution.bijector, invert.Invert) |
| 37 | + self.assertIsInstance(posterior_distribution.bijector.bijector, |
| 38 | + square.Square) |
| 39 | + self.assertIsInstance(posterior_distribution.distribution, |
| 40 | + inverse_gamma.InverseGamma) |
| 41 | + self.assertAllEqual(distribution.concentration, |
| 42 | + posterior_distribution.distribution.concentration) |
| 43 | + self.assertAllEqual(distribution.scale, |
| 44 | + posterior_distribution.distribution.scale) |
| 45 | + |
| 46 | + def testSingleObservation(self): |
| 47 | + concentration = 16. |
| 48 | + scale = 4. |
| 49 | + distribution = inverse_gamma.InverseGamma( |
| 50 | + concentration=concentration, scale=scale) |
| 51 | + posterior_distribution = sample_parameters.normal_scale_posterior_inverse_gamma_conjugate( |
| 52 | + distribution, observations=tf.constant([10.])) |
| 53 | + self.assertAllEqual( |
| 54 | + concentration + 0.5, # Add half the number of observations |
| 55 | + posterior_distribution.distribution.concentration) |
| 56 | + self.assertAllEqual( |
| 57 | + scale + 50, # Add half the square of observations sum. |
| 58 | + posterior_distribution.distribution.scale) |
| 59 | + |
| 60 | + def testTwoObservations(self): |
| 61 | + concentration = 16. |
| 62 | + scale = 4. |
| 63 | + distribution = inverse_gamma.InverseGamma( |
| 64 | + concentration=concentration, scale=scale) |
| 65 | + posterior_distribution = sample_parameters.normal_scale_posterior_inverse_gamma_conjugate( |
| 66 | + distribution, observations=tf.constant([10., 4.])) |
| 67 | + self.assertAllEqual( |
| 68 | + concentration + 1, # Add half the number of observations |
| 69 | + posterior_distribution.distribution.concentration) |
| 70 | + self.assertAllEqual( |
| 71 | + scale + 58, # Add half the square of observations sum. |
| 72 | + posterior_distribution.distribution.scale) |
| 73 | + |
| 74 | + def testUpperBoundPropagatedFromPrior(self): |
| 75 | + # If no upper bound is provided, expect there to be none. |
| 76 | + distribution = inverse_gamma.InverseGamma(16., 4.) |
| 77 | + posterior_distribution = sample_parameters.normal_scale_posterior_inverse_gamma_conjugate( |
| 78 | + distribution, observations=tf.constant([], dtype=tf.float32)) |
| 79 | + self.assertFalse(hasattr(posterior_distribution, 'upper_bound')) |
| 80 | + self.assertFalse( |
| 81 | + hasattr(posterior_distribution.distribution, 'upper_bound')) |
| 82 | + |
| 83 | + distribution = inverse_gamma.InverseGamma(16., 4.) |
| 84 | + distribution.upper_bound = 16. |
| 85 | + posterior_distribution = sample_parameters.normal_scale_posterior_inverse_gamma_conjugate( |
| 86 | + distribution, observations=tf.constant([], dtype=tf.float32)) |
| 87 | + self.assertAllEqual(posterior_distribution.distribution.upper_bound, 16.) |
| 88 | + self.assertAllEqual( |
| 89 | + posterior_distribution.upper_bound, |
| 90 | + # TODO(kloveless): This should have sqrt applied, but it is not for |
| 91 | + # temporary backwards compatibility. |
| 92 | + 16.) |
| 93 | + |
| 94 | + |
| 95 | +@test_util.test_all_tf_execution_regimes |
| 96 | +class SampleWithOptionalUpperBoundTest(test_util.TestCase): |
| 97 | + |
| 98 | + def testBasic(self): |
| 99 | + distribution = inverse_gamma.InverseGamma(16., 4.) |
| 100 | + seed = samplers.sanitize_seed((0, 1)) |
| 101 | + unbounded_result = sample_parameters.sample_with_optional_upper_bound( |
| 102 | + distribution, seed=seed) |
| 103 | + # Whatever the result, we want to get that value again minus a fixed offset. |
| 104 | + # Since we fix the seed, we know it is equal just because of the upper |
| 105 | + # bound. |
| 106 | + new_target_result = unbounded_result - 0.5 |
| 107 | + distribution.upper_bound = new_target_result |
| 108 | + self.assertAllEqual( |
| 109 | + new_target_result, |
| 110 | + sample_parameters.sample_with_optional_upper_bound( |
| 111 | + distribution, seed=seed)) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + test_util.main() |
0 commit comments