Caffe2 - Python API
A deep learning, cross platform ML framework
fishersnedecor.py
1 from numbers import Number
2 import torch
3 import math
4 from torch._six import nan
5 from torch.distributions import constraints
6 from torch.distributions.distribution import Distribution
7 from torch.distributions.gamma import Gamma
8 from torch.distributions.utils import broadcast_all
9 
10 
12  r"""
13  Creates a Fisher-Snedecor distribution parameterized by :attr:`df1` and :attr:`df2`.
14 
15  Example::
16 
17  >>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0]))
18  >>> m.sample() # Fisher-Snedecor-distributed with df1=1 and df2=2
19  tensor([ 0.2453])
20 
21  Args:
22  df1 (float or Tensor): degrees of freedom parameter 1
23  df2 (float or Tensor): degrees of freedom parameter 2
24  """
25  arg_constraints = {'df1': constraints.positive, 'df2': constraints.positive}
26  support = constraints.positive
27  has_rsample = True
28 
29  def __init__(self, df1, df2, validate_args=None):
30  self.df1, self.df2 = broadcast_all(df1, df2)
31  self._gamma1 = Gamma(self.df1 * 0.5, self.df1)
32  self._gamma2 = Gamma(self.df2 * 0.5, self.df2)
33 
34  if isinstance(df1, Number) and isinstance(df2, Number):
35  batch_shape = torch.Size()
36  else:
37  batch_shape = self.df1.size()
38  super(FisherSnedecor, self).__init__(batch_shape, validate_args=validate_args)
39 
40  def expand(self, batch_shape, _instance=None):
41  new = self._get_checked_instance(FisherSnedecor, _instance)
42  batch_shape = torch.Size(batch_shape)
43  new.df1 = self.df1.expand(batch_shape)
44  new.df2 = self.df2.expand(batch_shape)
45  new._gamma1 = self._gamma1.expand(batch_shape)
46  new._gamma2 = self._gamma2.expand(batch_shape)
47  super(FisherSnedecor, new).__init__(batch_shape, validate_args=False)
48  new._validate_args = self._validate_args
49  return new
50 
51  @property
52  def mean(self):
53  df2 = self.df2.clone()
54  df2[df2 <= 2] = nan
55  return df2 / (df2 - 2)
56 
57  @property
58  def variance(self):
59  df2 = self.df2.clone()
60  df2[df2 <= 4] = nan
61  return 2 * df2.pow(2) * (self.df1 + df2 - 2) / (self.df1 * (df2 - 2).pow(2) * (df2 - 4))
62 
63  def rsample(self, sample_shape=torch.Size(())):
64  shape = self._extended_shape(sample_shape)
65  # X1 ~ Gamma(df1 / 2, 1 / df1), X2 ~ Gamma(df2 / 2, 1 / df2)
66  # Y = df2 * df1 * X1 / (df1 * df2 * X2) = X1 / X2 ~ F(df1, df2)
67  X1 = self._gamma1.rsample(sample_shape).view(shape)
68  X2 = self._gamma2.rsample(sample_shape).view(shape)
69  tiny = torch.finfo(X2.dtype).tiny
70  X2.clamp_(min=tiny)
71  Y = X1 / X2
72  Y.clamp_(min=tiny)
73  return Y
74 
75  def log_prob(self, value):
76  if self._validate_args:
77  self._validate_sample(value)
78  ct1 = self.df1 * 0.5
79  ct2 = self.df2 * 0.5
80  ct3 = self.df1 / self.df2
81  t1 = (ct1 + ct2).lgamma() - ct1.lgamma() - ct2.lgamma()
82  t2 = ct1 * ct3.log() + (ct1 - 1) * torch.log(value)
83  t3 = (ct1 + ct2) * torch.log1p(ct3 * value)
84  return t1 + t2 - t3
def _get_checked_instance(self, cls, _instance=None)
def _extended_shape(self, sample_shape=torch.Size())