1*da0073e9SAndroid Build Coastguard Worker# mypy: allow-untyped-defs 2*da0073e9SAndroid Build Coastguard Workerfrom numbers import Number 3*da0073e9SAndroid Build Coastguard Worker 4*da0073e9SAndroid Build Coastguard Workerimport torch 5*da0073e9SAndroid Build Coastguard Workerfrom torch.distributions import constraints 6*da0073e9SAndroid Build Coastguard Workerfrom torch.distributions.exp_family import ExponentialFamily 7*da0073e9SAndroid Build Coastguard Workerfrom torch.distributions.utils import broadcast_all 8*da0073e9SAndroid Build Coastguard Workerfrom torch.types import _size 9*da0073e9SAndroid Build Coastguard Worker 10*da0073e9SAndroid Build Coastguard Worker 11*da0073e9SAndroid Build Coastguard Worker__all__ = ["Gamma"] 12*da0073e9SAndroid Build Coastguard Worker 13*da0073e9SAndroid Build Coastguard Worker 14*da0073e9SAndroid Build Coastguard Workerdef _standard_gamma(concentration): 15*da0073e9SAndroid Build Coastguard Worker return torch._standard_gamma(concentration) 16*da0073e9SAndroid Build Coastguard Worker 17*da0073e9SAndroid Build Coastguard Worker 18*da0073e9SAndroid Build Coastguard Workerclass Gamma(ExponentialFamily): 19*da0073e9SAndroid Build Coastguard Worker r""" 20*da0073e9SAndroid Build Coastguard Worker Creates a Gamma distribution parameterized by shape :attr:`concentration` and :attr:`rate`. 21*da0073e9SAndroid Build Coastguard Worker 22*da0073e9SAndroid Build Coastguard Worker Example:: 23*da0073e9SAndroid Build Coastguard Worker 24*da0073e9SAndroid Build Coastguard Worker >>> # xdoctest: +IGNORE_WANT("non-deterministic") 25*da0073e9SAndroid Build Coastguard Worker >>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0])) 26*da0073e9SAndroid Build Coastguard Worker >>> m.sample() # Gamma distributed with concentration=1 and rate=1 27*da0073e9SAndroid Build Coastguard Worker tensor([ 0.1046]) 28*da0073e9SAndroid Build Coastguard Worker 29*da0073e9SAndroid Build Coastguard Worker Args: 30*da0073e9SAndroid Build Coastguard Worker concentration (float or Tensor): shape parameter of the distribution 31*da0073e9SAndroid Build Coastguard Worker (often referred to as alpha) 32*da0073e9SAndroid Build Coastguard Worker rate (float or Tensor): rate = 1 / scale of the distribution 33*da0073e9SAndroid Build Coastguard Worker (often referred to as beta) 34*da0073e9SAndroid Build Coastguard Worker """ 35*da0073e9SAndroid Build Coastguard Worker arg_constraints = { 36*da0073e9SAndroid Build Coastguard Worker "concentration": constraints.positive, 37*da0073e9SAndroid Build Coastguard Worker "rate": constraints.positive, 38*da0073e9SAndroid Build Coastguard Worker } 39*da0073e9SAndroid Build Coastguard Worker support = constraints.nonnegative 40*da0073e9SAndroid Build Coastguard Worker has_rsample = True 41*da0073e9SAndroid Build Coastguard Worker _mean_carrier_measure = 0 42*da0073e9SAndroid Build Coastguard Worker 43*da0073e9SAndroid Build Coastguard Worker @property 44*da0073e9SAndroid Build Coastguard Worker def mean(self): 45*da0073e9SAndroid Build Coastguard Worker return self.concentration / self.rate 46*da0073e9SAndroid Build Coastguard Worker 47*da0073e9SAndroid Build Coastguard Worker @property 48*da0073e9SAndroid Build Coastguard Worker def mode(self): 49*da0073e9SAndroid Build Coastguard Worker return ((self.concentration - 1) / self.rate).clamp(min=0) 50*da0073e9SAndroid Build Coastguard Worker 51*da0073e9SAndroid Build Coastguard Worker @property 52*da0073e9SAndroid Build Coastguard Worker def variance(self): 53*da0073e9SAndroid Build Coastguard Worker return self.concentration / self.rate.pow(2) 54*da0073e9SAndroid Build Coastguard Worker 55*da0073e9SAndroid Build Coastguard Worker def __init__(self, concentration, rate, validate_args=None): 56*da0073e9SAndroid Build Coastguard Worker self.concentration, self.rate = broadcast_all(concentration, rate) 57*da0073e9SAndroid Build Coastguard Worker if isinstance(concentration, Number) and isinstance(rate, Number): 58*da0073e9SAndroid Build Coastguard Worker batch_shape = torch.Size() 59*da0073e9SAndroid Build Coastguard Worker else: 60*da0073e9SAndroid Build Coastguard Worker batch_shape = self.concentration.size() 61*da0073e9SAndroid Build Coastguard Worker super().__init__(batch_shape, validate_args=validate_args) 62*da0073e9SAndroid Build Coastguard Worker 63*da0073e9SAndroid Build Coastguard Worker def expand(self, batch_shape, _instance=None): 64*da0073e9SAndroid Build Coastguard Worker new = self._get_checked_instance(Gamma, _instance) 65*da0073e9SAndroid Build Coastguard Worker batch_shape = torch.Size(batch_shape) 66*da0073e9SAndroid Build Coastguard Worker new.concentration = self.concentration.expand(batch_shape) 67*da0073e9SAndroid Build Coastguard Worker new.rate = self.rate.expand(batch_shape) 68*da0073e9SAndroid Build Coastguard Worker super(Gamma, new).__init__(batch_shape, validate_args=False) 69*da0073e9SAndroid Build Coastguard Worker new._validate_args = self._validate_args 70*da0073e9SAndroid Build Coastguard Worker return new 71*da0073e9SAndroid Build Coastguard Worker 72*da0073e9SAndroid Build Coastguard Worker def rsample(self, sample_shape: _size = torch.Size()) -> torch.Tensor: 73*da0073e9SAndroid Build Coastguard Worker shape = self._extended_shape(sample_shape) 74*da0073e9SAndroid Build Coastguard Worker value = _standard_gamma(self.concentration.expand(shape)) / self.rate.expand( 75*da0073e9SAndroid Build Coastguard Worker shape 76*da0073e9SAndroid Build Coastguard Worker ) 77*da0073e9SAndroid Build Coastguard Worker value.detach().clamp_( 78*da0073e9SAndroid Build Coastguard Worker min=torch.finfo(value.dtype).tiny 79*da0073e9SAndroid Build Coastguard Worker ) # do not record in autograd graph 80*da0073e9SAndroid Build Coastguard Worker return value 81*da0073e9SAndroid Build Coastguard Worker 82*da0073e9SAndroid Build Coastguard Worker def log_prob(self, value): 83*da0073e9SAndroid Build Coastguard Worker value = torch.as_tensor(value, dtype=self.rate.dtype, device=self.rate.device) 84*da0073e9SAndroid Build Coastguard Worker if self._validate_args: 85*da0073e9SAndroid Build Coastguard Worker self._validate_sample(value) 86*da0073e9SAndroid Build Coastguard Worker return ( 87*da0073e9SAndroid Build Coastguard Worker torch.xlogy(self.concentration, self.rate) 88*da0073e9SAndroid Build Coastguard Worker + torch.xlogy(self.concentration - 1, value) 89*da0073e9SAndroid Build Coastguard Worker - self.rate * value 90*da0073e9SAndroid Build Coastguard Worker - torch.lgamma(self.concentration) 91*da0073e9SAndroid Build Coastguard Worker ) 92*da0073e9SAndroid Build Coastguard Worker 93*da0073e9SAndroid Build Coastguard Worker def entropy(self): 94*da0073e9SAndroid Build Coastguard Worker return ( 95*da0073e9SAndroid Build Coastguard Worker self.concentration 96*da0073e9SAndroid Build Coastguard Worker - torch.log(self.rate) 97*da0073e9SAndroid Build Coastguard Worker + torch.lgamma(self.concentration) 98*da0073e9SAndroid Build Coastguard Worker + (1.0 - self.concentration) * torch.digamma(self.concentration) 99*da0073e9SAndroid Build Coastguard Worker ) 100*da0073e9SAndroid Build Coastguard Worker 101*da0073e9SAndroid Build Coastguard Worker @property 102*da0073e9SAndroid Build Coastguard Worker def _natural_params(self): 103*da0073e9SAndroid Build Coastguard Worker return (self.concentration - 1, -self.rate) 104*da0073e9SAndroid Build Coastguard Worker 105*da0073e9SAndroid Build Coastguard Worker def _log_normalizer(self, x, y): 106*da0073e9SAndroid Build Coastguard Worker return torch.lgamma(x + 1) + (x + 1) * torch.log(-y.reciprocal()) 107*da0073e9SAndroid Build Coastguard Worker 108*da0073e9SAndroid Build Coastguard Worker def cdf(self, value): 109*da0073e9SAndroid Build Coastguard Worker if self._validate_args: 110*da0073e9SAndroid Build Coastguard Worker self._validate_sample(value) 111*da0073e9SAndroid Build Coastguard Worker return torch.special.gammainc(self.concentration, self.rate * value) 112