pytorch
98 строк · 3.4 Кб
1from numbers import Number2
3import torch4from torch import nan5from torch.distributions import constraints6from torch.distributions.distribution import Distribution7from torch.distributions.gamma import Gamma8from torch.distributions.utils import broadcast_all9
10__all__ = ["FisherSnedecor"]11
12
13class FisherSnedecor(Distribution):14r"""15Creates a Fisher-Snedecor distribution parameterized by :attr:`df1` and :attr:`df2`.
16
17Example::
18
19>>> # xdoctest: +IGNORE_WANT("non-deterministic")
20>>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0]))
21>>> m.sample() # Fisher-Snedecor-distributed with df1=1 and df2=2
22tensor([ 0.2453])
23
24Args:
25df1 (float or Tensor): degrees of freedom parameter 1
26df2 (float or Tensor): degrees of freedom parameter 2
27"""
28arg_constraints = {"df1": constraints.positive, "df2": constraints.positive}29support = constraints.positive30has_rsample = True31
32def __init__(self, df1, df2, validate_args=None):33self.df1, self.df2 = broadcast_all(df1, df2)34self._gamma1 = Gamma(self.df1 * 0.5, self.df1)35self._gamma2 = Gamma(self.df2 * 0.5, self.df2)36
37if isinstance(df1, Number) and isinstance(df2, Number):38batch_shape = torch.Size()39else:40batch_shape = self.df1.size()41super().__init__(batch_shape, validate_args=validate_args)42
43def expand(self, batch_shape, _instance=None):44new = self._get_checked_instance(FisherSnedecor, _instance)45batch_shape = torch.Size(batch_shape)46new.df1 = self.df1.expand(batch_shape)47new.df2 = self.df2.expand(batch_shape)48new._gamma1 = self._gamma1.expand(batch_shape)49new._gamma2 = self._gamma2.expand(batch_shape)50super(FisherSnedecor, new).__init__(batch_shape, validate_args=False)51new._validate_args = self._validate_args52return new53
54@property55def mean(self):56df2 = self.df2.clone(memory_format=torch.contiguous_format)57df2[df2 <= 2] = nan58return df2 / (df2 - 2)59
60@property61def mode(self):62mode = (self.df1 - 2) / self.df1 * self.df2 / (self.df2 + 2)63mode[self.df1 <= 2] = nan64return mode65
66@property67def variance(self):68df2 = self.df2.clone(memory_format=torch.contiguous_format)69df2[df2 <= 4] = nan70return (71272* df2.pow(2)73* (self.df1 + df2 - 2)74/ (self.df1 * (df2 - 2).pow(2) * (df2 - 4))75)76
77def rsample(self, sample_shape=torch.Size(())):78shape = self._extended_shape(sample_shape)79# X1 ~ Gamma(df1 / 2, 1 / df1), X2 ~ Gamma(df2 / 2, 1 / df2)80# Y = df2 * df1 * X1 / (df1 * df2 * X2) = X1 / X2 ~ F(df1, df2)81X1 = self._gamma1.rsample(sample_shape).view(shape)82X2 = self._gamma2.rsample(sample_shape).view(shape)83tiny = torch.finfo(X2.dtype).tiny84X2.clamp_(min=tiny)85Y = X1 / X286Y.clamp_(min=tiny)87return Y88
89def log_prob(self, value):90if self._validate_args:91self._validate_sample(value)92ct1 = self.df1 * 0.593ct2 = self.df2 * 0.594ct3 = self.df1 / self.df295t1 = (ct1 + ct2).lgamma() - ct1.lgamma() - ct2.lgamma()96t2 = ct1 * ct3.log() + (ct1 - 1) * torch.log(value)97t3 = (ct1 + ct2) * torch.log1p(ct3 * value)98return t1 + t2 - t399