[Scipy-tickets] [SciPy] #1798: scipy.stats.mstat.kurtosis crashes with bias=False

SciPy Trac scipy-tickets@scipy....
Sat Dec 22 01:58:17 CST 2012

#1798: scipy.stats.mstat.kurtosis crashes with bias=False
 Reporter:  true         |       Owner:  rgommers   
     Type:  defect       |      Status:  new        
 Priority:  normal       |   Milestone:  Unscheduled
Component:  scipy.stats  |     Version:  0.11.0     
 Keywords:               |  
 In the masked version of kurtosis, the variable n is an array generated by
 counting the unmasked elements of an array a across a given axis:
 n = a.count(axis)

 Because of this, when correcting for bias (bias=False), the relevant
 indices of n should be selected, just like m2 and m4:
 def kurtosis(a, axis=0, fisher=True, bias=True):
     a, axis = _chk_asarray(a, axis)
     n = a.count(axis)
     m2 = moment(a,2,axis)
     m4 = moment(a,4,axis)
     vals = ma.where(m2 == 0, 0, m4/ m2**2.0)
     if not bias:
         can_correct = (n > 3) & (m2 > 0)
         if can_correct.any():
 >>>         n = np.extract(can_correct, n) # this should be added
             m2 = np.extract(can_correct, m2)
             m4 = np.extract(can_correct, m4)
             nval = 1.0/(n-2)/(n-3)*((n*n-1.0)*m4/m2**2.0-3*(n-1)**2.0)
             np.place(vals, can_correct, nval+3.0)
     if fisher:
         return vals - 3
         return vals

 Otherwise, this will cause line 1460
 nval = 1.0/(n-2)/(n-3)*((n*n-1.0)*m4/m2**2.0-3*(n-1)**2.0)
 to throw an error like:
 ValueError: operands could not be broadcast together with shapes (x) (y)
 where x is the total number of kurtoses (kurtosises?) returned (what n is
 currently), and y is the number of kurtoses to be corrected (what n should

 This problem doesn't happen with the unmasked version of kurtosis because
 n is a scalar in that function:
 n = a.shape[axis]

Ticket URL: <http://projects.scipy.org/scipy/ticket/1798>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.

More information about the Scipy-tickets mailing list