<commit_before># Copyright 2021 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for `mvn_from_bijector.py`."""

from typing import Tuple

from absl.testing import absltest
from absl.testing import parameterized

import chex
from distrax._src.bijectors import linear
from distrax._src.bijectors.diag_linear import DiagLinear
from distrax._src.bijectors.triangular_linear import TriangularLinear
from distrax._src.distributions.mvn_from_bijector import MultivariateNormalFromBijector

import haiku as hk
import jax
import jax.numpy as jnp
import numpy as np
from tensorflow_probability.substrates import jax as tfp

tfd = tfp.distributions

Array = chex.Array


class MockLinear(linear.Linear):
  """A mock linear bijector."""

  def __init__(self, event_dims: int):
    super().__init__(event_dims, batch_shape=(), dtype=float)

  def forward_and_log_det(self, x: Array) -> Tuple[Array, Array]:
    """Computes y = f(x) and log|det J(f)(x)|."""
    return x, jnp.zeros_like(x)[:-1]


class MultivariateNormalFromBijectorTest(parameterized.TestCase):

  @parameterized.named_parameters(
      ('loc is 0d', 4, np.zeros(shape=())),
      ('loc and scale dims not compatible', 3, np.zeros((4,))),
  )
  def test_raises_on_wrong_inputs(self, event_dims, loc):
    bij = MockLinear(event_dims)
    with self.assertRaises(ValueError):
      MultivariateNormalFromBijector(loc, bij)

  @parameterized.named_parameters(
      ('no broadcast', np.ones((4,)), np.zeros((4,)), (4,)),
      ('broadcasted loc', np.ones((3, 4)), np.zeros((4,)), (3, 4)),
      ('broadcasted diag', np.ones((4,)), np.zeros((3, 4)), (3, 4)),
  )
  def test_loc_scale_and_shapes(self, diag, loc, expected_shape):
    scale = DiagLinear(diag)
    batch_shape = jnp.broadcast_shapes(diag.shape, loc.shape)[:-1]
    dist = MultivariateNormalFromBijector(loc, scale)
    np.testing.assert_allclose(dist.loc, np.zeros(expected_shape))
    self.assertTrue(scale.same_as(dist.scale))
    self.assertEqual(dist.event_shape, (4,))
    self.assertEqual(dist.batch_shape, batch_shape)

  @chex.all_variants
  def test_sample(self):
    prng = hk.PRNGSequence(jax.random.PRNGKey(42))
    diag = 0.5 + jax.random.uniform(next(prng), (4,))
    loc = jax.random.normal(next(prng), (4,))
    scale = DiagLinear(diag)
    dist = MultivariateNormalFromBijector(loc, scale)
    num_samples = 100_000
    sample_fn = lambda seed: dist.sample(seed=seed, sample_shape=num_samples)
    samples = self.variant(sample_fn)(jax.random.PRNGKey(2000))
    self.assertEqual(samples.shape, (num_samples, 4))
    np.testing.assert_allclose(jnp.mean(samples, axis=0), loc, rtol=0.1)
    np.testing.assert_allclose(jnp.std(samples, axis=0), diag, rtol=0.1)

  @chex.all_variants
  def test_log_prob(self):
    prng = hk.PRNGSequence(jax.random.PRNGKey(42))
    diag = 0.5 + jax.random.uniform(next(prng), (4,))
    loc = jax.random.normal(next(prng), (4,))
    scale = DiagLinear(diag)
    dist = MultivariateNormalFromBijector(loc, scale)
    values = jax.random.normal(next(prng), (5, 4))
    tfp_dist = tfd.MultivariateNormalDiag(loc=loc, scale_diag=diag)
    np.testing.assert_allclose(
        self.variant(dist.log_prob)(values), tfp_dist.log_prob(values))

  @chex.all_variants(with_pmap=False)
  @parameterized.named_parameters(
      ('no broadcast', (4,), (4,)),
      ('broadcasted loc', (3, 4), (4,)),
      ('broadcasted diag', (4,), (3, 4)),
  )
  def test_mean_median_mode(self, diag_shape, loc_shape):
    prng = hk.PRNGSequence(jax.random.PRNGKey(42))
    diag = jax.random.normal(next(prng), diag_shape)
    loc = jax.random.normal(next(prng), loc_shape)
    scale = DiagLinear(diag)
    batch_shape = jnp.broadcast_shapes(diag_shape, loc_shape)[:-1]
    dist = MultivariateNormalFromBijector(loc, scale)
    for method in ['mean', 'median', 'mode']:
      with self.subTest(method=method):
        fn = self.variant(getattr(dist, method))
        np.testing.assert_allclose(
            fn(), jnp.broadcast_to(loc, batch_shape + loc.shape[-1:]))

  @chex.all_variants(with_pmap=False)
  @parameterized.named_parameters(
      ('no broadcast', (4,), (4,)),
      ('broadcasted loc', (3, 4), (4,)),
      ('broadcasted diag', (4,), (3, 4)),<commit_msg>Increase rtol in `mvn_from_bijector_test.py`.<commit_after># Copyright 2021 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for `mvn_from_bijector.py`."""

from typing import Tuple

from absl.testing import absltest
from absl.testing import parameterized

import chex
from distrax._src.bijectors import linear
from distrax._src.bijectors.diag_linear import DiagLinear
from distrax._src.bijectors.triangular_linear import TriangularLinear
from distrax._src.distributions.mvn_from_bijector import MultivariateNormalFromBijector

import haiku as hk
import jax
import jax.numpy as jnp
import numpy as np
from tensorflow_probability.substrates import jax as tfp

tfd = tfp.distributions

Array = chex.Array


class MockLinear(linear.Linear):
  """A mock linear bijector."""

  def __init__(self, event_dims: int):
    super().__init__(event_dims, batch_shape=(), dtype=float)

  def forward_and_log_det(self, x: Array) -> Tuple[Array, Array]:
    """Computes y = f(x) and log|det J(f)(x)|."""
    return x, jnp.zeros_like(x)[:-1]


class MultivariateNormalFromBijectorTest(parameterized.TestCase):

  @parameterized.named_parameters(
      ('loc is 0d', 4, np.zeros(shape=())),
      ('loc and scale dims not compatible', 3, np.zeros((4,))),
  )
  def test_raises_on_wrong_inputs(self, event_dims, loc):
    bij = MockLinear(event_dims)
    with self.assertRaises(ValueError):
      MultivariateNormalFromBijector(loc, bij)

  @parameterized.named_parameters(
      ('no broadcast', np.ones((4,)), np.zeros((4,)), (4,)),
      ('broadcasted loc', np.ones((3, 4)), np.zeros((4,)), (3, 4)),
      ('broadcasted diag', np.ones((4,)), np.zeros((3, 4)), (3, 4)),
  )
  def test_loc_scale_and_shapes(self, diag, loc, expected_shape):
    scale = DiagLinear(diag)
    batch_shape = jnp.broadcast_shapes(diag.shape, loc.shape)[:-1]
    dist = MultivariateNormalFromBijector(loc, scale)
    np.testing.assert_allclose(dist.loc, np.zeros(expected_shape))
    self.assertTrue(scale.same_as(dist.scale))
    self.assertEqual(dist.event_shape, (4,))
    self.assertEqual(dist.batch_shape, batch_shape)

  @chex.all_variants
  def test_sample(self):
    prng = hk.PRNGSequence(jax.random.PRNGKey(42))
    diag = 0.5 + jax.random.uniform(next(prng), (4,))
    loc = jax.random.normal(next(prng), (4,))
    scale = DiagLinear(diag)
    dist = MultivariateNormalFromBijector(loc, scale)
    num_samples = 100_000
    sample_fn = lambda seed: dist.sample(seed=seed, sample_shape=num_samples)
    samples = self.variant(sample_fn)(jax.random.PRNGKey(2000))
    self.assertEqual(samples.shape, (num_samples, 4))
    np.testing.assert_allclose(jnp.mean(samples, axis=0), loc, rtol=0.1)
    np.testing.assert_allclose(jnp.std(samples, axis=0), diag, rtol=0.1)

  @chex.all_variants
  def test_log_prob(self):
    prng = hk.PRNGSequence(jax.random.PRNGKey(42))
    diag = 0.5 + jax.random.uniform(next(prng), (4,))
    loc = jax.random.normal(next(prng), (4,))
    scale = DiagLinear(diag)
    dist = MultivariateNormalFromBijector(loc, scale)
    values = jax.random.normal(next(prng), (5, 4))
    tfp_dist = tfd.MultivariateNormalDiag(loc=loc, scale_diag=diag)
    np.testing.assert_allclose(
        self.variant(dist.log_prob)(values), tfp_dist.log_prob(values),
        rtol=2e-7)

  @chex.all_variants(with_pmap=False)
  @parameterized.named_parameters(
      ('no broadcast', (4,), (4,)),
      ('broadcasted loc', (3, 4), (4,)),
      ('broadcasted diag', (4,), (3, 4)),
  )
  def test_mean_median_mode(self, diag_shape, loc_shape):
    prng = hk.PRNGSequence(jax.random.PRNGKey(42))
    diag = jax.random.normal(next(prng), diag_shape)
    loc = jax.random.normal(next(prng), loc_shape)
    scale = DiagLinear(diag)
    batch_shape = jnp.broadcast_shapes(diag_shape, loc_shape)[:-1]
    dist = MultivariateNormalFromBijector(loc, scale)
    for method in ['mean', 'median', 'mode']:
      with self.subTest(method=method):
        fn = self.variant(getattr(dist, method))
        np.testing.assert_allclose(
            fn(), jnp.broadcast_to(loc, batch_shape + loc.shape[-1:]))

  @chex.all_variants(with_pmap=False)
  @parameterized.named_parameters(
      ('no broadcast', (4,), (4,)),
      ('broadcasted loc', (3, 4), (4,)),
      ('broadcasted diag', (4,), (3, 4)),
{'fraction_non_alphanumeric': 0.1525737182780232, 'fraction_numerical': 0.016438919141066474, 'mean_word_length': 4.530681818181818, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1687056', 'n_tokens_mistral': 3484, 'n_tokens_neox': 3338, 'n_words': 743}