1# mypy: allow-untyped-defs 2import torch 3from torch import nan 4from torch.distributions import constraints 5from torch.distributions.transformed_distribution import TransformedDistribution 6from torch.distributions.transforms import AffineTransform, PowerTransform 7from torch.distributions.uniform import Uniform 8from torch.distributions.utils import broadcast_all, euler_constant 9 10 11__all__ = ["Kumaraswamy"] 12 13 14def _moments(a, b, n): 15 """ 16 Computes nth moment of Kumaraswamy using using torch.lgamma 17 """ 18 arg1 = 1 + n / a 19 log_value = torch.lgamma(arg1) + torch.lgamma(b) - torch.lgamma(arg1 + b) 20 return b * torch.exp(log_value) 21 22 23class Kumaraswamy(TransformedDistribution): 24 r""" 25 Samples from a Kumaraswamy distribution. 26 27 Example:: 28 29 >>> # xdoctest: +IGNORE_WANT("non-deterministic") 30 >>> m = Kumaraswamy(torch.tensor([1.0]), torch.tensor([1.0])) 31 >>> m.sample() # sample from a Kumaraswamy distribution with concentration alpha=1 and beta=1 32 tensor([ 0.1729]) 33 34 Args: 35 concentration1 (float or Tensor): 1st concentration parameter of the distribution 36 (often referred to as alpha) 37 concentration0 (float or Tensor): 2nd concentration parameter of the distribution 38 (often referred to as beta) 39 """ 40 arg_constraints = { 41 "concentration1": constraints.positive, 42 "concentration0": constraints.positive, 43 } 44 support = constraints.unit_interval 45 has_rsample = True 46 47 def __init__(self, concentration1, concentration0, validate_args=None): 48 self.concentration1, self.concentration0 = broadcast_all( 49 concentration1, concentration0 50 ) 51 finfo = torch.finfo(self.concentration0.dtype) 52 base_dist = Uniform( 53 torch.full_like(self.concentration0, 0), 54 torch.full_like(self.concentration0, 1), 55 validate_args=validate_args, 56 ) 57 transforms = [ 58 PowerTransform(exponent=self.concentration0.reciprocal()), 59 AffineTransform(loc=1.0, scale=-1.0), 60 PowerTransform(exponent=self.concentration1.reciprocal()), 61 ] 62 super().__init__(base_dist, transforms, validate_args=validate_args) 63 64 def expand(self, batch_shape, _instance=None): 65 new = self._get_checked_instance(Kumaraswamy, _instance) 66 new.concentration1 = self.concentration1.expand(batch_shape) 67 new.concentration0 = self.concentration0.expand(batch_shape) 68 return super().expand(batch_shape, _instance=new) 69 70 @property 71 def mean(self): 72 return _moments(self.concentration1, self.concentration0, 1) 73 74 @property 75 def mode(self): 76 # Evaluate in log-space for numerical stability. 77 log_mode = ( 78 self.concentration0.reciprocal() * (-self.concentration0).log1p() 79 - (-self.concentration0 * self.concentration1).log1p() 80 ) 81 log_mode[(self.concentration0 < 1) | (self.concentration1 < 1)] = nan 82 return log_mode.exp() 83 84 @property 85 def variance(self): 86 return _moments(self.concentration1, self.concentration0, 2) - torch.pow( 87 self.mean, 2 88 ) 89 90 def entropy(self): 91 t1 = 1 - self.concentration1.reciprocal() 92 t0 = 1 - self.concentration0.reciprocal() 93 H0 = torch.digamma(self.concentration0 + 1) + euler_constant 94 return ( 95 t0 96 + t1 * H0 97 - torch.log(self.concentration1) 98 - torch.log(self.concentration0) 99 ) 100