beta.py 3.9 KB

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