studentT.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import math
  2. import torch
  3. from torch._six import inf, nan
  4. from torch.distributions import Chi2, constraints
  5. from torch.distributions.distribution import Distribution
  6. from torch.distributions.utils import _standard_normal, broadcast_all
  7. class StudentT(Distribution):
  8. r"""
  9. Creates a Student's t-distribution parameterized by degree of
  10. freedom :attr:`df`, mean :attr:`loc` and scale :attr:`scale`.
  11. Example::
  12. >>> m = StudentT(torch.tensor([2.0]))
  13. >>> m.sample() # Student's t-distributed with degrees of freedom=2
  14. tensor([ 0.1046])
  15. Args:
  16. df (float or Tensor): degrees of freedom
  17. loc (float or Tensor): mean of the distribution
  18. scale (float or Tensor): scale of the distribution
  19. """
  20. arg_constraints = {'df': constraints.positive, 'loc': constraints.real, 'scale': constraints.positive}
  21. support = constraints.real
  22. has_rsample = True
  23. @property
  24. def mean(self):
  25. m = self.loc.clone(memory_format=torch.contiguous_format)
  26. m[self.df <= 1] = nan
  27. return m
  28. @property
  29. def mode(self):
  30. return self.loc
  31. @property
  32. def variance(self):
  33. m = self.df.clone(memory_format=torch.contiguous_format)
  34. m[self.df > 2] = self.scale[self.df > 2].pow(2) * self.df[self.df > 2] / (self.df[self.df > 2] - 2)
  35. m[(self.df <= 2) & (self.df > 1)] = inf
  36. m[self.df <= 1] = nan
  37. return m
  38. def __init__(self, df, loc=0., scale=1., validate_args=None):
  39. self.df, self.loc, self.scale = broadcast_all(df, loc, scale)
  40. self._chi2 = Chi2(self.df)
  41. batch_shape = self.df.size()
  42. super(StudentT, self).__init__(batch_shape, validate_args=validate_args)
  43. def expand(self, batch_shape, _instance=None):
  44. new = self._get_checked_instance(StudentT, _instance)
  45. batch_shape = torch.Size(batch_shape)
  46. new.df = self.df.expand(batch_shape)
  47. new.loc = self.loc.expand(batch_shape)
  48. new.scale = self.scale.expand(batch_shape)
  49. new._chi2 = self._chi2.expand(batch_shape)
  50. super(StudentT, new).__init__(batch_shape, validate_args=False)
  51. new._validate_args = self._validate_args
  52. return new
  53. def rsample(self, sample_shape=torch.Size()):
  54. # NOTE: This does not agree with scipy implementation as much as other distributions.
  55. # (see https://github.com/fritzo/notebooks/blob/master/debug-student-t.ipynb). Using DoubleTensor
  56. # parameters seems to help.
  57. # X ~ Normal(0, 1)
  58. # Z ~ Chi2(df)
  59. # Y = X / sqrt(Z / df) ~ StudentT(df)
  60. shape = self._extended_shape(sample_shape)
  61. X = _standard_normal(shape, dtype=self.df.dtype, device=self.df.device)
  62. Z = self._chi2.rsample(sample_shape)
  63. Y = X * torch.rsqrt(Z / self.df)
  64. return self.loc + self.scale * Y
  65. def log_prob(self, value):
  66. if self._validate_args:
  67. self._validate_sample(value)
  68. y = (value - self.loc) / self.scale
  69. Z = (self.scale.log() +
  70. 0.5 * self.df.log() +
  71. 0.5 * math.log(math.pi) +
  72. torch.lgamma(0.5 * self.df) -
  73. torch.lgamma(0.5 * (self.df + 1.)))
  74. return -0.5 * (self.df + 1.) * torch.log1p(y**2. / self.df) - Z
  75. def entropy(self):
  76. lbeta = torch.lgamma(0.5 * self.df) + math.lgamma(0.5) - torch.lgamma(0.5 * (self.df + 1))
  77. return (self.scale.log() +
  78. 0.5 * (self.df + 1) *
  79. (torch.digamma(0.5 * (self.df + 1)) - torch.digamma(0.5 * self.df)) +
  80. 0.5 * self.df.log() + lbeta)