Exemplo n.º 1
0
    def _testMoments(self, dt):
        try:
            from scipy import stats  # pylint: disable=g-import-not-at-top
        except ImportError as e:
            tf_logging.warn("Cannot test moments: %s" % e)
            return

        # The moments test is a z-value test.  This is the largest z-value
        # we want to tolerate. Since the z-test approximates a unit normal
        # distribution, it should almost definitely never exceed 6.
        z_limit = 6.0

        for stride in 0, 1, 4, 17:
            alphas = [0.2, 1.0, 3.0]
            if dt == dtypes.float64:
                alphas = [0.01] + alphas
            for alpha in alphas:
                for scale in 9, 17:
                    # Gamma moments only defined for values less than the scale param.
                    max_moment = min(6, scale // 2)
                    sampler = self._Sampler(20000,
                                            alpha,
                                            1 / scale,
                                            dt,
                                            use_gpu=False,
                                            seed=12345)
                    z_scores = util.test_moment_matching(
                        sampler(),
                        max_moment,
                        stats.gamma(alpha, scale=scale),
                        stride=stride,
                    )
                    self.assertAllLess(z_scores, z_limit)
Exemplo n.º 2
0
    def testMoments(self):
        try:
            from scipy import stats  # pylint: disable=g-import-not-at-top
        except ImportError as e:
            tf_logging.warn("Cannot test moments: %s", e)
            return

        # The moments test is a z-value test.  This is the largest z-value
        # we want to tolerate. Since the z-test approximates a unit normal
        # distribution, it should almost definitely never exceed 6.
        z_limit = 6.0
        for dt in _SUPPORTED_DTYPES:
            # Test when lam < 10 and when lam >= 10
            for stride in 0, 4, 10:
                for lam in (3., 20):
                    max_moment = 5
                    sampler = self._Sampler(10000,
                                            lam,
                                            dt,
                                            use_gpu=False,
                                            seed=12345)
                    z_scores = util.test_moment_matching(
                        sampler(),
                        max_moment,
                        stats.poisson(lam),
                        stride=stride,
                    )
                    self.assertAllLess(z_scores, z_limit)
Exemplo n.º 3
0
 def testMoments(self):
     try:
         from scipy import stats  # pylint: disable=g-import-not-at-top
     except ImportError as e:
         tf_logging.warn("Cannot test moments: %s", e)
         return
     # The moments test is a z-value test.  This is the largest z-value
     # we want to tolerate. Since the z-test approximates a unit normal
     # distribution, it should almost definitely never exceed 6.
     z_limit = 6.0
     for dt in _SUPPORTED_DTYPES:
         # Test when n * p > 10, and n * p < 10
         for stride in 0, 4, 10:
             for counts in (1., 10., 22., 50.):
                 for prob in (0.1, 0.5, 0.8):
                     sampler = self._Sampler(int(1e5),
                                             counts,
                                             prob,
                                             dt,
                                             seed=12345)
                     z_scores = util.test_moment_matching(
                         # Use float64 samples.
                         sampler().astype(np.float64),
                         number_moments=6,
                         dist=stats.binom(counts, prob),
                         stride=stride,
                     )
                     self.assertAllLess(z_scores, z_limit)
Exemplo n.º 4
0
  def _testMoments(self, dt):
    try:
      from scipy import stats  # pylint: disable=g-import-not-at-top
    except ImportError as e:
      tf_logging.warn("Cannot test moments: %s" % e)
      return

    # The moments test is a z-value test.  This is the largest z-value
    # we want to tolerate. Since the z-test approximates a unit normal
    # distribution, it should almost definitely never exceed 6.
    z_limit = 6.0

    for stride in 0, 1, 4, 17:
      alphas = [0.2, 1.0, 3.0]
      if dt == dtypes.float64:
        alphas = [0.01] + alphas
      for alpha in alphas:
        for scale in 9, 17:
          # Gamma moments only defined for values less than the scale param.
          max_moment = min(6, scale // 2)
          sampler = self._Sampler(
              20000, alpha, 1 / scale, dt, use_gpu=False, seed=12345)
          z_scores = util.test_moment_matching(
              sampler(),
              max_moment,
              stats.gamma(alpha, scale=scale),
              stride=stride,
          )
          self.assertAllLess(z_scores, z_limit)
Exemplo n.º 5
0
    def testMomentsForTensorInputs(self):
        try:
            from scipy import stats  # pylint: disable=g-import-not-at-top
        except ImportError as e:
            tf_logging.warn("Cannot test moments: %s", e)
            return
        # The moments test is a z-value test.  This is the largest z-value
        # we want to tolerate. Since the z-test approximates a unit normal
        # distribution, it should almost definitely never exceed 6.
        z_limit = 6.0

        class ScipyBinomialWrapper(object):
            """Wrapper for stats.binom to support broadcasting."""
            def __init__(self, counts, probs):
                self.counts = counts
                self.probs = probs

            def moment(self, i):
                counts, probs = np.broadcast_arrays(self.counts, self.probs)
                broadcast_shape = counts.shape

                counts = np.reshape(counts, (-1, ))
                probs = np.reshape(probs, (-1, ))
                counts_and_probs = np.stack([counts, probs], axis=-1)
                moments = np.fromiter((stats.binom(cp[0], cp[1]).moment(i)
                                       for cp in counts_and_probs),
                                      dtype=np.float64)
                return np.reshape(moments, broadcast_shape)

        gen = stateful_random_ops.Generator.from_seed(seed=23455)
        for dt in _SUPPORTED_DTYPES:
            # Test when n * p > 10, and n * p < 10
            for stride in 0, 4, 10:
                counts = np.float64(
                    np.random.randint(low=1, high=20, size=(2, 1, 4)))
                probs = np.random.uniform(size=(1, 3, 4))

                sampler = self._Sampler(int(5e4),
                                        counts,
                                        probs,
                                        dt,
                                        gen=gen,
                                        sample_shape=[10 * int(5e4), 2, 3, 4])
                # Use float64 samples.
                samples = self.evaluate(sampler()).astype(np.float64)
                z_scores = util.test_moment_matching(
                    samples,
                    number_moments=6,
                    dist=ScipyBinomialWrapper(counts, probs),
                    stride=stride,
                )
                self.assertAllLess(z_scores, z_limit)
 def testMoments(self):
   try:
     from scipy import stats  # pylint: disable=g-import-not-at-top
   except ImportError as e:
     tf_logging.warn("Cannot test moments: %s", e)
     return
   # The moments test is a z-value test.  This is the largest z-value
   # we want to tolerate. Since the z-test approximates a unit normal
   # distribution, it should almost definitely never exceed 6.
   z_limit = 6.0
   for dt in _SUPPORTED_DTYPES:
     # Test when n * p > 10, and n * p < 10
     for stride in 0, 4, 10:
       for counts in (1., 10., 22., 50.):
         for prob in (0.1, 0.5, 0.8):
           sampler = self._Sampler(int(1e5), counts, prob, dt, seed=12345)
           z_scores = util.test_moment_matching(
               # Use float64 samples.
               sampler().astype(np.float64),
               number_moments=6,
               dist=stats.binom(counts, prob),
               stride=stride,
           )
           self.assertAllLess(z_scores, z_limit)
  def testMoments(self):
    try:
      from scipy import stats  # pylint: disable=g-import-not-at-top
    except ImportError as e:
      tf_logging.warn("Cannot test moments: %s", e)
      return

    # The moments test is a z-value test.  This is the largest z-value
    # we want to tolerate. Since the z-test approximates a unit normal
    # distribution, it should almost definitely never exceed 6.
    z_limit = 6.0
    for dt in _SUPPORTED_DTYPES:
      # Test when lam < 10 and when lam >= 10
      for stride in 0, 4, 10:
        for lam in (3., 20):
          max_moment = 5
          sampler = self._Sampler(10000, lam, dt, use_gpu=False, seed=12345)
          z_scores = util.test_moment_matching(
              sampler(),
              max_moment,
              stats.poisson(lam),
              stride=stride,
          )
          self.assertAllLess(z_scores, z_limit)