weibull.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import torch
  2. from torch.distributions import constraints
  3. from torch.distributions.exponential import Exponential
  4. from torch.distributions.transformed_distribution import TransformedDistribution
  5. from torch.distributions.transforms import AffineTransform, PowerTransform
  6. from torch.distributions.utils import broadcast_all
  7. from torch.distributions.gumbel import euler_constant
  8. __all__ = ['Weibull']
  9. class Weibull(TransformedDistribution):
  10. r"""
  11. Samples from a two-parameter Weibull distribution.
  12. Example:
  13. >>> # xdoctest: +IGNORE_WANT("non-deterinistic")
  14. >>> m = Weibull(torch.tensor([1.0]), torch.tensor([1.0]))
  15. >>> m.sample() # sample from a Weibull distribution with scale=1, concentration=1
  16. tensor([ 0.4784])
  17. Args:
  18. scale (float or Tensor): Scale parameter of distribution (lambda).
  19. concentration (float or Tensor): Concentration parameter of distribution (k/shape).
  20. """
  21. arg_constraints = {'scale': constraints.positive, 'concentration': constraints.positive}
  22. support = constraints.positive
  23. def __init__(self, scale, concentration, validate_args=None):
  24. self.scale, self.concentration = broadcast_all(scale, concentration)
  25. self.concentration_reciprocal = self.concentration.reciprocal()
  26. base_dist = Exponential(torch.ones_like(self.scale), validate_args=validate_args)
  27. transforms = [PowerTransform(exponent=self.concentration_reciprocal),
  28. AffineTransform(loc=0, scale=self.scale)]
  29. super().__init__(base_dist, transforms, validate_args=validate_args)
  30. def expand(self, batch_shape, _instance=None):
  31. new = self._get_checked_instance(Weibull, _instance)
  32. new.scale = self.scale.expand(batch_shape)
  33. new.concentration = self.concentration.expand(batch_shape)
  34. new.concentration_reciprocal = new.concentration.reciprocal()
  35. base_dist = self.base_dist.expand(batch_shape)
  36. transforms = [PowerTransform(exponent=new.concentration_reciprocal),
  37. AffineTransform(loc=0, scale=new.scale)]
  38. super(Weibull, new).__init__(base_dist,
  39. transforms,
  40. validate_args=False)
  41. new._validate_args = self._validate_args
  42. return new
  43. @property
  44. def mean(self):
  45. return self.scale * torch.exp(torch.lgamma(1 + self.concentration_reciprocal))
  46. @property
  47. def mode(self):
  48. return self.scale * ((self.concentration - 1) / self.concentration) ** self.concentration.reciprocal()
  49. @property
  50. def variance(self):
  51. return self.scale.pow(2) * (torch.exp(torch.lgamma(1 + 2 * self.concentration_reciprocal)) -
  52. torch.exp(2 * torch.lgamma(1 + self.concentration_reciprocal)))
  53. def entropy(self):
  54. return euler_constant * (1 - self.concentration_reciprocal) + \
  55. torch.log(self.scale * self.concentration_reciprocal) + 1