pytorch
101 строка · 3.4 Кб
1# mypy: allow-untyped-defs
2from numbers import Number
3
4import torch
5from torch import nan
6from torch.distributions import constraints
7from torch.distributions.distribution import Distribution
8from torch.distributions.gamma import Gamma
9from torch.distributions.utils import broadcast_all
10from torch.types import _size
11
12
13__all__ = ["FisherSnedecor"]
14
15
16class FisherSnedecor(Distribution):
17r"""
18Creates a Fisher-Snedecor distribution parameterized by :attr:`df1` and :attr:`df2`.
19
20Example::
21
22>>> # xdoctest: +IGNORE_WANT("non-deterministic")
23>>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0]))
24>>> m.sample() # Fisher-Snedecor-distributed with df1=1 and df2=2
25tensor([ 0.2453])
26
27Args:
28df1 (float or Tensor): degrees of freedom parameter 1
29df2 (float or Tensor): degrees of freedom parameter 2
30"""
31arg_constraints = {"df1": constraints.positive, "df2": constraints.positive}
32support = constraints.positive
33has_rsample = True
34
35def __init__(self, df1, df2, validate_args=None):
36self.df1, self.df2 = broadcast_all(df1, df2)
37self._gamma1 = Gamma(self.df1 * 0.5, self.df1)
38self._gamma2 = Gamma(self.df2 * 0.5, self.df2)
39
40if isinstance(df1, Number) and isinstance(df2, Number):
41batch_shape = torch.Size()
42else:
43batch_shape = self.df1.size()
44super().__init__(batch_shape, validate_args=validate_args)
45
46def expand(self, batch_shape, _instance=None):
47new = self._get_checked_instance(FisherSnedecor, _instance)
48batch_shape = torch.Size(batch_shape)
49new.df1 = self.df1.expand(batch_shape)
50new.df2 = self.df2.expand(batch_shape)
51new._gamma1 = self._gamma1.expand(batch_shape)
52new._gamma2 = self._gamma2.expand(batch_shape)
53super(FisherSnedecor, new).__init__(batch_shape, validate_args=False)
54new._validate_args = self._validate_args
55return new
56
57@property
58def mean(self):
59df2 = self.df2.clone(memory_format=torch.contiguous_format)
60df2[df2 <= 2] = nan
61return df2 / (df2 - 2)
62
63@property
64def mode(self):
65mode = (self.df1 - 2) / self.df1 * self.df2 / (self.df2 + 2)
66mode[self.df1 <= 2] = nan
67return mode
68
69@property
70def variance(self):
71df2 = self.df2.clone(memory_format=torch.contiguous_format)
72df2[df2 <= 4] = nan
73return (
742
75* df2.pow(2)
76* (self.df1 + df2 - 2)
77/ (self.df1 * (df2 - 2).pow(2) * (df2 - 4))
78)
79
80def rsample(self, sample_shape: _size = torch.Size(())) -> torch.Tensor:
81shape = self._extended_shape(sample_shape)
82# X1 ~ Gamma(df1 / 2, 1 / df1), X2 ~ Gamma(df2 / 2, 1 / df2)
83# Y = df2 * df1 * X1 / (df1 * df2 * X2) = X1 / X2 ~ F(df1, df2)
84X1 = self._gamma1.rsample(sample_shape).view(shape)
85X2 = self._gamma2.rsample(sample_shape).view(shape)
86tiny = torch.finfo(X2.dtype).tiny
87X2.clamp_(min=tiny)
88Y = X1 / X2
89Y.clamp_(min=tiny)
90return Y
91
92def log_prob(self, value):
93if self._validate_args:
94self._validate_sample(value)
95ct1 = self.df1 * 0.5
96ct2 = self.df2 * 0.5
97ct3 = self.df1 / self.df2
98t1 = (ct1 + ct2).lgamma() - ct1.lgamma() - ct2.lgamma()
99t2 = ct1 * ct3.log() + (ct1 - 1) * torch.log(value)
100t3 = (ct1 + ct2) * torch.log1p(ct3 * value)
101return t1 + t2 - t3
102