chi2.py 972 B

1234567891011121314151617181920212223242526272829303132
  1. from torch.distributions import constraints
  2. from torch.distributions.gamma import Gamma
  3. __all__ = ['Chi2']
  4. class Chi2(Gamma):
  5. r"""
  6. Creates a Chi-squared distribution parameterized by shape parameter :attr:`df`.
  7. This is exactly equivalent to ``Gamma(alpha=0.5*df, beta=0.5)``
  8. Example::
  9. >>> # xdoctest: +IGNORE_WANT("non-deterinistic")
  10. >>> m = Chi2(torch.tensor([1.0]))
  11. >>> m.sample() # Chi2 distributed with shape df=1
  12. tensor([ 0.1046])
  13. Args:
  14. df (float or Tensor): shape parameter of the distribution
  15. """
  16. arg_constraints = {'df': constraints.positive}
  17. def __init__(self, df, validate_args=None):
  18. super().__init__(0.5 * df, 0.5, validate_args=validate_args)
  19. def expand(self, batch_shape, _instance=None):
  20. new = self._get_checked_instance(Chi2, _instance)
  21. return super().expand(batch_shape, new)
  22. @property
  23. def df(self):
  24. return self.concentration * 2