[docs]classStudentT(Distribution):r""" Creates a Student's t-distribution parameterized by degree of freedom :attr:`df`, mean :attr:`loc` and scale :attr:`scale`. Example:: >>> # xdoctest: +IGNORE_WANT("non-deterinistic") >>> m = StudentT(torch.tensor([2.0])) >>> m.sample() # Student's t-distributed with degrees of freedom=2 tensor([ 0.1046]) Args: df (float or Tensor): degrees of freedom loc (float or Tensor): mean of the distribution scale (float or Tensor): scale of the distribution """arg_constraints={"df":constraints.positive,"loc":constraints.real,"scale":constraints.positive,}support=constraints.realhas_rsample=True@propertydefmean(self):m=self.loc.clone(memory_format=torch.contiguous_format)m[self.df<=1]=nanreturnm@propertydefmode(self):returnself.loc@propertydefvariance(self):m=self.df.clone(memory_format=torch.contiguous_format)m[self.df>2]=(self.scale[self.df>2].pow(2)*self.df[self.df>2]/(self.df[self.df>2]-2))m[(self.df<=2)&(self.df>1)]=infm[self.df<=1]=nanreturnmdef__init__(self,df,loc=0.0,scale=1.0,validate_args=None):self.df,self.loc,self.scale=broadcast_all(df,loc,scale)self._chi2=Chi2(self.df)batch_shape=self.df.size()super().__init__(batch_shape,validate_args=validate_args)
[docs]defrsample(self,sample_shape=torch.Size()):# NOTE: This does not agree with scipy implementation as much as other distributions.# (see https://github.com/fritzo/notebooks/blob/master/debug-student-t.ipynb). Using DoubleTensor# parameters seems to help.# X ~ Normal(0, 1)# Z ~ Chi2(df)# Y = X / sqrt(Z / df) ~ StudentT(df)shape=self._extended_shape(sample_shape)X=_standard_normal(shape,dtype=self.df.dtype,device=self.df.device)Z=self._chi2.rsample(sample_shape)Y=X*torch.rsqrt(Z/self.df)returnself.loc+self.scale*Y
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.