Skip to content
This repository
Browse code

Fixed a bug in scipy.stats.mstats.kurtosis() that caused a ValueError…

… to be thrown when bias=False. In the same context, fixed a TypeError that was being thrown for one-dimensional input where all elements are masked.

Appropriate tests have been added to the testing suite for the function.

Fixed a bug in scipy.stats.mstats.kurtosis() that caused a ValueError to be
thrown when bias=False. In the same context, fixed a TypeError that was being
thrown for one-dimensional input where all elements are masked.

Appropriate tests have been added to the testing suite for the function.

Relates to ticket: http://projects.scipy.org/scipy/ticket/1798
  • Loading branch information...
commit 17a62cb77b620cfdb13abb592b87b16563cc64c9 1 parent 785491c
trueprice authored December 24, 2012 rgommers committed December 27, 2012
5  scipy/stats/mstats_basic.py
@@ -1448,13 +1448,14 @@ def skew(a, axis=0, bias=True):
1448 1448
 
1449 1449
 def kurtosis(a, axis=0, fisher=True, bias=True):
1450 1450
     a, axis = _chk_asarray(a, axis)
1451  
-    n = a.count(axis)
1452 1451
     m2 = moment(a,2,axis)
1453 1452
     m4 = moment(a,4,axis)
1454 1453
     vals = ma.where(m2 == 0, 0, m4/ m2**2.0)
1455 1454
     if not bias:
1456  
-        can_correct = (n > 3) & (m2 > 0)
  1455
+        n = a.count(axis)
  1456
+        can_correct = (n > 3) & (m2 is not ma.masked and m2 > 0)
1457 1457
         if can_correct.any():
  1458
+            n = np.extract(can_correct, n)
1458 1459
             m2 = np.extract(can_correct, m2)
1459 1460
             m4 = np.extract(can_correct, m4)
1460 1461
             nval = 1.0/(n-2)/(n-3)*((n*n-1.0)*m4/m2**2.0-3*(n-1)**2.0)
33  scipy/stats/tests/test_mstats_basic.py
@@ -289,6 +289,18 @@ class TestMoments(TestCase):
289 289
     testcase = [1,2,3,4]
290 290
     testmathworks = ma.fix_invalid([1.165 , 0.6268, 0.0751, 0.3516, -0.6965,
291 291
                                     np.nan])
  292
+    testcase_2d = ma.array(
  293
+    np.array([[ 0.05245846,  0.50344235,  0.86589117,  0.36936353,  0.46961149],
  294
+           [ 0.11574073,  0.31299969,  0.45925772,  0.72618805,  0.75194407],
  295
+           [ 0.67696689,  0.91878127,  0.09769044,  0.04645137,  0.37615733],
  296
+           [ 0.05903624,  0.29908861,  0.34088298,  0.66216337,  0.83160998],
  297
+           [ 0.64619526,  0.94894632,  0.27855892,  0.0706151 ,  0.39962917]]),
  298
+    mask = np.array([[ True, False, False,  True, False],
  299
+           [ True,  True,  True, False,  True],
  300
+           [False, False, False, False, False],
  301
+           [True, True, True, True, True],
  302
+           [False, False,  True, False, False]], dtype=np.bool))
  303
+
292 304
     def test_moment(self):
293 305
         """
294 306
         mean((testcase-mean(testcase))**power,axis=0),axis=0))**power))"""
@@ -335,7 +347,26 @@ def test_kurtosis(self):
335 347
         assert_almost_equal(y, 3.663542721189047,10)
336 348
         y = mstats.kurtosis(self.testcase,0,0)
337 349
         assert_almost_equal(y,1.64)
338  
-    #
  350
+
  351
+        # test that kurtosis works on multidimensional masked arrays
  352
+        correct_2d = ma.array(
  353
+          np.array([-1.5, -3., -1.47247052385,  0., -1.26979517952]),
  354
+          mask=np.array([False, False, False,  True, False], dtype=np.bool))
  355
+        assert_array_almost_equal(
  356
+          mstats.kurtosis(self.testcase_2d, 1), correct_2d)
  357
+        for i,row in enumerate(self.testcase_2d):
  358
+            assert_almost_equal(mstats.kurtosis(row), correct_2d[i])
  359
+
  360
+        correct_2d_bias_corrected = ma.array(
  361
+          np.array([-1.5, -3., -1.88988209538,  0., -0.5234638463918877]),
  362
+          mask=np.array([False, False, False,  True, False], dtype=np.bool))
  363
+        assert_array_almost_equal(
  364
+          mstats.kurtosis(self.testcase_2d, 1, bias=False),
  365
+          correct_2d_bias_corrected)
  366
+        for i,row in enumerate(self.testcase_2d):
  367
+            assert_almost_equal(mstats.kurtosis(row, bias=False),
  368
+              correct_2d_bias_corrected[i])
  369
+
339 370
     def test_mode(self):
340 371
         "Tests the mode"
341 372
         #

0 notes on commit 17a62cb

Please sign in to comment.
Something went wrong with that request. Please try again.