Sampling STudent T giving an error

Hello,

I’m trying to sample from the Student T distribution.

I use the following code:

m = StudentT(torch.tensor([4846]))
m.sample()

That is derived from the docs:

https://pytorch.org/docs/stable/distributions.html

Here is the error in and the traceback:

--------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-73-3011aef638a1> in <module>()
      6 annual_investment = torch.tensor([0])
      7 m = StudentT(torch.tensor([4846]))
----> 8 m.sample()
      9 # for x in range(iterations):
     10 

2 frames
/usr/local/lib/python3.6/dist-packages/torch/distributions/distribution.py in sample(self, sample_shape)
    117         """
    118         with torch.no_grad():
--> 119             return self.rsample(sample_shape)
    120 
    121     def rsample(self, sample_shape=torch.Size()):

/usr/local/lib/python3.6/dist-packages/torch/distributions/studentT.py in rsample(self, sample_shape)
     68         #   Y = X / sqrt(Z / df) ~ StudentT(df)
     69         shape = self._extended_shape(sample_shape)
---> 70         X = _standard_normal(shape, dtype=self.df.dtype, device=self.df.device)
     71         Z = self._chi2.rsample(sample_shape)
     72         Y = X * torch.rsqrt(Z / self.df)

/usr/local/lib/python3.6/dist-packages/torch/distributions/utils.py in _standard_normal(shape, dtype, device)
     39         return torch.normal(torch.zeros(shape, dtype=dtype, device=device),
     40                             torch.ones(shape, dtype=dtype, device=device))
---> 41     return torch.empty(shape, dtype=dtype, device=device).normal_()
     42 
     43 

RuntimeError: _th_normal_ not supported on CPUType for Long

Pass the degrees of freedom as a float or FloatTensor and it should work. :wink: