Caffe2 - Python API
A deep learning, cross platform ML framework
pareto.py
1 import torch
2 from torch.distributions import constraints
3 from torch.distributions.exponential import Exponential
4 from torch.distributions.transformed_distribution import TransformedDistribution
5 from torch.distributions.transforms import AffineTransform, ExpTransform
6 from torch.distributions.utils import broadcast_all
7 
8 
10  r"""
11  Samples from a Pareto Type 1 distribution.
12 
13  Example::
14 
15  >>> m = Pareto(torch.tensor([1.0]), torch.tensor([1.0]))
16  >>> m.sample() # sample from a Pareto distribution with scale=1 and alpha=1
17  tensor([ 1.5623])
18 
19  Args:
20  scale (float or Tensor): Scale parameter of the distribution
21  alpha (float or Tensor): Shape parameter of the distribution
22  """
23  arg_constraints = {'alpha': constraints.positive, 'scale': constraints.positive}
24 
25  def __init__(self, scale, alpha, validate_args=None):
26  self.scale, self.alpha = broadcast_all(scale, alpha)
27  base_dist = Exponential(self.alpha)
28  transforms = [ExpTransform(), AffineTransform(loc=0, scale=self.scale)]
29  super(Pareto, self).__init__(base_dist, transforms, validate_args=validate_args)
30 
31  def expand(self, batch_shape, _instance=None):
32  new = self._get_checked_instance(Pareto, _instance)
33  new.scale = self.scale.expand(batch_shape)
34  new.alpha = self.alpha.expand(batch_shape)
35  return super(Pareto, self).expand(batch_shape, _instance=new)
36 
37  @property
38  def mean(self):
39  # mean is inf for alpha <= 1
40  a = self.alpha.clone().clamp(min=1)
41  return a * self.scale / (a - 1)
42 
43  @property
44  def variance(self):
45  # var is inf for alpha <= 2
46  a = self.alpha.clone().clamp(min=2)
47  return self.scale.pow(2) * a / ((a - 1).pow(2) * (a - 2))
48 
49  @constraints.dependent_property
50  def support(self):
51  return constraints.greater_than(self.scale)
52 
53  def entropy(self):
54  return ((self.scale / self.alpha).log() + (1 + self.alpha.reciprocal()))
def _get_checked_instance(self, cls, _instance=None)