# Copyright 2016 The TensorFlow Authors. 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 languag
e governing permissions and
# limitations under the License.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from scipy import stats
import tensorflow as tf


class InverseGammaTest(tf.test.TestCase):

  def testInverseGammaShape(self):
    with self.test_session():
      alpha = tf.constant([3.0] * 5)
      beta = tf.constant(11.0)
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha, beta=beta)

      self.assertEqual(inv_gamma.batch_shape().eval(), (5,))
      self.assertEqual(inv_gamma.get_batch_shape(), tf.TensorShape([5]))
      self.assertAllEqual(inv_gamma.event_shape().eval(), [])
      self.assertEqual(inv_gamma.get_event_shape(), tf.TensorShape([]))

  def testInverseGammaLogPDF(self):
    with self.test_session():
      batch_size = 6
      alpha = tf.constant([2.0] * batch_size)
      beta = tf.constant([3.0] * batch_size)
      alpha_v = 2.0
      beta_v = 3.0
      x = np.array([2.5, 2.5, 4.0, 0.1, 1.0, 2.0], dtype=np.float32)
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha, beta=beta)
      expected_log_pdf = stats.invgamma.logpdf(x, alpha_v, scale=beta_v)
      log_pdf = inv_gamma.log_pdf(x)
      self.assertEqual(log_pdf.get_shape(), (6,))
      self.assertAllClose(log_pdf.eval(), expected_log_pdf)

      pdf = inv_gamma.pdf(x)
      self.assertEqual(pdf.get_shape(), (6,))
      self.assertAllClose(pdf.eval(), np.exp(expected_log_pdf))

  def testInverseGammaLogPDFMultidimensional(self):
    with self.test_session():
      batch_size = 6
      alpha = tf.constant([[2.0, 4.0]] * batch_size)
      beta = tf.constant([[3.0, 4.0]] * batch_size)
      alpha_v = np.array([2.0, 4.0])
      beta_v = np.array([3.0, 4.0])
      x = np.array([[2.5, 2.5, 4.0, 0.1, 1.0, 2.0]], dtype=np.float32).T
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha, beta=beta)
      expected_log_pdf = stats.invgamma.logpdf(x, alpha_v, scale=beta_v)
      log_pdf = inv_gamma.log_pdf(x)
      log_pdf_values = log_pdf.eval()
      self.assertEqual(log_pdf.get_shape(), (6, 2))
      self.assertAllClose(log_pdf_values, expected_log_pdf)

      pdf = inv_gamma.pdf(x)
      pdf_values = pdf.eval()
      self.assertEqual(pdf.get_shape(), (6, 2))
      self.assertAllClose(pdf_values, np.exp(expected_log_pdf))

  def testInverseGammaLogPDFMultidimensionalBroadcasting(self):
    with self.test_session():
      batch_size = 6
      alpha = tf.constant([[2.0, 4.0]] * batch_size)
      beta = tf.constant(3.0)
      alpha_v = np.array([2.0, 4.0])
      beta_v = 3.0
      x = np.array([[2.5, 2.5, 4.0, 0.1, 1.0, 2.0]], dtype=np.float32).T
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha, beta=beta)
      expected_log_pdf = stats.invgamma.logpdf(x, alpha_v, scale=beta_v)
      log_pdf = inv_gamma.log_pdf(x)
      log_pdf_values = log_pdf.eval()
      self.assertEqual(log_pdf.get_shape(), (6, 2))
      self.assertAllClose(log_pdf_values, expected_log_pdf)

      pdf = inv_gamma.pdf(x)
      pdf_values = pdf.eval()
      self.assertEqual(pdf.get_shape(), (6, 2))
      self.assertAllClose(pdf_values, np.exp(expected_log_pdf))

  def testInverseGammaCDF(self):
    with self.test_session():
      batch_size = 6
      alpha_v = 2.0
      beta_v = 3.0
      alpha = tf.constant([alpha_v] * batch_size)
      beta = tf.constant([beta_v] * batch_size)
      x = np.array([2.5, 2.5, 4.0, 0.1, 1.0, 2.0], dtype=np.float32)

      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha, beta=beta)
      expected_cdf = stats.invgamma.cdf(x, alpha_v, scale=beta_v)

      cdf = inv_gamma.cdf(x)
      self.assertEqual(cdf.get_shape(), (batch_size,))
      self.assertAllClose(cdf.eval(), expected_cdf)

  def testInverseGammaMode(self):
    with self.test_session():
      alpha_v = np.array([5.5, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha_v,
                                                        beta=beta_v)
      expected_modes = beta_v / (alpha_v + 1)
      self.assertEqual(inv_gamma.mode().get_shape(), (3,))
      self.assertAllClose(inv_gamma.mode().eval(), expected_modes)

  def testInverseGammaMeanAllDefined(self):
    with self.test_session():
      alpha_v = np.array([5.5, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(
          alpha=alpha_v, beta=beta_v)  # strict_statistics=True is the default.
      expected_means = stats.invgamma.mean(alpha_v, scale=beta_v)
      self.assertEqual(inv_gamma.mean().get_shape(), (3,))
      self.assertAllClose(inv_gamma.mean().eval(), expected_means)

  def testInverseGammaMeanStrictStats(self):
    with self.test_session():
      # Mean will not be defined for the first entry.
      alpha_v = np.array([1.0, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(
          alpha=alpha_v, beta=beta_v)  # strict_statistics=True is the default.
      with self.assertRaisesOpError('x < y'):
        inv_gamma.mean().eval()

  def testInverseGammaMeanNanStats(self):
    with self.test_session():
      # Mode will not be defined for the first two entries.
      alpha_v = np.array([0.5, 1.0, 3.0, 2.5])
      beta_v = np.array([1.0, 2.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha_v,
                                                        beta=beta_v,
                                                        strict_statistics=False)
      expected_means = beta_v / (alpha_v - 1)
      expected_means[0] = np.nan
      expected_means[1] = np.nan
      self.assertEqual(inv_gamma.mean().get_shape(), (4,))
      self.assertAllClose(inv_gamma.mean().eval(), expected_means)

  def testInverseGammaVarianceAllDefined(self):
    with self.test_session():
      alpha_v = np.array([7.0, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha_v,
                                                        beta=beta_v)
      expected_variances = stats.invgamma.var(alpha_v, scale=beta_v)
      self.assertEqual(inv_gamma.variance().get_shape(), (3,))
      self.assertAllClose(inv_gamma.variance().eval(), expected_variances)

  def testInverseGammaVarianceStrictStats(self):
    with self.test_session():
      alpha_v = np.array([1.5, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha_v,
                                                        beta=beta_v)
      with self.assertRaisesOpError('x < y'):
        inv_gamma.variance().eval()

  def testInverseGammaVarianceNanStats(self):
    with self.test_session():
      alpha_v = np.array([1.5, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      inv_gamma = tf.contrib.distributions.InverseGamma(alpha=alpha_v,
                                                        beta=beta_v,
                                                        strict_statistics=False)
      expected_variances = stats.invgamma.var(alpha_v, scale=beta_v)
      expected_variances[0] = np.nan
      self.assertEqual(inv_gamma.variance().get_shape(), (3,))
      self.assertAllClose(inv_gamma.variance().eval(), expected_variances)

  def testInverseGammaEntropy(self):
    with self.test_session():
      alpha_v = np.array([1.0, 3.0, 2.5])
      beta_v = np.array([1.0, 4.0, 5.0])
      expected_entropy = stats.invgamma.entropy