Best Python code snippet using avocado_python
negative_binomial_test.py
Source:negative_binomial_test.py  
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14# ==============================================================================15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18import numpy as np19from scipy import stats20from tensorflow.contrib.distributions.python.ops import negative_binomial21from tensorflow.python.framework import constant_op22from tensorflow.python.framework import dtypes23from tensorflow.python.framework import tensor_shape24from tensorflow.python.ops import array_ops25from tensorflow.python.ops import math_ops26from tensorflow.python.platform import test27# In all tests that follow, we use scipy.stats.nbinom, which28# represents a Negative Binomial distribution, with success and failure29# probabilities flipped.30class NegativeBinomialTest(test.TestCase):31  def testNegativeBinomialShape(self):32    with self.cached_session():33      probs = [.1] * 534      total_count = [2.0] * 535      negbinom = negative_binomial.NegativeBinomial(36          total_count=total_count, probs=probs)37      self.assertEqual([5], negbinom.batch_shape_tensor().eval())38      self.assertEqual(tensor_shape.TensorShape([5]), negbinom.batch_shape)39      self.assertAllEqual([], negbinom.event_shape_tensor().eval())40      self.assertEqual(tensor_shape.TensorShape([]), negbinom.event_shape)41  def testNegativeBinomialShapeBroadcast(self):42    with self.cached_session():43      probs = [[.1, .2, .3]] * 544      total_count = [[2.]] * 545      negbinom = negative_binomial.NegativeBinomial(46          total_count=total_count, probs=probs)47      self.assertAllEqual([5, 3], negbinom.batch_shape_tensor().eval())48      self.assertAllEqual(49          tensor_shape.TensorShape([5, 3]), negbinom.batch_shape)50      self.assertAllEqual([], negbinom.event_shape_tensor().eval())51      self.assertAllEqual(tensor_shape.TensorShape([]), negbinom.event_shape)52  def testLogits(self):53    logits = [[0., 9., -0.5]]54    with self.cached_session():55      negbinom = negative_binomial.NegativeBinomial(56          total_count=3., logits=logits)57      self.assertEqual([1, 3], negbinom.probs.get_shape())58      self.assertEqual([1, 3], negbinom.logits.get_shape())59      self.assertAllClose(logits, negbinom.logits.eval())60  def testInvalidP(self):61    invalid_ps = [-.01, 0., -2.,]62    with self.cached_session():63      with self.assertRaisesOpError("Condition x >= 0"):64        negbinom = negative_binomial.NegativeBinomial(65            5., probs=invalid_ps, validate_args=True)66        negbinom.probs.eval()67    invalid_ps = [1.01, 2., 1.001,]68    with self.cached_session():69      with self.assertRaisesOpError("probs has components greater than 1."):70        negbinom = negative_binomial.NegativeBinomial(71            5., probs=invalid_ps, validate_args=True)72        negbinom.probs.eval()73  def testInvalidNegativeCount(self):74    invalid_rs = [-.01, 0., -2.,]75    with self.cached_session():76      with self.assertRaisesOpError("Condition x > 0"):77        negbinom = negative_binomial.NegativeBinomial(78            total_count=invalid_rs, probs=0.1, validate_args=True)79        negbinom.total_count.eval()80  def testNegativeBinomialLogCdf(self):81    with self.cached_session():82      batch_size = 683      probs = [.2] * batch_size84      probs_v = .285      total_count = 5.86      x = np.array([2., 3., 4., 5., 6., 7.], dtype=np.float32)87      negbinom = negative_binomial.NegativeBinomial(88          total_count=total_count, probs=probs)89      expected_log_cdf = stats.nbinom.logcdf(x, n=total_count, p=1 - probs_v)90      log_cdf = negbinom.log_cdf(x)91      self.assertEqual([6], log_cdf.get_shape())92      self.assertAllClose(expected_log_cdf, log_cdf.eval())93      cdf = negbinom.cdf(x)94      self.assertEqual([6], cdf.get_shape())95      self.assertAllClose(np.exp(expected_log_cdf), cdf.eval())96  def testNegativeBinomialLogCdfValidateArgs(self):97    with self.cached_session():98      batch_size = 699      probs = [.9] * batch_size100      total_count = 5.101      with self.assertRaisesOpError("Condition x >= 0"):102        negbinom = negative_binomial.NegativeBinomial(103            total_count=total_count, probs=probs, validate_args=True)104        negbinom.log_cdf(-1.).eval()105  def testNegativeBinomialLogPmf(self):106    with self.cached_session():107      batch_size = 6108      probs = [.2] * batch_size109      probs_v = .2110      total_count = 5.111      x = np.array([2., 3., 4., 5., 6., 7.], dtype=np.float32)112      negbinom = negative_binomial.NegativeBinomial(113          total_count=total_count, probs=probs)114      expected_log_pmf = stats.nbinom.logpmf(x, n=total_count, p=1 - probs_v)115      log_pmf = negbinom.log_prob(x)116      self.assertEqual([6], log_pmf.get_shape())117      self.assertAllClose(expected_log_pmf, log_pmf.eval())118      pmf = negbinom.prob(x)119      self.assertEqual([6], pmf.get_shape())120      self.assertAllClose(np.exp(expected_log_pmf), pmf.eval())121  def testNegativeBinomialLogPmfValidateArgs(self):122    with self.cached_session():123      batch_size = 6124      probs = [.9] * batch_size125      total_count = 5.126      x = array_ops.placeholder(dtypes.float32, shape=[6])127      feed_dict = {x: [2.5, 3.2, 4.3, 5.1, 6., 7.]}128      negbinom = negative_binomial.NegativeBinomial(129          total_count=total_count, probs=probs, validate_args=True)130      with self.assertRaisesOpError("Condition x == y"):131        log_pmf = negbinom.log_prob(x)132        log_pmf.eval(feed_dict=feed_dict)133      with self.assertRaisesOpError("Condition x >= 0"):134        log_pmf = negbinom.log_prob([-1.])135        log_pmf.eval(feed_dict=feed_dict)136      negbinom = negative_binomial.NegativeBinomial(137          total_count=total_count, probs=probs, validate_args=False)138      log_pmf = negbinom.log_prob(x)139      self.assertEqual([6], log_pmf.get_shape())140      pmf = negbinom.prob(x)141      self.assertEqual([6], pmf.get_shape())142  def testNegativeBinomialLogPmfMultidimensional(self):143    with self.cached_session():144      batch_size = 6145      probs = constant_op.constant([[.2, .3, .5]] * batch_size)146      probs_v = np.array([.2, .3, .5])147      total_count = 5.148      x = np.array([[2., 3., 4., 5., 6., 7.]], dtype=np.float32).T149      negbinom = negative_binomial.NegativeBinomial(150          total_count=total_count, probs=probs)151      expected_log_pmf = stats.nbinom.logpmf(152          x, n=total_count, p=1 - probs_v)153      log_pmf = negbinom.log_prob(x)154      log_pmf_values = log_pmf.eval()155      self.assertEqual([6, 3], log_pmf.get_shape())156      self.assertAllClose(expected_log_pmf, log_pmf_values)157      pmf = negbinom.prob(x)158      pmf_values = pmf.eval()159      self.assertEqual([6, 3], pmf.get_shape())160      self.assertAllClose(np.exp(expected_log_pmf), pmf_values)161  def testNegativeBinomialMean(self):162    with self.cached_session():163      total_count = 5.164      probs = np.array([.1, .3, .25], dtype=np.float32)165      negbinom = negative_binomial.NegativeBinomial(166          total_count=total_count, probs=probs)167      expected_means = stats.nbinom.mean(n=total_count, p=1 - probs)168      self.assertEqual([3], negbinom.mean().get_shape())169      self.assertAllClose(expected_means, negbinom.mean().eval())170  def testNegativeBinomialVariance(self):171    with self.cached_session():172      total_count = 5.173      probs = np.array([.1, .3, .25], dtype=np.float32)174      negbinom = negative_binomial.NegativeBinomial(175          total_count=total_count, probs=probs)176      expected_vars = stats.nbinom.var(n=total_count, p=1 - probs)177      self.assertEqual([3], negbinom.variance().get_shape())178      self.assertAllClose(expected_vars, negbinom.variance().eval())179  def testNegativeBinomialStddev(self):180    with self.cached_session():181      total_count = 5.182      probs = np.array([.1, .3, .25], dtype=np.float32)183      negbinom = negative_binomial.NegativeBinomial(184          total_count=total_count, probs=probs)185      expected_stds = stats.nbinom.std(n=total_count, p=1 - probs)186      self.assertEqual([3], negbinom.stddev().get_shape())187      self.assertAllClose(expected_stds, negbinom.stddev().eval())188  def testNegativeBinomialSample(self):189    with self.cached_session() as sess:190      probs = [.3, .9]191      total_count = [4., 11.]192      n = int(100e3)193      negbinom = negative_binomial.NegativeBinomial(194          total_count=total_count, probs=probs)195      samples = negbinom.sample(n, seed=12345)196      self.assertEqual([n, 2], samples.get_shape())197      sample_mean = math_ops.reduce_mean(samples, axis=0)198      sample_var = math_ops.reduce_mean(199          (samples - sample_mean[array_ops.newaxis, ...])**2., axis=0)200      sample_min = math_ops.reduce_min(samples)201      [sample_mean_, sample_var_, sample_min_] = sess.run([202          sample_mean, sample_var, sample_min])203      self.assertAllEqual(np.ones(sample_min_.shape, dtype=np.bool),204                          sample_min_ >= 0.0)205      for i in range(2):206        self.assertAllClose(sample_mean_[i],207                            stats.nbinom.mean(total_count[i], 1 - probs[i]),208                            atol=0.,209                            rtol=.02)210        self.assertAllClose(sample_var_[i],211                            stats.nbinom.var(total_count[i], 1 - probs[i]),212                            atol=0.,213                            rtol=.02)214  def testLogProbOverflow(self):215    with self.cached_session() as sess:216      logits = np.float32([20., 30., 40.])217      total_count = np.float32(1.)218      x = np.float32(0.)219      nb = negative_binomial.NegativeBinomial(220          total_count=total_count, logits=logits)221      log_prob_ = sess.run(nb.log_prob(x))222      self.assertAllEqual(np.ones_like(log_prob_, dtype=np.bool),223                          np.isfinite(log_prob_))224  def testLogProbUnderflow(self):225    with self.cached_session() as sess:226      logits = np.float32([-90, -100, -110])227      total_count = np.float32(1.)228      x = np.float32(0.)229      nb = negative_binomial.NegativeBinomial(230          total_count=total_count, logits=logits)231      log_prob_ = sess.run(nb.log_prob(x))232      self.assertAllEqual(np.ones_like(log_prob_, dtype=np.bool),233                          np.isfinite(log_prob_))234if __name__ == "__main__":...binomial.py
Source:binomial.py  
1from numbers import Number2import torch3from torch.distributions import constraints4from torch.distributions.distribution import Distribution5from torch.distributions.utils import broadcast_all, probs_to_logits, lazy_property, logits_to_probs6class Binomial(Distribution):7    r"""8    Creates a Binomial distribution parameterized by `total_count` and9    either `probs` or `logits` (but not both). `total_count` must be10    broadcastable with `probs`/`logits`.11    Example::12        >>> m = Binomial(100, torch.tensor([0 , .2, .8, 1]))13        >>> x = m.sample()14        tensor([   0.,   22.,   71.,  100.])15        >>> m = Binomial(torch.tensor([[5.], [10.]]), torch.tensor([0.5, 0.8]))16        >>> x = m.sample()17        tensor([[ 4.,  5.],18                [ 7.,  6.]])19    Args:20        total_count (int or Tensor): number of Bernoulli trials21        probs (Tensor): Event probabilities22        logits (Tensor): Event log-odds23    """24    arg_constraints = {'total_count': constraints.nonnegative_integer,25                       'probs': constraints.unit_interval}26    has_enumerate_support = True27    def __init__(self, total_count=1, probs=None, logits=None, validate_args=None):28        if (probs is None) == (logits is None):29            raise ValueError("Either `probs` or `logits` must be specified, but not both.")30        if probs is not None:31            self.total_count, self.probs, = broadcast_all(total_count, probs)32            self.total_count = self.total_count.type_as(self.logits)33            is_scalar = isinstance(self.probs, Number)34        else:35            self.total_count, self.logits, = broadcast_all(total_count, logits)36            self.total_count = self.total_count.type_as(self.logits)37            is_scalar = isinstance(self.logits, Number)38        self._param = self.probs if probs is not None else self.logits39        if is_scalar:40            batch_shape = torch.Size()41        else:42            batch_shape = self._param.size()43        super(Binomial, self).__init__(batch_shape, validate_args=validate_args)44    def _new(self, *args, **kwargs):45        return self._param.new(*args, **kwargs)46    @constraints.dependent_property47    def support(self):48        return constraints.integer_interval(0, self.total_count)49    @property50    def mean(self):51        return self.total_count * self.probs52    @property53    def variance(self):54        return self.total_count * self.probs * (1 - self.probs)55    @lazy_property56    def logits(self):57        return probs_to_logits(self.probs, is_binary=True)58    @lazy_property59    def probs(self):60        return logits_to_probs(self.logits, is_binary=True)61    @property62    def param_shape(self):63        return self._param.size()64    def sample(self, sample_shape=torch.Size()):65        with torch.no_grad():66            max_count = max(int(self.total_count.max()), 1)67            shape = self._extended_shape(sample_shape) + (max_count,)68            bernoullis = torch.bernoulli(self.probs.unsqueeze(-1).expand(shape))69            if self.total_count.min() != max_count:70                arange = torch.arange(max_count, out=self.total_count.new_empty(max_count))71                mask = arange >= self.total_count.unsqueeze(-1)72                bernoullis.masked_fill_(mask, 0.)73            return bernoullis.sum(dim=-1)74    def log_prob(self, value):75        if self._validate_args:76            self._validate_sample(value)77        log_factorial_n = torch.lgamma(self.total_count + 1)78        log_factorial_k = torch.lgamma(value + 1)79        log_factorial_nmk = torch.lgamma(self.total_count - value + 1)80        max_val = (-self.logits).clamp(min=0.0)81        # Note that: torch.log1p(-self.probs)) = max_val - torch.log1p((self.logits + 2 * max_val).exp()))82        return (log_factorial_n - log_factorial_k - log_factorial_nmk +83                value * self.logits + self.total_count * max_val -84                self.total_count * torch.log1p((self.logits + 2 * max_val).exp()))85    def enumerate_support(self):86        total_count = int(self.total_count.max())87        if not self.total_count.min() == total_count:88            raise NotImplementedError("Inhomogeneous total count not supported by `enumerate_support`.")89        values = self._new(1 + total_count,)90        torch.arange(1 + total_count, out=values)91        values = values.view((-1,) + (1,) * len(self._batch_shape))92        values = values.expand((-1,) + self._batch_shape)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
