weibull.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.exponential import Exponential
  7. from torch.distributions.gumbel import euler_constant
  8. from torch.distributions.transformed_distribution import TransformedDistribution
  9. from torch.distributions.transforms import AffineTransform, PowerTransform
  10. from torch.distributions.utils import broadcast_all
  11. __all__ = ["Weibull"]
  12. class Weibull(TransformedDistribution):
  13. r"""
  14. Samples from a two-parameter Weibull distribution.
  15. Example:
  16. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  17. >>> m = Weibull(torch.tensor([1.0]), torch.tensor([1.0]))
  18. >>> m.sample() # sample from a Weibull distribution with scale=1, concentration=1
  19. tensor([ 0.4784])
  20. Args:
  21. scale (float or Tensor): Scale parameter of distribution (lambda).
  22. concentration (float or Tensor): Concentration parameter of distribution (k/shape).
  23. validate_args (bool, optional): Whether to validate arguments. Default: None.
  24. """
  25. arg_constraints = {
  26. "scale": constraints.positive,
  27. "concentration": constraints.positive,
  28. }
  29. # pyrefly: ignore [bad-override]
  30. support = constraints.positive
  31. def __init__(
  32. self,
  33. scale: Union[Tensor, float],
  34. concentration: Union[Tensor, float],
  35. validate_args: Optional[bool] = None,
  36. ) -> None:
  37. self.scale, self.concentration = broadcast_all(scale, concentration)
  38. self.concentration_reciprocal = self.concentration.reciprocal()
  39. base_dist = Exponential(
  40. torch.ones_like(self.scale), validate_args=validate_args
  41. )
  42. transforms = [
  43. PowerTransform(exponent=self.concentration_reciprocal),
  44. AffineTransform(loc=0, scale=self.scale),
  45. ]
  46. # pyrefly: ignore [bad-argument-type]
  47. super().__init__(base_dist, transforms, validate_args=validate_args)
  48. def expand(self, batch_shape, _instance=None):
  49. new = self._get_checked_instance(Weibull, _instance)
  50. new.scale = self.scale.expand(batch_shape)
  51. new.concentration = self.concentration.expand(batch_shape)
  52. new.concentration_reciprocal = new.concentration.reciprocal()
  53. base_dist = self.base_dist.expand(batch_shape)
  54. transforms = [
  55. PowerTransform(exponent=new.concentration_reciprocal),
  56. AffineTransform(loc=0, scale=new.scale),
  57. ]
  58. super(Weibull, new).__init__(base_dist, transforms, validate_args=False)
  59. new._validate_args = self._validate_args
  60. return new
  61. @property
  62. def mean(self) -> Tensor:
  63. return self.scale * torch.exp(torch.lgamma(1 + self.concentration_reciprocal))
  64. @property
  65. def mode(self) -> Tensor:
  66. return (
  67. self.scale
  68. * ((self.concentration - 1) / self.concentration)
  69. ** self.concentration.reciprocal()
  70. )
  71. @property
  72. def variance(self) -> Tensor:
  73. return self.scale.pow(2) * (
  74. torch.exp(torch.lgamma(1 + 2 * self.concentration_reciprocal))
  75. - torch.exp(2 * torch.lgamma(1 + self.concentration_reciprocal))
  76. )
  77. def entropy(self):
  78. return (
  79. euler_constant * (1 - self.concentration_reciprocal)
  80. + torch.log(self.scale * self.concentration_reciprocal)
  81. + 1
  82. )