gamma.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # mypy: allow-untyped-defs
  2. from typing import Optional, Union
  3. import torch
  4. from torch import Tensor
  5. from torch.distributions import constraints
  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__ = ["Gamma"]
  10. def _standard_gamma(concentration):
  11. return torch._standard_gamma(concentration)
  12. class Gamma(ExponentialFamily):
  13. r"""
  14. Creates a Gamma distribution parameterized by shape :attr:`concentration` and :attr:`rate`.
  15. Example::
  16. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  17. >>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0]))
  18. >>> m.sample() # Gamma distributed with concentration=1 and rate=1
  19. tensor([ 0.1046])
  20. Args:
  21. concentration (float or Tensor): shape parameter of the distribution
  22. (often referred to as alpha)
  23. rate (float or Tensor): rate parameter of the distribution
  24. (often referred to as beta), rate = 1 / scale
  25. """
  26. # pyrefly: ignore [bad-override]
  27. arg_constraints = {
  28. "concentration": constraints.positive,
  29. "rate": constraints.positive,
  30. }
  31. support = constraints.nonnegative
  32. has_rsample = True
  33. _mean_carrier_measure = 0
  34. @property
  35. def mean(self) -> Tensor:
  36. return self.concentration / self.rate
  37. @property
  38. def mode(self) -> Tensor:
  39. return ((self.concentration - 1) / self.rate).clamp(min=0)
  40. @property
  41. def variance(self) -> Tensor:
  42. return self.concentration / self.rate.pow(2)
  43. def __init__(
  44. self,
  45. concentration: Union[Tensor, float],
  46. rate: Union[Tensor, float],
  47. validate_args: Optional[bool] = None,
  48. ) -> None:
  49. self.concentration, self.rate = broadcast_all(concentration, rate)
  50. if isinstance(concentration, _Number) and isinstance(rate, _Number):
  51. batch_shape = torch.Size()
  52. else:
  53. batch_shape = self.concentration.size()
  54. super().__init__(batch_shape, validate_args=validate_args)
  55. def expand(self, batch_shape, _instance=None):
  56. new = self._get_checked_instance(Gamma, _instance)
  57. batch_shape = torch.Size(batch_shape)
  58. new.concentration = self.concentration.expand(batch_shape)
  59. new.rate = self.rate.expand(batch_shape)
  60. super(Gamma, new).__init__(batch_shape, validate_args=False)
  61. new._validate_args = self._validate_args
  62. return new
  63. def rsample(self, sample_shape: _size = torch.Size()) -> Tensor:
  64. shape = self._extended_shape(sample_shape)
  65. value = _standard_gamma(self.concentration.expand(shape)) / self.rate.expand(
  66. shape
  67. )
  68. value.detach().clamp_(
  69. min=torch.finfo(value.dtype).tiny
  70. ) # do not record in autograd graph
  71. return value
  72. def log_prob(self, value):
  73. value = torch.as_tensor(value, dtype=self.rate.dtype, device=self.rate.device)
  74. if self._validate_args:
  75. self._validate_sample(value)
  76. return (
  77. torch.xlogy(self.concentration, self.rate)
  78. + torch.xlogy(self.concentration - 1, value)
  79. - self.rate * value
  80. - torch.lgamma(self.concentration)
  81. )
  82. def entropy(self):
  83. return (
  84. self.concentration
  85. - torch.log(self.rate)
  86. + torch.lgamma(self.concentration)
  87. + (1.0 - self.concentration) * torch.digamma(self.concentration)
  88. )
  89. @property
  90. def _natural_params(self) -> tuple[Tensor, Tensor]:
  91. return (self.concentration - 1, -self.rate)
  92. # pyrefly: ignore [bad-override]
  93. def _log_normalizer(self, x, y):
  94. return torch.lgamma(x + 1) + (x + 1) * torch.log(-y.reciprocal())
  95. def cdf(self, value):
  96. if self._validate_args:
  97. self._validate_sample(value)
  98. return torch.special.gammainc(self.concentration, self.rate * value)