pytorch
97 строк · 3.4 Кб
1import torch2from torch import nan3from torch.distributions import constraints4from torch.distributions.transformed_distribution import TransformedDistribution5from torch.distributions.transforms import AffineTransform, PowerTransform6from torch.distributions.uniform import Uniform7from torch.distributions.utils import broadcast_all, euler_constant8
9__all__ = ["Kumaraswamy"]10
11
12def _moments(a, b, n):13"""14Computes nth moment of Kumaraswamy using using torch.lgamma
15"""
16arg1 = 1 + n / a17log_value = torch.lgamma(arg1) + torch.lgamma(b) - torch.lgamma(arg1 + b)18return b * torch.exp(log_value)19
20
21class Kumaraswamy(TransformedDistribution):22r"""23Samples from a Kumaraswamy distribution.
24
25Example::
26
27>>> # xdoctest: +IGNORE_WANT("non-deterministic")
28>>> m = Kumaraswamy(torch.tensor([1.0]), torch.tensor([1.0]))
29>>> m.sample() # sample from a Kumaraswamy distribution with concentration alpha=1 and beta=1
30tensor([ 0.1729])
31
32Args:
33concentration1 (float or Tensor): 1st concentration parameter of the distribution
34(often referred to as alpha)
35concentration0 (float or Tensor): 2nd concentration parameter of the distribution
36(often referred to as beta)
37"""
38arg_constraints = {39"concentration1": constraints.positive,40"concentration0": constraints.positive,41}42support = constraints.unit_interval43has_rsample = True44
45def __init__(self, concentration1, concentration0, validate_args=None):46self.concentration1, self.concentration0 = broadcast_all(47concentration1, concentration048)49finfo = torch.finfo(self.concentration0.dtype)50base_dist = Uniform(51torch.full_like(self.concentration0, 0),52torch.full_like(self.concentration0, 1),53validate_args=validate_args,54)55transforms = [56PowerTransform(exponent=self.concentration0.reciprocal()),57AffineTransform(loc=1.0, scale=-1.0),58PowerTransform(exponent=self.concentration1.reciprocal()),59]60super().__init__(base_dist, transforms, validate_args=validate_args)61
62def expand(self, batch_shape, _instance=None):63new = self._get_checked_instance(Kumaraswamy, _instance)64new.concentration1 = self.concentration1.expand(batch_shape)65new.concentration0 = self.concentration0.expand(batch_shape)66return super().expand(batch_shape, _instance=new)67
68@property69def mean(self):70return _moments(self.concentration1, self.concentration0, 1)71
72@property73def mode(self):74# Evaluate in log-space for numerical stability.75log_mode = (76self.concentration0.reciprocal() * (-self.concentration0).log1p()77- (-self.concentration0 * self.concentration1).log1p()78)79log_mode[(self.concentration0 < 1) | (self.concentration1 < 1)] = nan80return log_mode.exp()81
82@property83def variance(self):84return _moments(self.concentration1, self.concentration0, 2) - torch.pow(85self.mean, 286)87
88def entropy(self):89t1 = 1 - self.concentration1.reciprocal()90t0 = 1 - self.concentration0.reciprocal()91H0 = torch.digamma(self.concentration0 + 1) + euler_constant92return (93t0
94+ t1 * H095- torch.log(self.concentration1)96- torch.log(self.concentration0)97)98