votes up 1

'df' is 'nan' but must be greater than zero or 'np.inf'.

Package:
Exception Class:
ValueError

Raise code

        # Process degrees of freedom.
        if df is None:
            df = 1
        elif df <= 0:
            raise ValueError("'df' must be greater than zero.")
        elif np.isnan(df):
            raise ValueError("'df' is 'nan' but must be greater than zero or 'np.inf'.")

        return dim, loc, shape, df


class multivariate_t_frozen(multi_rv_frozen):

    def __init__(self, loc=None, shape=1, df=1, allow_singular=False,

Ways to fix

votes up 2 votes down

This happens when df is given np.nan value.

Here is how this exception can be reproduced

  • Install numpy and scipy
$ pip install scipy numpy

from scipy.stats import multivariate_t
import numpy as np
x = [0.45]
loc = [01]
shape = [10.1]
df = np.nan
multivariate_t.rvs(loc, shape, df)

Output:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-b500e16a423d> in <module>()  5 shape = [1, 0.1]  6 df = np.nan ----> 7 multivariate_t.rvs(loc, shape, df) 
/usr/local/lib/python3.7/dist-packages/scipy/stats/_multivariate.py in rvs(self, loc, shape, df, size, random_state)  4070 # http://rjournal.github.io/archive/2013-2/hofert.pdf  4071 # -> 4072 dim, loc, shape, df = self._process_parameters(loc, shape, df)  4073 if random_state is not None:  4074 rng = check_random_state(random_state) 
/usr/local/lib/python3.7/dist-packages/scipy/stats/_multivariate.py in _process_parameters(self, loc, shape, df)  4156 raise ValueError("'df' must be greater than zero.")  4157 elif np.isnan(df): -> 4158 raise ValueError("'df' is 'nan' but must be greater than zero or 'np.inf'.")  4159   4160 return dim, loc, shape, df 
ValueError: 'df' is 'nan' but must be greater than zero or 'np.inf'.

Fixed:

from scipy.stats import multivariate_t
x = [0.45]
loc = [01]
shape = [10.1]
df = 7
multivariate_t.rvs(loc, shape, df)

array([-1.09199999, 0.78086194])

Nov 25, 2021 kellemnegasi answer
kellemnegasi 18.3k

Add a possible fix

Please authorize to post fix