# [Scipy-svn] r3341 - in trunk/scipy/stats: . tests

scipy-svn@scip... scipy-svn@scip...
Thu Sep 20 15:18:35 CDT 2007

```Author: stefan
Date: 2007-09-20 15:18:15 -0500 (Thu, 20 Sep 2007)
New Revision: 3341

Modified:
trunk/scipy/stats/stats.py
trunk/scipy/stats/tests/test_stats.py
Log:
Fix stats.percentile for 2D input.

Modified: trunk/scipy/stats/stats.py
===================================================================
--- trunk/scipy/stats/stats.py	2007-09-20 19:43:27 UTC (rev 3340)
+++ trunk/scipy/stats/stats.py	2007-09-20 20:18:15 UTC (rev 3341)
@@ -966,24 +966,25 @@
return a + (b - a)*fraction;

def scoreatpercentile(a, per, limit=()):
-    """Calculates the score at the given 'per' percentile of the sequence
-    a.  For example, the score at per=50 is the median.
+    """Calculate the score at the given 'per' percentile of the
+    sequence a.  For example, the score at per=50 is the median.

-    If the desired quantile lies between two data points, we interpolate
-    between them.
-
+    If the desired quantile lies between two data points, we
+    interpolate between them.
+
If the parameter 'limit' is provided, it should be a tuple (lower,
-    upper) of two values.  Values of 'a' outside this (closed) interval
-    will be ignored.
+    upper) of two values.  Values of 'a' outside this (closed)
+    interval will be ignored.
+
"""
# TODO: this should be a simple wrapper around a well-written quantile
# function.  GNU R provides 9 quantile algorithms (!), with differing
# behaviour at, for example, discontinuities.
-    values = np.sort(a)
+    values = np.sort(a,axis=0)
if limit:
values = values[(limit[0] < a) & (a < limit[1])]
-
-    idx = per /100. * (len(values) - 1)
+
+    idx = per /100. * (values.shape[0] - 1)
if (idx % 1 == 0):
return values[idx]
else:

Modified: trunk/scipy/stats/tests/test_stats.py
===================================================================
--- trunk/scipy/stats/tests/test_stats.py	2007-09-20 19:43:27 UTC (rev 3340)
+++ trunk/scipy/stats/tests/test_stats.py	2007-09-20 20:18:15 UTC (rev 3341)
@@ -624,18 +624,28 @@
self.a1 = [3,4,5,10,-3,-5,6]
self.a2 = [3,-6,-2,8,7,4,2,1]
self.a3 = [3.,4,5,10,-3,-5,-6,7.0]
-
+
def check_median(self):
assert_equal(stats.median(self.a1), 4)
assert_equal(stats.median(self.a2), 2.5)
assert_equal(stats.median(self.a3), 3.5)
-
+
def check_percentile(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75)
-
+
+    def test_2D(self):
+        x = array([[1, 1, 1],
+                   [1, 1, 1],
+                   [4, 4, 3],
+                   [1, 1, 1],
+                   [1, 1, 1]])
+        assert_array_equal(stats.scoreatpercentile(x,50),
+                           [1,1,1])
+
+
class test_std(NumpyTestCase):
def check_basic(self):
a = [3,4,5,10,-3,-5,6]

```