|
| 1 | +# mypy: allow-untyped-defs |
| 2 | +import math |
| 3 | +from numbers import Number, Real |
| 4 | + |
| 5 | +import torch |
| 6 | +from torch import inf, nan |
| 7 | +from torch.distributions import constraints, Distribution |
| 8 | +from torch.distributions.utils import broadcast_all |
| 9 | + |
| 10 | + |
| 11 | +__all__ = ["GeneralizedPareto"] |
| 12 | + |
| 13 | + |
| 14 | +class GeneralizedPareto(Distribution): |
| 15 | + r""" |
| 16 | + Creates a Generalized Pareto distribution parameterized by :attr:`loc`, :attr:`scale`, and :attr:`concentration`. |
| 17 | +
|
| 18 | + The Generalized Pareto distribution is a family of continuous probability distributions on the real line. |
| 19 | + Special cases include Exponential (when :attr:`loc` = 0, :attr:`concentration` = 0), Pareto (when :attr:`concentration` > 0, |
| 20 | + :attr:`loc` = :attr:`scale` / :attr:`concentration`), and Uniform (when :attr:`concentration` = -1). |
| 21 | +
|
| 22 | + This distribution is often used to model the tails of other distributions. This implementation is based on the |
| 23 | + implementation in TensorFlow Probability. |
| 24 | +
|
| 25 | + Example:: |
| 26 | +
|
| 27 | + >>> # xdoctest: +IGNORE_WANT("non-deterministic") |
| 28 | + >>> m = GeneralizedPareto(torch.tensor([0.1]), torch.tensor([2.0]), torch.tensor([0.4])) |
| 29 | + >>> m.sample() # sample from a Generalized Pareto distribution with loc=0.1, scale=2.0, and concentration=0.4 |
| 30 | + tensor([ 1.5623]) |
| 31 | +
|
| 32 | + Args: |
| 33 | + loc (float or Tensor): Location parameter of the distribution |
| 34 | + scale (float or Tensor): Scale parameter of the distribution |
| 35 | + concentration (float or Tensor): Concentration parameter of the distribution |
| 36 | + """ |
| 37 | + |
| 38 | + arg_constraints = { |
| 39 | + "loc": constraints.real, |
| 40 | + "scale": constraints.positive, |
| 41 | + "concentration": constraints.real, |
| 42 | + } |
| 43 | + has_rsample = True |
| 44 | + |
| 45 | + def __init__(self, loc, scale, concentration, validate_args=None): |
| 46 | + self.loc, self.scale, self.concentration = broadcast_all( |
| 47 | + loc, scale, concentration |
| 48 | + ) |
| 49 | + if ( |
| 50 | + isinstance(loc, Number) |
| 51 | + and isinstance(scale, Number) |
| 52 | + and isinstance(concentration, Number) |
| 53 | + ): |
| 54 | + batch_shape = torch.Size() |
| 55 | + else: |
| 56 | + batch_shape = self.loc.size() |
| 57 | + super().__init__(batch_shape, validate_args=validate_args) |
| 58 | + |
| 59 | + def expand(self, batch_shape, _instance=None): |
| 60 | + new = self._get_checked_instance(GeneralizedPareto, _instance) |
| 61 | + batch_shape = torch.Size(batch_shape) |
| 62 | + new.loc = self.loc.expand(batch_shape) |
| 63 | + new.scale = self.scale.expand(batch_shape) |
| 64 | + new.concentration = self.concentration.expand(batch_shape) |
| 65 | + super(GeneralizedPareto, new).__init__(batch_shape, validate_args=False) |
| 66 | + new._validate_args = self._validate_args |
| 67 | + return new |
| 68 | + |
| 69 | + def rsample(self, sample_shape=torch.Size()): |
| 70 | + shape = self._extended_shape(sample_shape) |
| 71 | + u = torch.rand(shape, dtype=self.loc.dtype, device=self.loc.device) |
| 72 | + return self.icdf(u) |
| 73 | + |
| 74 | + def log_prob(self, value): |
| 75 | + if self._validate_args: |
| 76 | + self._validate_sample(value) |
| 77 | + z = self._z(value) |
| 78 | + eq_zero = torch.isclose(self.concentration, torch.tensor(0.0)) |
| 79 | + safe_conc = torch.where( |
| 80 | + eq_zero, torch.ones_like(self.concentration), self.concentration |
| 81 | + ) |
| 82 | + y = 1 / safe_conc + torch.ones_like(z) |
| 83 | + where_nonzero = torch.where(y == 0, y, y * torch.log1p(safe_conc * z)) |
| 84 | + log_scale = ( |
| 85 | + math.log(self.scale) if isinstance(self.scale, Real) else self.scale.log() |
| 86 | + ) |
| 87 | + return -log_scale - torch.where(eq_zero, z, where_nonzero) |
| 88 | + |
| 89 | + def log_survival_function(self, value): |
| 90 | + if self._validate_args: |
| 91 | + self._validate_sample(value) |
| 92 | + z = self._z(value) |
| 93 | + eq_zero = torch.isclose(self.concentration, torch.tensor(0.0)) |
| 94 | + safe_conc = torch.where( |
| 95 | + eq_zero, torch.ones_like(self.concentration), self.concentration |
| 96 | + ) |
| 97 | + where_nonzero = -torch.log1p(safe_conc * z) / safe_conc |
| 98 | + return torch.where(eq_zero, -z, where_nonzero) |
| 99 | + |
| 100 | + def log_cdf(self, value): |
| 101 | + return torch.log1p(-torch.exp(self.log_survival_function(value))) |
| 102 | + |
| 103 | + def cdf(self, value): |
| 104 | + return torch.exp(self.log_cdf(value)) |
| 105 | + |
| 106 | + def icdf(self, value): |
| 107 | + loc = self.loc |
| 108 | + scale = self.scale |
| 109 | + concentration = self.concentration |
| 110 | + eq_zero = torch.isclose(concentration, torch.zeros_like(concentration)) |
| 111 | + safe_conc = torch.where(eq_zero, torch.ones_like(concentration), concentration) |
| 112 | + logu = torch.log1p(-value) |
| 113 | + where_nonzero = loc + scale / safe_conc * torch.expm1(-safe_conc * logu) |
| 114 | + where_zero = loc - scale * logu |
| 115 | + return torch.where(eq_zero, where_zero, where_nonzero) |
| 116 | + |
| 117 | + def _z(self, x): |
| 118 | + return (x - self.loc) / self.scale |
| 119 | + |
| 120 | + @property |
| 121 | + def mean(self): |
| 122 | + concentration = self.concentration |
| 123 | + valid = concentration < 1 |
| 124 | + safe_conc = torch.where(valid, concentration, 0.5) |
| 125 | + result = self.loc + self.scale / (1 - safe_conc) |
| 126 | + return torch.where(valid, result, nan) |
| 127 | + |
| 128 | + @property |
| 129 | + def variance(self): |
| 130 | + concentration = self.concentration |
| 131 | + valid = concentration < 0.5 |
| 132 | + safe_conc = torch.where(valid, concentration, 0.25) |
| 133 | + result = self.scale**2 / ((1 - safe_conc) ** 2 * (1 - 2 * safe_conc)) |
| 134 | + return torch.where(valid, result, nan) |
| 135 | + |
| 136 | + def entropy(self): |
| 137 | + ans = torch.log(self.scale) + self.concentration + 1 |
| 138 | + return torch.broadcast_to(ans, self._batch_shape) |
| 139 | + |
| 140 | + @property |
| 141 | + def mode(self): |
| 142 | + return self.loc |
| 143 | + |
| 144 | + @constraints.dependent_property(is_discrete=False, event_dim=0) |
| 145 | + def support(self): |
| 146 | + lower = self.loc |
| 147 | + upper = torch.where( |
| 148 | + self.concentration < 0, lower - self.scale / self.concentration, inf |
| 149 | + ) |
| 150 | + return constraints.interval(lower, upper) |
0 commit comments