chi2.py 916 B

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