kumaraswamy.py 3.6 KB

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