kumaraswamy.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import torch
  2. from torch import nan
  3. from torch.distributions import constraints
  4. from torch.distributions.uniform import Uniform
  5. from torch.distributions.transformed_distribution import TransformedDistribution
  6. from torch.distributions.transforms import AffineTransform, PowerTransform
  7. from torch.distributions.utils import broadcast_all, euler_constant
  8. __all__ = ['Kumaraswamy']
  9. def _moments(a, b, n):
  10. """
  11. Computes nth moment of Kumaraswamy using using torch.lgamma
  12. """
  13. arg1 = 1 + n / a
  14. log_value = torch.lgamma(arg1) + torch.lgamma(b) - torch.lgamma(arg1 + b)
  15. return b * torch.exp(log_value)
  16. class Kumaraswamy(TransformedDistribution):
  17. r"""
  18. Samples from a Kumaraswamy distribution.
  19. Example::
  20. >>> # xdoctest: +IGNORE_WANT("non-deterinistic")
  21. >>> m = Kumaraswamy(torch.tensor([1.0]), torch.tensor([1.0]))
  22. >>> m.sample() # sample from a Kumaraswamy distribution with concentration alpha=1 and beta=1
  23. tensor([ 0.1729])
  24. Args:
  25. concentration1 (float or Tensor): 1st concentration parameter of the distribution
  26. (often referred to as alpha)
  27. concentration0 (float or Tensor): 2nd concentration parameter of the distribution
  28. (often referred to as beta)
  29. """
  30. arg_constraints = {'concentration1': constraints.positive, 'concentration0': constraints.positive}
  31. support = constraints.unit_interval
  32. has_rsample = True
  33. def __init__(self, concentration1, concentration0, validate_args=None):
  34. self.concentration1, self.concentration0 = broadcast_all(concentration1, concentration0)
  35. finfo = torch.finfo(self.concentration0.dtype)
  36. base_dist = Uniform(torch.full_like(self.concentration0, 0),
  37. torch.full_like(self.concentration0, 1),
  38. validate_args=validate_args)
  39. transforms = [PowerTransform(exponent=self.concentration0.reciprocal()),
  40. AffineTransform(loc=1., scale=-1.),
  41. PowerTransform(exponent=self.concentration1.reciprocal())]
  42. super().__init__(base_dist, transforms, validate_args=validate_args)
  43. def expand(self, batch_shape, _instance=None):
  44. new = self._get_checked_instance(Kumaraswamy, _instance)
  45. new.concentration1 = self.concentration1.expand(batch_shape)
  46. new.concentration0 = self.concentration0.expand(batch_shape)
  47. return super().expand(batch_shape, _instance=new)
  48. @property
  49. def mean(self):
  50. return _moments(self.concentration1, self.concentration0, 1)
  51. @property
  52. def mode(self):
  53. # Evaluate in log-space for numerical stability.
  54. log_mode = self.concentration0.reciprocal() * \
  55. (-self.concentration0).log1p() - (-self.concentration0 * self.concentration1).log1p()
  56. log_mode[(self.concentration0 < 1) | (self.concentration1 < 1)] = nan
  57. return log_mode.exp()
  58. @property
  59. def variance(self):
  60. return _moments(self.concentration1, self.concentration0, 2) - torch.pow(self.mean, 2)
  61. def entropy(self):
  62. t1 = (1 - self.concentration1.reciprocal())
  63. t0 = (1 - self.concentration0.reciprocal())
  64. H0 = torch.digamma(self.concentration0 + 1) + euler_constant
  65. return t0 + t1 * H0 - torch.log(self.concentration1) - torch.log(self.concentration0)