isinf(mu) error in Scipy stats when calling std for exponweib?

非 Y 不嫁゛ 提交于 2019-12-12 04:23:24

问题


I have been getting this error when I call std on a frozen exponweib distribution?

Here is the code:

d = st.exponweib
params = d.fit(y)

arg = params[:-2]
loc = params[-2]
scale = params[-1]

rv1 = d(arg,loc,scale)

print rv1.std()

The parameters after fitting are:

arg: (3.445136651705262, 0.10885378466279112)

loc: 11770.05

scale: 3.87424773976

Here is the error:

ValueError                                Traceback (most recent call last)
<ipython-input-637-4394814bbb8c> in <module>()
     11 rv1 = d(arg,loc,scale)
     12 
---> 13 print rv1.std()

.../anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.pyc in std(self)
    487 
    488     def std(self):
--> 489         return self.dist.std(*self.args, **self.kwds)
    490 
    491     def moment(self, n):

.../anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.pyc in std(self, *args, **kwds)
   1259         """
   1260         kwds['moments'] = 'v'
-> 1261         res = sqrt(self.stats(*args, **kwds))
   1262         return res
   1263 

.../anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.pyc in stats(self, *args, **kwds)
   1032                         mu = self._munp(1, *goodargs)
   1033                     mu2 = mu2p - mu * mu
-> 1034                     if np.isinf(mu):
   1035                         # if mean is inf then var is also inf
   1036                         mu2 = np.inf

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please let me what is wrong with what I'm doing or how to avoid this.


回答1:


The exponweib distribution has two required parameters a, c and two optional, loc and scale. When you call d(arg, loc, scale) the result is that arg is interpreted as a, loc is interpreted as c, and scale is interpreted as loc. And since your arg is a tuple of two elements, you end up with a tuple of random variables, neither of which is what you want.

Solution: unpack the tuple: d(*arg, loc, scale). Or even simpler, use

rv1 = d(*params)

which unpacks all the parameters for you, without you having to extract and name them.


By the way, when you want to provide your own loc and scale of a random variable, it's better to pass them as named arguments, like d(3, 5, loc=90, scale=0.3). This avoids the situation you encountered, when some of these parameters get interpreted as something else because you didn't get some argument right. In your example, d(arg, loc=loc, scale=scale) would immediately throw an error, "missing 1 required positional argument: 'c'" instead of taking loc instead of c.



来源:https://stackoverflow.com/questions/41200669/isinfmu-error-in-scipy-stats-when-calling-std-for-exponweib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!