inverse_gamma.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.gamma import Gamma
  7. from torch.distributions.transformed_distribution import TransformedDistribution
  8. from torch.distributions.transforms import PowerTransform
  9. __all__ = ["InverseGamma"]
  10. class InverseGamma(TransformedDistribution):
  11. r"""
  12. Creates an inverse gamma distribution parameterized by :attr:`concentration` and :attr:`rate`
  13. where::
  14. X ~ Gamma(concentration, rate)
  15. Y = 1 / X ~ InverseGamma(concentration, rate)
  16. Example::
  17. >>> # xdoctest: +IGNORE_WANT("non-deterinistic")
  18. >>> m = InverseGamma(torch.tensor([2.0]), torch.tensor([3.0]))
  19. >>> m.sample()
  20. tensor([ 1.2953])
  21. Args:
  22. concentration (float or Tensor): shape parameter of the distribution
  23. (often referred to as alpha)
  24. rate (float or Tensor): rate = 1 / scale of the distribution
  25. (often referred to as beta)
  26. """
  27. arg_constraints = {
  28. "concentration": constraints.positive,
  29. "rate": constraints.positive,
  30. }
  31. support = constraints.positive
  32. has_rsample = True
  33. base_dist: Gamma
  34. def __init__(
  35. self,
  36. concentration: Union[Tensor, float],
  37. rate: Union[Tensor, float],
  38. validate_args: Optional[bool] = None,
  39. ) -> None:
  40. base_dist = Gamma(concentration, rate, validate_args=validate_args)
  41. neg_one = -base_dist.rate.new_ones(())
  42. super().__init__(
  43. base_dist, PowerTransform(neg_one), validate_args=validate_args
  44. )
  45. def expand(self, batch_shape, _instance=None):
  46. new = self._get_checked_instance(InverseGamma, _instance)
  47. return super().expand(batch_shape, _instance=new)
  48. @property
  49. def concentration(self) -> Tensor:
  50. return self.base_dist.concentration
  51. @property
  52. def rate(self) -> Tensor:
  53. return self.base_dist.rate
  54. @property
  55. def mean(self) -> Tensor:
  56. result = self.rate / (self.concentration - 1)
  57. return torch.where(self.concentration > 1, result, torch.inf)
  58. @property
  59. def mode(self) -> Tensor:
  60. return self.rate / (self.concentration + 1)
  61. @property
  62. def variance(self) -> Tensor:
  63. result = self.rate.square() / (
  64. (self.concentration - 1).square() * (self.concentration - 2)
  65. )
  66. return torch.where(self.concentration > 2, result, torch.inf)
  67. def entropy(self):
  68. return (
  69. self.concentration
  70. + self.rate.log()
  71. + self.concentration.lgamma()
  72. - (1 + self.concentration) * self.concentration.digamma()
  73. )