beta.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from numbers import Real, Number
  2. import torch
  3. from torch.distributions import constraints
  4. from torch.distributions.dirichlet import Dirichlet
  5. from torch.distributions.exp_family import ExponentialFamily
  6. from torch.distributions.utils import broadcast_all
  7. __all__ = ['Beta']
  8. class Beta(ExponentialFamily):
  9. r"""
  10. Beta distribution parameterized by :attr:`concentration1` and :attr:`concentration0`.
  11. Example::
  12. >>> # xdoctest: +IGNORE_WANT("non-deterinistic")
  13. >>> m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
  14. >>> m.sample() # Beta distributed with concentration concentration1 and concentration0
  15. tensor([ 0.1046])
  16. Args:
  17. concentration1 (float or Tensor): 1st concentration parameter of the distribution
  18. (often referred to as alpha)
  19. concentration0 (float or Tensor): 2nd concentration parameter of the distribution
  20. (often referred to as beta)
  21. """
  22. arg_constraints = {'concentration1': constraints.positive, 'concentration0': constraints.positive}
  23. support = constraints.unit_interval
  24. has_rsample = True
  25. def __init__(self, concentration1, concentration0, validate_args=None):
  26. if isinstance(concentration1, Real) and isinstance(concentration0, Real):
  27. concentration1_concentration0 = torch.tensor([float(concentration1), float(concentration0)])
  28. else:
  29. concentration1, concentration0 = broadcast_all(concentration1, concentration0)
  30. concentration1_concentration0 = torch.stack([concentration1, concentration0], -1)
  31. self._dirichlet = Dirichlet(concentration1_concentration0, validate_args=validate_args)
  32. super().__init__(self._dirichlet._batch_shape, validate_args=validate_args)
  33. def expand(self, batch_shape, _instance=None):
  34. new = self._get_checked_instance(Beta, _instance)
  35. batch_shape = torch.Size(batch_shape)
  36. new._dirichlet = self._dirichlet.expand(batch_shape)
  37. super(Beta, new).__init__(batch_shape, validate_args=False)
  38. new._validate_args = self._validate_args
  39. return new
  40. @property
  41. def mean(self):
  42. return self.concentration1 / (self.concentration1 + self.concentration0)
  43. @property
  44. def mode(self):
  45. return self._dirichlet.mode[..., 0]
  46. @property
  47. def variance(self):
  48. total = self.concentration1 + self.concentration0
  49. return (self.concentration1 * self.concentration0 /
  50. (total.pow(2) * (total + 1)))
  51. def rsample(self, sample_shape=()):
  52. return self._dirichlet.rsample(sample_shape).select(-1, 0)
  53. def log_prob(self, value):
  54. if self._validate_args:
  55. self._validate_sample(value)
  56. heads_tails = torch.stack([value, 1.0 - value], -1)
  57. return self._dirichlet.log_prob(heads_tails)
  58. def entropy(self):
  59. return self._dirichlet.entropy()
  60. @property
  61. def concentration1(self):
  62. result = self._dirichlet.concentration[..., 0]
  63. if isinstance(result, Number):
  64. return torch.tensor([result])
  65. else:
  66. return result
  67. @property
  68. def concentration0(self):
  69. result = self._dirichlet.concentration[..., 1]
  70. if isinstance(result, Number):
  71. return torch.tensor([result])
  72. else:
  73. return result
  74. @property
  75. def _natural_params(self):
  76. return (self.concentration1, self.concentration0)
  77. def _log_normalizer(self, x, y):
  78. return torch.lgamma(x) + torch.lgamma(y) - torch.lgamma(x + y)