[Scipy-svn] r3373 - trunk/scipy/sandbox/dhuard
scipy-svn@scip...
scipy-svn@scip...
Thu Sep 27 09:03:09 CDT 2007
Author: dhuard
Date: 2007-09-27 09:03:00 -0500 (Thu, 27 Sep 2007)
New Revision: 3373
Added:
trunk/scipy/sandbox/dhuard/test_stats.py
Modified:
trunk/scipy/sandbox/dhuard/stats.py
Log:
Added tests for stats stub.
Modified: trunk/scipy/sandbox/dhuard/stats.py
===================================================================
--- trunk/scipy/sandbox/dhuard/stats.py 2007-09-27 13:25:57 UTC (rev 3372)
+++ trunk/scipy/sandbox/dhuard/stats.py 2007-09-27 14:03:00 UTC (rev 3373)
@@ -1,32 +1,36 @@
import scipy.interpolate as interpolate
import numpy as np
-def scoreatpercentile(data, per):
- """Return the score at the given 'per' percentile of the data.
+def scoreatpercentile(data, percentile):
+ """Return the score at the given percentile of the data.
- Example
- >>> scoreatpercentile(randn(100), 50)
- will return the median of the sample.
+ Example:
+ >>> data = randn(100)
+ >>> scoreatpercentile(data, 50)
+
+ will return the median of sample `data`.
"""
+ per = np.array(percentile)
cdf = empiricalcdf(data)
- interpolator = interpolate.interp1d(sort(cdf), sort(data))
+ interpolator = interpolate.interp1d(np.sort(cdf), np.sort(data))
return interpolator(per/100.)
def percentileofscore(data, score):
- """Percentile-position of score relative to data.
+ """Return the percentile-position of score relative to data.
score: Array of scores at which the percentile is computed.
Return percentiles (0-100).
Example
+ r = randn(50)
x = linspace(-2,2,100)
- percentileofscore(randn(50),x)
+ percentileofscore(r,x)
- Return an error if the score is outside the range of data.
+ Raise an error if the score is outside the range of data.
"""
cdf = empiricalcdf(data)
- interpolator = interpolate.interp1d(sort(data), sort(cdf))
+ interpolator = interpolate.interp1d(np.sort(data), np.sort(cdf))
return interpolator(score)*100.
def empiricalcdf(data, method='Hazen'):
@@ -46,11 +50,10 @@
i = np.argsort(np.argsort(data)) + 1.
N = len(data)
method = method.lower()
-
- if method == 'weibull':
+ if method == 'hazen':
+ cdf = (i-0.5)/N
+ elif method == 'weibull':
cdf = i/(N+1.)
- elif method == 'hazen':
- cdf = (i-0.5)/N
elif method == 'california':
cdf = (i-1.)/N
elif method == 'chegodayev':
Added: trunk/scipy/sandbox/dhuard/test_stats.py
===================================================================
--- trunk/scipy/sandbox/dhuard/test_stats.py 2007-09-27 13:25:57 UTC (rev 3372)
+++ trunk/scipy/sandbox/dhuard/test_stats.py 2007-09-27 14:03:00 UTC (rev 3373)
@@ -0,0 +1,45 @@
+"""
+Test statistical functions.
+"""
+
+
+from numpy.testing import *
+import stats
+import numpy as np
+
+N = 100
+np.random.seed(2)
+r = np.random.randn(N)
+
+class test_empiricalcdf(NumpyTestCase):
+ def check_hazen(self):
+
+ f = stats.empiricalcdf(r)
+ assert_equal(len(f), len(r))
+ assert_array_equal(np.argsort(r), np.argsort(f))
+ assert_array_equal(np.sort(f), (np.arange(N)+.5)/N)
+
+ def check_weibull(self):
+ f = stats.empiricalcdf(r, 'weibull')
+ assert_array_equal(np.sort(f), (np.arange(N)+1.)/(N+1.))
+
+ def check_california(self):
+ f = stats.empiricalcdf(r, 'california')
+ assert_array_equal(np.sort(f), (np.arange(N))/float(N))
+
+class test_scoreatpercentile(NumpyTestCase):
+ def check_simple(self):
+ r = np.random.randn(1000)
+ s = stats.scoreatpercentile(r, [15.9,50,84.1])
+ assert_array_almost_equal(s, [-1,0,1], 1)
+
+class test_percentileofscore(NumpyTestCase):
+ def check_simple(self):
+ r = np.random.randn(3000)
+ p = stats.percentileofscore(r, [-1,0,1])
+ assert_array_almost_equal(p, [15.9, 50, 84.1], 0)
+
+
+
+if __name__ == '__main__':
+ NumpyTest().run()
More information about the Scipy-svn
mailing list