tweedie_deviance.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import torch
  15. from torch import Tensor
  16. from torchmetrics.utilities.checks import _check_same_shape
  17. from torchmetrics.utilities.compute import _safe_xlogy
  18. def _tweedie_deviance_score_update(preds: Tensor, targets: Tensor, power: float = 0.0) -> tuple[Tensor, Tensor]:
  19. """Update and returns variables required to compute Deviance Score for the given power.
  20. Check for same shape of input tensors.
  21. Args:
  22. preds: Predicted tensor
  23. targets: Ground truth tensor
  24. power: see :func:`tweedie_deviance_score`
  25. Example:
  26. >>> targets = torch.tensor([1.0, 2.0, 3.0, 4.0])
  27. >>> preds = torch.tensor([4.0, 3.0, 2.0, 1.0])
  28. >>> _tweedie_deviance_score_update(preds, targets, power=2)
  29. (tensor(4.8333), tensor(4))
  30. """
  31. _check_same_shape(preds, targets)
  32. zero_tensor = torch.zeros(preds.shape, device=preds.device)
  33. if 0 < power < 1:
  34. raise ValueError(f"Deviance Score is not defined for power={power}.")
  35. if power == 0:
  36. deviance_score = torch.pow(targets - preds, exponent=2)
  37. elif power == 1:
  38. # Poisson distribution
  39. if torch.any(preds <= 0) or torch.any(targets < 0):
  40. raise ValueError(
  41. f"For power={power}, 'preds' has to be strictly positive and 'targets' cannot be negative."
  42. )
  43. deviance_score = 2 * (_safe_xlogy(targets, targets / preds) + preds - targets)
  44. elif power == 2:
  45. # Gamma distribution
  46. if torch.any(preds <= 0) or torch.any(targets <= 0):
  47. raise ValueError(f"For power={power}, both 'preds' and 'targets' have to be strictly positive.")
  48. deviance_score = 2 * (torch.log(preds / targets) + (targets / preds) - 1)
  49. else:
  50. if power < 0:
  51. if torch.any(preds <= 0):
  52. raise ValueError(f"For power={power}, 'preds' has to be strictly positive.")
  53. elif 1 < power < 2:
  54. if torch.any(preds <= 0) or torch.any(targets < 0):
  55. raise ValueError(
  56. f"For power={power}, 'targets' has to be strictly positive and 'preds' cannot be negative."
  57. )
  58. else:
  59. if torch.any(preds <= 0) or torch.any(targets <= 0):
  60. raise ValueError(f"For power={power}, both 'preds' and 'targets' have to be strictly positive.")
  61. term_1 = torch.pow(torch.max(targets, zero_tensor), 2 - power) / ((1 - power) * (2 - power))
  62. term_2 = targets * torch.pow(preds, 1 - power) / (1 - power)
  63. term_3 = torch.pow(preds, 2 - power) / (2 - power)
  64. deviance_score = 2 * (term_1 - term_2 + term_3)
  65. sum_deviance_score = torch.sum(deviance_score)
  66. num_observations = torch.tensor(torch.numel(deviance_score), device=preds.device)
  67. return sum_deviance_score, num_observations
  68. def _tweedie_deviance_score_compute(sum_deviance_score: Tensor, num_observations: Tensor) -> Tensor:
  69. """Compute Deviance Score.
  70. Args:
  71. sum_deviance_score: Sum of deviance scores accumulated until now.
  72. num_observations: Number of observations encountered until now.
  73. Example:
  74. >>> targets = torch.tensor([1.0, 2.0, 3.0, 4.0])
  75. >>> preds = torch.tensor([4.0, 3.0, 2.0, 1.0])
  76. >>> sum_deviance_score, num_observations = _tweedie_deviance_score_update(preds, targets, power=2)
  77. >>> _tweedie_deviance_score_compute(sum_deviance_score, num_observations)
  78. tensor(1.2083)
  79. """
  80. return sum_deviance_score / num_observations
  81. def tweedie_deviance_score(preds: Tensor, targets: Tensor, power: float = 0.0) -> Tensor:
  82. r"""Compute the `Tweedie Deviance Score`_.
  83. .. math::
  84. deviance\_score(\hat{y},y) =
  85. \begin{cases}
  86. (\hat{y} - y)^2, & \text{for }p=0\\
  87. 2 * (y * log(\frac{y}{\hat{y}}) + \hat{y} - y), & \text{for }p=1\\
  88. 2 * (log(\frac{\hat{y}}{y}) + \frac{y}{\hat{y}} - 1), & \text{for }p=2\\
  89. 2 * (\frac{(max(y,0))^{2 - p}}{(1 - p)(2 - p)} - \frac{y(\hat{y})^{1 - p}}{1 - p} + \frac{(
  90. \hat{y})^{2 - p}}{2 - p}), & \text{otherwise}
  91. \end{cases}
  92. where :math:`y` is a tensor of targets values, :math:`\hat{y}` is a tensor of predictions, and
  93. :math:`p` is the `power`.
  94. Args:
  95. preds: Predicted tensor with shape ``(N,...)``
  96. targets: Ground truth tensor with shape ``(N,...)``
  97. power:
  98. - `power < 0` : Extreme stable distribution. (Requires: preds > 0.)
  99. - `power = 0` : Normal distribution. (Requires: targets and preds can be any real numbers.)
  100. - `power = 1` : Poisson distribution. (Requires: targets >= 0 and y_pred > 0.)
  101. - `1 < p < 2` : Compound Poisson distribution. (Requires: targets >= 0 and preds > 0.)
  102. - `power = 2` : Gamma distribution. (Requires: targets > 0 and preds > 0.)
  103. - `power = 3` : Inverse Gaussian distribution. (Requires: targets > 0 and preds > 0.)
  104. - `otherwise` : Positive stable distribution. (Requires: targets > 0 and preds > 0.)
  105. Example:
  106. >>> from torchmetrics.functional.regression import tweedie_deviance_score
  107. >>> targets = torch.tensor([1.0, 2.0, 3.0, 4.0])
  108. >>> preds = torch.tensor([4.0, 3.0, 2.0, 1.0])
  109. >>> tweedie_deviance_score(preds, targets, power=2)
  110. tensor(1.2083)
  111. """
  112. sum_deviance_score, num_observations = _tweedie_deviance_score_update(preds, targets, power=power)
  113. return _tweedie_deviance_score_compute(sum_deviance_score, num_observations)