def testLogLogisticSample(self):
   log_logistic_scale = 1.5
   loc = np.log(log_logistic_scale).astype(np.float32)
   scale = 0.33
   dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)
   samples = self.evaluate(dist.sample(6000, seed=test_util.test_seed()))
   self.assertAllClose(np.mean(samples), self.evaluate(dist.mean()), atol=0.1)
   self.assertAllClose(np.std(samples), self.evaluate(dist.stddev()), atol=0.5)
  def testLogLogisticMode(self):
    log_logistic_scale = np.float32([3., 1.5, 0.75])
    loc = np.log(log_logistic_scale)
    scale = np.float32([0.4, 0.6, 1.5])
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    mode = log_logistic_scale * ((1. - scale) / (1. + scale))**scale
    mode[2] = 0.
    self.assertAllClose(self.evaluate(dist.mode()), mode)
  def testLogLogisticEntropy(self):
    log_logistic_scale = np.float32([3., 1.5, 0.75])
    loc = np.log(log_logistic_scale)
    scale = np.float32([0.4, 0.6, 1.5])
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    self.assertAllClose(
        self.evaluate(dist.entropy()),
        stats.fisk.entropy(loc=0., scale=log_logistic_scale, c=1. / scale))
  def testLogLogisticMeanNoNanAllowed(self):
    log_logistic_scale = np.float32([3., 1.5, 0.75])
    loc = np.log(log_logistic_scale)
    scale = np.float32([0.4, 0.6, 1.5])
    dist = tfd.LogLogistic(
        loc=loc, scale=scale, validate_args=True, allow_nan_stats=False)

    with self.assertRaisesOpError('Condition x < y.*'):
      self.evaluate(dist.mean())
  def testLogLogisticLogSurvivalScaleBatch(self):
    log_logistic_scale = 1.5
    loc = np.log(log_logistic_scale)
    scale = np.array([1.2, 5.1])
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    x = np.array([1.0], dtype=np.float32)

    logsf = dist.log_survival_function(x)
    self.assertAllClose(
        self.evaluate(logsf),
        stats.fisk.logsf(x, loc=0., scale=log_logistic_scale, c=1. / scale))
  def testLogLogisticLogSurvivalLocBatch(self):
    log_logistic_scale = [0.42, 1.3]
    loc = np.log(log_logistic_scale)
    scale = 2.5
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    x = np.array([[1e-4, 1.0], [3., 2.0]], dtype=np.float32)

    logsf = dist.log_survival_function(x)
    self.assertAllClose(
        self.evaluate(logsf),
        stats.fisk.logsf(x, loc=0., scale=log_logistic_scale, c=1. / scale))
  def testLogLogisticLogCDFScaleBatch(self):
    log_logistic_scale = [0.75, 2.5]
    loc = np.log(log_logistic_scale)
    scale = np.array([0.3, 2.1])
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    x = np.array([[1e-4, 1.0], [5.0, 2.0]], dtype=np.float32)

    log_cdf = dist.log_cdf(x)
    self.assertAllClose(
        self.evaluate(log_cdf),
        stats.fisk.logcdf(x, loc=0., scale=log_logistic_scale, c=1. / scale))
  def testLogLogisticCDFLocBatch(self):
    log_logistic_scale = [0.5, 1.5]
    loc = np.log(log_logistic_scale)
    scale = 2.5
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    x = np.array([1e-4], dtype=np.float32)

    cdf = dist.cdf(x)
    self.assertAllClose(
        self.evaluate(cdf),
        stats.fisk.cdf(x, loc=0., scale=log_logistic_scale, c=1. / scale))
  def testLogLogisticVariance(self):
    log_logistic_scale = np.float32([3., 1.5, 0.75])
    loc = np.log(log_logistic_scale)
    scale = np.float32([0.4, 0.3, 0.2])
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    # scipy.stats.fisk.var only works on scalars, so we calculate this in a
    # loop:
    scipy_var = [stats.fisk.var(
        loc=0., scale=s, c=1. / c) for (s, c) in zip(log_logistic_scale, scale)]

    self.assertAllClose(self.evaluate(dist.variance()), scipy_var)
    self.assertAllClose(self.evaluate(dist.stddev()), np.sqrt(scipy_var))
  def testLogLogisticLogPDFScaleBatch(self):
    log_logistic_scale = [1.5, 2.]
    loc = np.log(log_logistic_scale)
    scale = np.array([2.5, 5.])
    dist = tfd.LogLogistic(loc=loc, scale=scale, validate_args=True)

    x = np.array([2.0], dtype=np.float32)

    log_pdf = dist.log_prob(x)

    self.assertAllClose(
        self.evaluate(log_pdf),
        stats.fisk.logpdf(x, loc=0., scale=log_logistic_scale, c=1. / scale))
 def testSupportBijectorOutsideRange(self):
   dist = tfd.LogLogistic(loc=0., scale=0.5, validate_args=True)
   with self.assertRaisesOpError('must be greater than or equal to 0'):
     dist.experimental_default_event_space_bijector().inverse(
         [-4.2, -1e-6, -1.3])
 def testAssertValidSample(self):
   dist = tfd.LogLogistic(
       loc=np.log([1., 1., 4.]), scale=2., validate_args=True)
   with self.assertRaisesOpError('Sample must be non-negative.'):
     self.evaluate(dist.cdf([3., -0.2, 1.]))