Shortcuts

torch.distributions.fishersnedecor 的源代码

from numbers import Number

import torch
from torch import nan
from torch.distributions import constraints
from torch.distributions.distribution import Distribution
from torch.distributions.gamma import Gamma
from torch.distributions.utils import broadcast_all

__all__ = ["FisherSnedecor"]


[docs]class FisherSnedecor(Distribution): r""" 创建一个由 :attr:`df1` 和 :attr:`df2` 参数化的 Fisher-Snedecor 分布。 示例:: >>> # xdoctest: +IGNORE_WANT("非确定性") >>> m = FisherSnedecor(torch.tensor([1.0]), torch.tensor([2.0])) >>> m.sample() # 具有 df1=1 和 df2=2 的 Fisher-Snedecor 分布 tensor([ 0.2453]) 参数: df1 (float 或 Tensor): 自由度参数 1 df2 (float 或 Tensor): 自由度参数 2 """ arg_constraints = {"df1": constraints.positive, "df2": constraints.positive} support = constraints.positive has_rsample = True def __init__(self, df1, df2, validate_args=None): self.df1, self.df2 = broadcast_all(df1, df2) self._gamma1 = Gamma(self.df1 * 0.5, self.df1) self._gamma2 = Gamma(self.df2 * 0.5, self.df2) if isinstance(df1, Number) and isinstance(df2, Number): batch_shape = torch.Size() else: batch_shape = self.df1.size() super().__init__(batch_shape, validate_args=validate_args)
[docs] def expand(self, batch_shape, _instance=None): new = self._get_checked_instance(FisherSnedecor, _instance) batch_shape = torch.Size(batch_shape) new.df1 = self.df1.expand(batch_shape) new.df2 = self.df2.expand(batch_shape) new._gamma1 = self._gamma1.expand(batch_shape) new._gamma2 = self._gamma2.expand(batch_shape) super(FisherSnedecor, new).__init__(batch_shape, validate_args=False) new._validate_args = self._validate_args return new
@property def mean(self): df2 = self.df2.clone(memory_format=torch.contiguous_format) df2[df2 <= 2] = nan return df2 / (df2 - 2) @property def mode(self): mode = (self.df1 - 2) / self.df1 * self.df2 / (self.df2 + 2) mode[self.df1 <= 2] = nan return mode @property def variance(self): df2 = self.df2.clone(memory_format=torch.contiguous_format) df2[df2 <= 4] = nan return ( 2 * df2.pow(2) * (self.df1 + df2 - 2) / (self.df1 * (df2</
优云智算