negative_binomial.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # mypy: allow-untyped-defs
  2. from typing import Optional, Union
  3. import torch
  4. import torch.nn.functional as F
  5. from torch import Tensor
  6. from torch.distributions import constraints
  7. from torch.distributions.distribution import Distribution
  8. from torch.distributions.gamma import Gamma
  9. from torch.distributions.utils import (
  10. broadcast_all,
  11. lazy_property,
  12. logits_to_probs,
  13. probs_to_logits,
  14. )
  15. __all__ = ["NegativeBinomial"]
  16. class NegativeBinomial(Distribution):
  17. r"""
  18. Creates a Negative Binomial distribution, i.e. distribution
  19. of the number of successful independent and identical Bernoulli trials
  20. before :attr:`total_count` failures are achieved. The probability
  21. of success of each Bernoulli trial is :attr:`probs`.
  22. Args:
  23. total_count (float or Tensor): non-negative number of negative Bernoulli
  24. trials to stop, although the distribution is still valid for real
  25. valued count
  26. probs (Tensor): Event probabilities of success in the half open interval [0, 1)
  27. logits (Tensor): Event log-odds for probabilities of success
  28. """
  29. arg_constraints = {
  30. "total_count": constraints.greater_than_eq(0),
  31. "probs": constraints.half_open_interval(0.0, 1.0),
  32. "logits": constraints.real,
  33. }
  34. support = constraints.nonnegative_integer
  35. def __init__(
  36. self,
  37. total_count: Union[Tensor, float],
  38. probs: Optional[Tensor] = None,
  39. logits: Optional[Tensor] = None,
  40. validate_args: Optional[bool] = None,
  41. ) -> None:
  42. if (probs is None) == (logits is None):
  43. raise ValueError(
  44. "Either `probs` or `logits` must be specified, but not both."
  45. )
  46. if probs is not None:
  47. (
  48. self.total_count,
  49. self.probs,
  50. ) = broadcast_all(total_count, probs)
  51. self.total_count = self.total_count.type_as(self.probs)
  52. else:
  53. assert logits is not None # helps mypy
  54. (
  55. self.total_count,
  56. self.logits,
  57. ) = broadcast_all(total_count, logits)
  58. self.total_count = self.total_count.type_as(self.logits)
  59. self._param = self.probs if probs is not None else self.logits
  60. batch_shape = self._param.size()
  61. super().__init__(batch_shape, validate_args=validate_args)
  62. def expand(self, batch_shape, _instance=None):
  63. new = self._get_checked_instance(NegativeBinomial, _instance)
  64. batch_shape = torch.Size(batch_shape)
  65. new.total_count = self.total_count.expand(batch_shape)
  66. if "probs" in self.__dict__:
  67. new.probs = self.probs.expand(batch_shape)
  68. new._param = new.probs
  69. if "logits" in self.__dict__:
  70. new.logits = self.logits.expand(batch_shape)
  71. new._param = new.logits
  72. super(NegativeBinomial, new).__init__(batch_shape, validate_args=False)
  73. new._validate_args = self._validate_args
  74. return new
  75. def _new(self, *args, **kwargs):
  76. return self._param.new(*args, **kwargs)
  77. @property
  78. def mean(self) -> Tensor:
  79. return self.total_count * torch.exp(self.logits)
  80. @property
  81. def mode(self) -> Tensor:
  82. return ((self.total_count - 1) * self.logits.exp()).floor().clamp(min=0.0)
  83. @property
  84. def variance(self) -> Tensor:
  85. return self.mean / torch.sigmoid(-self.logits)
  86. @lazy_property
  87. def logits(self) -> Tensor:
  88. return probs_to_logits(self.probs, is_binary=True)
  89. @lazy_property
  90. def probs(self) -> Tensor:
  91. return logits_to_probs(self.logits, is_binary=True)
  92. @property
  93. def param_shape(self) -> torch.Size:
  94. return self._param.size()
  95. @lazy_property
  96. def _gamma(self) -> Gamma:
  97. # Note we avoid validating because self.total_count can be zero.
  98. return Gamma(
  99. concentration=self.total_count,
  100. rate=torch.exp(-self.logits),
  101. validate_args=False,
  102. )
  103. def sample(self, sample_shape=torch.Size()):
  104. with torch.no_grad():
  105. rate = self._gamma.sample(sample_shape=sample_shape)
  106. return torch.poisson(rate)
  107. def log_prob(self, value):
  108. if self._validate_args:
  109. self._validate_sample(value)
  110. log_unnormalized_prob = self.total_count * F.logsigmoid(
  111. -self.logits
  112. ) + value * F.logsigmoid(self.logits)
  113. log_normalization = (
  114. -torch.lgamma(self.total_count + value)
  115. + torch.lgamma(1.0 + value)
  116. + torch.lgamma(self.total_count)
  117. )
  118. # The case self.total_count == 0 and value == 0 has probability 1 but
  119. # lgamma(0) is infinite. Handle this case separately using a function
  120. # that does not modify tensors in place to allow Jit compilation.
  121. log_normalization = log_normalization.masked_fill(
  122. self.total_count + value == 0.0, 0.0
  123. )
  124. return log_unnormalized_prob - log_normalization